The core idea behind CSS emerging alongside HTML 3 was the separation of content and presentation. HTML is used to define the structure and semantics of a web page, such as headings, paragraphs, lists, etc. CSS, on the other hand, is used to control the visual appearance of these elements, including colors, fonts, margins, and more.
The “cascade” in CSS refers to the way multiple style rules are combined and applied to an element. When there are conflicting style rules, the browser uses a set of rules to determine which style should take precedence. For example, inline styles (styles applied directly to an element using the style
attribute) generally have the highest precedence, followed by internal stylesheets (defined in the <style>
tag in the HTML head), and then external stylesheets (linked using the <link>
tag).
Selectors are used to target specific HTML elements to apply styles. In the early days, simple selectors like element selectors were commonly used. An element selector targets all instances of a particular HTML element. For example, the following CSS rule targets all <p>
elements on the page:
p {
color: blue;
}
Inline styles are applied directly to an HTML element using the style
attribute. This is the simplest way to apply styles, but it can make the HTML code cluttered and difficult to maintain.
<p style="color: red; font-size: 16px;">This is a paragraph with inline styles.</p>
Internal stylesheets are defined within the <style>
tag in the HTML head. This allows you to apply styles to multiple elements on the same page.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: green;
}
p {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to my page</h1>
<p>This is a paragraph.</p>
</body>
</html>
External stylesheets are stored in separate .css
files and linked to the HTML page using the <link>
tag. This is the most recommended way to apply styles as it allows for better organization and reusability.
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h2>External Stylesheet Example</h2>
<p>This paragraph uses styles from an external file.</p>
</body>
</html>
styles.css
h2 {
text-align: center;
}
p {
line-height: 1.5;
}
body {
font-size: 14px;
line-height: 1.6;
}
p {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
Understand the box model, which consists of content, padding, border, and margin. You can use CSS to control the dimensions and spacing of elements.
div {
width: 200px;
padding: 20px;
border: 1px solid black;
margin: 10px;
}
As mentioned earlier, separating the content (HTML) from the presentation (CSS) makes the code more maintainable and easier to understand. Use external stylesheets whenever possible.
Class selectors are used to apply styles to multiple elements, while ID selectors are used to target a single unique element. Avoid overusing ID selectors, as they can lead to specificity issues.
<!-- HTML -->
<p class="highlight">This paragraph has a special style.</p>
/* CSS */
.highlight {
background-color: yellow;
}
Minimize the number of HTTP requests by combining and minifying CSS files. This reduces the load time of the web page.
The emergence of CSS alongside HTML 3 was a significant milestone in the development of the web. It introduced the concept of separating content and presentation, which has since become a fundamental principle in web design. By understanding the fundamental concepts, usage methods, common practices, and best practices of CSS, developers can create more visually appealing and maintainable web pages. As the web continues to evolve, CSS has become even more powerful, but the basics learned from its early days still form the foundation of modern web design.