HTML/CSS/Style Basics/ID Selector

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

An ID should be unique within a document. If it is not, ID will match all elements with the same IDs.

   <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"> <head> <title></title> <style type="text/css">

  1. paragraph3{
 margin-bottom:40px;

} </style> </head> <body>

This is a header this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test.

This is a text.

This is a text.

</body> </html>

</source>
   
  


Combine the style from ID and 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"> <head> <title></title> <style type="text/css">

  • .z {
 padding: 5px;
 border: 5px solid black;

}

  1. float {
 width: 150px;
 height: 50px;

}

</style> </head> <body>

this is a test.
this is a test.
Sized Float

</body> </html>

</source>
   
  


id locator with #

   <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"> <head> <title></title> <style type="text/css">

  1. banner {
width: 550px;
height: 561px;
overflow: hidden;

} </style> </head> <body>

...

</body> </html>

</source>
   
  


tagName#id: use id name plus tag name to mark a 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 xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css">

 h2#warning {
  background: red;
 }
 h2#questions {
  background: pink;
 }
 h2#comment {
  background: yellow;
 }
 h2#document {
  background: purple;
 }
 h2#print {
  background: gold;
 }
 h2#search {
  background: gray;
 }

</style> </head> <body>

warning

question

comment

document

print

this is a text.

</body> </html>

</source>
   
  


To style one paragraph with an extra-large bottom margin of 40 pixels using #paragraph3{margin-bottom:40px;}.

   <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"> <head> <title></title> <style type="text/css">

  1. paragraph3{
 margin-bottom:40px;

} </style> </head> <body>

This is a header this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test.

This is a text.

This is a text.

</body> </html>

</source>