css introduction
css introduction
the layout of web pages. It allows developers to change colors, fonts, spacing,
background, and other visual properties to enhance the design of a website.
1. Introduction to CSS
What is CSS?
CSS is a stylesheet language that defines the appearance of HTML elements. It helps in
creating visually appealing and responsive web pages by controlling colors, fonts,
spacing, positioning, and layouts.
Separates content from design – HTML structures the page, while CSS handles styling.
Enhances user experience – Allows for responsive designs that adapt to different screen
sizes.
Reduces code duplication – A single CSS file can style multiple pages, making
maintenance easier.
Types of CSS
CSS can be applied in three ways:
1. Inline CSS
Definition:
Inline CSS applies styles directly within an HTML element using the style attribute.
Example:
Explanation:
The style attribute inside the <p> tag applies CSS directly to that element.
2. Internal CSS
Definition:
Internal CSS is written inside a <style> tag within the <head> section of an HTML
document.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
P{
Color: red;
Font-size: 18px;
</style>
</head>
<body>
</body>
</html>
Explanation:
The <style> tag contains CSS rules that apply to the entire page.
Useful for styling a single HTML document without affecting other pages.
3. External CSS
Definition:
External CSS is stored in a separate .css file and linked to an HTML document using the
<link> tag.
Example:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
P{
Color: green;
Font-size: 20px;
Explanation:
The <link> tag in the <head> section connects the CSS file to the HTML document.
Definition:
CSS rules consist of selectors and declaration blocks that define how HTML elements
should be displayed.
Structure:
Selector {
Property: value;
HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>This is a paragraph.</p>
</body>
</html>
CSS (styles.css):
P{
Color: blue;
Font-size: 16px;
Explanation:
Definition:
1. Element Selector
Example:
<h1>Welcome to CSS</h1>
<p>This is a paragraph.</p>
H1 {
Color: red;
P{
Font-size: 18px;
2. Class Selector
Definition: Targets elements that have a specific class.
Example:
.highlight {
Background-color: yellow;
3. ID Selector
Example:
#main-title {
Font-size: 24px;
Color: green;
4. CSS Background
Definition:
Example:
Div {
Background-color: lightblue;
Padding: 10px;
Explanation:
Background Image:
Div {
Background-image: url(‘background.jpg’);
Background-size: cover;
}
Background Repeat:
Div {
Background-image: url(‘pattern.png’);
Background-repeat: repeat-x;
Background Position:
Div {
Background-image: url(‘image.jpg’);
Background-position: center;
Fixed Background:
Div {
Background-image: url(‘image.jpg’);
Background-attachment: fixed;
Definition:
1. Default Cursor:
Div {
Cursor: default;
2. Pointer Cursor:
<button>Click Me</button>
Button {
Cursor: pointer;
3. Text Cursor:
P{
Cursor: text;
4. Move Cursor:
Div {
Cursor: move;
}
The cursor changes to a move icon.
5. Custom Cursor:
Div {