style
attribute with any tag; value uses CSS syntax
with property:value
pairs separated by semicolons
<p style="color:red">This text is <em>red</i>
This text is red
Hello, <strong style="color:blue; font-size:18pt">blue text</strong>!
green
or burlywood
rgb
to specify red, green, blue decimal values (0-255)
<p style="color: rgb(0, 255, 0)">green</p>
<p style="color: rgb(255, 0, 255)">purple</p>
<p style="color: rgb(255, 255, 255)">white</p>
<p style="color: rgb(0, 0, 0)">black</p>
#RRGGBB
to specify red, green, blue using hexidecimal values (00-FF)
<p style="color: #FF00FF">magenta</p>
<style>
in the <head>
section of a
web page
h1
selects
<h1>
tags
.className
styles apply to elements with
class="className"
attribute
#idName
styles apply to the element with
id="idName"
attribute
<head>
<style type="text/css">
/* h1 and p are element selectors */
h1 { color: red; }
p { background-color: blue; }
/* Class selector */
.special {
font-size: small;
font-family: arial, sans-serif;
}
/* ID selector */
#email { text-decoration: none; }
/* Apply same styles to h2 AND h3 */
h2, h3 { text-decoration: underline; }
/* Descendant selector: only em used in li is bold */
li em { font-weight: bold; }
</style>
<!-- etc... -->
</head>
<h1>
tags in the page will appear red
<h1>I'm red</h1>
special
class by using the
class
attribute
<p class="special">I've got a blue background and use a small, Arial font</p>
#email
ID is not underlined
<a id="email" href="mailto:admin@gmail.com">System Administrator</a>
h5.coolGreen { color: rgb(10, 200, 200) }
The coolGreen
class can only be used with <h5>
tags:
<h5 class="coolGreen">I'm green</h5>
<h1 class="coolGreen">I'm NOT green!</h1>
<h5 class="coolGreen special">I'm green with small text</h5>
selector:pseudo-class
or selector.class:pseudo-class
a:link { color:red } /* unvisited link */
a:visited { color:green; } /* visited link */
a:hover { color:orange; } /* mouse over link */
a:active { color:blue; } /* selected link */
.css
extension)
that contains a CSS style sheet, and it can be imported into any web page
styles.css
(note: no HTML in the file):
body { font-family: arial; }
a { text-decoration: none; }
h1.cool { color: blue; text-align: center; }
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<p style="color: blue">This is a <strong>test</strong></p>
<strong>
inherited properties from its parent <p>
, so
"test" is blue
This is a test
<p style="color: blue">This is a <strong style="color: green">test</strong></p>
<strong>
overrides its parent and makes "test" green
This is a test
<b style="font-weight: normal;">Text is NOT bold</b>
background-image
do not
<body style="background-image: url(bison.png)">
Only the <body>
uses the bison.png image for its background, none of
its children do
<style>
p { color: red; }
</style>
<!-- inline beats embedded style -->
<p style="color: blue">I'm blue</p>
<style>
#mypar { color: purple; }
p { color: red; }
</style>
<!-- specific id beats general paragraph -->
<p id="mypar">I'm purple</p>
<!-- p { color: yellow; } -->
<link href="styles.css" rel="stylesheet">
<style>
p { color: green; }
</style>
<p>I'm green</p>
<style>
p { color: green; }
</style>
<!-- p { color: yellow; } -->
<link href="styles.css" rel="stylesheet">
<p>I'm yellow</p>