美文网首页
html在浏览器下默认样式

html在浏览器下默认样式

作者: l柱 | 来源:发表于2016-10-25 09:14 被阅读0次

    前言

    不同的浏览器对于相同元素的默认样式是不一致的,每个浏览器都拥有一套属于它自己的样式表;浏览器相同版本不同的浏览器默认样式一般也都是不同的,但大部分还都能遵循W3C的标准。

    基本上,不同内核的两个浏览器在某些元素的表现都会存在差异,比如缩进的大小、字体选择、字符样式等。也许一个很漂亮的CSS样式表在一个浏览器上表现良好,在另外一个浏览器上即使是没有CSS Bug的情况也会变得结构混乱起来,我认为都是浏览器默认样式在作怪。

    浏览器之所以有一套默认样式表,是为了网页页面在没有样式表的情况下也能够使用户凑活着看。

    HTML各个标签的默认样式解析

    了解HTML各个标签的默认样式,有助于我们加强对标签的理解,也有利于我们了解标签在各个浏览器下的表现和减少不必要的的开发时间

    以下样式表是从CSS2.2规范的附录D节选而来。需要指出的是尽管CSS2.2称“建议开发人员使用(这个样式表)作为实现中的默认样式表”,但这不一定可行。例如对于以下规则:

    // 在下方我特意把ol、ul提出来放到了上方,是为了更好的找到列表特有的样式,在阅读的时候请注意!
    ol, ul, dir, menu, dd {
        margin-left: 40px;
    }
    

    这个规则描述了传统列表的缩进距离40px,他使用外边距达到这个效果。不过,有些浏览器会使用40px的左内边距padding-left而不是外边距margin-left,而且认为这种解决方案会更好。因此不能指望以此作为用户代理的默认样式,这个样式表更多地是为了说明相关内容,只是一个学习的工具罢了!

    // 块元素 
    html, body, div,
    dd, dl, dt, ol, ul, p,
    h1, h2, h3, h4, h5, h6, 
    center, address, blockquote, pre, dir, hr, menu,
    fieldset, form, frame, frameset, noframes { display: block; unicode-bidi: embed;}
    
    // 文档
    head            { display: none;}
    body            { margin: 8px;}
    
    // 列表
    ol ul, ul ol, ul ul, ol ol    { margin-top: 0; margin-bottom: 0;}
    ol, ul                        { margin-left: 40px;}
    ol                            { list-style-type: decimal;}
    li                            { display: list-item;}
    
    // 表格
    table             { display: table; border-spacing: 2px;}
    thead             { display: table-header-group;}
    tbody             { display: table-row-group;}
    thead,tbody,tfoot { vertical-align: middle;}
    tr                { display: table-row;}
    td, th            { display: table-cell;}
    td, th, tr        { vertical-align: inherit;}
    th                { font-weight: bolder; text-align: center;}
    tfoot             { display: table-footer-group;}
    col               { display: table-column;}
    colgroup          { display: table-column-group;}
    caption           { display: table-caption; text-align: center;}
    
    // 标题
    h1                                              { font-size: 2em; margin: .67em 0;}
    h2                                              { font-size: 1.5em; margin: .75em 0;}
    h3                                              { font-size: 1.17em; margin: .83em 0;}
    h4,ul,ol,dl,p,blockquote,dir,fieldset,form,menu { margin: 1.12em 0;}
    h5                                              { font-size: .83em; margin: 1.5em 0;}
    h6                                              { font-size: .75em; margin: 1.67em 0;}
    h1,h2,h3,h4,h5,h6,b,strong                      { font-weight: bolder;}
    
    // 其他元素
    blockquote                      { margin-left: 40px; margin-right: 40px;}
    pre                             { white-space: pre;}
    pre, tt, code, kbd, samp        { font-family: monospace;}
    i, cite, em, var, address       { font-style: italic;}
    button, textarea, input, select { display: inline-block;}
    big                             { font-size: 1.17em;}
    small, sub, sup { font-size: .83em;}
    sub             { vertical-align: sub;}
    sup             { vertical-align: super;}
    s, strike, del  { text-decoration: line-through;}
    hr              { border: 1px inset;}
    dir, menu, dd   { margin-left: 40px;}
    u, ins          { text-decoration: underline;}
    center          { text-align: center;}
    
    // 伪类
    br:before       { content: "\A"; white-space: pre-line;}
    :link, :visited { text-decoration: underline;}
    :focus          { outline: thin dotted invert;}
    
    // 定义BDO元素当其属性为DIR="ltr/rtl"时的默认文本读写显示顺序
    BDO[DIR="ltr"]  { direction: ltr; unicode-bidi: bidi-override;}
    BDO[DIR="rtl"]  { direction: rtl; unicode-bidi: bidi-override;}
    
    // 定义任何元素当其属性为DIR="rtl/rtl"时的默认文本读写显示顺序
    *[DIR="ltr"]    { direction: ltr; unicode-bidi: embed;}
    *[DIR="rtl"]    { direction: rtl; unicode-bidi: embed;}
    
    // 定义标题和列表默认的打印样式
    @media print {
      h1                        { page-break-before: always;}
      h1, h2, h3, h4, h5, h6    { page-break-after: avoid;}
      ul, ol, dl                { page-break-before: avoid;}
    }
    
    参考资源:

    1.IE6、7、8、9浏览器默认样式
    2.Firefox浏览器默认样式
    3.webkit内核浏览器默认样式
    4.HTML4的W3C标准中默认的CSS样式

    相关文章

      网友评论

          本文标题:html在浏览器下默认样式

          本文链接:https://www.haomeiwen.com/subject/bskruttx.html