Cascading Style Sheets
Cascading Style Sheets
• A CSS rule has two main parts: a selector, and one or more declarations:
Example:
p{
color: red;
text-align: center;
}
CSS SELECTOR - Used to "find" (or select) HTML elements based on their element name, id,
class, attribute, and more
Example
p{
text-align: center;
color: red;
}
THE CLASS SELECTOR
• selects elements with a specific class attribute
• to select elements with a specific class, write a period (.) character, followed by the name of the
class
Example
.center {
text-align: center;
color: red;
}
• You can also specify that only specific HTML elements should be affected by a class.
Example
p.center {
text-align: center;
color: red;
}
GROUPING SELECTORS
Note:
If you have elements with the same style definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
CSS Comments
Comments are used to explain the code, and may help when you edit the source code
later.
Comments are ignored by browsers.
A CSS comment starts with /* and ends with */. Comments can also span multiple lines:
Example
p{
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
• The External Style Sheets allows you to place all your CSS rules in a separate plain-text file and
connect it to your documents.
• External Style sheets are saved with the file extension “.css” and it is connected through the use
of the link element.
• The link element should always be within the document header, <head>, element.
Example
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
2. INTERNAL STYLE SHEET
Example
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
3. INLINE STYLES
Example
EXAMPLE OF CSS:
Sizes
Padding and margin styles can be shorten too.
If you write them this way
• padding-left: 3px;
• padding-top: 4px;
• padding-bottom: 4px;
• padding-right: 3px;
Borders
• border-width: 1px;
• border-style: solid;
• border-color: #000;
Background
• background-color: #f00;
• background-image: url(background.gif);
• background-repeat: no-repeat;
• background-attachment: fixed;
• background-position: 0 0;
Fonts
• font-style: italic;
• font-variant: small-caps;
• font-weight: bold;
• font-size: 20px;
• line-height: 140%;
• font-family: "Lucida Grande",sans-serif;
Lists
• list-style-type: square;
• list-style-position: inside;
• list-style-image: url(image.gif);