DEV Community

wasifali
wasifali

Posted on

Introduction of CSS, What is CSS, Why we use CSS and How CSS describe the HTML elements

What is CSS?

CSS stands for Cascading Style Sheets
CSS describes how HTML elements are to be displayed on screen, paper, or in other media
CSS saves a lot of work. It can control the layout of multiple web pages all at once
External stylesheets are stored in CSS files
Why we Use CSS?
CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.

Example

body {
  background-color: lightblue;
}

h1 {
  color: white;
  text-align: center;
}

p {
  font-family: verdana;
  font-size: 20px;
}
Enter fullscreen mode Exit fullscreen mode

CSS Solved a Big Problem

HTML was never intended to contain tags for formatting a web page!
HTML was created to describe the content of a web page, like:
<h1>This is a heading</h1>
<p>This is a paragraph.</p>

CSS Saves a Lot of Work!

The style definitions are normally saved in external .css files.

CSS Syntax

A CSS rule consists of a selector and a declaration block.
h1 {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.

Example

p {
  color: red;
  text-align: center;
}
Enter fullscreen mode Exit fullscreen mode

Example Explained

p is a selector in CSS (it points to the HTML element you want to style: <p>).
color is a property, and red is the property value
text-align is a property, and center is the property value

CSS Selectors

CSS selectors are used to "find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
Simple selectors (select elements based on name, id, class)
Combinator selectors (select elements based on a specific relationship between them)
Pseudo-class selectors (select elements based on a certain state)
Pseudo-elements selectors (select and style a part of an element)
Attribute selectors (select elements based on an attribute or attribute value)

The CSS element Selector

The element selector selects HTML elements based on the element name.

Example

p {
  text-align: center;
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)