CSS 101: Essential Techniques for Modern Web Design

Sabat AliSabat Ali
4 min read

CSS (Cascading Style Sheets) is a language used to describe the presentation of a document written in HTML. It controls the look and feel of a website, making it more visually appealing and responsive. In this blog, we’ll cover the essential techniques that form the foundation of modern web design using CSS.


What is CSS?

CSS is responsible for how elements on a web page are displayed, from layout to fonts, colors, spacing, and responsiveness. It separates content (HTML) from presentation, making it easier to manage the style across multiple pages of a website.


1. CSS Syntax

CSS rules are composed of two main parts:

  • Selectors: These are used to target HTML elements that you want to style.

  • Declarations: This is where the styling happens. Each declaration consists of a property and a value.

selector {
  property: value;
}

Example:

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

In this example, the h1 selector targets all <h1> elements in the HTML and changes their color to blue and the font size to 32px.


2. Basic Selectors

CSS provides different types of selectors to target specific elements. The most commonly used ones are:

  • Type Selector: Targets HTML tags directly.

      p {
        color: green;
      }
    
  • Class Selector: Targets elements with a specific class attribute. Prefixed with a dot ..

      .highlight {
        background-color: yellow;
      }
    
  • ID Selector: Targets a single element with a unique ID. Prefixed with a #.

      #header {
        font-weight: bold;
      }
    

3. Common CSS Properties

Here are some of the most commonly used CSS properties:

Text Properties

  • color: Sets the color of the text.

      body {
        color: #333;
      }
    
  • font-family: Specifies the font type.

      p {
        font-family: 'Arial', sans-serif;
      }
    
  • font-size: Defines the size of the text.

      h1 {
        font-size: 36px;
      }
    
  • text-align: Aligns text horizontally within its container.

      h1 {
        text-align: center;
      }
    

Box Model Properties

The CSS box model defines the space around elements. It consists of:

  • margin: The space outside the element's border.

  • padding: The space inside the element's border.

  • border: The border surrounding the element.

div {
  margin: 20px;
  padding: 15px;
  border: 2px solid black;
}

4. Layout Techniques with CSS

CSS has powerful layout tools that allow you to control the positioning and alignment of elements on a page. The two most common layout systems are Flexbox and Grid.

Flexbox

Flexbox is a one-dimensional layout model used for creating flexible and responsive layouts.

.container {
  display: flex;
  justify-content: space-between;
}

This code makes the child elements of the .container class align horizontally, with equal spacing between them.

Grid

Grid is a two-dimensional layout system, giving you control over both rows and columns.

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

Here, three columns are created in the .grid-container, with a gap of 20px between each item.


5. Responsive Design with Media Queries

Responsive design ensures that websites look good on all devices, whether it’s a desktop, tablet, or smartphone. CSS media queries allow you to apply styles based on the device’s screen size.

@media (max-width: 768px) {
  body {
    font-size: 14px;
  }

  .container {
    flex-direction: column;
  }
}

In this example, if the screen width is 768px or smaller, the font size is reduced to 14px and the flexbox container stacks its items vertically instead of horizontally.


6. CSS Animations

CSS allows you to create smooth transitions and animations without the need for JavaScript.

Transitions

Transitions make it easy to animate changes to CSS properties.

button {
  background-color: blue;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: red;
}

In this code, when the user hovers over the button, it smoothly transitions from blue to red.

Keyframe Animations

Keyframe animations allow for more complex animations by defining different stages of the animation.

@keyframes move {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(100px);
  }
}

.box {
  animation: move 2s infinite;
}

The .box will move 100px horizontally in 2 seconds and repeat this movement infinitely.


7. Conclusion

CSS is an essential tool for creating modern web designs. With selectors, the box model, responsive design, and layout systems like Flexbox and Grid, you can build visually appealing and user-friendly websites. As you grow in your CSS journey, experimenting with animations and advanced layouts will help you create even more interactive and dynamic designs.

0
Subscribe to my newsletter

Read articles from Sabat Ali directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sabat Ali
Sabat Ali