
saxenadivya0007
Friday, 2024-07-26
CSS allows web developers to separate the content of a webpage from its design, making it easier to update and maintain websites. By using CSS, developers can control the layout, colors, fonts, and other visual aspects of a website with ease. This helps create a consistent and professional look across all pages of a site. Additionally, CSS allows for responsive design, making websites adapt to different screen sizes and devices. Overall, CSS is an essential tool for creating visually appealing and user-friendly websites.
A CSS rule consists of a selector and a declaration block:
css
Copy code
selector {
property: value;
property: value;
}
css
Copy code
body {
background-color: lightblue;
}
h1 {
color: navy;
text-align: center;
}
p {
font-size: 20px;
color: grey;
}
In this example:
body selector sets the background color of the entire page to light blue.h1 selector changes the color of all <h1> elements to navy and centers the text.p selector changes the font size of all <p> elements to 20 pixels and the color to grey.style attribute.<style> tag in the HTML <head> section..css file.html Copy code <p style="color: blue;">This is a blue paragraph.</p>
html
Copy code
<head>
<style>
p { color: blue; }
</style>
</head>
Link to an external stylesheet:
html Copy code <head> <link rel="stylesheet" type="text/css" href="styles.css"> </head>
In the styles.css file:
css
Copy code
p {
color: blue;
}
Selectors define which HTML elements the CSS rule applies to. Common types include:
p, h1)..classname).#idname).[type="text"]).css
Copy code
/* Element selector */
h1 {
color: red;
}
/* Class selector */
.blue-text {
color: blue;
}
/* ID selector */
#main-header {
font-size: 2em;
}
/* Attribute selector */
input[type="text"] {
border: 1px solid black;
}