举例讲解CSS的子元素选择器用法

举例讲解CSS的子元素选择器用法

基础 子元素选择器只能一级一级向下寻找,不能跨越

 HTML代码:

XML/HTML Code
复制内容到剪贴板

  1. <p>  
  2.     this is my <strong><i>w</i>eb</strong> page.   
  3. </p>  

CSS代码:

CSS Code
复制内容到剪贴板

  1. p>strong { colorpurple; } p>strong>i { font-size50px; }   

1、与后代选择器相比,子元素选择器只能选择作为某元素子元素的元素。

2、子元素选择器使用大于号”>”做为连接符。

示例1:

XML/HTML Code
复制内容到剪贴板

  1. <html>  
  2.   
  3. <head>  
  4.     <style type=“text/css”>  
  5.         h1 > strong {   
  6.             color: red;   
  7.         }   
  8.     </style>  
  9. </head>  
  10.   
  11. <body>  
  12.     <h1>This is <strong>very</strong> <strong>very</strong> important.</h1>  
  13.     <h1>This is <em>really <strong>very</strong></em> important.</h1>  
  14. </body>  
  15.   
  16. </html>  

示例2 

XML/HTML Code
复制内容到剪贴板

  1. <html>  
  2.   
  3. <head>  
  4.     <style type=“text/css”>  
  5.         table.company td > p {   
  6.             color: red;   
  7.         }   
  8.     </style>  
  9. </head>  
  10.   
  11. <body>  
  12.     <table class=‘company’>  
  13.         <tr>  
  14.             <td>  
  15.                 <p>hello</p>  
  16.             </td>  
  17.         </tr>  
  18.     </table>  
  19.     <table>  
  20.         <tr>  
  21.             <td>  
  22.                 <p>world</p>  
  23.             </td>  
  24.         </tr>  
  25.     </table>  
  26. </body>  
  27.   
  28. </html>  

举例讲解CSS的子元素选择器用法

相关文章:

你感兴趣的文章:

标签云: