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:
- CSS Selectors
- Box Model
- 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; }
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
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!

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use
