Cascading Style Sheets (CSS) is a stylesheet language used to describe the presentation of a document written in HTML or XML. CSS controls the layout, colors, fonts, and overall visual appearance of web pages, allowing developers to separate content (HTML) from design (CSS). This separation of concerns makes web development more efficient, manageable, and flexible.
There are three main ways to apply CSS to an HTML document:
Inline CSS applies styles directly to individual HTML elements using the style attribute. This method is useful for quick, one-off changes but is not recommended for larger projects due to its lack of reusability and maintainability.
Example:
<body>
<h1 style="color: blue; text-align: center;">Hello, World!</h1>
</body>
Internal CSS is defined within a <style> tag in the <head> section of an HTML document. This method is useful for styling a single HTML document and provides better organization compared to inline CSS.
Example:
</head>
<body>
<h1>Hello, World!</h1>
External CSS is the most common and preferred method for linking CSS to HTML. Styles are defined in a separate CSS file, which is linked to the HTML document using the <link> tag. This method promotes reusability, maintainability, and consistency across multiple HTML documents.
Example of an HTML document linked to an external CSS file:
<head>
<body>
<h1>Hello, World!</h1>
</html>
External CSS file: styles.css
Basic Syntax of CSS
CSS syntax consists of selectors and declarations. A CSS rule-set is composed of a selector followed by a declaration block.
Selectors define which HTML elements the styles will be applied to. Common types of selectors include:
Element Selector: Selects all elements of a given type.
h1 { /* styles */ }
Class Selector: Selects elements with a specific class attribute.
.example-class { /* styles */ }
ID Selector: Selects a single element with a specific ID attribute.
#example-id { /* styles */ }
Declarations define the styles to be applied to the selected elements. A declaration consists of a property and a value, separated by a colon, and enclosed in curly braces.
Example:
Multiple declarations can be included within a single declaration block, separated by semicolons.
CSS provides a wide range of properties to style HTML elements. Some common properties include:
CSS is a powerful tool for web developers, allowing them to create visually appealing and consistent designs across multiple web pages. By separating content from presentation, CSS enhances maintainability and flexibility in web development. Understanding how to link CSS to HTML and mastering its basic syntax is essential for anyone looking to create modern, responsive, and user-friendly websites. As you progress through this course, you’ll delve deeper into CSS properties, selectors, and advanced techniques to further enhance your web development skills
Drag file here or click the button.
Drag file here or click the button.