Cascading Style Sheets (CSS) : The Basics of
Cascading Style Sheets (CSS) : The Basics of
Shakespeare
Longfellow
CSS
Lets Write Some Poetry!
YOU
Introduction
What do you know about CSS?
What do you hope to do with CSS?
How familiar are you with HTML?
Presentation Summary
1. What is CSS?
An Example
Consider the boldface font in the following examples:
Chunky bacon is delicious.
Meaning is
conveyed by the
styling
Remove the style
and meaning is lost
No additional meaning is
lost when removed
What is CSS?
Style.css
/* Styles for sitename.com*/
body {
font-family:Arial;
background: #000;
}
#container {
text-align:left;
width:1020px;
}
#header {
height:232px;
}
#footer {
width: 100%;
padding: 0 10px;
margin-bottom: 10px;
And so on.
CSS Benefits
Separates structure from presentation
Easy to learn
menu
main
footer
Selectors
A selector, here in green, is often an element of
HTML.
body { property: value; }
h1 { property: value; }
em { property: value; }
p { property: value; }
body {
background: purple;
color: green;
}
Grouping Selectors
Group the same selector with different declarations
together on one line.
h1 {color: black;}
h1 {font-weight: bold;}
h1 {background: white;}
Example of grouping selectors (both are correct):
h1 {
color: black;
font-weight: bold;
background: white;
}
Grouping Selectors
Group different selectors with the same declaration on
one line.
h1 {color: yellow;}
h2 {color: yellow;}
h3 {color: yellow;}
Comments in CSS
Explain the purpose of the coding
Help others read and understand the code
Serve as a reminder to you for what it all means
Paragraph
To start off our understanding of cascading
style sheets, we're going to use a special line of
CSS code that does something HTML alone
could never do right we're going to indent
every paragraph automatically.
Here's the CSS code:
p { text-indent: 3em; }
import
@import can be used in conjunction with the
other methods. Imagine you want 2 pages out of
your initial 10 pages to have, in addition to the
normal indent, each and every paragraph in
blue text. You could write a second style sheet,
we'll call it coolblue.css, and inside that sheet
you have:
p { color: blue; }
import
Then, in those 2 special pages, you place the
normal CSS link, but you'll add special
code,@import, to add the extra color.
<link rel="stylesheet" type="text/css"
href="main.css" title="Default">
<style type="text/css">
<!-@import url(coolblue.css);
--></style>
Basis overview
Those are the basics. Let's review the ways you
can include a style sheet:
Write it inline inside each and every tag
Place a <style> </style> at the beginning of the
web page
Dedicate and link a CSS file and write it inside
that file
use @import to include it as portion of a page's
CSS
Exercise 1
Use <style> to make all paragraphs have 10 spaces indentation
(hint: 6em) and make the text red. Hint: Combine both into one
line of code using the ; separator. Remember to create a
paragraph in the <body> to see the style in action! Generic text
below.
This is the first paragraph
with the red text and large indent.
This is the second paragraph
with the red text and large indent.
Solution
<html>
<head>
<style type="text/css">
<!-p { text-indent: 6em; color: red; }
--></style>
</head>
<body>
<p>This is the first paragraph<br>
with the red text and large indent.</p>
<p>This is the second paragraph<br>
with the red text and large indent.</p>
</body>
</head>
</html>
Headers
If you want to make all H1, H2, and H3 red, and all
H4, H5, H6 yellow, your style could look like this:
h1, h2, h3 { color: red; }
h4, h5, h6 { color: yellow; }
You can use the comma to say you want to define a
style for multiple selectors at the same time.
You can set a style for nearly all HTML elements.
Normal paragraph
PSEUDO-ELEMENTS
There are two important pseudo-elements that are built into CSS
capable web browsers. (There are also common pseudo-classes
which you'll learn in the links chapter.)
These two elements are :first-letter and :first-line. Notice that
pseudo-elements are defined with a : instead of a . or # (this is
because they have special meanings to a web browser).
Here's a silly example for each: Imagine you want the first letter
of each paragraph to be red, or the first-line of each paragraph to
be green.
p:first-letter { color: red; }
p:first-line { color: green; }
Cont
Big deal right?
But CSS adds some special features. One of the most important
is thebackground-repeat property.
It has these values: repeat, repeat-x, repeat-y, or no-repeat. A
regular web page has a default of background-repeat: repeat,
which means the image is repeated both horizontally and
vertically. With CSS, you can set the background to repeat
horizontally (repeat-x), repeat vertically (repeat-y), or not repeat
at all (no-repeat).
Images cont
We can edit the style mentioned above to have the body's
background never repeat by adding background-repeat: norepeat:
body { background-image: url(graphic.jpg);
color: #FFFFFF; background-color: #000000;
background-repeat: no-repeat; }
If you want to include the repeat in your standard background
tag (for example, if are not using CSS for the rest of your page),
you can add style="background-repeat: no-repeat;", so it looks
like this:
<body background="graphic.jpg" text="#FFFFFF"
bgcolor="#000000" style="background-repeat: no-repeat;">
Key features
If you want a background to be at the top right,
use: background-position: top right. If you want
it to be at the bottom center, use backgroundposition: bottom center. This is typically most
useful used with background-repeat: no repeat.
Key Cont
As you can see, the coding for the background can get pretty
long. CSS lets you combine it all into a single property
statement, known as background. It follows this format:
background: background-color || background-image ||
background-repeat || background-attachment || backgroundposition
If you want a background color of white, a background
image lightpattern.jpg, the background to never repeat, and
never scroll, you could use:
body { background: #FFFFFF url(lightpattern.jpg) no-repeat
fixed; }
Key Cont
Remember, you'll also need to set the text color, so add color: #000000 (if
you want black text)
body { background: #FFFFFF url(lightpattern.jpg) no-repeat fixed; color:
#000000; }
Notice that the browser is smart enough to realize that a value (in this case:
background-position) is missing and it ignores that value.
Always set a text and bgcolor in <body> for full browser compatibility.
la la la la
<html>
<head>
<style type="text/css">
<!-body { background: #EEEEEE url(/graphx/back.jpg) repeat-y scroll; }
--></style>
</head>
<body>la la la la
</body>
</html>
<html><head><style type="text/css"><!-- body { background: #FFFFFF
url(/graphx/coddsite.gif) no-repeat fixed center left; }-></style></head><body></body></html>
menu
main
footer
<div id=container>
<div id=header>Insert Title</div>
<div id=main">content
<div id=menu>content</div>
</div>
<div id=footer>content</div>
</div>
HTML Code:
<h1 id=mainHeading>Names</h1>
<p class=name>Joe</p>
CSS Code:
Background-color
Width
Padding
Margin
Border-width
Border-color
Border-style
HTML
div id=header
div id=content
div id=footer
CSS
#content {
background-color: #ccc;
margin-bottom: 10px;
border: 1px dashed blue;
color: #fff;
width: auto;
}
margin
padding
width
height
border
div id=box
#box {width=50px}
#box {height=auto}
#box {width=50em}
#box {width=100%}
#box {width=auto}
div id=box
div id=box1
div id=box2
div id=box3
#box3 { background-color: white; border:
1px solid #000; clear: both;}
#box {
border: red dotted 1px;
#box {
border-color: red;
border-style: dotted;
border-width: 2px;
padding: 10px;
Padding: 10px 10px;
div id=box
margin: 10px;
or
div id=box
margin-left: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-top: 10px;
Text Properties
MAIN HEADING
.mainHeading {
color: red;
letter-spacing: 5px;
text-transform: uppercase;
word-spacing: 15px;
text-align: left;
font-family: Times;
text-decoration: underline;
font-size: 12px;
font-style: italic;
font-weight: bold;
}
CSS Colors
Standard
Hexadecimal
#ffffff
#fff
#cccf0f3
White
Black
Blue
Fuchsia
Gray
Green
Lime
Aqua
Styling Links
The links property defines how inactive, hovered,
active, and visited link states appear to the user.
a:link {color: red; text-decoration:
none;border-bottom: 1px dashed red;
background: white;}
a:visited {color: yellow;}
a:active {color: green;}
a:hover {color: orange;}
Including Images
Properties for working with images include:
Background-image
Background-repeat
Background-position
Background-attachment
Layering
Background colors
and images are
layered like sheets
of paper one on top
of the other.
div id=bg
div id=main
div id=box
Background-Image
The background-image property sets an image
in the background of an element.
Background images and colors are layered.
Background-Repeat
The background-repeat property sets an
image in the background of an element and
tiles, or repeats, it. Tiling is the default.
li {
background-image:url(flower.jpg);
background-repeat:no-repeat;
}
repeat
Image Positioning
The background-position
property positions the image
using either combined
keywords (top, bottom, left,
right, and center); length
values; or percentage values.
background-position: right top;
/*can also use number values*/
background-attachment: fixed;
/*can also use scroll*/
left
center
top
top
left
center
right
bottom
bottom
bottom
Resources
Great Book
Thank You
I hope you enjoyed this presentation and learned
some basic CSS. I hope this will help with