Introducing CSS

Hello there! Today, we are venturing into an exploration of CSS (Cascading Style Sheets), a language that contributes endlessly to bringing color, life, and structure to webpages. Think about this: once you have constructed a building with bare walls with HTML, CSS becomes the tool that paints the walls, decorates the room, and makes it cozy. Amazing, isn't it? Let's dive in!

Basic Syntax of CSS

The syntax of CSS is similar to writing commands for your dog — you choose the dog's name (selector), and then tell it what to do (declarations). For instance, let's create a CSS rule-set:

h1 { 
  color: blue; 
  font-size: 12px; 
}

In this example, h1 is the selector (our pet), color and font-size are the properties (commands), and blue and 12px are the corresponding values that indicate how to perform the command. This code makes all <h1> elements have a blue text that is 12 pixels big.

Connecting CSS to HTML

There are three ways we can connect CSS to HTML:

  • Inline CSS: We apply styles directly within the HTML element using the style attribute:
<h1 style="color:blue;">A Blue Heading</h1>
  • Internal CSS: Here, CSS rules reside within the <style> tag inside the head section of the HTML document.
<head>
  <style>
    h1 {color: red;}
    p {color: blue;}
  </style>
</head>
  • External CSS: For larger projects, we link separate .css files to the HTML document using the <link> tag:
<head>
  <link rel="stylesheet" href="styles.css">
</head>

Accompanied by the corresponding styles.css:

h1 {color: red;}
p {color: blue;}
CSS Selectors

CSS selectors are used to "find" HTML elements based on properties of the elements. There are several different types of selectors in CSS:

  • Element Selector: This targets HTML elements by their tag name. Any style assigned to an element selector will apply to all elements with that tag on a page. For instance:
p {
  color: red;
  font-weight: bold;
}

In this example, all <p> (paragraph) elements on the page would receive the styles specified (in this case, they will have red text in bold).

  • Class Selector: This targets HTML elements with a particular class attribute. They are preceded by a dot (.) in CSS (The /* and */ notations used in the code snippet below mark the comments in CSS):
.important-text {
  color: red;
  font-style: italic; /* makes the text italic */
}

In your HTML, you'd then add the class attribute with the value important-text to the elements you want affected:

<p class="important-text">This text is considered important!</p>

This class can be reused across multiple elements on your page, allowing them all to share the same styles.

  • Id Selector: This targets a unique HTML element by its id attribute. It is preceded by a hash (#) in CSS:
#special-text {
  color: red;
}

And, in HTML you'd assign this id to one specific element:

<p id="special-text">This text is unique and hence, red!</p>

Remember, id is meant to be unique, and should only be applied to one element on a page.

By mastering these selectors, you'll be able to control the presentation of every element on your page for a beautiful and consistent design.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal