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)

Dec 01, 2024 am 09:45 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn&#039;t have to be the case. Let’s look at how PHP projects can enforce a basic

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

See all articles