What is CSS?

Frontend

readTime

6 min

What is CSS?

What is CSS?

CSS (Cascading Style Sheets) is a language used to describe how HTML elements should be displayed on a web page. CSS allows you to separate the structure of a document (created with HTML) from its visual presentation, including styles like colors, fonts, sizes, margins, alignments, and more. With CSS, you can change the appearance of an entire website from a single file, saving time and simplifying the process of maintaining and updating web pages.

CSS was created primarily to separate the structure of a document from its visual appearance. This separation increases the accessibility of a website, reduces the complexity of the document, and makes it easier to update and maintain the code.

The relationship between HTML and CSS is tightly connected. While HTML provides the structure and backbone of a webpage, CSS focuses on the style and aesthetics of the site.

Technically, CSS is not strictly necessary for a website to function, but you probably wouldn’t want to look at a website that only contains HTML. Such a site would appear plain and primitive without CSS’s ability to enhance the visual presentation.

In short, CSS allows you to avoid repeating style instructions in HTML. It saves time, shortens the code, and makes it less prone to errors.

How Does CSS Work?

CSS uses a simple syntax based on English with a set of rules that govern how it works. HTML was never meant for styling elements, but rather for creating the layout of a page using elements like headings, paragraphs, images, and links.

The structure of CSS syntax is straightforward. It consists of a selector and a declaration block. You select an element, and then declare what style changes you want to apply to it.

  • Selector: It specifies the HTML element(s) you want to style.
  • Declaration block: This contains one or more declarations separated by semicolons.

Each declaration consists of a CSS property and a value, separated by a colon. A CSS declaration always ends with a semicolon, and the declaration block is enclosed in curly braces {}.

Here's an example of the basic CSS syntax:

css
h1 {
  color: blue;
  font-size: 24px;
}

In the above example:

  • The selector h1 targets all <h1> elements in the HTML document.
  • The declaration block { color: blue; font-size: 24px; } specifies that the text inside all <h1> elements should be blue and have a font size of 24 pixels.

The power of CSS lies in its ability to be applied globally. You can write one set of rules and apply it to multiple pages, or even an entire website, by linking to a single CSS file. This provides a consistent style and appearance across all pages.

Why Do You Need to Use CSS?

1. You Don’t Have to Repeat Yourself

When creating basic websites with HTML, CSS, and JavaScript, you can define your styles once in a CSS file, and they will apply to multiple elements and pages. This eliminates the need to write the same styling rules over and over.

2. It Saves a Lot of Time

Since CSS styles are saved in external CSS files, you can modify the appearance of the entire website by changing just one file. This drastically reduces the time spent updating or restyling web pages.

3. It Provides Extensive Styling Options

CSS offers a variety of ways to style elements. Rather than styling each element individually, you can style by classes, descendants, siblings, nth-child, even/odd elements, and more. These powerful selectors allow you to apply styles more efficiently and selectively.


Core Features of CSS

1. Selectors

CSS provides different ways to target HTML elements. Here are the most common types of selectors:

  • Type Selector: Targets all elements of a specific type (e.g., h1, p, div).

    css
    p {
      color: green;
    }
    
  • Class Selector: Targets elements with a specific class attribute. A class can be applied to multiple elements.

    css
    .btn {
      background-color: blue;
      color: white;
    }
    
  • ID Selector: Targets a single element with a specific ID attribute. Each ID must be unique on the page.

    css
    #header {
      font-size: 32px;
    }
    
  • Descendant Selector: Targets elements that are inside a specific parent element.

    css
    div p {
      color: red;
    }
    
  • Attribute Selector: Targets elements based on the presence or value of an attribute.

    css
    input[type="text"] {
      border: 1px solid black;
    }
    

2. Box Model

The box model is a fundamental concept in CSS, which determines how elements are rendered on a page. Every element is considered a rectangular box, and the box model is used to calculate:

  • Content: The actual content of the element (text, images, etc.).
  • Padding: The space between the content and the border.
  • Border: A line surrounding the padding (if any) and content.
  • Margin: The space between the border and surrounding elements.

Here's an example:

css
div {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
  margin: 10px;
}

This structure determines how the element takes up space on the page. The width of the div will be 300px for the content, but with padding, border, and margin included, the total space taken up will be larger.

3. Flexbox

Flexbox is a layout module in CSS that allows you to design complex layouts more easily. Flexbox makes it simpler to align items vertically, horizontally, and to distribute space within a container.

css
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

In the example above, justify-content centers the items horizontally, and align-items centers them vertically within the container.

4. Grid Layout

The Grid layout is another powerful CSS module for creating two-dimensional layouts. It allows you to define rows and columns and place items within those spaces.

css
.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 20px;
}

Here, the grid-template-columns property creates two equal-width columns, and grid-gap adds space between the grid items.

5. Media Queries

Media Queries in CSS are used to apply styles based on the screen size or device characteristics. They make your website responsive, adjusting its layout for different devices like phones, tablets, and desktops.

css
@media (max-width: 768px) {
  body {
    background-color: lightgray;
  }
}

In this example, if the screen width is 768 pixels or less (commonly for tablets or mobile devices), the background color of the body element changes to light gray.

6. Pseudo-classes and Pseudo-elements

  • Pseudo-classes are used to define a special state of an element. For example, :hover applies styles when the user hovers over an element with the mouse.

    css
    a:hover {
      color: red;
    }
    
  • Pseudo-elements allow you to style specific parts of an element, such as the first letter or first line.

    css
    p::first-letter {
      font-size: 200%;
      color: blue;
    }
    

How CSS Enhances Web Development

1. Better Design Control

With CSS, you can precisely control every aspect of the appearance of a website. It provides the tools to create a visually appealing layout while ensuring that the structure remains clear and accessible.

2. Consistency Across Pages

CSS allows for consistent styling across multiple pages of a website. By defining styles in an external CSS file and linking it to multiple HTML documents, you ensure that all pages have the same look and feel, reducing redundancy and improving maintainability.

3. Responsive Web Design

With the growing number of devices, including smartphones, tablets, and desktops, it’s essential for websites to adjust to different screen sizes. CSS enables responsive design, allowing developers to create flexible layouts that adapt to different devices, improving the user experience.

4. Ease of Maintenance

With CSS, changes to the design can be made in one place, and they will be reflected across the entire site. This makes it easier to maintain and update a website over time.

5. Performance Benefits

CSS can improve the performance of a website by reducing the amount of code needed in HTML files. By separating the styling from the content, it minimizes file sizes, leading to faster load times and better performance on the client side.


How to Get Started with CSS

  1. Basic Setup: Start by linking an external CSS file to your HTML document. Use the <link> tag inside the <head> section to link your CSS file.
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <title>My Website</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    <p>This is a sample webpage.</p>
  </body>
</html>
  1. Start Writing CSS: Open the linked CSS file and start by styling some basic elements, like setting the background color or changing the font size.
css
body {
  background-color: #f0f0f0;
  font-family: Arial, sans-serif;
}

h1 {
  color: #333;
}

p {
  font-size: 16px;
  line-height: 1.5;
}

Conclusion

CSS is an indispensable tool for any web developer, allowing for the creation of visually engaging and responsive websites. By learning how to use CSS effectively, you can control the appearance of your website and make it more user-friendly and aesthetically pleasing. From controlling layout with Flexbox and Grid to ensuring your site looks great on any device with media queries, CSS is essential for modern web development.

authorImg

Witek Pruchnicki

I passionately share knowledge about programming and more in various ways.