Skip to main content

Command Palette

Search for a command to run...

CSS Basics - Styling the Web

Published
3 min read
CSS Basics - Styling the Web
A

follow: https://x.com/iadityarxj

CSS (Cascading Style Sheets) is what makes the web look good. Without it, websites would just be plain text on a white background—pretty boring, right? CSS lets you add colors, fonts, layouts, and spacing, bringing webpages to life. In this guide, we’ll break down two key concepts: the CSS Box Model and the CSS Specificity Algorithm—both of which are essential for making your designs work as expected.

Understanding the CSS Box Model: Margins, Borders, and Padding

Imagine every element on your webpage is a box. The CSS Box Model determines how that box behaves, how much space it takes up, and how it interacts with other elements. Knowing how this works will save you a ton of headaches when trying to get your layout just right.

The Four Parts of the Box Model:

  1. Content – This is the actual text, image, or anything inside the element.

  2. Padding – The space between the content and the border.

  3. Border – The outline surrounding the padding and content.

  4. Margin – The space between this element and the next one.

Real-World Example

Let’s say you style a div like this:

.box {
    width: 200px;
    height: 100px;
    padding: 20px;
    border: 5px solid black;
    margin: 30px;
    background-color: lightblue;
}

This means:

  • The content inside is 200px wide.

  • There’s 20px of padding around the content.

  • A 5px solid black border wraps around it.

  • A 30px margin separates it from other elements.

Why Should You Care?

If you’ve ever wondered why elements on your page aren’t lining up as expected, chances are the box model is the culprit. Maybe your padding is pushing things out, or your margins are creating extra space. Understanding these four layers can save you a lot of trial and error!

CSS Specificity: Who Wins When Styles Clash?

Ever had a style that just refused to apply no matter what? That’s because CSS follows a specificity hierarchy, meaning some styles are more powerful than others. Knowing this will help you avoid frustration and make your styles work predictably.

How Specificity Works

CSS assigns a “weight” to different types of selectors. When multiple styles target the same element, the rule with the highest specificity wins.

Selector TypeExampleSpecificity Score
Universal selector*0
Element and pseudo-elementsp, h1, ::before1
Class, attributes, pseudo-classes.nav, [type="text"], :hover10
ID selector#header100
Inline stylesstyle="color: red;"1000
!important (overrides everything)color: red !important;Overrides all

Specificity in Action

Some example:

h1 {
    color: blue;
}  /* Specificity: 1 */

.header {
    color: green;
}  /* Specificity: 10 */

#main-title {
    color: red;
}  /* Specificity: 100 */

If an <h1> element has all these styles applied, the ID selector #main-title wins because it has the highest specificity, so the text will be red.

Common Mistakes to Avoid

  1. Overusing Inline Styles – They override everything, making it hard to maintain your styles.

  2. Ignoring Specificity – Leads to confusion when styles don’t apply as expected.

  3. Relying Too Much on IDs – IDs have high specificity, making styles difficult to override without adding unnecessary complexity.

  4. Not Accounting for the Box Model – Overlooking margins, padding, and borders can cause unexpected layout issues.

Wrapping Up

Mastering CSS basics like the Box Model and Specificity Algorithm will help you style your webpages with confidence. The best way to learn? Experiment! Try different values, play around with styles, and see how small tweaks can dramatically change the look and feel of a page. Keep practicing, and soon CSS will feel like second nature!