• CSS继续


    CSS 定位
        CSS 定位 (Positioning) 属性允许你对元素进行定位。
        CSS 为定位和浮动提供了一些属性,利用这些属性,可以建立列式布局,将布局的一部分与另一部分重叠,还可以完成多年来通常需要使用多个表格才能完成的任务。
        div、h1 或 p 元素常常被称为块级元素。这意味着这些元素显示为一块内容,即“块框”。与之相反,span 和 strong 等元素称为“行内元素”,这是因为它们的内容显示在行中,即“行内框”。
        使用 display 属性改变生成的框的类型: display 属性设置为 block,可以让行内元素(比如 <a> 元素)表现得像块级元素一样。还可以通过把 display 设置为 none,让生成的元素根本没有框。这样的话,该框及其所有内容就不再显示,不占用文档中的空间。
        但是在一种情况下,即使没有进行显式定义,也会创建块级元素。这种情况发生在把一些文本添加到一个块级元素(比如 div)的开头。即使没有把这些文本定义为段落,它也会被当作段落对待:

        <div>
            some text
            <p>Some more text.</p>
        </div>

        在这种情况下,这个框称为无名块框,因为它不与专门定义的元素相关联
        块级元素的文本行也会发生类似的情况。
    CSS 定位机制
        CSS 有三种基本的定位机制:普通流、浮动和绝对定位
        除非专门指定,否则所有框都在普通流中定位。也就是说,普通流中的元素的位置由元素在 (X)HTML 中的位置决定。
        块级框从上到下一个接一个地排列,框之间的垂直距离是由框的垂直外边距计算出来。
        行内框在一行中水平布置。可以使用水平内边距、边框和外边距调整它们的间距。但是,垂直内边距、边框和外边距不影响行内框的高度。由一行形成的水平框称为行框(Line Box),行框的高度总是足以容纳它包含的所有行内框。不过,设置行高可以增加这个框的高度。
    CSS position 属性

    static
        元素框正常生成。块级元素生成一个矩形框,作为文档流的一部分,行内元素则会创建一个或多个行框,置于其父元素中。
    relative
        元素框偏移某个距离。元素仍保持其未定位前的形状,它原本所占的空间仍保留。
    absolute
        元素框从文档流完全删除,并相对于其包含块定位。包含块可能是文档中的另一个元素或者是初始包含块。元素原先在正常文档流中所占的空间会关闭,就好像元素原来不存在一样。元素定位后生成一个块级框,
       而不论原来它在正常流中生成何种类型的框。 fixed 元素框的表现类似于将 position 设置为 absolute,不过其包含块是视窗本身。 提示:相对定位实际上被看作普通流定位模型的一部分,因为元素的位置相对于它在普通流中的位置。
    <html>
    <head>
    <style type="text/css">
    h2.pos_left
    {
    position:relative;
    left:-20px
    }
    h2.pos_right
    {
    position:relative;
    left:20px
    }
    </style>
    </head>
    
    <body>
    <h2>这是位于正常位置的标题</h2>
    <h2 class="pos_left">这个标题相对于其正常位置向左移动</h2>
    <h2 class="pos_right">这个标题相对于其正常位置向右移动</h2>
    <p>相对定位会按照元素的原始位置对该元素进行移动。</p>
    <p>样式 "left:-20px" 从元素的原始左侧位置减去 20 像素。</p>
    <p>样式 "left:20px" 向元素的原始左侧位置增加 20 像素。</p>
    </body>
    
    </html>
    <html>
    <head>
    <style type="text/css">
    h2.pos_abs
    {
    position:absolute;
    left:100px;
    top:150px
    }
    </style>
    </head>
    
    <body>
    <h2 class="pos_abs">这是带有绝对定位的标题</h2>
    <p>通过绝对定位,元素可以放置到页面上的任何位置。下面的标题距离页面左侧 100px,距离页面顶部 150px。</p>
    </body>
    
    </html>
    <html>
    <head>
    <style type="text/css">
    p.one
    {
    position:fixed;
    left:5px;
    top:5px;
    }
    p.two
    {
    position:fixed;
    top:30px;
    right:5px;
    }
    </style>
    </head>
    <body>
    
    <p class="one">一些文本。</p>
    <p class="two">更多的文本。</p>
    
    </body>
    </html>
    <html>
    <head>
    <style type="text/css">
    img
    {
    position:absolute;
    top:0px
    }
    </style>
    </head>
    <body>
    <!--图片挡住了This is a-->
    <h1>This is a Heading</h1>
    <img class="normal" src="/i/eg_smile.gif" />
    <p>一些文本。一些文本。一些文本。一些文本。一些文本。一些文本。</p>
    
    </body>
    </html>
    <html>
    <head>
    <style type="text/css">
    img
    {
    position:absolute;
    top:5%
    }
    </style>
    </head>
    <body>
    <!--使用百分比设置图像的上边缘-->
    <h1>这是标题</h1>
    <img class="normal" src="/i/eg_smile.gif" />
    <p>一些文本。一些文本。一些文本。一些文本。一些文本。一些文本。</p>
    
    </body>
    </html>
    <html>
    <head>
    <style type="text/css">
    img
    {
    position:absolute;
    bottom:0px
    }
    </style>
    </head>
    <body>
    <!--使用像素值设置图像的底部边缘-->
    <h1>这是标题</h1>
    <img class="normal" src="/i/eg_smile.gif" />
    <p>一些文本。一些文本。一些文本。一些文本。一些文本。一些文本。</p>
    
    </body>
    </html>
    <html>
    <head>
    <style type="text/css">
    img
    {
    position:absolute;
    bottom:5%
    }
    </style>
    </head>
    <body>
    <!--使用百分比设置图像的底部边缘-->
    <h1>这是标题</h1>
    <img class="normal" src="/i/eg_smile.gif" />
    <p>一些文本。一些文本。一些文本。一些文本。一些文本。一些文本。</p>
    
    </body>
    </html>
    <html>
    <head>
    <style type="text/css">
    img
    {
    position:absolute;
    left:100px
    }
    </style>
    </head>
    <body>
    <!--使用固定值设置图像的左边缘,另还可用百分比设置-->
    <h1>这是标题</h1>
    <img class="normal" src="/i/eg_smile.gif" />
    <p>一些文本。一些文本。一些文本。一些文本。一些文本。一些文本。</p>
    
    </body>
    </html>
    <html>
    <head>
    <style type="text/css">
    img
    {
    position:absolute;
    right:0px
    }
    </style>
    </head>
    <body>
    <!--使用固定值设置图像的右边缘,另还可用百分比设置-->
    <h1>这是标题</h1>
    <img class="normal" src="/i/eg_smile.gif" />
    <p>一些文本。一些文本。一些文本。一些文本。一些文本。一些文本。</p>
    
    </body>
    </html>
    <html>
    <head>
    <style type="text/css">
    <!--如何使用滚动条来显示元素内溢出的内容-->
    div 
    {
    background-color:#00FFFF;
    width:150px;
    height:150px;
    overflow: scroll;
    }
    </style>
    </head>
    
    <body>
    <p>如果元素中的内容超出了给定的宽度和高度属性,overflow 属性可以确定是否显示滚动条等行为。</p>
    
    <div>
    这个属性定义溢出元素内容区的内容会如何处理。如果值为 scroll,不论是否需要,用户代理都会提供一种滚动机制。因此,有可能即使元素框中可以放下所有内容也会出现滚动条。默认值是 visible。
    </div>
    </body>
    
    </html>
    <html>
    <head>
    <style type="text/css">
    <!--隐藏溢出内容-->
    div 
    {
    background-color:#00FFFF;
    width:150px;
    height:150px;
    overflow: hidden
    }
    </style>
    </head>
    
    <body>
    <p>如果元素中的内容超出了给定的宽度和高度属性,overflow 属性可以确定是否显示滚动条等行为。</p>
    
    <div>
    这个属性定义溢出元素内容区的内容会如何处理。如果值为 scroll,不论是否需要,用户代理都会提供一种滚动机制。因此,有可能即使元素框中可以放下所有内容也会出现滚动条。默认值是 visible。
    </div>
    </body>
    
    </html>
    <html>
    <head>
    <style type="text/css">
    <!--设置浏览器来自动地处理溢出-->
    div 
    {
    background-color:#00FFFF;
    width:150px;
    height:150px;
    overflow: auto
    }
    </style>
    </head>
    
    <body>
    <p>如果元素中的内容超出了给定的宽度和高度属性,overflow 属性可以确定是否显示滚动条等行为。</p>
    
    <div>
    这个属性定义溢出元素内容区的内容会如何处理。如果值为 scroll,不论是否需要,用户代理都会提供一种滚动机制。因此,有可能即使元素框中可以放下所有内容也会出现滚动条。默认值是 visible。
    </div>
    </body>
    
    </html>

      设置元素的形状

    <html>
    <head>
    <style type="text/css">
    img 
    {
    position:absolute;
    clip:rect(0px 50px 200px 0px)
    }
    </style>
    </head>
    
    <body>
    <p>clip 属性剪切了一幅图像:</p>
    <p><img border="0" src="/i/eg_bookasp.gif" width="120" height="151"></p>
    </body>
    
    </html>
    <html>
    
    <head>
    <style type="text/css">
    <!--垂直排列图像-->
    img.top {vertical-align:text-top}
    img.bottom {vertical-align:text-bottom}
    </style>
    </head>
    
    <body>
    
    <p>
    这是一幅<img class="top" border="0" src="/i/eg_cute.gif" />位于段落中的图像。
    </p> 
    
    <p>
    这是一幅<img class="bottom" border="0" src="/i/eg_cute.gif" />位于段落中的图像。
    </p>
    
    </body>
    
    </html>
    Z-index可被用于将在一个元素放置于另一元素之后。
    <html>
    <head>
    <style type="text/css">
    img.x
    {
    position:absolute;
    left:0px;
    top:0px;
    z-index:-1
    }
    </style>
    </head>
    
    <body>
    <h1>这是一个标题</h1>
    <img class="x" src="/i/eg_mouse.jpg" /> 
    <p>默认的 z-index 是 0。Z-index -1 拥有更低的优先级。</p>
    </body>
    
    </html>

    浮动

    <html>
    <head>
    <style type="text/css">
    img 
    {
    float:right
    }
    </style>
    </head>
    
    <body>
    <p>在下面的段落中,我们添加了一个样式为 <b>float:right</b> 的图像。结果是这个图像会浮动到段落的右侧。</p>
    <p>
    <img src="/i/eg_cute.gif" />
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    </p>
    </body>
    
    </html>
    <html>
    <head>
    <style type="text/css">
    img 
    {
    float:right;
    border:1px dotted black;
    margin:0px 0px 15px 20px;
    }
    </style>
    </head>
    
    <body>
    <p>在下面的段落中,图像会浮动到右侧,并且添加了点状的边框。我们还为图像添加了边距,这样就可以把文本推离图像:上和右外边距是 0px,下外边距是 15px,而图像左侧的外边距是 20px。</p>
    <p>
    <img src="/i/eg_cute.gif" />
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    </p>
    </body>
    
    </html>
    <html>
    <head>
    <style type="text/css">
    div
    {
    float:right;
    width:120px;
    margin:0 0 15px 20px;
    padding:15px;
    border:1px solid black;
    text-align:center;
    }
    </style>
    </head>
    
    <body>
    <div>
    <img src="/i/eg_cute.gif" /><br />
    CSS is fun!
    </div>
    <p>
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    </p>
    
    <p>
    在上面的段落中,div 元素的宽度是 120 像素,它其中包含图像。div 元素浮动到右侧。我们向 div 元素添加了外边距,这样就可以把 div 推离文本。同时,我们还向 div 添加了边框和内边距。
    </p>
    </body>
    
    </html>
    <html>
    <head>
    <style type="text/css">
    span
    {
    float:left;
    width:0.7em;
    font-size:400%;
    font-family:algerian,courier;
    line-height:80%;
    }
    </style>
    </head>
    
    <body>
    <p>
    <span>T</span>his is some text.
    This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    This is some text. This is some text. This is some text.
    </p>
    
    <p>
    在上面的段落中,文本的第一个字母包含在一个 span 元素中。这个 span 元素的宽度是当前字体尺寸的 0.7 倍。span 元素的字体尺寸是 400%,行高是 80%。span 中的字母字体是 "Algerian"
    </p>
    
    </body>
    </html>
    <html>
    <head>
    <style type="text/css">
    ul
    {
    float:left;
    width:100%;
    padding:0;
    margin:0;
    list-style-type:none;
    }
    a
    {
    float:left;
    width:7em;
    text-decoration:none;
    color:white;
    background-color:purple;
    padding:0.2em 0.6em;
    border-right:1px solid white;
    }
    a:hover {background-color:#ff3300}
    li {display:inline}
    </style>
    </head>
    
    <body>
    <ul>
    <li><a href="#">Link one</a></li>
    <li><a href="#">Link two</a></li>
    <li><a href="#">Link three</a></li>
    <li><a href="#">Link four</a></li>
    </ul>
    
    <p>
    在上面的例子中,我们把 ul 元素和 a 元素浮向左浮动。li 元素显示为行内元素(元素前后没有换行)。这样就可以使列表排列成一行。ul 元素的宽度是 100%,
    列表中的每个超链接的宽度是 7em(当前字体尺寸的 7 倍)。我们添加了颜色和边框,以使其更漂亮。
    创建水平菜单
    </p> </body> </html>
    <html>
    <head>
    <style type="text/css">
    div.container
    {
    width:100%;
    margin:0px;
    border:1px solid gray;
    line-height:150%;
    }
    div.header,div.footer
    {
    padding:0.5em;
    color:white;
    background-color:gray;
    clear:left;
    }
    h1.header
    {
    padding:0;
    margin:0;
    }
    div.left
    {
    float:left;
    width:160px;
    margin:0;
    padding:1em;
    }
    div.content
    {
    margin-left:190px;
    border-left:1px solid gray;
    padding:1em;
    }
    </style>
    </head>
    <body>
    
    <div class="container">
    
    <div class="header"><h1 class="header">W3School.com.cn</h1></div>
    
    <div class="left"><p>"Never increase, beyond what is necessary, the number of entities required to explain anything." William of Ockham (1285-1349)</p></div>
    
    <div class="content">
    <h2>Free Web Building Tutorials</h2>
    <p>At W3School.com.cn you will find all the Web-building tutorials you need,
    from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP.</p>
    <p>W3School.com.cn - The Largest Web Developers Site On The Net!</p></div>
    
    <div class="footer">Copyright 2008 by YingKe Investment.</div>
    </div>
    
    </body>
    </html>
    <html>
    <!--清除元素侧面-->
    <head>
    <style type="text/css">
    img
      {
      float:left;
      clear:both;
      }
    </style>
    </head>
    
    <body>
    <img src="/i/eg_smile.gif" />
    <img src="/i/eg_smile.gif" />
    </body>
    
    </html>

    CSS选择器

      分组

    /* no grouping */
    h1 {color:blue;}
    h2 {color:blue;}
    h3 {color:blue;}
    h4 {color:blue;}
    h5 {color:blue;}
    h6 {color:blue;}
    
    /* grouping */
    h1, h2, h3, h4, h5, h6 {color:blue;}

      选择性分组

    /* group 1 */
    h1 {color:silver; background:white;}
    h2 {color:silver; background:gray;}
    h3 {color:white; background:gray;}
    h4 {color:silver; background:white;}
    b {color:gray; background:white;}
    
    /* group 2 */
    h1, h2, h4 {color:silver;}
    h2, h3 {background:gray;}
    h1, h4, b {background:white;}
    h3 {color:white;}
    b {color:gray;}
    
    /* group 3 */
    h1, h4 {color:silver; background:white;}
    h2 {color:silver;}
    h3 {color:white;}
    h2, h3 {background:gray;}
    b {color:gray; background:white;}

    通配符选择器

    <!--CSS2 引入了一种新的简单选择器 - 通配选择器(universal selector),显示为一个星号(*)。该选择器可以与任何元素匹配,就像是一个通配符。-->
    * {color:red;}

    对声明进行分组

    h1 {
      font: 28px Verdana;
      color: blue;
      background: red;
      }

    结合选择器和声明进行的分组

    h1, h2, h3, h4, h5, h6 {
      color:gray;
      background: white;
      padding: 10px;
      border: 1px solid black;
      font-family: Verdana;
      }

    类选择器

    <h1 class="important">
    This heading is very important.
    </h1>
    
    <p class="important">
    This paragraph is very important.
    </p>

      使用以下语法向这些归类的元素应用样式,即类名前有一个点号(.),然后结合通配选择器:

    *.important {color:red;}
    div.important {color:red;}

      想选择所有类名相同的元素,可以在类选择器中忽略通配选择器

    .important {color:red;}

    结合元素选择器

    p.important {color:red;}  

      选择器现在会匹配 class 属性包含 important 的所有 p 元素,但是其他任何类型的元素都不匹配,不论是否有此 class 属性。选择器 p.important 解释为:“其 class 属性值为 important 的所有段落”。 因为 h1 元素不是段落,这个规则的选择器与之不匹配,因此 h1 元素不会变成红色文本。

    CSS 多类选择器

    <p class="important warning">
    This paragraph is a very important warning.
    </p>

      假设 class 为 important 的所有元素都是粗体,而 class 为 warning 的所有元素为斜体,class 中同时包含 important 和 warning 的所有元素还有一个银色的背景 。就可以写作:

    .important {font-weight:bold;}
    .warning {font-style:italic;}
    .important.warning {background:silver;}

      通过把两个类选择器链接在一起,仅可以选择同时包含这些类名的元素(类名的顺序不限)。
      如果一个多类选择器包含类名列表中没有的一个类名,匹配就会失败。

    .important.urgent {background:silver;}

      如果一个 p 元素的 class 属性中只有词 important 和 warning,将不能匹配

    <p class="important urgent warning">
    This paragraph is a very important and urgent warning.
    </p>

    ID选择器

      ID 属性不允许有以空格分隔的词列表,ID 选择器会使用一次。

    <html>
    <head>
    <style type="text/css">
    #mostImportant {color:red; background:yellow;}
    </style>
    </head>
    
    <body>
    <h1 id="mostImportant">This is important!</h1>
    
    <p>This is a paragraph.</p>
    
    <p>This is a paragraph.</p>
    
    <p>This is a paragraph.</p>
    
    <p>...</p>
    </body>
    </html>

    类选择器和 ID 选择器可能是区分大小写的。

    以下的 CSS 和 HTML,元素不会变成粗体:
    #intro {font-weight:bold;}
    <p id="Intro">This is a paragraph of introduction.</p>

    属性选择器

      如果希望把包含标题(title)的所有元素变为红色,可以写作:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <style type="text/css">
    [title]
    {
    color:red;
    }
    </style>
    </head>
    
    <body>
    <h1>可以应用样式:</h1>
    <h2 title="Hello world">Hello world</h2>
    <a title="W3School" href="http://w3school.com.cn">W3School</a>
    
    <hr />
    
    <h1>无法应用样式:</h1>
    <h2>Hello world</h2>
    <a href="http://w3school.com.cn">W3School</a>
    </body>
    </html>

    可以根据多个属性进行选择,只需将属性选择器链接在一起即可

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <style type="text/css">
    a[href][title]
    {
    color:red;
    }
    </style>
    </head>
    
    <body>
    <h1>可以应用样式:</h1>
    <a title="W3School Home" href="http://w3school.com.cn">W3School</a>
    
    <hr />
    
    <h1>无法应用样式:</h1>
    <a href="http://w3school.com.cn">W3School</a>
    </body>
    </html>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <style type="text/css">
    a[href]
    {
    color:red;
    }
    </style>
    </head>
    
    <body>
    <h1>可以应用样式:</h1>
    <a href="http://w3school.com.cn">W3School</a>
    
    <hr />
    
    <h1>无法应用样式:</h1>
    <a name="w3school">W3School</a>
    </body>
    </html>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <style type="text/css">
    img[alt]
    {
    border: 5px solid red;
    }
    </style>
    </head>
    
    <body>
    <h1>可以应用样式:</h1>
    <img src="/i/w3school_logo_black.gif" alt="W3School Logo" />
    
    <hr />
    
    <h1>无法应用样式:</h1>
    <img src="/i/w3school_logo_black.gif" />
    </body>
    </html>

    根据具体属性值选择

    a[href="http://www.w3school.com.cn/about_us.asp"] {color: red;}

    与简单属性选择器类似,可以把多个属性-值选择器链接在一起来选择一个文档。

    a[href="http://www.w3school.com.cn/"][title="W3School"] {color: red;}

    属性与属性值必须完全匹配
        请注意,这种格式要求必须与属性值完全匹配。
        如果属性值包含用空格分隔的值列表,匹配就可能出问题。

    <p class="important warning">This paragraph is a very important warning.</p>
    p[class="important"]这行这样是不行的
    p[class="important warning"] {color: red;}必须写成这样

    根据部分属性值选择

    p[class~="important"] {color: red;}

    "~=" 属性选择器能用于任何属性,而不只是 class

    img[title~="Figure"] {border: 1px solid gray;}

    子串匹配属性选择器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <style type="text/css">
    a[href*="w3school.com.cn"]
    {
    color: red;
    }
    </style>
    </head>
    
    <body>
    <h1>可以应用样式:</h1>
    <a href="http://www.w3school.com.cn/">W3School</a>
    <a href="http://www.w3school.com.cn/css/">CSS</a>
    <a href="http://www.w3school.com.cn/html/">HTML</a>
    
    <hr />
    
    <h1>无法应用样式:</h1>
    <a href="http://www.w3c.org/">W3C</a>
    <a href="http://www.microsoft.com">Microsoft</a>
    <a href="http://www.apple.com.cn">Apple</a>
    </body>
    </html>
    [abc^="def"]     选择 abc 属性值以 "def" 开头的所有元素
    [abc$="def"]     选择 abc 属性值以 "def" 结尾的所有元素
    [abc*="def"]     选择 abc 属性值中包含子串 "def" 的所有元素

    特定属性选择类型

    *[lang|="en"] {color: red;}

      上面这个规则会选择 lang 属性等于 en 或以 en- 开头的所有元素。因此,以下示例标记中的前三个元素将被选中,而不会选择后两个元素:

    <p lang="en">Hello!</p>
    <p lang="en-us">Greetings!</p>
    <p lang="en-au">G'day!</p>
    <p lang="fr">Bonjour!</p>
    <p lang="cy-en">Jrooana!</p>

    CSS 后代选择器
        后代选择器(descendant selector)又称为包含选择器。
        后代选择器可以选择作为某元素后代的元素。
        只对 h1 元素中的 em 元素应用样式,可以这样写:

    h1 em {color:red;}

      在后代选择器中,规则左边的选择器一端包括两个或多个用空格分隔的选择器。选择器之间的空格是一种结合符(combinator)。每个空格结合符可以解释为“... 在 ... 找到”、“... 作为 ... 的一部分”、“... 作为 ... 的后代”,但是要求必须从右向左读选择器。
      因此,h1 em 选择器可以解释为 “作为 h1 元素后代的任何 em 元素”。如果要从左向右读选择器,可以换成以下说法:“包含 em 的所有 h1 会把以下样式应用到该 em”。

    CSS 子元素选择器
    与后代选择器相比,子元素选择器(Child selectors)只能选择作为某元素子元素的元素。

    h1 > strong {color:red;}
    第一个 h1 下面的两个 strong 元素变为红色,但是第二个 h1 中的 strong 不受影响
    <h1>This is <strong>very</strong> <strong>very</strong> important.</h1>
    <h1>This is <em>really <strong>very</strong></em> important.</h1>
    <!DOCTYPE HTML>
    <html>
    <head>
    <style type="text/css">
    h1 > strong {color:red;}
    </style>
    </head>
    
    <body>
    <h1>This is <strong>very</strong> <strong>very</strong> important.</h1>
    <h1>This is <em>really <strong>very</strong></em> important.</h1>
    </body>
    </html>
    子结合符两边可以有空白符,这是可选的。因此,以下写法都没有问题:
    
    h1 > strong
    h1> strong
    h1 >strong
    h1>strong

    结合后代选择器和子选择器

    table.company td > p
    上面的选择器会选择作为 td 元素子元素的所有 p 元素,这个 td 元素本身从 table 元素继承,该 table 元素有一个包含 company 的 class 属性。

    CSS 相邻兄弟选择器
        相邻兄弟选择器(Adjacent sibling selector)可选择紧接在另一元素后的元素,且二者有相同父元素。如果需要选择紧接在另一个元素后的元素,而且二者有相同的父元素,可以使用相邻兄弟选择器

    h1 + p {margin-top:50px;}
    <div>
      <ul>
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
      </ul>
      <ol>
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
      </ol>
    </div>

      在上面的片段中,div 元素中包含两个列表:一个无序列表,一个有序列表,每个列表都包含三个列表项。这两个列表是相邻兄弟,列表项本身也是相邻兄弟。不过,第一个列表中的列表项与第二个列表中的列表项不是相邻兄弟,因为这两组列表项不属于同一父元素(最多只能算堂兄弟)。
      请记住,用一个结合符只能选择两个相邻兄弟中的第二个元素。请看下面的选择器:

    li + li {font-weight:bold;}

      上面这个选择器只会把列表中的第二个和第三个列表项变为粗体。第一个列表项不受影响。

    结合其他选择器

    html > body table + ul {margin-top:20px;}

      这个选择器解释为:选择紧接在 table 元素后出现的所有兄弟 ul 元素,该 table 元素包含在一个 body 元素中,body 元素本身是 html 元素的子元素。

    伪类
        伪类的语法:

            selector : pseudo-class {property: value}

        CSS 类也可与伪类搭配使用:

      selector.class : pseudo-class {property: value}

    伪类与 CSS 类
        伪类可以与 CSS 类配合使用:

            a.red : visited {color: #FF0000}
            <a class="red" href="css_syntax.asp">CSS Syntax</a>

    CSS2 - :first-child 伪类
        first-child 伪类来选择元素的第一个子元素

        <div>
        <p>These are the necessary steps:</p>
        <ul>
        <li>Intert Key</li>
        <li>Turn key <strong>clockwise</strong></li>
        <li>Push accelerator</li>
        </ul>
        <p>Do <em>not</em> push the brake at the same time as the accelerator.</p>
        </div>

        在上面的例子中,作为第一个元素的元素包括第一个 p、第一个 li 和 strong 和 em 元素

        p:first-child {font-weight: bold;}
        li:first-child {text-transform:uppercase;}

        第一个规则将作为某元素第一个子元素的所有 p 元素设置为粗体。第二个规则将作为某个元素(在 HTML 中,这肯定是 ol 或 ul 元素)第一个子元素的所有 li 元素变成大写。
        提示:最常见的错误是认为 p:first-child 之类的选择器会选择 p 元素的第一个子元素。
        注释:必须声明 <!DOCTYPE>,这样 :first-child 才能在 IE 中生效。

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <style type="text/css">
    p:first-child {
      color: red;
      } 
    </style>
    </head>
    
    <body>
    <p>some text</p>
    <p>some text</p>
    </body>
    </html>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <style type="text/css">
    p > i:first-child {
      font-weight:bold;
      } 
    </style>
    </head>
    
    <body>
    <p>some <i>text</i>. some <i>text</i>.</p>
    <p>some <i>text</i>. some <i>text</i>.</p>
    </body>
    </html>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <style type="text/css">
    p:first-child i {
      color:blue;
      } 
    </style>
    </head>
    
    <body>
    <p>some <i>text</i>. some <i>text</i>.</p>
    <p>some <i>text</i>. some <i>text</i>.</p>
    </body>
    </html>

    CSS2 - :lang 伪类
        :lang 伪类使你有能力为不同的语言定义特殊的规则。在下面的例子中,:lang 类为属性值为 no 的 q 元素定义引号的类型:

    <html>
    <head>
    
    <style type="text/css">
    q:lang(no)
       {
       quotes: "~" "~"
       }
    </style>
    
    </head>
    
    <body>
    <p>文字<q lang="no">段落中的引用的文字</q>文字</p>
    </body></html>

    CSS 伪元素 (Pseudo-elements)
        伪元素的语法:

    selector:pseudo-element {property:value;}

      CSS 类也可以与伪元素配合使用:

    selector.class:pseudo-element {property:value;}
    <html>
    <head>
    <style type="text/css">
    p:first-line 
    {
    color: #ff0000;
    font-variant: small-caps
    }
    </style>
    </head>
    
    <body>
    <p>
    You can use the :first-line pseudo-element to add a special effect to the first line of a text!
    </p>
    </body>
    
    </html>
    注释:"first-line" 伪元素只能用于块级元素。
    
    注释:下面的属性可应用于 "first-line" 伪元素:
    
        font
        color
        background
        word-spacing
        letter-spacing
        text-decoration
        vertical-align
        text-transform
        line-height
        clear
    p:first-letter
      {
      color:#ff0000;
      font-size:xx-large;
      }
    注释:"first-letter" 伪元素只能用于块级元素。
    
    注释:下面的属性可应用于 "first-letter" 伪元素:
    
        font
        color
        background
        margin
        padding
        border
        text-decoration
        vertical-align (仅当 float 为 none 时)
        text-transform
        line-height
        float
        clear

    伪元素和 CSS 类
        伪元素可以与 CSS 类配合使用:

    p.article:first-letter
      {
      color: #FF0000;
      }
    
    <p class="article">This is a paragraph in an article。</p>
    例子会使所有 class 为 article 的段落的首字母变为红色。

    多重伪元素
        可以结合多个伪元素来使用。
        在下面的例子中,段落的第一个字母将显示为红色,其字体大小为 xx-large。第一行中的其余文本将为蓝色,并以小型大写字母显示。段落中的其余文本将以默认字体大小和颜色来显示:

    <html>
    
    <head>
    <style type="text/css">
    p:first-letter
      {
      color:#ff0000;
      font-size:xx-large;
      }
    
    p:first-line 
      {
      color:#0000ff;
      font-variant:small-caps;
      }
    </style>
    </head>
    
    <body>
    <p>You can combine the :first-letter and :first-line pseudo-elements to add a special effect to the first letter and the first line of a text!</p>
    </body>
    
    </html>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <style type="text/css">
    h1:before {content:url(/i/w3school_logo_white.gif)}
    </style>
    </head>
    
    <body>
    <h1>This is a heading</h1>
    <p>The :before pseudo-element inserts content before an element.</p>
    <h1>This is a heading</h1>
    <p><b>注释:</b>如果已规定 !DOCTYPE,那么 Internet Explorer 8 (以及更高版本)支持 content 属性。
    </body>
    </html>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <style type="text/css">
    h1:after {content:url(/i/w3school_logo_white.gif)}
    </style>
    </head>
    
    <body>
    <h1>This is a heading</h1>
    <p>The :before pseudo-element inserts content before an element.</p>
    <h1>This is a heading</h1>
    <p><b>注释:</b>如果已规定 !DOCTYPE,那么 Internet Explorer 8 (以及更高版本)支持 content 属性。
    </body>
    </html>

     

     





     

  • 相关阅读:
    高速传输线PCB设计
    带状线和微带线
    资源分配
    异步时钟切换电路
    Mathcad操作tips:2D绘图
    Mathcad操作tips:函数、符号计算
    慢性胃炎注意事项
    Arduino I2C + 三轴加速度计ADXL345
    Arduino SPI + SPI Flash芯片W25Q80BV
    Arduino I2C + 三轴加速度计LIS3DH
  • 原文地址:https://www.cnblogs.com/changzuidaerguai/p/6995652.html
Copyright © 2020-2023  润新知