UNIT 3 Cascading Style Sheet
UNIT 3 Cascading Style Sheet
INTRODUCTION TO CSS:-
Cascading Style Sheets (CSS) is a stylesheet language used to describe the presentation of a
document written in HTML or XML. It controls the layout, colors, fonts, and overall visual
appearance of web pages.
TYPES OF CSS:-
Inline CSS
Description: Styles are applied directly within an HTML element using the style attribute.
<h1 style="color: blue; text-align: center;">Hello, World!</h1>
Internal CSS (Embedded CSS)
Description: Styles are defined within a <style> tag in the <head> section of an HTML
document.
<style>
h1 {
color: blue;
text-align: center;
}
</style>
External CSS
Description: Styles are defined in a separate CSS file linked to the HTML document using the
<link> tag.
<link rel="stylesheet" type="text/css" href="styles.css">
CSS PSEUDO :-
CSS pseudo-classes and pseudo-elements are powerful tools for styling HTML elements based
on their state or specific parts of the elements.
element: specifies the HTML element
pseudo-class: specifies the specific state of the element that we want to target
Pseudo-class keywords are added to the selectors and preceded by a colon (:).
Example:
li:hover {
background-color: #f0f0f0; /* Change background on hover */
}
li {
margin-right: 20px; /* Space between items */
}
CSS MARGIN PROPERTIES:
1. Margin Properties Overview
The margin property can be set using either individual side properties or a shorthand property.
The four main sides are:
/* Example: */
div {
margin: 10px 20px 15px 5px; /* top, right, bottom, left */
}
Shorthand Variations:
All sides equal:
margin: 10px; /* 10px on all sides */
Vertical | Horizontal:
margin: 10px 20px; /* 10px top/bottom, 20px left/right */
Top | Right/Left | Bottom:
margin: 10px 5px 15px; /* top, right/left, bottom */
4. Using Percentages
Margins can also be set using percentages, which are relative to the width of the containing
element:
Example:
div {
margin: 5%; /* 5% of the container's width */
}
5. Negative Margins
CSS allows negative margins. This can pull elements closer together or even make them
overlap.
Example:
div {
margin-top: -10px; /* Pulls the element 10px upwards */
}
6. Collapsing Margins
In certain situations, particularly with vertical margins on block elements, margins can
collapse. This means adjacent vertical margins (top margin of one element and bottom margin
of the next) may combine into a single margin, taking the larger value. Understanding this
behavior is crucial for achieving intended layouts.
8. Responsive Margins
When designing responsive layouts, you can use media queries to adjust margins based on
screen size.
Example:
@media (max-width: 600px) {
div {
margin: 10px; /* Smaller margin on mobile */
}
}
CSS COMMENTS:
CSS comments begin with /* and end with */. Everything between these symbols will not be
processed as CSS.
Example:
/* This is a single-line comment */
/*
This is a multi-line comment
that can span multiple lines.
*/
body {
background-color: #f0f0f0; /* Set background color for the body */
}
p{
margin: 20px; /* Add spacing around paragraphs */
}