Introduction

CSS is the language we use to style a Web page.

What is CSS?

Cascading Style Sheets, fondly referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independently of the HTML that makes up each web page. It describes how a webpage should look: it prescribes colours, fonts, spacing, and much more. In short, you can make your website look however you want. CSS lets developers and designers define how it behaves, including how elements are positioned in the browser.
While HTML uses tags, CSS uses rulesets. CSS is easy to learn and understand, but it provides powerful control over the presentation of an HTML document.

syntax

CSS comprises style rules that are interpreted by the browser and then applied to the corresponding elements in your document. A style rule set consists of a selector and declaration block.

  1. Selector: A selector in CSS is used to target and select specific HTML elements to apply styles to.
  2. Declaration: A declaration in CSS is a combination of a property and its corresponding value.
    Selector -- h1 Declaration -- {color:blue;font size:12px;}
    • The selector points to the HTML element you want to style.
    • The declaration block contains one or more declarations separated by semicolons.
    • Each declaration includes a CSS property name and a value, separated by a colon.

Selector

A CSS selector selects the HTML element(s) for styling purposes. CSS selectors select HTML elements according to their id, class, type, attribute, etc.

CSS selectors: There are many basic different types of selectors.

  • Simple Selector: It is used to select the HTML elements based on their element name, id, attributes, etc.

  1. Element selector
  2. Id selector
  3. Class selector
  4. Universal selector
  5. Group selector

Element selector

The element selector selects HTML elements based on the element name (or tag) for example p, h1, div, span, etc.


style.css: The following code is used in the above Example. You can see the CSS rules applied to all p tags and h1 tags.

syntax

h1 { color: red; font-size: 3rem; }

p { color: white; background-color: gray; }

Id selector

The id selector uses the id attribute of an HTML element to select a specific element.

Note: An id of the element is unique on a page to use the id selector.

style.css: The following code is used in the above Example using the id selector.

Syntax:

#div-container{
color: blue;
background-color: gray; }

Class selector

The class selector selects HTML elements with a specific class attribute.

style.css: The following code is used in the above Example using the class selector. To use a class selector you must use ( . ) followed by class name in CSS. This rule will be applied to the HTML element with the class attribute “paragraph-class“

Syntax:

.paragraph-class {
color:white;
font-family: monospace;
background-color: purple; }

Universal selector

The Universal selector (*) in CSS is used to select all the elements in an HTML document. It also includes other elements which are inside under another element.

style.css: The following code is used in the above Example using the universal selector. This CSS rule will be applied to each and every HTML element on the page:

Syntax:

* {
color: white;
background-color: black;
}

Group selector

This selector is used to style all comma-separated elements with the same style.
style.css: The following code is used in the above Example using the group selector. Suppose you want to apply common styles to different selectors, instead of writing rules separately you can write them in groups as shown below.

Syntax:

#div-container, .paragraph-class, h1{
color: white;
background-color: purple;
font-family: monospace;
}