HTML/CSS/Style Basics/Style overwrite

Материал из Web эксперт
Перейти к: навигация, поиск

CLASS overrules ELEMENT

   <source lang="html4strict">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>Specificity</title>
 <style type="text/css" media="screen">
   h1 {
     color:#000;
   }
   p {
     color:#000;
   }
   
   div h1 {
     color:#333;
   }
   div p {
     color:#333;
   }
   
   div.module h1 {
     color:#666;
   }
   div.module p {
     color:#666;
   }
   
 </style>
 

</head> <body>

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

</body> </html>

</source>
   
  


class style overwrites the wildcard style

   <source lang="html4strict">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html>

   <head>
       <title> Property Inheritance </title>
       <style type="text/css">
     * {
         font-family: sans-serif;
     }
     div {
         font-family: "Times New Roman";
     }
       </style>
   </head>
 <body>
     This font is sans-serif.
         This font is Times New Roman.
 </body>

</html>

</source>
   
  


ID overrules CLASS

   <source lang="html4strict">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>Specificity</title>
 <style type="text/css" media="screen">
   h1 {
     color:#000;
   }
   p {
     color:#000;
   }
   
   div h1 {
     color:#333;
   }
   div p {
     color:#333;
   }
   
   div.module h1 {
     color:#666;
   }
   div.module p {
     color:#666;
   }
   .module h1 {
     color:#000;
   }
   .module p {
     color:#000;
   }
   
 </style>
 

</head> <body>

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

</body> </html>

</source>
   
  


Id style overwrites the tag name style

   <source lang="html4strict">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html>

   <head>
       <title> Property Inheritance </title>
       <style type="text/css">
     ul li {
         border: thin solid black;
     }
     li#navigation {
         border: thick solid black;
     }
       </style>
   </head>
 <body>
  • This is a test.
 </body>

</html>

</source>
   
  


!important overrides all, including inline styles

   <source lang="html4strict">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>Specificity</title>
 <style type="text/css" media="screen">
   h1 {
     color:#000;
   }
   p {
     color:#000;
   }
   
   div h1 {
     color:#333;
   }
   div p {
     color:#333;
   }
   
   div.module h1 {
     color:#666;
   }
   div.module p {
     color:#666;
   }
   .module h1 {
     color:#000;
   }
   .module p {
     color:#000;
   }
    div#content h1 {
     color:#999;
   }
    div#content p {
     color:#999;
   }
   h1 {
     color:#ccc !important;
   }
    p {
     color:#ccc !important;
   }
 </style>
 

</head> <body>

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

</body> </html>

</source>
   
  


inline styles overrule everything except !important

   <source lang="html4strict">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>Specificity</title>
 <style type="text/css" media="screen">
   h1 {
     color:#000;
   }
   p {
     color:#000;
   }
   
   div h1 {
     color:#333;
   }
   div p {
     color:#333;
   }
   
   div.module h1 {
     color:#666;
   }
   div.module p {
     color:#666;
   }
   .module h1 {
     color:#000;
   }
   .module p {
     color:#000;
   }
    div#content h1 {
     color:#999;
   }
    div#content p {
     color:#999;
   }
   
 </style>
 

</head> <body>

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

Page Title

This is a test. This is a test.

</body> </html>

</source>
   
  


Style cascade

   <source lang="html4strict">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html>

   <head>
       <title> Property Inheritance </title>
       <style type="text/css">
     div {
         text-align: left;
         color: white;
     }
     div {
         text-align: center;
         color: blue;
     }
       </style>
   </head>
   <body>
        The text is aligned center and blue.
   </body>

</html>

</source>
   
  


Style cascade 1

   <source lang="html4strict">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html>

   <head>
       <title> Property Inheritance </title>
       <style type="text/css">
     div {
         text-align: left;
         color: white;
     }
     div {
         text-align: center;
         color: blue;
     }
       </style>
   </head>
   <body>
        The text is aligned center and blue.
   </body>

</html>

</source>