What Is CSS?: CSS Stands For Cascading Style Sheets
What Is CSS?: CSS Stands For Cascading Style Sheets
CSS Introduction wish to define, the property is the attribute you wish
to change, and each property can take a value. The
What is CSS? property and value are separated by a colon, and
surrounded by curly braces:
Style sheets allow style information to be specified in To make the style definitions more readable, you can
many ways. describe one property on each line, like this:
The style rule below will match the element that has
The paragraph above will be styled by the class
an id attribute with a value of "green":
"center" AND the class "bold".
You can also omit the tag name in the selector to #green {color:green}
define a style that will be used by all HTML elements
that have a certain class. In the example below, all The style rule below will match the p element that has
HTML elements with class="center" will be center- an id with a value of "para1":
aligned:
p#para1
.center {text-align:center} {
text-align:center;
In the code below both the h1 element and the p color:red
element have class="center". This means that both }
elements will follow the rules in the ".center" selector:
<head>
<link rel="stylesheet"
type="text/css" href="mystyle.css"
/>
</head>
The HTML file below links to an
external style sheet with the The browser will read the style definitions from
<link> tag: the file mystyle.css, and format the document
according to it.
<html>
<head> An external style sheet can be written in any
<link rel="stylesheet" text editor. The file should not contain any html
type="text/css" tags. Your style sheet should be saved with a
href="ex2.css" /> .css extension. An example of a style sheet file
</head> is shown below:
<body> hr {color:sienna}
<h1>This is a header 1</h1> p {margin-left:20px}
<hr /> body {background-
<p>You can see that the style image:url("images/back40.gif")}
sheet formats the text</p>
<html>
<head>
<style type="text/css">
body
{
background: #00ff00
url('smiley.gif') no-repeat fixed
center;
}
</style>
</head>
<body>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
<p>This is some text</p>
</body>
</html>
h1 {text-decoration:overline}
Text Color h2 {text-decoration:line-through}
h3 {text-decoration:underline}
The color property is used to set the color of
h4 {text-decoration:blink}
the text. The color can be set by:
<p class="big">
This is a paragraph with a bigger
line-height.
This is a paragraph with a bigger
line-height.
This is a paragraph with a bigger
line-height.
</p>
</body>
</html>
<body>
<p>
</body>
</html>
CSS Font
Disable text wrapping inside an
element CSS font properties define the font
This example demonstrates how to family, boldness, size, and the style of a
disable text wrapping inside an text.
element.
<html>
<head>
<style type="text/css">
Difference Between Serif and
p Sans-serif Fonts
{
white-space:nowrap;
}
</style>
</head>
<body>
<p>
This is some text. This is some
text. This is some text.
This is some text. This is some On computer screens, sans-serif fonts are
text. This is some text. considered easier to read than serif fonts.
This is some text. This is some
text. This is some text. CSS Font Families
This is some text. This is some In CSS, there is two types of font
text. This is some text. family names:
</p> generic family - a group of font
families with a similar look (like
</body> "Serif" or "Monospace")
</html> font family - a specific font
family (like "Times New Roman" or
Vertical alignment of an image "Arial")
This example demonstrates how to Generic Font Description
set the vertical align of an image family family
in a text.
Serif Times New Serif fonts have
<html>
Roman small lines at
<head>
Introduction to CSS page 10
Georgia the ends on some size adjustments to make
characters paragraphs look like headings, or
headings look like paragraphs.
Sans- Arial "Sans" means Always use the proper HTML tags,
serif Verdana without - these like <h1> - <h6> for headings and
fonts do not <p> for paragraphs.
have the lines The font-size value can be an
at the ends of absolute, or relative size.
characters Absolute size:
Monospace Courier All monospace Sets the text to a specified size
New characters has Does not allow a user to change
Lucida the same width the text size in all browsers (bad
Console for accessibility reasons)
Absolute size is useful when the
Font Family physical size of the output is
The font family of a text is set known
with the font-family property. Relative size:
The font-family property can hold Sets the size relative to
several font names as a "fallback" surrounding elements
system. If the browser does not Allows a user to change the text
support the first font, it tries size in browsers
the next font. If you do not specify a font
Start with the font you want, and size, the default size for normal
end with a generic family, to let text, like paragraphs, is 16px
the browser pick a similar font in (16px=1em).
the generic family, if no other Setting Text Size Using Pixels
fonts are available. Setting the text size with pixels,
gives you full control over the
Note: If the name of a font family text size:
is more than one word, it must be
Example
in quotation marks, like font-
family: "Times New Roman". h1 {font-size:40px}
More than one font family is h2 {font-size:30px}
specified in a comma-separated p {font-size:14px}
list:
The example above allows Firefox,
Chrome, and Safari to resize the
p{font-family:"Times New text, but not Internet Explorer.
Roman",Georgia,Serif} The text can be resized in all
browsers using the zoom tool
(however, this resizes the entire
page, not just the text).
Font Style Setting Text Size Using Em
The font-style property is mostly To avoid the resizing problem with
used to specify italic text. Internet Explorer, many developers
This property has three values: use em instead of pixels.
normal - The text is shown The em size unit is recommended by
normally the W3C.
italic - The text is shown in 1em is equal to the current font
italics size. The default text size in
oblique - The text is "leaning" browsers is 16px. So, the default
(oblique is very similar to size of 1em is 16px.
italic, but less supported)
p.normal {font-style:normal}
p.italic {font-style:italic}
p.oblique {font-style:oblique}
Font Size The size can be calculated from
The font-size property sets the pixels to em using this formula:
size of the text. pixels/16=em
Being able to manage the text size Example
is important in web design.
However, you should not use font
<body>
<p class="normal">This is a
paragraph.</p>
<p class="light">This is a
paragraph.</p>
<p class="thick">This is a
paragraph.</p>
<p class="thicker">This is a
paragraph.</p>
</body>
</html>
<html>
<head>
<style type="text/css">
p.ex1
{
font:italic arial,sans-serif;
}
Explanation of the different parts:
p.ex2
{ • Margin - Clears an area around the
font:italic bold 12px/30px border. The margin does not have a
arial,sans-serif; background color, and it is completely
} transparent
</style> • Border - A border that lies around the
</head> padding and content. The border is
affected by the background color of the
box
<body>
<p class="ex1">This is a • Padding - Clears an area around the
paragraph. This is a paragraph. content. The padding is affected by the
background color of the box
This is a paragraph. This is a
paragraph. This is a paragraph. • Content - The content of the box,
where text and images appear
This is a paragraph. This is a
paragraph. This is a
paragraph.</p>
<p class="ex2">This is a Width and Height of an
paragraph. This is a paragraph. Element
This is a paragraph. This is a
paragraph. This is a paragraph.
Important: When you specify the width and
This is a paragraph. This is a
height properties of an element with CSS, you
paragraph. This is a are just setting the width and height of the
paragraph.</p> content area. To know the full size of the
</body> element, you must also add the padding,
</html> border and margin.
Total element height = height + top padding + solid: Defines a solid border
bottom padding + top border + bottom border
+ top margin + bottom margin
double: Defines two borders. The width of the
two borders are the same as the border-width
value
Browsers Compatibility Issue
If you tested the previous example in Internet groove: Defines a 3D grooved border. The
Explorer, you saw that the total width was not effect depends on the border-color value
exactly 250px.
ridge: Defines a 3D ridged border. The effect
IE includes padding and border in the width, depends on the border-color value
when the width property is set, unless a
DOCTYPE is declared.
inset: Defines a 3D inset border. The effect
To fix this problem, just add a DOCTYPE to the depends on the border-color value
code:
Border Width
The border-width property is used to set the
width of the border.
The border-style property specifies what kind of Note: The "border-width" property does not
border to display. work if it is used alone. Use the "border-style"
property to set the borders first.
None of the other border properties will have
any effect unless border-style is set.
Example
Example
When using the border property, the order of
p the values are:
{
border-top-style:dotted; • border-width
border-right-style:solid; • border-style
border-bottom-style:dotted;
border-left-style:solid; • border-color
}
More Examples
All the top border properties in
one declaration
This example demonstrates a
shorthand property for setting all
of the properties for the top
border in one declaration.
<html>
<head>
<style type="text/css">
p
{
border-style:solid;
border-top:thick double #ff0000;
}
</style>
</head>
<body>
<p>This is some text in a
paragraph.</p>
</body>
</html>
Set the style of the bottom border
This example demonstrates how to
set the style of the bottom
border.
Set the width of the left border
This example demonstrates how to
set the width of the left border.
<html>
<head>
<style type="text/css">
p
{
border-style:solid;
border-left-width:15px;
}
</style>
</head>
<body>
<p><b>Note:</b> The "border-left-
width" property does not work if
it is used alone. Use the "border-
style" property to set the borders
first.</p>
</body>
</html>