background-position百分比原理
今天帮别人调代码时,看到一个样式:
background-position: 50% 0;
background-size: 100% auto;
对background-size:100% auto,意思是背景图片宽度为元素宽度*100%,高度等比缩放。详情可见css3 background。
对background-position很自然的以为百分比是根据父元素宽度计算的,但background-position真的不是,它有一套自己的原理。下面详细介绍。
一、等价写法
在看各类教程时有以下等价写法:
- top left, left top 等价于 0% 0%.
- top, top center, center top 等价于 50% 0%.
- right top, top right 等价于 100% 0%.
- left, left center, center left 等价于 0% 50%.
- center, center center 等价于 50% 50%.
- right, right center, center right 等价于 100% 50%.
- bottom left, left bottom 等价于 0% 100%.
- bottom, bottom center, center bottom 等价于 50% 100%.
- bottom right, right bottom 等价于 100% 100%.
那么为什么left,top就等价于0% 0%,right bottom等价于100% 100%呢?
二、background-position百分比计算公式
background-postion:x y;
x:{容器(container)的宽度—背景图片的宽度}*x百分比,超出的部分隐藏。
y:{容器(container)的高度—背景图片的高度}*y百分比,超出的部分隐藏。
有了这个公式,就很容易理解百分百写法了,推算一下也就很容易理解上面各类等价写法了。
三、举例
1、background-position:center center等价于background-position:50% 50%等价于background-position:?px ?px
例子中用到背景图如下【尺寸:200px*200px】:
背景图在容器中居中。
<style type="text/css">
.wrap{
width: 300px;
height: 300px;
border:1px solid green;
background-image: url(img/image.png);
background-repeat: no-repeat;
/* background-position: 50% 50%;*/
background-position: center center;
}
</style>
<div class="wrap">
</div>
效果都是让背景图片居中
如上通过设置百分比和关键字能实现背景图居中,如果要实现通过具体值来设置图片居中该设置多少?
根据上面公式:
x=(容器的宽度-背景图宽度)*x百分比=(300px-200px)*50%=50px;
y=(容器的高度-背景图高度)*y百分比=(300px-200px)*50%=50px;
即设置background-postion:50px 50px;
测试一下:
<style type="text/css">
.wrap{
width: 300px;
height: 300px;
border:1px solid green;
background-image: url(img/image.png);
background-repeat: no-repeat;
/* background-position: 50% 50%;*/
/* background-position: center center;*/
background-position: 50px 50px;
}
</style>
<div class="wrap">
</div>
效果同样居中。
background-position百分比原理的更多相关文章
- mysql之 MySQL 主从基于position复制原理概述
1 .主从复制简介MySQL 主从复制就是将一个 MySQL 实例(Master)中的数据实时复制到另一个 MySQL 实例(slave)中,而且这个复制是一个异步复制的过程.实现整个复制操作主要由三 ...
- jQuery视差滚动插件,(附原理分析,调用方法)
演示地址:http://www.jq22.com/jquery-info1799 jquery插件,使用验证过可用. 分析源代码后总结原理:设置background样式为fixed,判断浏览器滚动距离 ...
- html5+css3中的background: -moz-linear-gradient 用法
在CSS中background: -moz-linear-gradient 让网站背景渐变的属性,目前火狐3.6以上版本和google浏览器支持这个属性. background: -moz-linea ...
- html5+css3中的background: -moz-linear-gradient 用法 (转载)
转载至-->http://www.cnblogs.com/smile-ls/archive/2013/06/03/3115599.html 在CSS中background: -moz-linea ...
- css3的Background新属性
前言 CSS3中出现了几种关于背景图片的新属性:background-origin.background-clip.background-position等.之前大致了解了下,但是background ...
- 背景图片之background的用法
常用的background背景属性有: background-color 设置颜色作为对象背景颜色background-image 设置图片作为背景图片background-repeat 设置背景平铺 ...
- 不用任何图片,只用简单的css写出唯美的钟表,就问你行吗?
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAGHCAIAAABJR31QAAAgAElEQVR4nOy9aXhc1ZUurPvcH7f73n ...
- 不要告诉我你懂margin
分类: Html/CSS | 转载请注明: 出自 海玉的博客 本文地址: http://www.hicss.net/do-not-tell-me-you-understand-margin/ 你真的了 ...
- css内边距与外边距的区别
你真的了解margin吗?你知道margin有什么特性吗?你知道什么是垂直外边距合并?margin在块元素.内联元素中的区别?什么时候该用 padding而不是margin?你知道负margin吗?你 ...
随机推荐
- java内存模型-volatile
volatile 的特性 当我们声明共享变量为 volatile 后,对这个变量的读/写将会很特别.理解 volatile 特性的一个好方法是:把对 volatile 变量的单个读/写,看成是使用同一 ...
- jQuery Wookmark Load 瀑布流布局实例演示
瀑布流布局非常适合大量图片的展示,一改过去裁剪图片尺寸统一的排版,每张图片都能完全展示,并错落有致,让人眼前一亮. 版本: jQuery v1.4.3+ jQuery Wookmark Load v1 ...
- JavaScript——99乘法表
<!DOCTYPE html> <html> <head> <title>99乘法表</title> <style type=&quo ...
- JavaScript学习笔记2之Tab切换
1.Tab切换简写版1 页面布局如下: <div id="tab"> <h1 id="title"> <span class=&q ...
- ae工具是一种特殊的命令
itool继承icommand,所以itool工具的调用类似于icommand,而icommand的调用主要是oncreate和onclick方法,oncreate需要传入事件执行的的对象,oncli ...
- Android布局优化策略
我们要知道布局是否合理,可以通过Hierarchy Viewer这个工具.打开Hierarchy Viewer(定位到tools/目录下,直接执行hierarchyviewer的命令,选定需要查看的P ...
- 《The Linux Command Line》 读书笔记02 关于命令的命令
<The Linux Command Line> 读书笔记02 关于命令的命令 命令的四种类型 type type—Indicate how a command name is inter ...
- 使用TextKit
使用TextKit TextKit是在iOS7中新出的,实现了对CoreText的封装,使用起来更加方便. 虽然是新出的,但也不代表立马就能上手-_-!!,TextKit可以实现图文混排效果,很好用. ...
- Asp.net中GridView使用详解(引)
GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到Gr ...
- 自定义button
改变button内部label和imageView的frame - (CGRect)titleRectForContentRect:(CGRect)contentRect - (CGRect)imag ...