Unit 5 Cascading Style Sheets (2)
Unit 5 Cascading Style Sheets (2)
CSS (Cascading Style Sheets) is a language used to describe the presentation of a document
written in HTML or XML. CSS controls the layout, color, font, and overall design of web pages.
It separates the content of a web page from its visual design, making it easier to manage and
update.
1. Introduction to CSS
2. Benefits of CSS
• Separation of Content and Design: By separating the content (HTML) from the design
(CSS), developers can update styles without altering the structure of the HTML
document.
• Consistency: CSS allows the consistent styling of all pages within a website.
• Flexibility: You can easily change the design of an entire website by editing just one
CSS file.
• Reduced File Size: Instead of repeating styles in every HTML file, you can store styles
in a single CSS file, making the website faster to load.
• Improved Accessibility: CSS helps in making the website more user-friendly, especially
for people with disabilities, as it allows for better control over presentation.
3. CSS Syntax
CSS uses a simple syntax for defining styles, where the selector points to the HTML element,
and the declaration block specifies the styles.
Basic Syntax:
css
selector {
property: value;
}
Example:
css
h1 {
color: blue;
font-size: 24px;
}
• This changes the color of all <h1> headings to blue and sets the font size to 24px.
4. CSS Selectors
CSS selectors are patterns used to select HTML elements to apply styles. There are several types
of selectors:
a. Element Selector
The element selector targets all instances of a particular HTML element (tag). It applies the
styles to all matching elements in the HTML document.
Syntax:
css
element {
property: value;
}
Example:
css
p{
color: green;
font-size: 14px;
}
• This applies the style to all <p> (paragraph) elements in the document, making the text
green and setting the font size to 14px.
b. Id Selector
The id selector targets an element with a specific id attribute. It’s unique to that element,
meaning it can only be used once in the document.
Syntax:
css
#id {
property: value;
}
Example:
css
#header {
background-color: blue;
color: white;
}
• This applies the style only to the element with the id="header", changing its background
color to blue and text color to white.
c. Class Selector
The class selector targets all elements with a specific class attribute. Unlike the id, the class
selector can be used multiple times on different elements.
Syntax:
css
.class {
property: value;
}
Example:
css
.button {
background-color: red;
color: white;
}
• This applies the style to all elements with the class button, making the background color
red and text color white.
d. Universal Selector
The universal selector (*) targets all elements in the document. It can be used to apply a
common style to everything.
Syntax:
css
*{
property: value;
}
Example:
css
*{
margin: 0;
padding: 0;
}
• This removes the default margin and padding from all elements on the page.
e. Group Selector
The group selector allows you to apply the same styles to multiple elements by grouping them
together. This is useful for reducing redundancy.
Syntax:
css
element1, element2, element3 {
property: value;
}
Example:
css
h1, h2, h3 {
color: purple;
}
• This applies the same color (purple) to all <h1>, <h2>, and <h3> headings.
5. Types of CSS
There are three main types of CSS, based on where the styles are written:
a. Inline CSS
Inline CSS is written directly within an HTML element using the style attribute. This method is
used for styling individual elements.
Syntax:
html
<element style="property: value;">
Content
</element>
Example:
html
<p style="color: red; font-size: 18px;">This is a red paragraph.</p>
• The styles are applied to the <p> tag directly, changing the text color to red and the font
size to 18px.
b. Internal CSS
Internal CSS is written within the <style> tag inside the <head> section of the HTML
document. It applies styles to the entire page but is not reusable across multiple pages.
Syntax:
html
<head>
<style>
selector {
property: value;
}
</style>
</head>
Example:
html
<head>
<style>
p{
color: blue;
font-size: 16px;
}
</style>
</head>
• This will apply the styles to all <p> elements in that specific HTML document.
c. External CSS
External CSS is the most common and efficient method of styling. The styles are written in a
separate CSS file, which is linked to the HTML document. This method allows the same
stylesheet to be used across multiple pages of a website.
Syntax:
html
<head>
<link rel="stylesheet" href="styles.css">
</head>
css
h1 {
color: blue;
font-size: 24px;
}
html
<head>
<link rel="stylesheet" href="styles.css">
</head>
• This applies the styles from the styles.css file to the HTML elements.
5.2 Style Sheet Properties: Font, Text, Box, Color, and Background Properties; Creating
and Using a Simple External CSS File; Using Internal and Inline CSS; Background and
Color Gradients in CSS; Setting Fonts
This section delves into CSS properties for fonts, text, box models, colors, backgrounds, and
gradients. Additionally, we’ll explore how to use external, internal, and inline CSS styles, as
well as how to work with background gradients and font settings.
1. Font Properties
CSS provides several properties to control the font of text. These properties allow for specifying
the font type, size, weight, style, and line spacing.
Example:
css
h1 {
font-family: 'Arial', sans-serif;
font-size: 32px;
font-weight: bold;
font-style: italic;
line-height: 1.5;
}
• This will make the <h1> text Arial, size 32px, bold, italic, and with 1.5 line height.
2. Text Properties
CSS also allows fine control over text alignment, decoration, and spacing.
Example:
css
p{
text-align: justify;
text-decoration: underline;
text-transform: uppercase;
letter-spacing: 1px;
}
• This will justify the text inside <p>, underline it, make it uppercase, and add 1px of
spacing between the letters.
The box model is a concept that defines the layout of elements in CSS. Each element is a box
consisting of the content, padding, border, and margin.
Example:
css
div {
width: 300px;
height: 150px;
padding: 20px;
border: 2px solid black;
margin: 10px;
}
• This will create a div with a 300px width, 150px height, 20px padding, 2px solid black
border, and 10px margin from other elements.
4. Color Properties
CSS allows you to control the text color, background color, and border color.
Color Properties:
• color: Specifies the color of the text.
• background-color: Specifies the background color of the element.
• border-color: Defines the color of the border.
Example:
css
body {
color: white; /* Text color */
background-color: #333333; /* Background color */
}
button {
border: 2px solid #ff5733; /* Border color */
}
• This applies white text color on the body, a dark gray background color, and an orange
border color to buttons.
5. Background Properties
The background properties allow you to set a background image, background color, and
control how the image is displayed.
Example:
css
body {
background-color: lightblue;
background-image: url('background.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
• This will set a light blue background color, use the image background.jpg, prevent it
from repeating, center it, and ensure it covers the entire background area.
Linear Gradients:
A linear gradient changes colors in a straight line, either vertically, horizontally, or at an angle.
css
background: linear-gradient(to right, #ff7e5f, #feb47b);
• This creates a gradient from #ff7e5f (a peach color) to #feb47b (a soft yellow).
Radial Gradients:
css
background: radial-gradient(circle, #ff7e5f, #feb47b);
• This creates a gradient starting from the center of the element, with the colors
transitioning from #ff7e5f to #feb47b.
Example:
css
div {
background: linear-gradient(to right, #ff7e5f, #feb47b); /* Linear gradient */
color: white;
}
• The div will have a gradient background that transitions from peach to yellow, with the
text color set to white.
To separate structure from style and improve website maintainability, it is recommended to store
CSS rules in an external CSS file.
Steps:
css
/* styles.css */
body {
background-color: lightblue;
font-family: Arial, sans-serif;
}
h1 {
color: darkblue;
text-align: center;
}
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Web Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is an example using an external CSS file.</p>
</body>
</html>
• The link tag connects the external CSS file to the HTML, applying all styles defined in
styles.css.
In addition to external CSS, you can use internal and inline CSS.
Internal CSS:
Internal CSS is written inside the <style> tag in the <head> section of the HTML file.
html
<head>
<style>
body {
background-color: lightgray;
}
h1 {
color: blue;
}
</style>
</head>
Inline CSS:
Inline CSS is used for styling individual HTML elements directly within the style attribute.
html
<p style="color: red; font-size: 20px;">This is an inline styled paragraph.</p>
• Inline CSS is limited to the individual element, whereas internal and external styles can
apply to multiple elements on the page.
5.3 Working with Block Elements and Objects, Lists and Tables, CSS ID and Class, Box
Model (Introduction, Border Properties, Padding Properties, Margin Properties), CSS
Advanced (Grouping, Dimension, Display, Positioning, Floating, Align, Pseudo-Classes,
Navigation Bar, Image Sprites, Attribute Selector), CSS Color, Creating Page Layout and
Site Designs
In this section, we'll cover a range of topics that include working with block-level elements,
lists, tables, CSS ID and Class selectors, the box model, and advanced CSS techniques for
creating layouts, navigation bars, styling images, and much more.
In CSS, elements can be categorized into block-level and inline elements. Block-level elements
occupy the full width of their parent container, starting on a new line. Common block-level
elements include <div>, <p>, <h1>, <ul>, and <ol>.
Block Elements:
html
<div>
<h1>Heading 1</h1>
<p>This is a paragraph of text.</p>
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
</ul>
</div>
Lists:
Lists in HTML can be ordered (<ol>) or unordered (<ul>). Each item in the list is marked by an
<li> tag.
html
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
html
<ol>
<li>First item</li>
<li>Second item</li>
</ol>
Tables:
Tables are used for displaying tabular data. They are created using the <table>, <tr>, <th>, and
<td> elements.
• Table Example:
html
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
• ID Selector (#): Targets an element with a specific id. IDs should be unique within a
page.
css
#header {
background-color: blue;
color: white;
}
html
<div id="header">This is a header</div>
• Class Selector (.): Targets multiple elements with the same class. Classes can be reused.
css
.highlight {
background-color: yellow;
}
html
Copy
<div class="highlight">This text is highlighted</div>
The box model in CSS describes the layout of elements and how their content, padding, border,
and margin affect their total size.
• border: Defines the border around an element (e.g., 2px solid black).
• padding: Space between the element’s content and the border.
• margin: Space outside the element's border, between other elements.
Example:
css
div {
width: 300px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
• The div will have 300px width, 20px padding, 5px solid black border, and 10px
margin.
5. CSS Advanced
Grouping Selectors:
Grouping selectors allows you to apply the same styles to multiple elements.
css
h1, h2, p {
color: blue;
}
This will apply the blue color to <h1>, <h2>, and <p> elements.
Dimension Properties:
CSS properties like width, height, max-width, and min-height control the dimensions of
elements.
css
div {
width: 50%;
height: 200px;
}
Display Property:
• block: Element takes up the full width available and starts on a new line.
• inline: Element only takes up as much width as it needs and does not start a new line.
• inline-block: Behaves like an inline element but can have width and height.
• none: Hides the element.
css
div {
display: inline-block;
}
The position property defines how elements are positioned in the document.
css
div {
position: absolute;
top: 20px;
left: 50px;
}
• float: Moves an element to the left or right, allowing text and inline elements to wrap
around it.
• clear: Prevents elements from wrapping around floating elements.
css
img {
float: left;
margin-right: 20px;
}
Pseudo-Classes:
A pseudo-class targets elements in specific states (e.g., :hover, :focus, :first-child).
css
a:hover {
color: red;
}
Navigation Bar:
A navigation bar can be created using <nav>, <ul>, and <li> elements. Use CSS to style the
links.
html
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
css
Copy
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
display: inline;
margin-right: 20px;
}
nav ul li a {
text-decoration: none;
color: black;
}
nav ul li a:hover {
color: blue;
}
Image Sprites:
Image sprites combine multiple images into a single image file. CSS is used to display only the
portion of the image you need.
css
.icon {
background-image: url('sprite.png');
width: 32px;
height: 32px;
background-position: -64px 0;
}
• This would display the 32x32px portion of sprite.png at the 64px offset.
Attribute Selector:
css
a[href^="https"] {
color: green;
}
• This targets links (<a>) whose href attribute starts with https and styles them with a
green color.
6. CSS Color
css
div {
background-color: rgba(255, 0, 0, 0.5);
}
Multi-Column Layout:
css
.container {
display: flex;
justify-content: space-between;
}
Example: Grid:
css
.container {
display: grid;
grid-template-columns: 1fr 3fr 1fr;
}
Responsive Design:
Use media queries to make the website responsive to different screen sizes.
css
@media (max-width: 768px) {
.container {
display: block;
}
}