CSS and Selectors blog post image

What is CSS? Types, Selectors, Flexbox, Grid & More Explained

CSS (Cascading Style Sheets) is essential for styling web pages and enhancing their visual appeal. It allows developers to control layout, typography, colors, spacing, and responsiveness. This article will explore fundamental CSS concepts, including selectors, properties, values, box model, positioning, flexbox, grid, and responsive design.

 

If you’re new to web development, it’s highly recommended to start with understanding HTML, the foundational structure of any webpage. You can check out our previous article:👉 HTML for beginners to advanced and best practices . Once you’re comfortable with HTML, CSS becomes your toolkit for turning plain content into a beautiful and responsive website.

 

Topics covered in this article are:

What is CSS?

Types of CSS

CSS Selectors

CSS Properties & Values

Box Model

CSS Positioning

Flexbox

Grid Layout

Practicing CSS Hands-On

Conclusion

What is CSS?

CSS is like the designer behind a webpage—it controls how things look and feel. From colors and spacing to fonts and layout, CSS gives style to plain HTML content and makes websites visually appealing. It separates content from design, improving maintainability and consistency across web pages.

In simple terms, CSS tells the browser how your website should look. It’s like dressing up the content created with HTML—changing colors, font sizes, spacing, and layout.

Types of CSS

CSS can be applied in three ways:

You can write CSS in three different ways, depending on what works best for your project. All three do the same thing—style your web page—but in different places.

  1. Inline CSS – This method involves writing styles directly in an HTML element’s style attribute. It’s quick for small tweaks but not ideal for maintaining large projects.

    <h1 style="color: red;">This is a heading</h1>
  2. Internal CSS – Defined within a <style> tag inside the <head> section.

    <style>
        p {
            color: green;
            font-size: 16px;
        }
    </style>
  3. External CSS – Stored in a separate file (.css) and linked using <link>.

    <link rel="stylesheet" href="styles.css">

CSS Selectors

CSS selectors are like guides that help the browser find and style specific elements on a webpage. Whether you want to target all paragraphs or just one special button, selectors let you apply styles exactly where needed. Think of them like labels or addresses that target the right part of your webpage.

Common CSS Selectors:

  • Element Selector: Targets every element of a specific type, like all <p> or <h1> tags.

    p {
        color: blue;
    }
  • Class Selector (.): Targets all elements that share a common class name, allowing you to apply the same style to multiple items.

    .highlight {
        background-color: yellow;
    }
  • ID Selector (#): Styles a single unique element with a specific ID. It’s best used for elements that appear only once.

    #header {
        font-size: 24px;
    }
  • Group Selector (,): Targets several elements at once by separating them with commas.

    h1, h2, p {
        font-family: Arial, sans-serif;
    }
  • Descendant Selector ( ): Styles elements that are nested inside a specific parent.

    div p {
        color: gray;
    }

CSS Properties & Values

CSS consists of properties (what to style) and values (how to style it).

In simple terms, a property is the thing you want to change (like color or size), and the value is how you want to change it.

Example:

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

Box Model

The box model is like a set of invisible layers around each element that define how it’s spaced and displayed on a webpage.

Think of every HTML element as a box made of four layers: content, padding, border, and margin. Understanding these layers helps you control spacing and layout better.

  • Content: The actual content inside an element.

  • Padding: Space between content and border.

  • Border: Surrounds the padding and content.

  • Margin: Space outside the border, separating elements.

Example:

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

CSS Positioning

Positioning controls where elements appear on a page, giving you full control over their layout and behavior.

  • Static (default): Elements flow naturally in the document.

  • Relative: Moves the element slightly from its original position.

  • Absolute: Places the element based on the closest positioned parent.

  • Fixed: Keeps the element in place even when scrolling.

  • Sticky: Acts like relative until it reaches a point during scroll, then sticks in place.

Example:

				
					.fixed-header {
    position: fixed;
    top: 0;
    width: 100%;
    background-color: black;
    color: white;
}
				
			

Flexbox

Flexbox is perfect for arranging items in a straight line—either across (row) or down (column). It adjusts layout automatically, even on different screen sizes.

Example:

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

Grid Layout

CSS Grid helps you build layouts in both rows and columns, making it easier to manage complex designs.

While Flexbox is ideal for layouts in one direction, Grid allows you to design in two directions—both across and down. It lets you create sections on the page and place items exactly where you want.

Example:

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

Responsive Design

Responsive design ensures your site looks great on all devices, from desktop to mobile.

You can use media queries to change styles based on screen size.

Example:

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

Practicing CSS Hands-On

The best way to truly learn CSS is by getting your hands dirty with code. Try experimenting with different selectors, properties, and layout techniques to see how they affect your webpage. Create small projects such as a personal profile card, a responsive navigation bar, or a simple grid layout. Use developer tools in your browser to inspect and tweak styles in real time. By actively building and testing, you’ll reinforce your understanding of CSS and become more confident in applying styles creatively and efficiently.

Conclusion

CSS is crucial for designing attractive and responsive websites. It empowers developers and designers to transform plain HTML content into beautiful, user-friendly web pages. Understanding how to use CSS selectors, properties, the box model, positioning, flexbox, and grid not only helps create visually appealing designs but also ensures your website functions well across all devices.

Keep experimenting, build mini projects, and revisit concepts regularly to strengthen your CSS skills. In the next article, we’ll dive into CSS positions, animations and transitions to add dynamic effects and interactivity to your designs. Stay tuned!

For continuous learning, refer to MDN Web Docs, upskillinghub, and W3Schools which provide up-to-date information on CSS properties, layout techniques, and the latest web standards.

 

Do you have any questions about CSS? Let us know in the comments!

Share your Experience on Social Networks

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *