CSS学习之样式继承(Inheritance)和优先级 - CSS: The Missing Manual

  • 子元素会继承所有父元素的属性。但有特例情况,如border属性不会被继承。

  • 属性继承规则

    • 不可继承的:display、margin、border、padding、background、height、min-height、max- height、width、min-width、max-width、overflow、position、left、right、top、 bottom、z-index、float、clear、table-layout、vertical-align、page-break-after、 page-bread-before、unicode-bidi
    • 所有元素可继承:visibility和cursor。
    • 内联元素可继承:letter-spacing、word-spacing、white-space、line-height、color、font、 font-family、font-size、font-style、font-variant、font-weight、text- decoration、text-transform、direction。
    • 块状元素可继承:text-indent和text-align。
    • 列表元素可继承:list-style、list-style-type、list-style-position、list-style-image。
    • 表格元素可继承:border-collapse
  • 属性继承的特殊情况
    • 影响元素的位置的属性及margins、背景颜色、borders属性不会被继承
    • 浏览器为某些标签继承了一些特定的属性,例如链接是蓝色的等,首先去清除这些浏览器原有样式是一个常见的做法
    • 当样式冲突时,规则如下:
      1. 浏览器缺省设置 < 外部样式 < 内部样式表(head标签内部) < 内联样式( HTML 元素内部)
      2. id选择器 > class选择器 > 标签选择器;
      3. 一个标签有多个id或多个class的时候,对前面那个的css定义优先。
        例: < div id=”a” id=”b”> #a{…} #b{…} 此时样式为#a定义的样式
      4. 选择器的权值如下图
        这里写图片描述
        例如 .main p 和 p.intro 的权值都为11,当权值不同时,取权值大的样式,当权值相同时,取在后面定义的样式。
  • 在样式表最开头清除所有默认样式(CSS reset)
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video 
{
 margin: 0;
 padding: 0;
 border: 0;
 font-size: 100%;
 vertical-align: baseline;
}

article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section 
{
 display: block;
}

body 
{
 line-height: 1.2;
}
ol 
{
 padding-left: 1.4em;
 list-style: decimal;
}

ul 
{
 padding-left: 1.4em
 list-style: square;
}

table 
{
 border-collapse: collapse;
 border-spacing: 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值