Cascading Style Sheets
Source: [Link]
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
• External stylesheets are stored in CSS files
CSS Syntax
• A CSS rule 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.
• Multiple CSS declarations are separated with semicolons, and declaration blocks are
surrounded by curly braces.
CSS Syntax
Example
In this example all <p> elements will be center-aligned, with a red text color:
p{
color: red;
text-align: center;
Example Explained
• p is a selector in CSS (it points to the HTML element you want to style: <p>).
• color is a property, and red is the property value
• text-align is a property, and center is the property value
CSS Comments
• CSS comments are not displayed in the browser, but
they can help document your source code.
• Comments are used to explain the code, and may help
when you edit the source code at a later date.
• Comments are ignored by browsers.
• A CSS comment is placed inside the <style> element, and
starts with /* and ends with */
CSS Comments
• Example
/* This is a single-line comment */
p{
color: red;
}
• You can add comments wherever you want in the code:
p{
color: red; /* Set text color to red */
}
CSS Comments
• Comments can also span multiple lines:
/* This is
a multi-line
comment */
p{
color: red;
}
CSS Selectors
A CSS selector selects the HTML
element(s) you want to style.
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
• Simple selectors (select elements based on name, id, class)
• Combinator selectors (select elements based on a specific relationship
between them)
• Pseudo-class selectors (select elements based on a certain state)
• Pseudo-elements selectors (select and style a part of an element)
• Attribute selectors (select elements based on an attribute or attribute value)
The CSS element Selector
The element selector selects HTML elements based on the element name.
Example
Here, all <p> elements on the page will be center-aligned, with a red text color:
p{
text-align: center;
color: red;
}
The CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is 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
The CSS rule below will be applied to the HTML element with id="para1":
#para1 {
text-align: center;
color: red;
}
The CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is 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
The CSS rule below will be applied to the HTML element with id="para1":
#para1 {
text-align: center;
color: red;
} Note: An id name cannot start with a number!
The CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed
by the class name.
Example
In this example all HTML elements with class="center" will be red and center-aligned:
.center {
text-align: center;
color: red;
}
The CSS class Selector
You can also specify that only specific HTML elements should be
affected by a class.
Example
In this example only <p> elements with class="center" will be red
and center-aligned:
[Link] {
text-align: center;
color: red;
}
The CSS class Selector
HTML elements can also refer to more than one class.
Example
In this example the <p> element will be styled according to
class="center" and to class="large":
<p class="center large">This paragraph refers to two classes.</p>
Note: A class name cannot start with a number!
The CSS Universal Selector
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
*{
text-align: center;
color: blue;
}
The CSS Grouping Selector
The grouping selector selects all the
HTML elements with the same style
definitions.
The CSS Grouping Selector
Look at the following CSS code (the h1, h2, and p elements have the same style
definitions):
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
The CSS Grouping Selector
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
Example
• In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
How To Add CSS
When a browser reads a style sheet, it
will format the HTML document
according to the information in the
style sheet.
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
1. External CSS
2. Internal CSS
3. Inline CSS
External CSS
With an external style sheet, you can change the look of
an entire website by changing just one file!
Each HTML page must include a reference to the
external style sheet file inside the <link> element, inside
the head section.
Example External CSS
External styles are defined within the <link> element,
inside the <head> section of an HTML page:
Example External CSS
• An external style sheet can be written in any text
editor, and must be saved with a .css extension.
• The external .css file should not contain any HTML tags.
• Here is how the "[Link]" file looks:
Note: Do not add a space between the property value and the unit:
Incorrect (space): margin-left: 20 px;
Correct (nospace): margin-left: 20px;
Internal CSS
An internal style sheet may be used if one single HTML
page has a unique style.
The internal style is defined inside the <style> element,
inside the head section.
Example Internal CSS
Internal styles are defined within the <style> element,
inside the <head> section of an HTML page:
Inline CSS
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.
Example Inline CSS
Inline styles are defined within the "style" attribute of
the relevant element:
Tip: An inline style loses many of the advantages of a style sheet (by
mixing content with presentation). Use this method sparingly.
Multiple Style Sheets
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.
Example Multiple Style Sheets
If the internal style is defined after the link to the
external style sheet, the <h1> elements will be "orange":
Example Multiple Style Sheets
However, if the internal style is defined before the link
to the external style sheet, the <h1> elements will be
"navy":
Cascading Order
What style will be used when there is more than one
style specified for an HTML element?
All the styles in a page will "cascade" into a new "virtual" style sheet by the
following rules, where number one has the highest priority:
1. Inline style (inside an HTML element)
2. External and internal style sheets (in the head section)
3. Browser default
So, an inline style has the highest priority, and will override external and
internal styles and browser defaults.
CSS Colors
Colors are specified using predefined color names, or
RGB, HEX, HSL, RGBA, HSLA values.
In CSS, a color can be specified by using a predefined
color name:
CSS Background Color
You can set the background color for HTML elements:
Example:
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
CSS Text Color
You can set the color of text:
Example:
CSS Border Color
You can set the color of borders:
Example:
CSS Color Values
In CSS, colors can also be specified using RGB values, HEX values, HSL values,
RGBA values, and HSLA values:
Same as color name "Tomato":
Same as color name "Tomato", but 50% transparent:
Example:
CSS RGB Colors
An RGB color value represents RED, GREEN, and BLUE light sources.
• In CSS, a color can be specified as an RGB value, using this formula:
rgb(red, green, blue)
• Each parameter (red, green, and blue) defines the intensity of the color
between 0 and 255.
• For example, rgb(255, 0, 0) is displayed as red, because red is set to its
highest value (255) and the others are set to 0.
• To display black, set all color parameters to 0, like this: rgb(0, 0, 0).
• To display white, set all color parameters to 255, like this: rgb(255, 255, 255).
CSS HEX Colors
A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG
(green) and BB (blue) hexadecimal integers specify the components of the
color.
In CSS, a color can be specified using a hexadecimal value in the form:
#rrggbb
• Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00
and ff (same as decimal 0-255).
• For example, #ff0000 is displayed as red, because red is set to its highest
value (ff) and the others are set to the lowest value (00).
• To display black, set all values to 00, like this: #000000.
• To display white, set all values to ff, like this: #ffffff.
CSS HSL Colors
HSL stands for hue, saturation, and lightness.
In CSS, a color can be specified using hue, saturation, and lightness (HSL) in the
form: hsl(hue, saturation, lightness)
• Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and
240 is blue.
• Saturation is a percentage value, 0% means a shade of gray, and 100% is the
full color.
• Lightness is also a percentage, 0% is black, 50% is neither light or dark, 100%
is white
CSS Backgrounds
The CSS background properties are used to add background effects for
elements.
CSS background properties:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
• background (shorthand property)
CSS background-color
The background-color property specifies the background color of an element.
Example
The background color of a page is set like this:
• With CSS, a color is most often specified by:
• a valid color name - like "red"
• a HEX value - like "#ff0000"
• an RGB value - like "rgb(255,0,0)"
CSS background-color
Other Elements
You can set the background color for any HTML elements:
Example
Here, the <h1>, <p>, and <div> elements will have different background colors:
CSS Opacity / Transparency
The opacity property specifies the opacity/transparency of an element. It can
take a value from 0.0 - 1.0. The lower value, the more transparent:
Example:
Note: When using the opacity property to add transparency to the background of an element, all
of its child elements inherit the same transparency. This can make the text inside a fully
transparent element hard to read.
Transparency using RGBA
If you do not want to apply opacity to child elements, like in our example
above, use RGBA color values. The following example sets the opacity for the
background color and not the text:
• In addition to RGB, you can use an RGB color value with an alpha channel
(RGBA) - which specifies the opacity for a color.
• An RGBA color value is specified with: rgba(red, green, blue, alpha).
The alpha parameter is a number between 0.0 (fully transparent) and 1.0
(fully opaque).
Example:
CSS background-image
The background-image property specifies an image to use as the background of
an element.
By default, the image is repeated so it covers the entire element.
Example
• Set the background image for a page:
• The background image can also be set for specific elements, like the <p>
element:
Note: When using a background image, use an image that does not disturb the text.
CSS background-repeat
• By default, the background-image property repeats an image both
horizontally and vertically.
• Some images should be repeated only horizontally or vertically, or they will
look strange.
• If the image above is repeated only horizontally (background-repeat: repeat-
x;), the background will look better:
Tip: To repeat an image vertically, set background-repeat: repeat-y;
CSS background-repeat: no-repeat
• Showing the background image only once is also specified by the background-
repeat property:
Example
• Show the background image only once:
• In the example, the background image is placed in the same place as the text.
We want to change the position of the image, so that it does not disturb the
text too much.
CSS background-position
The background-position property is used to specify the position of the
background image.
Example
Position the background image in the top-right corner:
CSS background-attachment
The background-attachment property specifies whether the background image
should scroll or be fixed (will not scroll with the rest of the page):
Example
Specify that the background image should be fixed:
CSS background - Shorthand property
To shorten the code, it is also possible to specify all the background properties
in one single property. This is called a shorthand property.
Instead of writing:
You can use the shorthand property background:
CSS background - Shorthand property
When using the shorthand property the order of the property values is:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
It does not matter if one of the property values is missing, as long as the other
ones are in this order. Note that we do not use the background-attachment
property in the examples above, as it does not have a value.
CSS Borders
The CSS border properties allow you to specify the style, width, and color of an
element's border.
CSS Borders
The border-style property specifies what kind of border to display.
The following values are allowed:
• dotted - Defines a dotted border
• dashed - Defines a dashed border
• solid - Defines a solid border
• double - Defines a double border
• groove - Defines a 3D grooved border. The effect depends on the border-color
value
• ridge - Defines a 3D ridged border. The effect depends on the border-color value
• inset - Defines a 3D inset border. The effect depends on the border-color value
• outset - Defines a 3D outset border. The effect depends on the border-color value
• none - Defines no border
• hidden - Defines a hidden border
The border-style property can have from one to four values (for the top border,
right border, bottom border, and the left border).
CSS Borders
Example
• Demonstration of the different border styles:
Note: None of the OTHER CSS border
properties (which you will learn more about
in the next chapters) will have ANY effect
unless the border-style property is set!
Result:
CSS Border Width
The border-width property specifies the width of the four borders.
The width can be set as a specific size (in px, pt, cm, em, etc) or by using one of
the three pre-defined values: thin, medium, or thick:
Demonstration of the different border styles:
• Results:
CSS Border Specific Side Widths
The border-width property can have from one to four values (for the top
border, right border, bottom border, and the left border):
CSS Border Color
The border-color property is used to set the color of the four borders.
The color can be set by:
name - specify a color name, like "red"
HEX - specify a HEX value, like "#ff0000"
RGB - specify a RGB value, like "rgb(255,0,0)"
HSL - specify a HSL value, like "hsl(0, 100%, 50%)"
Transparent
Note: If border-color is not set, it inherits the color of the element.
CSS Border Color
CSS Border Color
The border-color property can have from one to four values (for the top
border, right border, bottom border, and the left border).
The color of the border can also be specified using a hexadecimal value (HEX),
RGB values, and HSL values.
CSS Border Sides
In CSS, there are also properties for specifying each of the borders (top, right,
bottom, and left):
Result:
The example above gives the same result as this:
CSS Border Sides
So, here is how it works:
If the border-style property has four values:
border-style: dotted solid double dashed;
top border is dotted
right border is solid
bottom border is double
left border is dashed
If the border-style property has three values:
border-style: dotted solid double;
top border is dotted
right and left borders are solid
bottom border is double
CSS Border Sides
If the border-style property has two values:
border-style: dotted solid;
top and bottom borders are dotted
right and left borders are solid
If the border-style property has one value:
border-style: dotted;
all four borders are dotted
CSS Border Sides
Like you saw in the previous page, there are many properties to
consider when dealing with borders.
To shorten the code, it is also possible to specify all the individual
border properties in one property.
The border property is a shorthand property for the following
individual border properties:
• border-width
• border-style (required)
• border-color
CSS Border Sides
You can also specify all the individual border properties for just one side:
CSS Rounded Borders
The border-radius property is used to add rounded borders to an element:
Source:
[Link]
Thank you!