Cascading Style Sheets

background image
Home / Learn / Web Development /
Cascading Style Sheets

Cascading Style Sheets (CSS) is a stylesheet language used for describing the look and formatting of a document written in HTML. CSS is used to control the style of a web document in a simple and easy-to-use way. It is used to control the layout, colors, fonts, and other visual aspects of a website or web application.

CSS is typically used in conjunction with HTML to create the layout and design of a website or web application. It allows developers to separate the content of a website, written in HTML, from the design and layout, defined in the CSS. This makes it easier to make global design changes to a website, as the CSS can be modified without changing the underlying HTML.

With CSS, you can:

  • Control the font and text size and color of elements on the page

  • Set the background color or image of an element

  • Set the layout of a page, including the positioning of elements and the sizing of elements

  • Hide or show elements on the page

CSS is a powerful tool for separating the content of a web page (written in HTML) from its presentation (defined by the styles in CSS). This separation can make it easier to maintain and update a website, because the HTML can be focused on the structure and content of the page, while the CSS handles the visual styling.

CSS consists of a series of rules that are applied to the elements in an HTML document. Each rule consists of a selector, which defines the elements to which the rule applies, and a declaration block, which defines the styles to be applied to the elements. For example, the following CSS rule would make all <h1> elements red. An id is a unique identifier for a page element. It can be used to select a single element on the page, and apply styles to it. The class attribute is used to apply styles to multiple elements on a page. It can be used on any element, and elements can have multiple classes.

For example:

<style>
  h1 {
    color: red;
  }
  .red-text {
    color: red;
  }
  #big-header {
    font-size: 32px;
  }
</style>

<h1 id="big-header" class="red-text">Welcome!</h1>

In this example, the h1 element has both an id of "big-header" and a class of "red-text". The id is used to apply a style of font-size: 32px, while the class is used to apply a style of color: red.

The style attribute is used to apply styles directly to an element, and takes precedence over any styles applied through a class or id.

For example:

<h1 id="big-header" class="red-text" style="color: blue;">Welcome!</h1>

In this example, the text color of the h1 element will be blue, because the style attribute takes precedence over the class attribute.

CSS can be written directly in an HTML document, or it can be linked to an external stylesheet file. This allows the same styles to be reused across multiple pages on a website.

CSS is an essential part of web development, as it is used to control the visual appearance of websites and web applications.