Copy of Module 3. Introduction to CSS
Copy of Module 3. Introduction to CSS
Introduction to CSS
What is CSS?
CSS, or Cascading Style Sheets, is a stylesheet language used to control the presentation and
layout of web pages. It works alongside HTML to define how elements should appear on the
screen, such as their colours, sizes, positions, and overall style.
selector {
property: value;
}
Selectors
Target specific elements in the HTML:
1. Type Selector: Targets all elements of a given type.
Example:
h1 { color: red; }
2. Class Selector: Targets elements with a specific class, prefixed with ..
Example:
.example { font-size: 20px; }
3. ID Selector: Targets an element with a specific ID, prefixed with #
Example:
#unique { text-align: center; }
4. Group Selector: Targets multiple elements, separated by a comma.
Example:
h1, p { margin: 10px; }
Inline CSS
Inline CSS applies styles directly to a specific HTML element using the style attribute within the
opening tag.
Advantages
● Quick and easy to apply for small, specific changes.
● No need for additional files or <style> tags.
Disadvantages
● Not reusable across multiple elements or pages.
● Makes the HTML code cluttered and harder to maintain.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Inline CSS Example</title>
</head>
<body>
<h1 style="color: blue; font-size: 24px;">This is a heading styled with Inline CSS</h1>
<p style="background-color: yellow; padding: 10px;">This paragraph has a yellow
background and padding.</p>
</body>
</html>
Output:
Internal CSS
Internal CSS is used when styles are defined within a <style> tag inside the <head> section of
the HTML document. These styles apply only to the page where they are defined.
Advantages
● Centralized styling for the page.
● No need for external files, making it easier to manage single-page projects.
Disadvantages
● Styles cannot be reused across multiple pages.
● Increases the size of the HTML document, potentially slowing page load time.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Internal CSS Example</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: lightgray;
}
h1 {
color: green;
text-align: center;
}
p{
color: black;
font-size: 18px;
}
</style>
</head>
<body>
<h1>This is a heading styled with Internal CSS</h1>
<p>This paragraph is styled using Internal CSS.</p>
</body>
</html>
Output:
External CSS
External CSS is defined in a separate file with a .css extension. The file is linked to the HTML
document using the <link> tag in the <head> section.
Advantages
● Reusable across multiple HTML pages, ensuring consistent design.
● Keeps HTML code clean and focused on structure.
● Easy to update styles for an entire website by modifying a single file.
Disadvantages
● Requires additional HTTP requests to load the CSS file, potentially increasing load time
(can be minimized with caching).
● External files must be correctly linked; otherwise, styles won't be applied.
Example
External styles are defined within the <link> element, inside the <head> section of an HTML
page:
<!DOCTYPE html>
<html>
<head>
<title>External CSS Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading styled with External CSS</h1>
<p>This paragraph is styled using External CSS.</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.
● Here is how the "style.css" file looks:
"style.css"
body {
font-family: Verdana, sans-serif;
background-color: #f0f0f0;
}
h1 {
color: darkblue;
text-align: center;
}
p{
color: gray;
font-size: 16px;
}
Output:
Syntax
CSS comments begin with /* and end with */. Anything between these markers is considered a
comment.
Single-Line Comment
Example:
.CSS
body {
background-color: lightblue; /* Set the background color of the page */
}
Multi-Line Comment
.CSS
/*
This section styles the header of the webpage.
It sets the font size, color, and text alignment.
*/
h1 {
font-size: 24px;
color: navy;
text-align: center;
}
Best Practices
● Use comments to clarify complex or important code.
● Avoid overusing comments for obvious styles to keep your CSS clean.
● Use multi-line comments for detailed explanations and single-line comments for short
notes.
Comments are a vital part of writing maintainable and collaborative CSS code. They improve
readability and make it easier to update or debug styles in the future.
2. Hexadecimal Colors
● Represented as #RRGGBB, where RR, GG, and BB are hexadecimal values for red, green,
and blue.
3. RGB Colors
● Use rgb() to define red, green, and blue values between 0 and 255.
Text Colour
● You can set the colour of the text.
p{
color: blue; /* Sets text color */
}
Background Color
● You can change the background colour according to you.
body {
background-color: #f0f0f0; /* Light gray background */
}
Border Color
● You can set the colour of the border colour.
div {
border: 2px solid rgb(255, 165, 0); /* Orange border */
}
Opacity
● To apply transparency to an entire element, use the opacity property (value between 0 and 1).
div {
background-color: blue;
opacity: 0.5; /* 50% transparent */
}
Values:
● left: Aligns text to the left.
● right: Aligns text to the right.
● center: Centers the text.
● justify: Aligns text to both left and right, adding space between words.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.left {
text-align: left;
}
.center {
text-align: center;
}
.right {
text-align: right;
}
.justify {
text-align: justify;
}
</style>
</head>
<body>
<p class="left">This text is left-aligned.</p>
<p class="center">This text is center-aligned.</p>
<p class="right">This text is right-aligned.</p>
<p class="justify">This text is justified. It stretches the text so that it aligns with both left
and right margins.</p>
</body>
</html>
Output:
2. Horizontal Alignment
The margin property can align elements horizontally when combined with auto.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightblue;
margin: 0 auto; /* Horizontally centers the block */
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
Output:
3. Vertical Alignment
The vertical-align property aligns inline or table-cell elements vertically.
Values:
● top: Aligns to the top.
● middle: Aligns to the middle.
● bottom: Aligns to the bottom.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
height: 100px;
display: table;
}
.text {
display: table-cell;
vertical-align: middle;
background-color: lightgray;
}
</style>
</head>
<body>
<div class="container">
<div class="text">Vertically Centered Text</div>
</div>
</body>
</html>
Output:
4. Flexbox Alignment
Using the CSS display: flex; property provides powerful alignment options.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
justify-content: center; /* Horizontal alignment */
align-items: center; /* Vertical alignment */
height: 100vh;
background-color: lightgreen;
}
.box {
width: 100px;
height: 100px;
background-color: coral;
}
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html>
Output:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Links Example</title>
<style>
/* Style for unvisited links */
a:link {
color: blue; /* Blue for unvisited links */
text-decoration: none; /* Removes underline */
font-weight: bold;
}
Visit Google
Explanation
● Unvisited Link (:link): Links that the user hasn't clicked yet are styled in blue and appear
bold without underlines.
● Visited Link (:visited): Links that the user has clicked are styled in purple.
● Hovered Link (:hover): When the mouse pointer hovers over a link, it turns orange and
gets an underline.
● Active Link (:active): While clicking on a link, it temporarily turns red.
● Focused Link (:focus): When the link is focused (e.g., using the Tab key), it shows a teal
outline
3.8 CSS for Images
CSS provides numerous properties to style images, enhancing their appearance, positioning,
and behaviour on a webpage. With CSS, you can control the size, alignment, borders, effects,
and more for images.
/* Hover Effect */
.image3 {
width: 300px;
height: auto;
transition: transform 0.3s, filter 0.3s;
}
.image3:hover {
transform: scale(1.1);
filter: brightness(1.2);
}
</style>
</head>
<body>
<h1>CSS Images Example</h1>
<p>Styled images using CSS:</p>
<div>
<h3>Basic Styled Image</h3>
<img src="https://via.placeholder.com/300x200" alt="Sample Image" class="image1">
</div>
<div>
<h3>Circular Image</h3>
<img src="https://via.placeholder.com/150" alt="Sample Image" class="image2">
</div>
</body>
</html>
Explanation
1. Basic Styling (.image1):
● The image is resized to a width of 300px with a proportional height.
● It has a solid border, rounded corners, and a shadow for a 3D effect.
2. Circular Image (.image2):
● The image is made circular using border-radius: 50%.
● It has a colored border for emphasis.
Output: