Home > Web Front-end > CSS Tutorial > CSS is Not Hard(You&#re just Missing These Basics)

CSS is Not Hard(You&#re just Missing These Basics)

Linda Hamilton
Release: 2024-12-01 09:45:14
Original
195 people have browsed it

When I first started out with web development, I thought my biggest challenge would be JavaScript (not saying JS isn't still a handful) but then came so many bad reps and horror stories about CSS.

Despite all these, I took a deep dive into Frontend development, and boy, did CSS do a number on me. Nothing ever worked out well, and do not even get me started on centering a div. It was a nightmare.

But, then, here’s what I realized, CSS wasn’t the Problem, the problem was that I didn’t understand the basics, and once I got the hang of them, styling kind of became easier and surprisingly fun.

In this article, I would walk you through two fundamental concepts that will set the stage for mastering CSS:

  1. CSS Selectors
  2. Box Model
  3. Positioning and Layout
  • CSS Selectors: CSS Selectors are tools that let you target certain HTML elements that need styling.

Let’s explore some of the commonly used selectors:

A. Universal Selectors: This is used to target every element on a page. It is represented by the asterisk symbol(*) and is usually used for global resets or to apply base styles.

*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
Copy after login
Copy after login

B. Class Selectors: This is used to select HTML elements with a specific class attribute value. To select elements with a specific class, write a period(.) followed by the value of the class attribute.

<div>





<pre class="brush:php;toolbar:false">.box{
  background-color: teal;
  padding: 30px;
  border-radius: 10px;
  border: 2px solid;
  text-align: center;
}

Copy after login

C. Type Selectors: This is used to target all elements of a specific type. It is also called the element selector.

p{
text-align: center;
font-size: 1.2rem
}

Copy after login

Use case: This applies a uniform style to all

tags.

D. Attribute Selectors: This selector targets HTML elements based on their attributes and values.

input[type = "text"] {
background-color: cyan;
color: gray;
}

Copy after login

E. Pseudo-classes: These select elements for styling based on their state.

a:hover{
color: blue;
transition: ease-in 0.5s;
}

Copy after login

There are other useful CSS Selectors but these would do for now.

  • The Box Model

The Box Model is essentially a box that wraps around HTML elements, basically every element in HTML is just one big (or small) rectangular box. What the box model does is to help control how much space each box occupies and how they interact with other elements.

Each box has four main parts:

1. Content: This is what is inside the box – texts, images and other elements.
2. Padding: This is the space between the content and the edge of the box.
3. Border: This is the edge of the box.
4. Margin: This is the space outside the border which separates this box from other boxes.

Here’s an illustration explaining the Box Model

CSS is Not Hard(You

Note: the dashed line is the border of the box.

The Box Model is essential for anything you want to do in CSS, from positioning elements to managing spacing. For instance, i drew the Box Model Illustration using just HTML and CSS to show how the different layers work together.

Here's the code that helped me bring the illustration to life:

*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
Copy after login
Copy after login

WRAPPING UP(it's about time)
CSS may seem like a lot at first, but once you grab the basics you are ready to conquer the world(your world, at least).
Here's the key takeaway from this article: CSS is not hard, you just need to focus more on the fundamentals.
Keep learning and most importantly keep practicing, and soon, you'll be styling web pages like a pro.

What's Next?
Now that you have learned the basics of CSS Selectors and the Box Model, you are one step closer to mastering CSS, But there's more to explore.
In the Next article, we'll dive into CSS positioning and Layout Techniques like Flexbox and Grid.

Happy coding!!!!

The above is the detailed content of CSS is Not Hard(You&#re just Missing These Basics). For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template