What
is CSS?
- CSS
stands for Cascading Style Sheets
- CSS describes how HTML elements are to be displayed
on screen, paper, or in other media
- CSS saves a lot of work. It can control the
layout of multiple web pages all at once
- CSS is used to define styles for your web pages,
including the design, layout and variations in display for different
devices and screen sizes.
- External stylesheets are stored in CSS files
CSS Syntax
A CSS rule-set consists of a selector and a
declaration block:
- The selector points to the HTML element you want
to style.
- The declaration block contains one or more
declarations separated by semicolons.
- Each declaration includes a CSS property name
and a value, separated by a colon.
- A CSS declaration always ends with a semicolon,
and declaration blocks are surrounded by curly braces.
Example:
p {
color: red;
text-align: center;
}
Three
Ways to Insert CSS
There are three ways of inserting a
style sheet:
- External style sheet
- Internal style sheet
- Inline style
1. External
Style Sheet
With an external style sheet, you can change the look
of an entire website by changing just one file!
Each page must include a reference to the external
style sheet file inside the <link> element. The <link> element goes
inside the <head> section:
An external style sheet can be written in any text
editor. The file should not contain any html tags. The style sheet file must be
saved with a .css extension.
Example:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
Here is how the
"myStyle.css" looks:
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value and the
unit (such as margin-left:20 px;
). The
correct way is: margin-left:20px;
2. Internal
Style Sheet
An internal style sheet may be used if one single
page has a unique style.
Internal styles are defined within the <style>
element, inside the <head> section of an HTML page:
Example:
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
3. Inline
Styles
An inline style may be used to apply a unique style
for a single element.
To use inline styles, add the style attribute to the
relevant element. The style attribute can contain any CSS property.
The example below shows how to change the color and
the left margin of a <h1> element:
Example:
<h1 style="color:blue;margin-left:30px;">This
is a heading.</h1>
Note: If some properties have been defined for the same
selector (element) in different style sheets, the value from the last read
style sheet will be used.
CSS Selectors
CSS selectors are used to "find" (or select) HTML
elements based on their element name, id, class, attribute, and more.
The element
Selector
The element selector selects elements based on the
element name.
Example:
p {
text-align: center;
color: red;
}
The id Selector
·
The id selector uses the id attribute of an HTML
element to select a specific element.
·
The id of an element should be unique within a
page, so the id selector is used to select one unique element!
·
To select an element with a specific id, write a
hash (#) character, followed by the id of the element.
Example:
#para1 {
text-align: center;
color: red;
}
<p
id="para1">
.....................................................
...................................................
</p>
The class
Selector
·
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;
}
Grouping
Selectors
If you have elements with the same style definitions,
it will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a
comma.
Example:
h1, h2, p {
text-align: center;
color: red;
}
Important CSS
properties
Color - The color property
specifies the color of text.
Example:
body {
color: red;
}
h1 {
color: #00ff00;
}
Background-image
- The background-image property sets one or more background images
for an element. By default, a background-image is placed at the top-left corner
of an element, and repeated both vertically and horizontally.
Example:
body {
background-image: url("img_tree.gif"),
url("paper.gif");
background-color: #cccccc;
}
Background-color
- Set the background color for a page:
Example:
body {background-color: coral;}
Background - Set
different background properties in one declaration for background color, image,
position, size, attachment, etc.
Example:
body {
background: lightblue
url("img_tree.gif") no-repeat fixed center;
}
Border - The border property
is a shorthand property for:
Example:
h1 {
border: 5px solid red;
}
h2 {
border: 4px dotted blue;
}
Text-align - The text-align property
specifies the horizontal alignment of text in an element.
Values are left|right|center|justify|initial|inherit;
Example:
div {
text-align: center;
}
Text-decoration
- The text-decoration property specifies the decoration added to
text, and is a shorthand property for:
- text-decoration-line (required)
- text-decoration-color
- text-decoration-style
Example:
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
h4 {
text-decoration: underline overline;
}
Font - The font property
is a shorthand property for:
The font-size and font-family values are required. If one of the
other values is missing, their default value are used.
Example:
p {
font: italic bold 12px/30px Georgia, serif;
}
Font-size - The font-size property
sets the size of a font.
div {
font-size: 15px;
}
Font-family - The font-family property
specifies the font for an element.
The font-family property can hold several font names as
a "fallback" system. If the browser does not support the first font,
it tries the next font.
Example:
H2 {
font-family: "Times New Roman", Times,
serif;
}
Font-style - The font-style property
specifies the font style for a text. Values are normal|italic|oblique|initial|inherit
Example:
p {
font-style: italic;
}
font-weight - The font-weight property
sets how thick or thin characters in text should be displayed.
The values are - normal|bold|bolder|lighter|number|initial|inherit;
Example:
p {
font-weight: bold;
}
letter-spacing
- The letter-spacing property increases or decreases the space
between characters in a text. The values are normal|length|initial|inherit;
Example:
h2 {
letter-spacing: 2px;
}
Float - The float property
specifies how an element should float.
Example:
img {
float: right;
}
Margin - The margin property
sets the margins for an element, and is a shorthand property for the following
properties:
Example:
p {
margin: 35px;
}
p {
margin: 10px 5px 15px 20px;
}
Padding - An element's
padding is the space between its content and its border.
The padding property is a shorthand property for:
·
Example:
Example:
p {
padding: 35px;
}
z-index - The z-index property
specifies the stack order of an element.
An element with greater stack order is always in front of an
element with a lower stack order.
Example:
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
Position - The position property
specifies the type of positioning method used for an element (static, relative,
absolute, fixed, or sticky).
Example:
h2 {
position: absolute;
left: 100px;
top: 150px;
}
Value
|
Description
|
static
|
Default value. Elements render in order, as they appear in the
document flow
|
absolute
|
The element is positioned relative to its first positioned (not
static) ancestor element
|
fixed
|
The element is positioned relative to the browser window
|
relative
|
The element is positioned relative to its normal position, so
"left:20px" adds 20 pixels to the element's LEFT position
|
sticky
|
The element is positioned based on the user's scroll position
A
sticky element toggles between relative and fixed , depending on the
scroll position. It is positioned relative until a given offset position is
met in the viewport - then it "sticks" in place (like
position:fixed).
Note: Not supported in
IE/Edge 15 or earlier. Supported in Safari from version 6.1 with a -webkit-
prefix.
|
initial
|
Sets this property to its default value.
|
inherit
|
Inherits this property from its parent element.
|
|
|
|
|
Top - The top property
affects the vertical position of a positioned element. This property has no
effect on non-positioned elements.
Example:
div {
position: absolute;
top: 50px;
border: 3px solid green;
}