Applying a Style to the Next Element
|
You can create a style that would be applied to an HTML
element only if/when that element follows a designated one. This is done using the
+ conditional operator between two names of tags or classes in the style definition.
Here are examples:
|
<!DOCTYPE html>
<html>
<head>
<title>Energy</title>
<style>
h1 {
color: Red;
font-size: 24pt;
font-family: Bodoni;
}
/* h3 comes after h1 */
h1 + h3 {
font-size: 14pt;
color: Purple;
font-family: Cambria;
}
/* h3 comes after p */
p + h3 {
font-size: 16pt;
color: Green;
font-family: Algerian;
}
/* h3 precedes p */
h3 + p {
color: Olive;
font-family: Times New Roman;
}
/* h3 precedes ul */
h3 + ul {
font-size: 0.22in;
font-family: Georgia;
color: fuchsia;
}
/* p precedes ul */
p + ul {
color: Blue;
font-size: 0.22in;
font-family: Script;
}
/* p comes after ul */
ul + p {
color: Maroon;
font-size: 0.18in;
font-family: Verdana;
}
a:link.references, a:active.references,
a:visited.references, a:hover.references
{
font-size: 12pt;
font-family: Courier New;
}
a:link.references { color: Red; }
a:active.references { color: Bisque }
a:visited.references { color: Blue }
a:hover.references { color: Green }
</style>
</head>
<body>
<h1>Energy</h1>
<h3>Introduction</h3>
<p>Energy is the result of (or produced from) work. Work can be done in various ways
such as a person standing up to walk or run, burning a piece of wood to heat up a
house, blowing in a musical instrument (like a trumpet or a saxophone) to produce
music, letting water from a river rotate some turbines in a dam.</p>
<h3>Means of Work</h3>
<ul>
<li>Heat: Energy can be the effect of an object that heats itself. This is the
example of the sun. An object can also be heated by other means. Such heating
effect produces energy
<li>Move: This is work as the effect of an object that moves from one point to
another. It is this movement, or change in location, that produces energy</li>
</ul>
<p>Some of the most common ways to use energy are to produce electricity at home
or in the office, to keep a vehicle moving through gas in the tank, or to boost
agriculture production by spraying chemicals to plants. As a result, there are
various ways to get energy: water, wind, heat, coal, etc.</p>
<h3>Forms of Energy</h3>
<p>Energy can be stagnant or transformed. Therefore, the two most common forms of
energy are:</p>
<ul>
<li>Potential Energy: This is energy that is stored in a substance or component.
For example, there is energy stored in water even when that water is not being
used or exploited</li>
<li>Kinetic Energy: The energy is kinetic as a result of work. For example,
the movement of water in a river or in an ocean is work and that work
produces energy</li>
</ul>
<p>For the potential energy to be usable, it must be transferred to kinetic
energy. Examples of how this transfer is done can be resumed as follows:</p>
<table border=5>
<thead>
<tr>
<th><p>Object or Source</p></td>
<th><p>Potential Energy</p></td>
<th><p>Kinetic Energy</p></td>
</tr>
</thead>
<tr>
<td>Person</td>
<td>Sitting down</td>
<td>Walking or running</td>
</tr>
<tr>
<td>Rock</td>
<td>Laying at the pick of a hill</td>
<td>Thrown away and moving/rolling</td>
</tr>
<tr>
<td>Water or Liquid</td>
<td>In a Bucket or in a Bottle</td>
<td>Bucket leaking or bottle spilling</td>
</tr>
<tr>
<td>Wood</td>
<td>Laying down</td>
<td>Burning</td>
</tr>
<tr>
<td>Atom</td>
<td>Untouched</td>
<td>Atom being split
</tr>
<tr>
<td>Musical Instrument</td>
<td>Untouched</td>
<td>Being played and producing sound</td>
</tr>
</table>
<p><a class="references" href="formulas.htm">Energy Formulas</a> |
<a href="resources.htm" class="references">Labs Resources</a> |
<a href="help.htm" class="references">School Help</a></p>
</body>
</html>
This is for an HTML document that contains p, h3,
and list elements among others:
- h1 + h3 indicates that the style must be
applied to an h3 element if/when that h3 element immediately follows
an h1 element. If a certain h3 element is not created immediately
after an h1 element, that style doesn't apply
- p + ul indicates that the style must apply to an unordered list if/when that list immediately follows a p
element. If an unordered list doesn't immediately a p element, that
style doesn't apply
- ul + p is the exact opposite of p + ul. It indicates
that the style should apply to a p paragraph that comes immediately
after an unordered list. If a p element is created after a
type of element other than an unordered list, the style must be ignored
The above code would produce:



