百分比旨在相对父元素的同样特性的尺寸。
假如用来设定字体样式,则相对性的便是父元素的字体样式尺寸。
大多数数访问器中<html> 和<body> 标识中的默认设置字体样式规格是100%.
CSS Code拷贝內容到剪贴板
- html {font-size: 100%;}
- body {font-size: 100%;}
- 100% = 1em = 16px = 12pt
假如用来设定宽和高非字体样式规格,则以百分比为企业的长度值是根据具备同样特性的父元素的长度值。
CSS Code拷贝內容到剪贴板
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf⑻">
- <title>JS Bin</title>
- <style type="text/css">
- div.parent {
- margin:150px;
- width: 200px;
- height: 200px;
- border: 1px solid blue;
- }
- div.child {
- width: 50%;
- height: 50%;
- border: 1px dashed black;
- }
- </style>
- </head>
- <body>
- <div class="parent">
- <div class="child"></div>
- </div>
- </body>
- </html>
再唠叨1点有关父元素的难题:作甚父元素,相对性精准定位(relative),肯定精准定位(absolute),波动(float),固定不动(fixed)中怎样寻找父元素?
因为HTML是树形构造,标识套标识,1般状况下的父子关联很明亮。
XML/HTML Code拷贝內容到剪贴板
- <div class="parent">
- <div class="child"></div>
- </div>
1.相对性精准定位元素,它的父元素合乎标识嵌套循环。
2.肯定精准定位元素,它的父元素是离它近期的精准定位元素(肯定精准定位,相对性精准定位,固定不动,但不包含波动)或视窗规格(没寻找精准定位元素时)。
3.波动元素,它的父元素也合乎标识嵌套循环。
4.固定不动元素(独特的肯定精准定位),它的父元素是视窗(访问器默认设置用来展现內容的地区的规格,并不是html 或body 的规格)。
留意1下肯定精准定位就可以了,别的的相对性简易。
CSS Code拷贝內容到剪贴板
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf⑻">
- <title>JS Bin</title>
- <style type="text/css">
- html {
- width:1000px;
- }
- body {
- width:800px;
- }
- #box {
- width:50%;
- height:300px;
- position: absolute;
- margin-left: 200px;
- border: 1px solid red;
- }
- #can {
- position:absolute;
- top:100px;
- left:100px;
- width:50%;
- height:50%;
- border:1px solid black;
- }
- </style>
- </head>
- <body>
- <div id="box">
- <div id="can"></div>
- </div>
-
- </body>
- </html>
box 宽度为视窗的1半,can 的宽高是 box 的宽高的1半。
将 can 设定为 position: fixed; 则其父元素将已不是 box 而是访问器视窗。
can 的宽高是视窗宽高的1半。
波动元素的父元素跟一般元素的父元素是1样的。
CSS Code拷贝內容到剪贴板
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf⑻">
- <title>JS Bin</title>
- <style type="text/css">
- html {
- width:1000px;
- }
- body {
- width:800px;
- }
- #box {
- width:50%;
- height:300px;
- position: absolute;
- margin-left: 200px;
- border: 1px solid red;
- }
- #can {
- float:left;
- width:50%;
- height:50%;
- border:1px solid black;
- }
- </style>
- </head>
- <body>
- <div id="box">
- <div id="can"></div>
- </div>
-
- </body>
- </html>
留意: padding、 margin 假如设定了百分比,上,下,左,右 用的全是父元素宽度 的值为规范去测算。
percentage转px
Example 1: Margins
CSS Code拷贝內容到剪贴板
- <div style="width: 20px">
- <div id="temp1" style="margin-top: 50%">Test top</div>
- <div id="temp2" style="margin-right: 25%">Test rightright</div>
- <div id="temp3" style="margin-bottom: 75%">Test bottombottom</div>
- <div id="temp4" style="margin-left: 100%">Test left</div>
- </div>
获得的offset以下:
CSS Code拷贝內容到剪贴板
- temp1.marginTop = 20px * 50% = 10px;
- temp2.marginRight = 20px * 25% = 5px;
- temp3.marginBottom = 20px * 75% = 15px;
- temp4.marginLeft = 20px * 100% = 20px;
随后,我又检测了padding,原认为padding的值会依据运用了该特性的有关元向来测算,但让我诧异的是,padding也是依据运用该特性的父元素的宽来测算的,跟margin主要表现1致。(再插1句:当按百分比设置1个元素的宽度时,它是相对父器皿的宽度测算的,元素竖向的百分比设置也是相对器皿的宽度,而并不是高宽比。)
但有1个坑,上面全是针对待定位元素。好奇心的我又很好奇心,针对非静态数据精准定位元素的top, right, bottom, 和left的百分比值又如何测算呢?
Example 2: Positioned Elements
CSS Code拷贝內容到剪贴板
- <div style="height: 100px; width: 50px">
- <div id="temp1" style="position: relative; top: 50%">Test top</div>
- <div id="temp2" style="position: relative; right: 25%">Test rightright</div>
- <div id="temp3" style="position: relative; bottom: 75%">Test bottombottom</div>
- <div id="temp4" style="position: relative; left: 100%">Test left</div>
- </div>
获得的offset以下:
CSS Code拷贝內容到剪贴板
- temp1.top = 100px * 50% = 50px;
- temp2.rightright = 100px * 25% = 25px;
- temp3.bottombottom = 100px * 75% = 75px;
- temp4.left = 100px * 100% = 100px;
针对精准定位元素,这些值也是相对父元素的,但与非精准定位元素不一样的是,它们是相对父元素的高而并不是宽。
when the parent element does not have a height, then percentage values are processed as auto instead
尽管有点疑惑,但只必须记牢:针对margin和padding,百分比依照父元素的宽测算,针对精准定位元素,则依照父元素的高测算。
文章内容的最终,强烈推荐1个网站:http://www.css3.com,上面有许多有关CSS难题的資源。