Design comparison
Solution retrospective
This code could certainly be cleaner. Is it best practice to be extremely (what feels like over the top) specific when writing in CSS ALL the class selectors? For example
.container .card .main-paragraph { color: var(--stat-white); margin: 20px 50px 20px 50px; line-height: 1.7; align-items: center; }
within in my code there is only one .main-paragraph so do i really need to precede it with .container .card?
Community feedback
- @b-a-merrittPosted about 3 years ago
Hey Rachel!
I'll answer your question broadly, because I think there are circumstances when it would be necessary to use that many selectors. I think a deeper understanding of how CSS is parsed would benefit you.
Browsers construct a CSS Object Model (like the DOM) starting with the selectors on the right and then working left. Single selectors will be nearest the root while more specific selectors will be further down the branches. For example, on line 77 of your stylesheet, the selector
.container .card h1 span
is parsed like this:- The browser finds all
span
elements - Of those, the browser creates a branch that also have
h1
selectors - Of those, the browser creates another branch for those that have the
card
class - Finally, another branch is made for those that have the
container
class
I'm sure you've guessed it by now -- the best practice is to limit how many times the browser has to do this. When using selectors, use as many as are needed to grab just that one (or those ones). In your project, you have only one
span
on the entire page. You could have just saidspan { --var(S-violet) }
If chaining selectors, consider having the more specific on the right and the least specific on the left. That way, the browser doesn't have to gather up all the
<p>
elements before finding the one#identifier
.Note: Don't look at my work for an example on how best to do this! I'm the epitome of hypocrisy.
Additionally, you used a
div
with the classcontainer
right belowmain
. Perhaps consider just usingmain
? Semantic HTML elements likemain
orsection
can be selected just as easily, if not easier. You could delete thatdiv
and change all CSS selectors tomain
instead.Anyway, the result looks good! Good job
Marked as helpful2@Mozzarella-chzPosted about 3 years ago@b-a-merritt
Thanks for the reply. So from a little bit of digging on forums I found that if a selector looks like X Y Z A B.error{} then the reason that is not good is because it is 'heavy' and will slow down the browser as it loads the page. And it's just time consuming/unnecessary to write out that much code. And so the rule of thumb for descendant selectors is to have just 2 selectors identified if possible? The ancestor and descendent?
You really sent me on a hunt with this one. I really feel like I learned a lot and expanded into new learning!
And your last point about my container and main divs is the exact thing i pointed out to someone on their code as being redundant. Ha! The hypocrisy carries on.
Thanks again for your feedback!!!
0 - The browser finds all
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord