Css
Css
1 Introduction
Definition: CSS (Cascading Style Sheets) is a style sheet language used to describe the
presentation of a document written in HTML or XML. It allows web developers to control the
layout and appearance of multiple web pages with consistent styling rules.
Importance of CSS:
• Separation of content and presentation: HTML focuses on structure, while CSS defines
how elements look.
• Reusability: Styles can be reused across multiple pages, saving time and effort.
• Efficiency: It allows changes to be made centrally without editing individual HTML files.
• Flexibility: CSS supports responsive design to adapt websites to different devices and
screen sizes.
Syntax:
css
selector {
property: value;
________________________________________
1. Inline CSS:
o Example:
html
o Advantages:
2. Internal CSS:
o Written inside a <style> tag within the <head> section of the HTML document.
o Example:
html
<style>
h1 {
color: blue;
</style>
o Advantages:
o Disadvantages:
3. External CSS:
o Defined in a separate file with a .css extension and linked to the HTML using the <link>
tag.
o Example:
html
css
/* styles.css */
h1 {
color: green;
o Advantages:
o Disadvantages:
o Example:
________________________________________
CSS style rules specify how elements are presented. The general format is:
css
selector {
property: value;
Explanation of Components:
2. Property: Indicates the visual attribute to modify (e.g., color, margin, padding).
3. Value: Assigns the desired style to the property (e.g., red, 20px).
Example:
css
h1 {
________________________________________
Selectors define which HTML elements will be styled. Types of selectors include:
1. Element Selector:
o Targets all elements of a specified type.
o Example:
css
p{
color: gray;
2. Class Selector:
o Example:
css
.highlight {
background-color: yellow;
html
3. ID Selector:
o Example:
css
#main-header {
font-size: 32px;
html
4. Group Selector:
o Example:
css
h1, h2, p {
margin: 10px;
5. Universal Selector:
o Example:
css
*{
margin: 0;
padding: 0;
________________________________________
1. Length Units:
§ Example:
css
p{
font-size: 16px;
§ Example:
css
h1 {
2. Colors:
o Can be specified as:
o Example:
css
body {
background-color: #f0f0f0;
3. URLs:
o Example:
css
body {
background-image: url('background.jpg');
________________________________________
1. font-family:
o Example:
css
p{
2. font-size:
o Defines text size.
o Example:
css
h1 {
font-size: 24px;
3. font-weight:
o Adjusts thickness.
o Example:
css
p{
font-weight: bold;
4. font-style:
o Example:
css
em {
font-style: italic;
________________________________________
1. list-style-type:
o Example:
css
ul {
list-style-type: square;
2. list-style-position:
o Example:
css
ul {
list-style-position: inside;
3. list-style-image:
o Example:
css
ul {
list-style-image: url('bullet.png');
________________________________________
3.8 Color
CSS supports specifying colors for text, backgrounds, borders, and other elements.
• Example:
css
p{
}
3.9 Alignment of Text
• Horizontal Alignment:
o Example:
css
h1 {
text-align: center;
• Vertical Alignment:
o Example:
css
span {
vertical-align: middle;
________________________________________
Example:
css
div {
margin: 10px;
padding: 20px;
}
Here is the breakdown of the topics based on the image provided. Below are notes for the
requested topics under "3. Cascading Style Sheets":
3.1 Introduction
CSS (Cascading Style Sheets) is used to control the presentation of web pages,
including layout, colors, and fonts.
It separates content (HTML) from design, making websites easier to manage.
Stylesheets can be linked externally, embedded in HTML, or applied inline.
1. Inline CSS:
o Applied directly to an element using the style attribute.
o Example: <h1 style="color: red;">Heading</h1>
o Overrides other CSS.
2. Internal CSS:
o Defined in the <style> tag within the <head> of an HTML document.
o Example:
html
CopyEdit
<style>
h1 {
color: blue;
</style>
3. External CSS:
o Saved in a separate .css file and linked using the <link> tag.
o Example:
html
CopyEdit
4. Browser Default:
o Each browser applies its default styles when no CSS is provided.
3.3 Style Specification Formats
Selectors:
o Target specific HTML elements (e.g., h1, .class, #id).
Properties:
o Define what to style, such as color, font-size, or margin.
Values:
o Specify the styling, e.g., red, 16px, or auto.
1. Element Selector:
o Targets all elements of a specific type.
o Example: p { color: green; }
2. Class Selector:
o Targets elements with a specific class.
o Example: .note { font-style: italic; }
3. ID Selector:
o Targets a single element with a specific ID.
o Example: #header { background-color: yellow; }
4. Group Selector:
o Targets multiple elements.
o Example: h1, h2 { font-weight: bold; }
5. Universal Selector:
o Targets all elements.
o Example: * { margin: 0; }
Length Units:
o Absolute: px, cm, mm.
o Relative: em, rem, %.
Colors:
o Named colors (e.g., red, blue).
o Hexadecimal (#RRGGBB).
o RGB/RGBA (rgb(255, 0, 0)).
URLs:
o Used for background images: url('image.jpg').
3.8 Color
text-align:
o Aligns text horizontally: left, right, center, justify.
vertical-align:
o Aligns inline elements vertically: top, middle, bottom.
Example:
css
div {
margin: 10px;
padding: 20px;
css
body {
background-image: url('bg.jpg');
background-repeat: no-repeat;
background-size: cover;
html
<div class="container">Content</div>
2. <span>:
o An inline container for applying styles or scripts to text.
o Example:
html
CSS Specificity:
o Inline > Internal > External.
Important Rule:
o Overrides all rules: color: blue !important;.