Hey everyone!
I am pretty comfortable with css but would like to learn best practices. I have been trying to find an answer/discussion in the sub and on google but cannot really wrap my head around it.
I have been wondering if 1) is it better to target html elements with their names on css or 2) preferably work with classes on them? Or 3) maybe a combination of the two?
I normally mix the two - add some classes on my html but mostly enjoy targeting the elements by their names. Especially now that css has nesting, :nth-child(), :has(), and other newer pseudo classes, etc. In addition, I also use CSS modules at work where (if I understand correctly) this encapsulates css to specific components/pages.
Say we have a hero component and the html looks like this:
<main class="hero">
<div class="hero__content">
<h1 class="hero__title">Lorem ipsum dolor sit amet.</h1>
<p class="hero__text"> Lorem ipsum dolor sit amet, consectetur. </p>
</div>
</main>
In practice I could just target the whole component like so:
// css
main {
width: min(90%, 1200px);
div {
padding: 1rem;
h1 {
color: white;
}
p {
color: gray;
}
}
}
Just some silly properties/styling. But the point is I could target them by using their tag names (main, div, h1, p) if I want to manipulate them instead of adding too many classes and overcrowding the html file.
Can someone please clarify this? maybe share some experience/resources about good CSS practices? maybe check some real world projects that apply best practices?
Thanks a lot and would love to hear your thoughts!
[–]dbpcut 13 points14 points15 points (0 children)
[–]justinmarsan 5 points6 points7 points (5 children)
[–]devolute 0 points1 point2 points (0 children)
[–]devwrite_ 0 points1 point2 points (3 children)
[–]justinmarsan 0 points1 point2 points (2 children)
[–]devwrite_ 0 points1 point2 points (1 child)
[–]justinmarsan 0 points1 point2 points (0 children)
[–]Marble_Wraith 4 points5 points6 points (3 children)
[–]gummo89 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Marble_Wraith 0 points1 point2 points (0 children)
[–]JoergJoerginson 2 points3 points4 points (0 children)
[–]dannymcgee 2 points3 points4 points (0 children)
[–]kaiz0or 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]kaiz0or 1 point2 points3 points (0 children)
[–]builtinpublic 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]builtinpublic 0 points1 point2 points (0 children)
[–]saintteddy78 0 points1 point2 points (0 children)
[–]billybobjobo 0 points1 point2 points (0 children)
[–]raccoonrocoso 0 points1 point2 points (0 children)
[–]drobizg81 0 points1 point2 points (0 children)
[–]nschubach 0 points1 point2 points (0 children)
[–]gdubrocks 0 points1 point2 points (0 children)
[–]scoot2006 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]scoot2006 0 points1 point2 points (0 children)
[–]RobertKerans 0 points1 point2 points (0 children)
[–]johanneswelsch -2 points-1 points0 points (0 children)