HTML/CSS/Style Basics/Element Selector

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

Group style in different css sections

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
     <head>
      <meta http-equiv="content-type" content="text/xhtml; charset=windows-1252" />
      <meta http-equiv="content-language" content="en-us" />
        <title>Exercise 1</title>
        <style type="text/css" media="all">
            /* Paragraph text is black and in 12pt font */
            p {
                font-size: 12pt;
                color: black;
            }
            /* h1, h2 and p are in Times New Roman font */
            h1, h2, p {
                font-family: "Times New Roman";
            }
            /* h1 and h2 have green text and 22pt font */
            h1, h2 {
                color: green;
                font-size: 22pt;
            }
        </style>
     </head>
     <body>
        <h1>
            Heading 1
        </h1>
        <h2>
            Heading 2
        </h2>
        <p>
            Some paragraph text.
        </p>
    </body>
</html>



h1 and h2 have green text and 22pt font

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
     <head>
      <meta http-equiv="content-type" content="text/xhtml; charset=windows-1252" />
      <meta http-equiv="content-language" content="en-us" />
        <style type="text/css" media="all">
            
            h1, h2 {
                color: green;
                font-size: 22pt;
            }
        </style>
     </head>
     <body>
        <h1>
            Heading 1
        </h1>
        <h2>
            Heading 2
        </h2>
        <p>
            Some paragraph text.
        </p>
    </body>
</html>



Mix group selector and element selector

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>CSS Positioning Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style rel="stylesheet" type="text/css">
div.page {
  border: 1px solid #000000;
  margin: 10px;
  padding: 10px;
  width: 250px;
}
span.explanation {
  font-size: 10px;
  border: 1px solid #000000;
}
strong {
  font-weight: bold;
  font-size: 18px;
  line-height: 22px;
  border: 1px solid #000000;
}
code {
  font-size: 12px;
  font-weight: bold;
  border: 1px solid #000000;
}
</style>
</head>
<body>
  <div class="page">
    <span class="explanation">The 
        <code>span</code> 
        <strong>inline</strong> 
    </span>
  </div>
</body>
</html>



Selectors choose the element to apply formatting to.These may also be grouped together.

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
     <head>
      <meta http-equiv="content-type" content="text/xhtml; charset=windows-1252" />
      <meta http-equiv="content-language" content="en-us" />
        <title>Selectors and Grouping</title>
        <style rel="stylesheet" type="text/css">
            p {
        font-family: Arial;
        font-size: 14pt;
    }
    h1 {
        color: black;
        border: 5px solid black;
    }
    h2 {
        color: orange;
        border: 4px solid orange;
    }
    h3 {
        color: blue;
        border: 3px solid blue;
    }
    h1, h2, h3 {
        letter-spacing: 5px;
        padding: 10px;
    }
        </style>
     </head>
     <body>
        <h1>
            Heading 1
        </h1>
        <h2>
            Heading 2
        </h2>
        <h3>
            Heading 3
        </h3>
        <p>
            Selectors choose the element to apply formatting to.  These may also be 
            grouped together.
        </p>
    </body>
</html>



Separate the style

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: //www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http: //www.w3.org/1999/xhtml"xml: lang="en"lang="en">
<head>
<title></title>
<style type="text/css">
body, p {
  font-family: verdana, arial, helvetica, sans-serif;
  color: #000;
}
body {
  margin: 0px;
  padding: 0px;
  background-color: #ccc;
}
p {
  font-size: 0.7em;
  line-height: 1.4em;
}
#MainText {
  padding: 10px;
  margin: 5px;
  background-color: #fff;
}
</style>
</head>
<body>
<div id="MainText">
<p>this is a test. this is a test. this is a test. this is a test. .</p>
</div>
</body>
</html>



Set style for div, h1 and p together

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>CSS Positioning Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style rel="stylesheet" type="text/css">
div,h1,p {
  font-family: arial, verdana, sans-serif;
  border: 1px solid #000000;
  margin: 5px;
}
</style>
</head>
<body>
  <div id="page">
    <h1>This is the heading</h1>
    <p>Here is paragraph one.</p>
    <p>Here is paragraph two.</p>
    <p>Here is paragraph three.</p>
  </div>
</body> 
</html>



