CH 3css
CH 3css
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a
nightmare for web developers. Development of large websites, where fonts and color information
were added to every single page, became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
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.
EXAMPLE
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
text-align: center;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
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
• External CSS
• Internal CSS
• 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 styles are defined within the <link> element, inside the <head> section of an HTML
page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
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.
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
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 styles are defined within the <style> element, inside the <head> section of an HTML
page:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
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 styles are defined within the "style" attribute of the relevant element:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
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.
Assume that an external style sheet has the following style for the <h1> element:
h1 {
color: navy;
}
Then, assume that an internal style sheet also has the following style for the <h1> element:
h1 {
color: orange;
}
Example
If the internal style is defined after the link to the external style sheet, the <h1> elements will
be "orange":
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
Example
However, if the internal style is defined before the link to the external style sheet, the <h1>
elements will be "navy":
<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a later
date.
A CSS comment is placed inside the <style> element, and starts with /* and ends with */:
EXAMPLE -1
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
EXAMPLE-2
<!DOCTYPE html>
<html>
<head>
<style>
p{
}
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: red; /* Set text color to red */
}
</style>
</head>
<body>
<h2>My Heading</h2>
</body>
</html>
CSS Color Names
In CSS, a color can be specified by using a predefined color name:
Tomato
Orange
DodgerBlue
MediumSeaGreen
Gray
SlateBlue
Violet
LightGray
<html>
<body>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo
consequat.
</p>
</body>
</html>
<html>
<body>
<p style="color:DodgerBlue;">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p style="color:MediumSeaGreen;">Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</body>
</html>
<body>
</body>
</html>
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).
RED (255)
GREEN (99)
BLUE (71)
Example
rgb(255, 0, 0)
rgb(0, 0, 255)
rgb(255, 165, 0)
<!DOCTYPE html>
<html>
<body>
<p>By using equal values for red, green, and blue, you will get different shades of gray.</p>
</body>
</html>
CSS background-image
<!DOCTYPE html>
<html>
<head>
<style>
Body
background-image: url("gradient_bg.png");
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
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
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
margin-right: 200px;
background-attachment: fixed;
</style>
</head>
<body>
<p>The background-attachment property specifies whether the background image should scroll or be fixed (will not scroll with
the rest of the page).</p>
<p><strong>Tip:</strong> If you do not see any scrollbars, try to resize the browser window.</p>
</body>
</html>
example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
margin-right: 200px;
background-attachment: scroll;
}
</style>
</head>
<body>
<p>The background-attachment property specifies whether the background image should scroll or be fixed (will not scroll with
the rest of the page).</p>
<p><strong>Tip:</strong> If you do not see any scrollbars, try to resize the browser window.</p>
</body>
</html>
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
The element selector selects HTML elements based on the element name.
<!DOCTYPE html>
<html>
<head>
<style>
text-align: center;
color: red;
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
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.
<!DOCTYPE html>
<html>
<head>
<style>
#para1
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
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.
<!DOCTYPE html>
<html>
<head>
<style>
.center
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
Another example
<!DOCTYPE html>
<html>
<head>
<style>
p.center
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
The universal selector (*) selects all HTML elements on the page.
<!DOCTYPE html>
<html>
<head>
<style>
*{
text-align: center;
color: blue;
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>And me!</p>
</body>
</html>
5. The CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p
text-align: center;
color: red;
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>
The following example selects all <a> elements with a target attribute:
<!DOCTYPE html>
<html>
<head>
<style>
a[target] {
background-color: yellow;
</style>
</head>
<body>
<a href="https://www.w3schools.com">w3schools.com</a>
</body>
</html>
The [attribute="value"] selector is used to select elements with a specified attribute and value.
The following example selects all <a> elements with a target="_blank" attribute:
<!DOCTYPE html>
<html>
<head>
<style>
a[target=_blank] {
background-color: yellow;
</style>
</head>
<body>
<a href="https://www.w3schools.com">w3schools.com</a>
</body>
</html>
The [attribute~="value"] selector is used to select elements with an attribute value containing a
specified word.
The following example selects all elements with a title attribute that contains a space-separated
list of words, one of which is "flower":
<!DOCTYPE html>
<html>
<head>
<style>
[title~=flower] {
</style>
</head>
<body>
<p>All images with the title attribute containing the word "flower" get a yellow border.</p>
</body>
</html>
output
CSS [attribute~="value"] Selector
All images with the title attribute containing the word "flower" get a yellow border.
The [attribute|="value"] selector is used to select elements with the specified attribute starting
with the specified value.
The following example selects all elements with a class attribute value that begins with "top":
Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen(
- ), like class="top-text"!
<!DOCTYPE html>
<html>
<head>
<style>
[class|=top] {
background: yellow;
</style>
</head>
<body>
<h1 class="top-header">Welcome</h1>
</body>
</html>
output
Welcome
Hello world!
The [attribute^="value"] selector is used to select elements whose attribute value begins with a
specified value.
The following example selects all elements with a class attribute value that begins with "top":
<!DOCTYPE html>
<html>
<head>
<style>
[class^="top"] {
background: yellow;
</style>
</head>
<body>
<h1 class="top-header">Welcome</h1>
</body>
</html>
Output
Welcome
Hello world!
The [attribute$="value"] selector is used to select elements whose attribute value ends with a
specified value.
The following example selects all elements with a class attribute value that ends with "test":
<!DOCTYPE html>
<html>
<head>
<style>
[class$="test"] {
background: yellow;
</style>
</head>
<body>
</body>
</html>
output
The [attribute*="value"] selector is used to select elements whose attribute value contains a
specified value.
The following example selects all elements with a class attribute value that contains "te":
<!DOCTYPE html>
<html>
<head>
<style>
[class*="te"] {
background: yellow;
</style>
</head>
<body>
</body>
</html>
output
example
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
</html>
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:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
border-style: solid;
border-width: 5px;
p.two {
border-style: solid;
border-width: medium;
p.three {
border-style: dotted;
border-width: 2px;
p.four {
border-style: dotted;
border-width: thick;
p.five {
border-style: double;
border-width: 15px;
p.six {
border-style: double;
border-width: thick;
</style>
</head>
<body>
</html>
Example 2
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
border-style: solid;
border-width: 5px 20px; /* 5px top and bottom, 20px on the sides */
p.two {
border-style: solid;
border-width: 20px 5px; /* 20px top and bottom, 5px on the sides */
p.three {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px bottom and 35px left */
</style>
</head>
<body>
<p>The border-width property can have from one to four values (for the top border, right border, bottom border, and the left
border):</p>
<p class="one">Some text.</p>
</body>
</html>
In CSS, there are also properties for specifying each of the borders (top, right, bottom, and left):
Example
p {
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
p.round1 {
border-radius: 5px;
}
p.round2 {
border-radius: 8px;
p.round3 {
border-radius: 12px;
</style>
</head>
<body>
</body>
</html>
CSS Layout - The display Property
The display property is the most important CSS property for controlling layout.
Every HTML element has a default display value depending on what type of element it is. The
default display value for most elements is block or inline.
<!DOCTYPE html>
<html>
<head>
<style>
li {
display: inline;
</style>
</head>
<body>
<ul>
</ul>
</body>
</html>
Output
block
<!DOCTYPE html>
<html>
<head>
<style>
span {
display: block;
</style>
</head>
<body>
<span>A display property with a value of "block" results in</span> <span>a line break between the two
elements.</span>
</body>
</html>
Output
<!DOCTYPE html>
<html>
<head>
<style>
a{
display: block;
</style>
</head>
<body>
</body>
</html>
output
HTML
CSS
JavaScript
<!DOCTYPE html>
<html>
<head>
<style>
h1.hidden
visibility: hidden;
</style>
</head>
<body>
</body>
</html>
Output
The CSS box model is essentially a box that wraps around every HTML element. It consists of:
margins, borders, padding, and the actual content. The image below illustrates the box model:
Explanation of the different parts:
• Content - The content of the box, where text and images appear
• Padding - Clears an area around the content. The padding is transparent
• Border - A border that goes around the padding and content
• Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space between
elements.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
padding: 50px;
margin: 20px;
</style>
</head>
<body>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of: borders, padding, margins,
and the actual content.</p>
<div>This text is the content of the box. We have added a 50px padding, 20px margin and a 15px green border. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</body>
</html>
The position property specifies the type of positioning method used for an element (static, relative,
fixed, absolute or sticky).
The position property specifies the type of positioning method used for an element.
• static
• relative
• fixed
• absolute
• sticky
Elements are then positioned using the top, bottom, left, and right properties. However, these
properties will not work unless the position property is set first. They also work differently
depending on the position value.
1. position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always positioned
according to the normal flow of the page:
<!DOCTYPE html>
<html>
<head>
<style>
div.static
position: static;
</style>
</head>
<body>
<h2>position: static;</h2>
<p>An element with position: static; is not positioned in any special way; it is
<div class="static">
</div>
</body>
</html>
Output
position: static;
An element with position: static; is not positioned in any special way; it is always positioned according
to the normal flow of the page:
2. position: relative;
An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it
to be adjusted away from its normal position. Other content will not be adjusted to fit into any
gap left by the element.
<!DOCTYPE html>
<html>
<head>
<style>
div.relative
position: relative;
left: 30px;
</style>
</head>
<body>
<h2>position: relative;</h2>
<p>An element with position: relative; is positioned relative to its normal position:</p>
<div class="relative">
</div>
</body>
</html>
Output
position: relative;
An element with position: relative; is positioned relative to its normal position:
3. position: fixed;
An element with position: fixed; is positioned relative to the viewport, which means it always
stays in the same place even if the page is scrolled. The top, right, bottom, and left properties
are used to position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
<!DOCTYPE html>
<html>
<head>
<style>
div.fixed
position: fixed;
bottom: 0;
right: 0;
width: 300px;
</style>
</head>
<body>
<h2>position: fixed;</h2>
<p>An element with position: fixed; is positioned relative to the viewport, which means it always
stays in the same place even if the page is scrolled:</p>
<div class="fixed">
</div>
</body>
</html>
4. position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned ancestor
(instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document
body, and moves along with page scrolling.
<!DOCTYPE html>
<html>
<head>
<style>
div.relative
position: relative;
width: 400px;
height: 200px;
}
div.absolute
position:absolute;
top: 10px;
right: 0;
width: 200px;
height: 100px;
</style>
</head>
<body>
<h2>position: absolute;</h2>
<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor
(instead of positioned relative to the viewport, like fixed):</p>
</div>
</body>
</html>
5. position: sticky;
An element with position: sticky; 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).
<!DOCTYPE html>
<html>
<head>
<style>
div.sticky
position:sticky;
top: 0;
padding: 5px;
background-color: #cae8ca;
</style>
</head>
<body>
<p>Try to <b>scroll</b> inside this frame to understand how sticky positioning works.</p>
<div style="padding-bottom:2000px">
<p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its
scroll position.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo,
maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam
omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no
molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo,
maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam
omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no
molestiae voluptatibus.</p>
</div>
</body>
</html>