DEV Community

Cover image for My Journey Learning CSS - Selectors and Box Model ๐Ÿš€ (Day-8)
Angshuman
Angshuman

Posted on

2

My Journey Learning CSS - Selectors and Box Model ๐Ÿš€ (Day-8)

Cascading Style Sheets (CSS) is a fundamental part of web design, allowing developers to control the look and feel of a webpage. Today, we'll dive into essential CSS selectors and the CSS Box Model, along with an interesting concept known as Margin Collapse

Understanding CSS Selectors ๐Ÿ“Š

CSS selectors are used to target and style specific HTML elements. Here are some key types of selectors:

Element Selector

This selector applies styles to all instances of a specific HTML element.

div {
color: blue;
}

The above rule applies to all <p> elements, making the text color blue.

Class Selector

Class selectors are reusable and apply styles to elements with a specific class.

.my-class {
font-size: 18px;
}

Apply this style by adding class="my-class" to an element.

ID Selector

ID selectors target a unique element with a specific ID.

#unique-id {
background-color: yellow;
}

Since IDs are unique, this style applies only to one element per page.

Child Selector

This targets only direct child elements.

div > p {
color: red;
}

Only <p> elements directly inside a <div> are styled.

Descendant Selector

Unlike the child selector, this applies styles to all matching nested elements.

div p {
color: green;
}

This rule affects all <p> elements inside a <div>, no matter how deeply nested they are.

Universal Selector

Applies styles to all elements on a page.

* {
margin: 0;
padding: 0;
}

Useful for resetting default browser styles.

Pseudo Selectors

These include pseudo-classes and pseudo-elements:

Image description

CSS Box Model ๐Ÿ“Š

Every element in CSS is structured as a box consisting of the following:

  1. Content - The actual text, image, or media.
  2. Padding - Space between the content and border.
  3. Border - A boundary around the element.
  4. Margin - Space outside the element, affecting its distance from others.

Example of Box Model

Image description

Margin Collapse ๐Ÿ“Š

Margin collapse occurs when vertical margins of adjacent elements overlap instead of adding up.

Example

Instead of 50px (20px + 30px), the actual margin will be 30px (the larger value).

Avoiding Margin Collapse

Use padding instead of margin or add overflow: hidden; to the parent container.

Conclusion ๐ŸŽฏ

Understanding CSS selectors and the box model is crucial for designing responsive and well-structured web pages. Experiment with different selectors and box model properties to refine your CSS skills!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (1)

Collapse
 
beastful profile image
Ilya Afendin โ€ข

Keep it up!

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more