Notice that the various p elements in the documents
are formatted differently.
The above technique can also be applied to identified elements and classes. Here are examples:
<!DOCTYPE html>
<html>
<head>
<title>Energy</title>
<style>
#main-title {
color: Red;
font-size: 24pt;
font-family: Bodoni;
}
/* sub-title comes after main-title */
#main-title + .sub-title {
font-size: 14pt;
color: Purple;
font-family: Cambria;
}
/* sub-title comes after p */
p + .sub-title {
font-size: 16pt;
color: Green;
font-family: Algerian;
}
/* sub-title precedes p */
.sub-title + p {
color: Olive;
font-family: Times New Roman;
}
/* h3 precedes ul */
.sub-title + ul {
font-size: 0.22in;
font-family: Georgia;
color: fuchsia;
}
/* p comes after ul */
ul + p {
color: Maroon;
font-size: 0.18in;
font-family: Verdana;
}
/* p precedes ul */
p + ul {
color: Blue;
font-size: 0.22in;
font-family: Script;
}
a:link.references, a:active.references,
a:visited.references, a:hover.references
{
font-size: 12pt;
font-family: Courier New;
}
a:link.references { color: Red; }
a:active.references { color: Bisque }
a:visited.references { color: Blue }
a:hover.references { color: Green }
</style>
</head>
<body>
<p id="main-title">Energy</p>
<p class="sub-title">Introduction</p>
<p>Energy is the result of (or produced from) work. Work can be done in various
ways such as a person standing up to walk or run, burning a piece of wood to heat
up a house, blowing in a musical instrument (like a trumpet or a saxophone) to
produce music, letting water from a river rotate some turbines in a dam.</p>
<p class="sub-title">Means of Work</p>
<ul>
<li>Heat: Energy can be the effect of an object that heats itself. This is the
example of the sun. An object can also be heated by other means. Such heating
effect produces energy
<li>Move: This is work as the effect of an object that moves from one point to
another. It is this movement, or change in location, that produces energy</li>
</ul>
<p>Some of the most common ways to use energy are to produce electricity at home or
in the office, to keep a vehicle moving through gas in the tank, or to boost
agriculture production by spraying chemicals to plants. As a result, there are
various ways to get energy: water, wind, heat, coal, etc.</p>
<p class="sub-title">Forms of Energy</p>
<p>Energy can be stagnant or transformed. Therefore, the two most common forms of
energy are:</p>
<ul>
<li>Potential Energy: This is energy that is stored in a substance or component.
For example, there is energy stored in water even when that water is not being
used or exploited</li>
<li>Kinetic Energy: The energy is kinetic as a result of work. For example,
the movement of water in a river or in an ocean is work and that work
produces energy</li>
</ul>
<p>For the potential energy to be usable, it must be transferred to kinetic
energy. Examples of how this transfer is done can be resumed as follows:</p>
<table border=5>
<thead>
<tr>
<th><p>Object or Source</p></td>
<th><p>Potential Energy</p></td>
<th><p>Kinetic Energy</p></td>
</tr>
</thead>
<tr>
<td>Person</td>
<td>Sitting down</td>
<td>Walking or running</td>
</tr>
<tr>
<td>Rock</td>
<td>Laying at the pick of a hill</td>
<td>Thrown away and moving/rolling</td>
</tr>
<tr>
<td>Water or Liquid</td>
<td>In a Bucket or in a Bottle</td>
<td>Bucket leaking or bottle spilling</td>
</tr>
<tr>
<td>Wood</td>
<td>Laying down</td>
<td>Burning</td>
</tr>
<tr>
<td>Atom</td>
<td>Untouched</td>
<td>Atom being split
</tr>
<tr>
<td>Musical Instrument</td>
<td>Untouched</td>
<td>Being played and producing sound</td>
</tr>
</table>
<p><a class="references" href="formulas.htm">Energy Formulas</a> |
<a href="resources.htm" class="references">Labs Resources</a> |
<a href="help.htm" class="references">School Help</a></p>
</body>
</html>
The above code would produce:

Applying a Style to a Nested Element
You can indicate that an element should receive a certain
style only if it is nested in a specific element. This is done by leaving an
empty space or using the > operator between the nesting tag or element and the
nested tag or element in the style definition. Consider the following example:
<!DOCTYPE html>
<html>
<head>
<title>Energy</title>
<style>
p {
font-family: Times New Roman;
color: black;
font-size: 10pt; }
h3 {
font-family: Georgia;
font-size: x-large;
color: red; }
h3 + p {
font-family: Georgia;
font-size: 0.42cm;
color: blue; }
li > h3 {
color: fuchsia;
font-family: Bodoni MT Black;
font-size: 0.22in; }
p + ul {
font-family: Georgia;
font-size: 0.42cm;
color: green; }
ul + p {
color: Maroon;
font-family: Agency FB;
font-size: 0.24in; }
a:link.references {
color: Red;
font-size: 12pt;
font-family: Courier New; }
a:active.references {
color: Bisque;
font-size: 12pt;
font-family: Courier New; }
a:visited.references {
color: Blue;
font-size: 12pt;
font-family: Courier New; }
a:hover.references {
color: Maroon;
font-size: 12pt;
font-family: Courier New; }
</style>
<head>
<body>
<h3>Forms of Energy</h3>
<p>Energy is work that produces heat. Work is the effect of an object that moves
from one point to another. It is this movement, or change in location, that
produces energy. As a result, there are various ways to get energy: water, wind,
heat (if it is moving from an autonomous source, such as the sun), coal, etc.</p>
<p>The two most common forms of energy are:</p>
<ul>
<li><h3>Potential Energy</h3>
This is energy that is stored in a substance or component. For example,
there is energy stored in water even when that water is not being used or
exploited</li>
<li><h3>Kinetic Energy</h3>
<p>The energy is kinetic as a result of work. For example, the movement of
water in a river or in an ocean is work and that work can produce energy</p></li>
</ul>
<p>For the potential energy to be usable, it must be transferred to kinetic
energy.</p>
<p><a class="references" href="formulas.htm">Energy Formulas</a> |
<a href="resources.htm" class="references">Labs Resources</a> |
<a href="help.htm" class="references">School Help</a></p>
</body>
</html>
The li > h3 code indicates that its style should
apply to an h3 element if that element is nested in a list item. If the
h3
element is not nested in a list item, the style doesn't apply. The above code
would produce:

You can also conditionally apply a style on an element only if the element has a certain identification or is using a certain class. Here are examples:
<!DOCTYPE html>
<html>
<head>
<title>Energy</title>
<style>
#main-title {
color: Red;
font-size: 24pt;
font-family: Bodoni;
}
/* sub-title comes after main-title */
#main-title + .sub-title {
font-size: 14pt;
color: Purple;
font-family: Cambria;
}
/* sub-title comes after p */
p + .sub-title {
font-size: 16pt;
color: Green;
font-family: Algerian;
}
/* sub-title precedes p */
.sub-title + p {
color: Olive;
font-family: Times New Roman;
}
/* h3 precedes ul */
.sub-title + ul {
font-size: 0.22in;
font-family: Georgia;
color: fuchsia;
}
/* p comes after ul */
ul + p {
color: Maroon;
font-size: 0.18in;
font-family: Verdana;
}
/* p precedes ul */
p + ul {
color: Blue;
font-size: 0.22in;
font-family: Script;
}
/* An element with class "key-word" is nested in a list item. */
li > .key-word {
color: fuchsia;
font-family: Bodoni MT Black;
font-size: 0.22in;
}
/* An element with class "key-word" is nested in a table cell. */
td .key-word {
color: DarkGreen;
font-family: Comic Sans MS;
font-size: 0.24in;
}
/* An element with class "key-word" is nested in a paragraph. */
p > .key-word {
color: Navy;
font-family: Bell MT;
font-size: 0.26in;
}
a:link.references, a:active.references,
a:visited.references, a:hover.references
{
font-size: 12pt;
font-family: Courier New;
}
a:link.references { color: Red; }
a:active.references { color: Bisque }
a:visited.references { color: Blue }
a:hover.references { color: Green }
</style>
</head>
<body>
<p id="main-title">Energy</p>
<p class="sub-title">Introduction</p>
<p>Energy is the result of (or produced from) work. Work can be done in various
ways such as a <quote class="key-word">person</quote> standing up to walk or run,
burning a piece of <quote class="key-word">wood</quote> to heat up a house, blowing
in a <quote class="key-word">musical instrument</quote> (like a trumpet or a
saxophone) to produce music, letting <quote class="key-word">water</quote> from
a river rotate some turbines in a dam.</p>
<p class="sub-title">Means of Work</p>
<ul>
<li>Heat: Energy can be the effect of an object that heats itself. This is the
example of the sun. An object can also be heated by other means. Such heating
effect produces energy
<li>Move: This is work as the effect of an object that moves from one point to
another. It is this movement, or change in location, that produces energy</li>
</ul>
<p>Some of the most common ways to use energy are to produce
<quote class="key-word">electricity</quote> at home or in the office, to keep a
vehicle moving through <quote class="key-word">gas</quote> in the tank, or to
boost agriculture production by spraying <quote class="key-word">chemicals</quote>
to plants. As a result, there are various ways to get energy: water, wind, heat,
coal, etc.</p>
<p class="sub-title">Forms of Energy</p>
<p>Energy can be stagnant or transformed. Therefore, the two most common forms of
energy are:</p>
<ul>
<li><p class="key-word">Potential Energy</p>
<p>This is energy that is stored in a substance or component.
For example, there is energy stored in water even when that water is not being
used or exploited</li>
<li><p class="key-word">Kinetic Energy</p>
The energy is kinetic as a result of work. For example,
the movement of water in a river or in an ocean is work and that work produces
energy</li>
</ul>
<p>For the potential energy to be usable, it must be transferred to kinetic
energy. Examples of how this transfer is done can be resumed as follows:</p>
<table border=5>
<thead>
<tr>
<th><p>Object or Source</p></td>
<th><p>Potential Energy</p></td>
<th><p>Kinetic Energy</p></td>
</tr>
</thead>
<tr>
<td><p class="key-word">Person</p></td>
<td>Sitting down</td>
<td>Walking or running</td>
</tr>
<tr>
<td><p class="key-word">Rock</p></td>
<td>Laying at the pick of a hill</td>
<td>Thrown away and moving/rolling</td>
</tr>
<tr>
<td><p class="key-word">Water or Liquid</p></td>
<td>In a Bucket or in a Bottle</td>
<td>Bucket leaking or bottle spilling</td>
</tr>
<tr>
<td><p class="key-word">Wood</p></td>
<td>Laying down</td>
<td>Burning</td>
</tr>
<tr>
<td><p class="key-word">Atom</p></td>
<td>Untouched</td>
<td>Atom being split
</tr>
<tr>
<td><p class="key-word">Musical Instrument</p></td>
<td>Untouched</td>
<td>Being played and producing sound</td>
</tr>
</table>
<p><a class="references" href="formulas.htm">Energy Formulas</a> |
<a href="resources.htm" class="references">Labs Resources</a> |
<a href="help.htm" class="references">School Help</a></p>
</body>
</html>
The above code would produce:


