Chapter 3 - Cascading style sheet(SS)
Chapter 3 - Cascading style sheet(SS)
Instructor: Melaku M.
h1 is a selector in CSS, color:blue is declaration, color is a property, blue is the property value
Using CSS
❖CSS can be added to HTML documents in 3 ways:
➢Inline - by using the style attribute inside HTML elements
➢Internal - by using a <style> element in the <head> section
➢External - by using a <link> element to link to an external CSS file
❖The most common way to add CSS, is to keep the styles in external
CSS files.
1. Inline CSS
❖An inline CSS is used to apply a unique style to a single HTML element.
❖The following example sets the text color of the <h1> element to blue, and the
text color of the <p> element to red:
❖An internal CSS is used to define a style for a single HTML page.
❖The example in next slide sets the text color of ALL the <h1>
elements (on that page) to blue, and the text color of ALL the <p>
elements to red. In addition, the page will be displayed with a
"powderblue" background color:
2. Internal CSS
<html>
<head>
<style>
body{background-color: powderblue;}
h1 {color: blue;}
p {color: red; }
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
3. External CSS
❖An external style sheet is used to define the style for many HTML pages.
❖To use an external style sheet, add a link to it in the <head> section of each
HTML page:
CSS Colors, Fonts and Sizes
❖Demonstrating some of the commonly used CSS properties.
❖The CSS padding property defines a padding (space) between the text and
the border.
❖The CSS margin property defines a margin (space) outside the border.
<html>
<head>
<style>
h1 { color: blue;
font-family: verdana;
font-size: 30px;}
p { color: red;
font-family: courier;
font-size: 16px;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Border
The CSS border property defines a border around an HTML element.
Border and padding
The CSS padding property defines a padding (space) between the text and the border.
CSS Margin
The CSS margin property defines a margin (space) outside the border.
Style Sheet Rules
❖Use the HTML style attribute for inline styling
❖Use the HTML <head> element to store <style> and <link> elements
❖Use the CSS padding property for space inside the border
❖Use the CSS margin property for space outside the border.
Style rule Precedence: Specificity Hierarchy
❖In OOP, a child class inherits properties from a parent class, establishing a
hierarchical relationship between them.
❖In this example, the child paragraph element inherits the font-size and color
styles from its parent, the .parent div. However, it also has its own font-weight
style, which overrides the inherited font-weight from its parent.
List of Inherited and non-inherited properties
Foreground and Background Properties
➢Foreground and background properties in CSS are used to style the content
and the area surrounding it respectively.
1. Content - The content of the box, where text and images appear
2. Padding - This is the space between the content box and the border.
3. Border - This is the outer edge of the element. It can be styled with
different colors, widths, and styles (e.g., solid, dashed, dotted).
4. Margin - Margin: This is the space between the border of the element
and other elements on the page. It can be used to control the spacing
between elements.
Demonstrating the Box Model
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px; }
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<div>The CSS box model is essentially a box that wraps around every HTML element. It
consists of: borders, padding, margins, and the actual content </div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: white;
width: 400px;
border: 15px solid blue;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<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>
</body>
</html>
Table styling properties
CSS provides a rich set of properties to style tables, allowing you to
customize their appearance, layout and enhance its functionality.
A typical HTML table structure looks like this:
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
<tr>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</tbody>
</table>
CSS Properties for Table Styling
►padding: Sets the space between the cell content and the cell border.
►border: Sets the border style, width, and color for the cell.
►text-align: Specifies the horizontal alignment of the cell content.
►vertical-align: Specifies the vertical alignment of the cell content.
►width: Sets the width of the cell.
►height: Sets the height of the cell.
►font-weight: Sets the font weight (e.g., bold).
►background-color: Sets the background color of the header cell.
►color: Sets the color of the header cell text.
►border-spacing: Sets the spacing between table borders when border-
collapse is set to separate.
►table-layout: Determines how the table layout is calculated.
• auto: The browser determines the table layout.
• fixed: The table layout is based on the width of table cells.
CSS Properties for Table Styling
1. Borders: Add borders to the table and its cells.
<style>
table {
border-collapse: collapse; /* Merges table cell borders */
width: 100%; /* Full-width table */
}
th, td {
border: 1px solid #ddd; /* Light gray border */
}
</style>
CSS Properties for Table Styling
2. Padding and Alignment: Control padding and text alignment within cells
<style>
th, td {
padding: 8px; /* Space inside cells */
text-align: left; /* Align text to the left */
}
</style>
CSS Properties for Table Styling
3. Background Colors: Set background colors for headers and alternating rows.
<style>
th {
background-color: #f2f2f2; /* Light gray for headers */
}
tr:nth-child(even) {
background-color: #f9f9f9; /* Light gray for even rows */
}
</style>
CSS Properties for Table Styling
4. Hover Effects: Change the background color of rows on hover.
<style>
tr:hover {
background-color: #f1f1f1; /* Highlight on hover */
}
</style>
CSS Properties for Table Styling
5. Font Styles: Customize font size and style..
<style>
th {
font-weight: bold; /* Bold text for headers */
}
td {
font-size: 14px; /* Set a specific font size */
}
</style>
CSS Measuring Units
❖CSS provides various measuring units that help define sizes, spacing, and
positions of elements on a webpage.