Set style for more than one tags

 
<!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" lang="en" xml:lang="en">
<head>
<title></title>
<style type="text/css">
h1, h2, h3 {
 font-size: 200%;
 background-image: url(bkgd2.jpg);
 background-repeat: no-repeat;
 background-attachment: fixed;
 background-position: center;
 padding: 1.5em;
 text-align: center;
 color: white;
}
</style>
</head>
<body>
  <h1>header 1</h1>
  <p>This is a text. This is a text. This is a text. This is a text.
  This is a text. This is a text. This is a text. This is a text.
  This is a text. This is a text. This is a text. This is a text.
  This is a text. This is a text. This is a text. This is a text.
  This is a text. This is a text. This is a text. This is a text.
  This is a text. This is a text. This is a text. This is a text.</p>
</body>
</html>



Style reuse across tags

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: //www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http: //www.w3.org/1999/xhtml"xml: lang="en"lang="en">
<head>
<title></title>
<style type="text/css">

body, div, p, td, ol, ul, li, h1, h2, h3 {
  font-family: verdana, arial, helvetica, sans-serif;
  color: #000;
}
p {
  font-size: 0.7em;
  line-height: 1.4em;
}
#MainText {
  padding: 10px;
  margin: 5px;
  background-color: #fff;
}
</style>
</head>
<body>
<div id="MainText">
<p>this is a test. this is a test. this is a test. this is a test. .</p>
</div>
</body>
</html>



Styles are shared between h2 and label elements that appear inside of a form

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
     <head>
        <style type="text/css">
            form h2, form label {
                font-size: 15px;
                padding: 3px;
            }
            
        </style>
        <title>Feedback Form - Widgets and What"s-its</title>
    </head>
    <body>
        <h1>Widgets and What"s-its</h1>
        <form>
            <h2>Tell us what"s on your mind..</h2>
            <div>
                <label for="feedback[name]">Name:</label>
                <input type="text" size="25" name="feedback[name]" />
            </div>
            <div>
                <label for="feedback[email]">Email:</label>
                <input type="text" size="25" name="feedback[email]" />
            </div>
            <div>
                <label for="feedback[address]">Address:</label>
                <textarea name="feedback[address]" cols="40" rows="3" wrap="virtual"></textarea>
            </div>
            <div>
                <label for="feedback[message]">Comments:</label>
                <textarea name="feedback[message]" cols="40" rows="6" wrap="virtual"></textarea>
            </div>
            <div>
                <label for="feedback[heard]">How"d you hear about us?</label>           
                <select name="feedback[heard]">
                    <option value="">Choose...</option>
                    <option value="newspaper">Newspaper</option>
                    <option value="magazine">Magazine</option>
                    <option value="television">Television</option>
                    <option value="radio">Radio</option>
                    <option value="other">Other</option>
                </select>
            </div>
        </form>
    </body>
</html>



Table in a DIV tag

 
<!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">
.center {
  text-align: center;
}
.center table {
 width: 50%;
 margin: 0 auto;
 text-align: left;
}
</style>
</head>
<body>
  <h2 id="warning">Lorem ipsum dolor sit amet</h2> 
  <div class="center">
   <table width="50%" border="1" cellpadding="30">
    <tr>
     <td>This is the first cell</td>
     <td>This is the second cell</td>
    </tr>
    <tr>
     <td>This is the third cell, it"s under the first cell</td>
     <td>This is the fourth cell, it"s under the second cell.</td>
    </tr>
   </table>
  </div>

</body>
</html>



Tag style selector

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title> exercise 4-1 </title>
        <style type="text/css" media="all">
            body {
                font-size: 12px;
            }
            h1 {
                font-size: 2.1em;
                background: pink;
                padding: 1em;
                border: thin solid black;
            }
            p {
                font-size: 0.9em;
                padding: 1em;
                border: thin solid black;
                background: gray;
                color: white;
            }
        </style>
    </head>
    <body>
        <h1>
            This is a heading
        </h1>
        <p>
            This is a paragraph of text.
        </p>
    </body>
</html>



To style all elements in a document with no bottom margin using * {margin-bottom:0;}

 

<!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">
* {
   margin-bottom:0;
}
</style>
</head>
<body>
  <h2>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. 
  </h2>
  <p>This is a text.</p>
</body>
</html>



To style all paragraphs in a document with a bottom margin of 10 pixels using p{margin-bottom:10px;}.

 

<!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">
p {
   margin-bottom:10px;
}
</style>
</head>
<body>
  <h2>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. 
  </h2>
  <p>This is a text.</p>
</body>
</html>