Week5
Week5
Week5
mohammed.abdulhameed@uob.edu.ly
2024-2025
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.
p{
text-align: center;
color: red;
}
CSS Selectors (con’t)
2. 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":
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
Note: an ID name cannot start with a number!
CSS Selectors (con’t)
3. 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:
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red and center-aligned paragraph.</p>
</body>
CSS Selectors (con’t)
• You can also specify that only specific HTML elements should be affected by a class.
Example:
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be red and center-aligned.</p>
</body>
CSS Selectors (con’t)
• HTML elements can also refer to more than one class.
Example:
<head>
<style>
p.center {
text-align: center;
color: red;
}
p.large {
font-size: 300%;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be red and center-aligned.</p>
<p class="center large">This paragraph will be red, center-aligned, and in a large font-size.</p>
</body>
Example:
<head>
<style>
*{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>Every element on the page will be affected by the style.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
CSS Selectors (con’t)
5. The CSS Grouping Selector
• The grouping selector selects all the HTML elements with the same style definitions.
• Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
Example:
<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>
• It will be better to group the selectors, to minimize the code.
• To group selectors, separate each selector with a comma.
CSS Selectors (con’t)
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.
1. 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.
How To Add CSS (con’t)
Example:
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
• 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 "mystyle.css" file looks:
How To Add CSS (con’t)
Example: “mystyle.css”
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value (20) and the unit (px):
Incorrect (space): margin-left: 20 px;
Correct (no space): margin-left: 20px;
How To Add CSS (con’t)
2. 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:
<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>
How To Add CSS (con’t)
3. 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:
<html>
<body>
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
How To Add CSS (con’t)
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:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>The style of this document is a combination of an external stylesheet, and internal style</p>
</body>
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 */:
Example:
/* This is a single-line comment */
p{
color: red;
}
p{
color: red; /* Set text color to red */
}
CSS Comments (con’t)
HTML and CSS Comments
• From the HTML tutorial, you learned that you can add comments to your HTML source by
using the <!--...--> syntax.
• In the following example, we use a combination of HTML and CSS comments:
Example:
<head>
<style>
p{
color: red; /* Set text color to red */
}
</style>
</head>
<body>
<h2>My Heading</h2>
<!-- These paragraphs will be red -->
<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
<p>HTML and CSS comments are not shown in the output.</p>
</body>
CSS Colors
• Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.
Example:
<body>
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">
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>
CSS Colors (con’t)
3. CSS Text Color
• You can set the color of text:
Example:
<body>
<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>
CSS Colors (con’t)
4. CSS Border Color
• You can set the color of borders:
Example:
<body>
<h1 style="border: 2px solid Tomato;">Hello World</h1>
<h1 style="border: 2px solid DodgerBlue;">Hello World</h1>
<h1 style="border: 2px solid Violet;">Hello World</h1>
</body>
CSS Colors (con’t)
5. 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":
Example:
<body>
<p>Same as color name "Tomato":</p>
<p>In addition to the predefined color names, colors can be specified using RGB, HEX, HSL, or even transparent
colors using RGBA or HSLA color values.</p>
</body>
CSS Colors (con’t)
5.1 CSS RGB Colors
• An RGB color value represents RED, GREEN, and BLUE light sources.
• 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).
Example:
<body>
<h1>Specify colors using RGB values</h1>
<h2 style="background-color:rgb(255, 0, 0);">rgb(255, 0, 0)</h2>
<h2 style="background-color:rgb(0, 0, 255);">rgb(0, 0, 255)</h2>
<h2 style="background-color:rgb(60, 179, 113);">rgb(60, 179, 113)</h2>
<h2 style="background-color:rgb(238, 130, 238);">rgb(238, 130, 238)</h2>
<h2 style="background-color:rgb(255, 165, 0);">rgb(255, 165, 0)</h2>
<h2 style="background-color:rgb(106, 90, 205);">rgb(106, 90, 205)</h2>
</body>
CSS Colors (con’t)
5.2 RGBA Value
• RGBA color values are an extension of RGB color values with an alpha channel - 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 (not transparent at
all):
Example:
<body>
<h1>Make transparent colors with RGBA</h1>
<h2 style="background-color:rgba(255, 99, 71, 0);">rgba(255, 99, 71, 0)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.2);">rgba(255, 99, 71, 0.2)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.4);">rgba(255, 99, 71, 0.4)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.6);">rgba(255, 99, 71, 0.6)</h2>
<h2 style="background-color:rgba(255, 99, 71, 0.8);">rgba(255, 99, 71, 0.8)</h2>
<h2 style="background-color:rgba(255, 99, 71, 1);">rgba(255, 99, 71, 1)</h2>
</body>
CSS Colors (con’t)
5.3 CSS HEX Colors
• 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).
Example:
<body>
<h1>Specify colors using HEX values</h1>
<h2 style="background-color:#ff0000;">#ff0000</h2>
<h2 style="background-color:#0000ff;">#0000ff</h2>
<h2 style="background-color:#3cb371;">#3cb371</h2>
<h2 style="background-color:#ee82ee;">#ee82ee</h2>
<h2 style="background-color:#ffa500;">#ffa500</h2>
<h2 style="background-color:#6a5acd;">#6a5acd</h2>
</body>
CSS Colors (con’t)
5.4 CSS HSL Colors
• 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.
Example:
<body>
<h1>Specify colors using HSL values</h1>
<h2 style="background-color:hsl(0, 100%, 50%);">hsl(0, 100%, 50%)</h2>
<h2 style="background-color:hsl(240, 100%, 50%);">hsl(240, 100%, 50%)</h2>
<h2 style="background-color:hsl(147, 50%, 47%);">hsl(147, 50%, 47%)</h2>
<h2 style="background-color:hsl(300, 76%, 72%);">hsl(300, 76%, 72%)</h2>
<h2 style="background-color:hsl(39, 100%, 50%);">hsl(39, 100%, 50%)</h2>
<h2 style="background-color:hsl(248, 53%, 58%);">hsl(248, 53%, 58%)</h2>
</body>
CSS Colors (con’t)
5.5 HSLA Value
• HSLA color values are an extension of HSL color values with an alpha channel - which
specifies the opacity for a color.
• An HSLA color value is specified with: hsla(hue, saturation, lightness, alpha).
• The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at
all).
Example:
<body>
<h1>Make transparent colors with HSLA</h1>
<h2 style="background-color:hsla(9, 100%, 64%, 0);">hsla(9, 100%, 64%, 0)</h2>
<h2 style="background-color:hsla(9, 100%, 64%, 0.2);">hsla(9, 100%, 64%, 0.2)</h2>
<h2 style="background-color:hsla(9, 100%, 64%, 0.4);">hsla(9, 100%, 64%, 0.4)</h2>
<h2 style="background-color:hsla(9, 100%, 64%, 0.6);">hsla(9, 100%, 64%, 0.6)</h2>
<h2 style="background-color:hsla(9, 100%, 64%, 0.8);">hsla(9, 100%, 64%, 0.8)</h2>
<h2 style="background-color:hsla(9, 100%, 64%, 1);">hsla(9, 100%, 64%, 1)</h2>
</body>
CSS Background
• The CSS background properties are used to add background effects for elements.
• 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:
div {
background-color: green; opacity: 0.3;
}
• The background-position property is used to specify the position of the background image.
• The background-attachment property specifies whether the background image should scroll
or be fixed (will not scroll with the rest of the page)
Example:
body {
background-image: url("img.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
background-attachment: scroll;
}
CSS Background (con’t)
• It is also possible to specify all the background properties in one single property. This is called
a shorthand property.
• background: #ffffff url("img.png") no-repeat right top; the order of the properties 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.
Example:
body {
background-color: #ffffff;
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}