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 个像素