CSS 样式总结
· 阅读需 9 分钟
行内元素
-
width、height 无效,可通过 line-height 设置
-
margin、padding 左右有效,上下无效
float
- none:设置对象不浮动
- left:设置对象浮在左边
- right:设置对象浮在右边
clear
- none:允许两边都可以有浮动对象
- both:不允许有浮动对象
- left:不允许左边有浮动对象
- right:不允 许右边有浮动对象
position (子绝父相)
- static:常规留
- relative:参照自身
- absolute:参照父元素,无父元素则参照 body
- fixed:以窗口为参照
line-height 特殊使用
- 行高会继承,因此直接给父级,当 line-height 等于 height 的时候垂直居中
解决边框叠加问题
margin-left: -1px; /* 解决边框叠加问题 */
清除浮动
/* 清除浮动 */
.clearfix:after {
content: '';
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
*zoom: 1; /* I6/I7专用 */
}
background
所有背景属性都不能继承
背景色
p {
background-color: gray;
}
背景图像
body {
background-image: url(/i/eg_bg_04.gif);
}
/** 背景重复 */
body {
background-image: url(/i/eg_bg_03.gif);
background-repeat: repeat-y;
}
值 | 描述 |
---|---|
repeat | 默认。背景图像将在垂直方向和水平方向重复。 |
repeat-x | 背景图像将在水平方向重复。 |
repeat-y | 背景图像将在垂直方向重复。 |
no-repeat | 背景图像将仅显示一次。 |
inherit | 规定应该从父元素继承 background-repeat 属性的设置。 |
背景定位
p {
background-position: top;
background-position: 50% 50%;
background-position: 50px 100px;
}
// 左上为原点
单一关键字 | 等价的关键字 |
---|---|
center | center center |
top | top center 或 center top |
bottom | bottom center 或 center bottom |
right | right center 或 center right |
left | left center 或 center left |
背景关联
background-attachment: fixed // 固定背景图片;;
值 | 描述 |
---|---|
scroll | 默认值。背景图像会随着页面其余部分的滚动而移动。 |
fixed | 当页面的其余部分滚动时,背景图像不会移动。 |
inherit | 规定应该从父元素继承 background-attachment 属性的设置。 |
text
简写
- font-style - font-variant - font-weight - font-size/line-height - font-family p.ex2 {
font: italic bold 12px/20px arial, sans-serif;
}
缩进 text-indent(可继承)
p {
text-indent: 5em;
}
p {
text-indent: 20%;
} // 缩进值是父元素的 20%,即 100 个像素
对齐 text-align
值 | 描述 |
---|---|
left | 把文本排列到左边。默认值:由浏览器决定。 |
right | 把文本排列到右边。 |
center | 把文本排列到中间。 |
justify | 实现两端对齐文本效果。 |
inherit | 规定应该从父元素继承 text-align 属性的值。 |
字间隔 word-spacing
p {
word-spacing: 25px;
}
值 | 描述 |
---|---|
normal | 默认。定义单词间的标准空间。 |
length | 定义单词间的固定空间。 |
inherit | 规定应该从父元素继承 word-spacing 属性的值。 |
字母间隔 letter-spacing
h1 {
letter-spacing: -0.5em;
}
h4 {
letter-spacing: 20px;
}
值 | 描述 |
---|---|
normal | 默认。规定字符间没有额外的空间。 |
length | 定义字符间的固定空间(允许使用负值)。 |
inherit | 规定应该从父元素继承 letter-spacing 属性的值。 |
文本装饰 text-decoration 值覆盖
h2 {
text-decoration: underline overline;
}
值 | 描述 |
---|---|
none | 默认。定义标准的文本。 |
underline | 定义文本下的一条线。 |
overline | 定义文本上的一 条线。 |
line-through | 定义穿过文本下的一条线。 |
blink | 定义闪烁的文本。 |
inherit | 规定应该从父元素继承 text-decoration 属性的值。 |
处理空白符 white-space
值 | 描述 |
---|---|
normal | 默认。空白会被浏览器忽略。 |
pre | 空白会被浏览器保留。其行为方式类似 HTML 中的 标签。 |
nowrap | 文本不会换行,文本会在在同一行上继续,直到遇到 标签为止。 |
pre-wrap | 保留空白符序列,但是正常地进行换行。 |
pre-line | 合并空白符序列,但是保留换行符。 |
inherit | 规定应该从父元素继承 white-space 属性的值。 |
字体
font-family 指定字体
p {
font-family: Times, TimesNR, 'New Century Schoolbook', Georgia, 'New York', serif;
}