这是本文档旧的修订版!
注意, css 的诞生最早是为了适应富文本的流式布局, 至今遗留了很多流式风格的布局性质.使用时详查文档,不可类比桌面UI布局而想当然
Cascading Style Sheets
LESS
Tips
- div 水平居中:
margin:0 auto
- 文字垂直居中简单做法:在父级块元素让
height
与line-height
相同 - 层级:
z-index
对static
不起作用 - 溢出:
overflow:hidden
; max-height
与overflow:auto
配合可在元素内容过高时才出现滚动条- 子块元素的
margin-top
影响了父级元素的margin-top
, 可以在父级元素使用overflow:hidden
来避免. - float 的元素影响兄弟 float 元素与兄弟 inline 元素的定位, 但不影响非 float 的兄弟块级元素的定位; float 最好需要指定宽度;All about floats
- float 会出现在之前的块级元素之下? clear 的元素会被强制放在 float 元素之下?
- 单词内换行:
word-break: break-all
- jquery disable form:
$form.find(':input:not(:disabled)').prop('disabled',true);//disable all $form.find(':input:disabled').prop('disabled', false);//enable all
- 目前至少ios7 ios8 android4 的css transform 还需要用
-webkit-transform
Selector
css3 media queries
- 响应式css: 适应不同分辨率终端.
Calculating a selector's specificity
Copy from W3C CSS 3 selector.
A selector's specificity is calculated as follows:
- count the number of ID selectors in the selector (= a)
- count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)
- count the number of type selectors and pseudo-elements in the selector (= c)
- ignore the universal selector
Selectors inside the negation pseudo-class are counted like any other, but the negation itself does not count as a pseudo-class.
Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity.
Examples:
* /* a=0 b=0 c=0 -> specificity = 0 */ LI /* a=0 b=0 c=1 -> specificity = 1 */ UL LI /* a=0 b=0 c=2 -> specificity = 2 */ UL OL+LI /* a=0 b=0 c=3 -> specificity = 3 */ H1 + *[REL=up] /* a=0 b=1 c=1 -> specificity = 11 */ UL OL LI.red /* a=0 b=1 c=3 -> specificity = 13 */ LI.red.level /* a=0 b=2 c=1 -> specificity = 21 */ #x34y /* a=1 b=0 c=0 -> specificity = 100 */ #s12:not(FOO) /* a=1 b=0 c=1 -> specificity = 101 */
Positioning
W3C positioning-scheme CSS 2.1 defines three positioning schemes:
- Normal flow:
Inline
items are laid out in the same way as the letters in words in text, one after the other across the available space until there is no more room, then starting a new line below.Block
items stack vertically, like paragraphs and like the items in a bulleted list. Normal flow also includes relative positioning of block or inline items, and run-in boxes. - Floats: A floated item is taken out of the normal flow and shifted to the left or right as far as possible in the space available. Other content then flows alongside the floated item.
- Absolute positioning: An absolutely positioned item has no place in, and no effect on, the normal flow of other items. It occupies its assigned position in its container independently of other items.w3c positioning-scheme
Position: top, bottom, left, and right
There are four possible values of the position
property. If an item is positioned in any way other than static
, then the further properties top
, bottom
, left
, and right
are used to specify offsets and positions.
- Static: The default value places the item in the
normal flow
- Relative: The item is placed in the
normal flow
, and then shifted or offset from that position. Subsequent flow items are laid out as if the item had not been moved. - Absolute: Specifies
absolute positioning
. The element is positioned in relation to its nearest non-static ancestor. - Fixed: The item is
absolutely positioned
in a fixed position on the screen even as the rest of the document is scrolled.
Float and clear
The float
property may have one of three values. Absolutely
positioned or fixed
items cannot be floated. Other elements normally flow around floated items, unless they are prevented from doing so by their clear
property.
- left: The item
floats
to the left of the line that it would have appeared in; other items may flow around its right side. - right: The item
floats
to the right of the line that it would have appeared in; other items may flow around its left side. - clear: Forces the element to appear underneath ('clear') floated elements to the left (
clear:left
), right (clear:right
) or both sides (clear:both
).
tips
CSS选择器优先级:
- 浏览器用户设置 > 浏览器内置 > 网页行内style > 非行内style
- css选择器的优先,分三组计算次数1.ID, 2.类、伪类、属性, 3.标签, 三组数量按奥运会金银铜奖牌板的方式排序,来决定优先级。
元素布局上分位块级元素(block)与行内元素(inline), 行内元素排列行为类似文本,先横排,没空间才换行。 块级元素直接换行(垂直排列),比如div
p
等。而像span
a
等则为行内元素。 这两者在定位、大小设置上行为有差异, 可以使用display
来改变元素类型。
在布局上,行内框在一行中水平布置。可以使用水平内边距、边框和外边距调整它们的间距。但是,垂直内边距、边框和外边距不影响行内框的高度。由一行形成的水平框称为行框(Line Box),行框的高度总是足以容纳它包含的所有行内框。不过,设置行高可以增加这个行框的高度——但还是决定不了行内框的高度!
浏览器含有默认的CSS设置,比如ul标签含有默认的上下margin, 去除各个浏览器的默认设置,可以使用 http://cssreset.com, 把提供的css代码拷贝来就行,但是一些默认样式可能就需要自己来设置了。