CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML. It allows you to control the layout, colors, fonts, and overall visual appearance of web pages, providing separation of content (HTML) and design (CSS).
HTML structures the content of the page, while CSS defines the style and layout. By linking a CSS file or adding CSS code to HTML, you can control the appearance of multiple web pages at once, making design updates easy and consistent across a site.
Example HTML Structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Introduction to CSS</title>
<!-- Linking External CSS File -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, CSS!</h1>
<p>This is an example of how CSS works with HTML.</p>
</body>
</html>
Example External CSS (styles.css):
/* This CSS file controls the style of the HTML page */
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
h1 {
color: #2c3e50;
text-align: center;
}
p {
color: #34495e;
font-size: 18px;
line-height: 1.6;
text-align: center;
}
In this example:
styles.css
).body
, h1
, and p
elements.style
attribute. This method is useful for small or specific changes but not recommended for larger projects as it can make the code harder to maintain.Example of Inline CSS:
<p style="color: blue; text-align: center;">This is a paragraph styled using inline CSS.</p>
<style>
tag inside the HTML <head>
section. This method is suitable for individual pages where specific styling is needed, but it doesn’t scale well for multiple pages.Example of Internal CSS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: #e0f7fa;
}
h1 {
color: #006064;
}
p {
font-size: 16px;
}
</style>
<title>Internal CSS Example</title>
</head>
<body>
<h1>Internal CSS</h1>
<p>This page is styled using internal CSS.</p>
</body>
</html>
.css
extension) and linked to the HTML file using the <link>
tag. This method is the most common and recommended for large projects.Example of External CSS (HTML and CSS shown above).
CSS is written using a combination of selectors and declarations. The basic syntax follows this structure:
selector {
property: value;
property: value;
}
p
, h1
, body
).color
, font-size
).blue
, 16px
).Example:
p {
color: red;
font-size: 14px;
}
Comments in CSS are used to explain sections of code or leave notes. They do not affect the code and are ignored by the browser. You can add a comment using /* comment here */
.
Example:
/* This is a comment explaining the styles below */
h1 {
color: green;
text-align: center;
}
CSS stands for Cascading Style Sheets, meaning that rules are applied in order of importance and specificity. When multiple CSS rules conflict, the browser uses the following order of precedence:
<style>
element) – medium priority.If you have the following styles applied to an element:
<p style="color: blue;">This is a paragraph.</p>
p {
color: red;
}
The text will be blue, as the inline style takes precedence over the external CSS rule.
Next -> Css Selector
Back to Contents page click here