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:
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; }
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; }
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 }
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; }
E. Pseudo-classes: These select elements for styling based on their state.
a:hover{ color: blue; transition: ease-in 0.5s; }
There are other useful CSS Selectors but these would do for now.
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
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; }
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(Youre just Missing These Basics). For more information, please follow other related articles on the PHP Chinese website!