Curriculum
Course: HTML5 and CSS from Zero to Hero
Login

Curriculum

HTML5 and CSS from Zero to Hero

Video lesson

What’s CSS?

Introduction to CSS

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.

Why CSS is Important

  • Separation of Concerns: By separating content from presentation, CSS makes HTML code cleaner and easier to maintain.
  • Reusability: CSS allows you to style multiple HTML elements simultaneously, promoting code reusability and consistency across a website.
  • Design Flexibility: CSS provides a wide range of styling options, enabling developers to create visually appealing and responsive web designs.
  • Performance: CSS can reduce page load times by minimizing the amount of HTML code and leveraging browser caching.

Linking CSS to HTML

There are three main ways to apply CSS to an HTML document:

    1. inline,
    2. internal, and
    3. external.

Inline CSS

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:

<!DOCTYPE html>
 <html>
<head> <title>Inline CSS Example</title> </head>
<body>
<h1 style="color: blue; text-align: center;">Hello, World!</h1>
</body>
</html>

Internal CSS

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:

<!DOCTYPE html>
<html>
<head>
<title>Internal CSS Example</title>
<style>
h1 {
color: blue;
text-align: center;
}
</style>
</head>

<body>

<h1>Hello, World!</h1>

</body>
</html>

External CSS

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:

<!DOCTYPE html>
<html>

<head>

<title>External CSS Example</title>

<link rel=“stylesheet” href=“styles.css”>

</head>

<body>

<h1>Hello, World!</h1>

</body>

</html>

 

External CSS file: styles.css

/* styles.css */
h1 {
color: blue;
text-align: center;
}

 

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

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

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:

h1 {
color: blue;
/* Property: color, Value: blue */
text-align: center;
/* Property: text-align, Value: center */

 

}

Multiple declarations can be included within a single declaration block, separated by semicolons.

CSS Properties

CSS provides a wide range of properties to style HTML elements. Some common properties include:

  • Color: Sets the text color of an element.
  • Background: Sets the background color or image of an element.
  • Font: Sets the font properties of an element, including font-family, font-size, font-weight, and font-style.
  • Text Alignment: Aligns the text within an element.
  • Padding and Margin: Sets the space inside (padding) and outside (margin) an element’s border.
  • Border: Sets the border properties of an element, including border-width, border-style, and border-color.

Conclusion

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