1

I want to assign one class, like "centered" to some, all, or each of my text elements, like <h1 class="centered">, <h2 class="centered">, <h3 class="centered">, <p class="centered">.

I need to target only specific elements because I may want some different rules for block elements like <div class="centered">.

If you think this question is easy to get an answer for, try Googling it. No matter how I phrase the question, Google thinks I'm asking "how to assign multiple classes to one element". I know how to give an element multiple classes, so, no, that's not what I want.

What I've tried:

h1, h2, h3, p .centered{
    text-align:center;
}

That can't be right. That's just saying the centered class shares the same css rules with h1, h2, and h3.

Right now I'm reduced to this method, which seems inelegant, and probably amateur:

h1.centered{
    text-align:center;
}
h2.centered{
    text-align:center;
}
h3.centered{
    text-align:center;
}

...etc.

3
  • 2
    If you want to have different rules for a certain set of elements, it would probably be easier to create a separate class for that. Or create one rule with the general centered CSS, and then create separate rules modifying the general CSS for just the elements you want to be different. By the way, all the elements you mentioned are block elements. Commented Mar 17, 2016 at 20:21
  • @BurningLights I didn't mention it, but I did end up creating a separate class for my div. The "h" and "p" classes are block elements? I thought they were inline?
    – TARKUS
    Commented Mar 17, 2016 at 20:30
  • See the complete list of block elements at: developer.mozilla.org/en-US/docs/Web/HTML/…. "h" and "p" elements are on there. :) Commented Mar 17, 2016 at 21:00

2 Answers 2

6

You just define your class in css and use it on elements you want in html

.centered {
  text-align:center;
}
<h1 class="centered">Lorem ipsum.</h1>
<h2 class="centered">Lorem ipsum.</h2>
<h3 class="centered">Lorem ipsum.</h3>
<p  class="centered">Lorem ipsum.</p>

4
  • I already know that. I may have to re-think my question. I think I over-complicated the problem, thanks. As @BurningLights pointed out, I should just create a separate class for a div, and just give it another name.
    – TARKUS
    Commented Mar 17, 2016 at 20:32
  • 1
    You could use :not() jsfiddle.net/Lg0wyt9u/203 or you can also do this jsfiddle.net/Lg0wyt9u/204 or this jsfiddle.net/Lg0wyt9u/206 Commented Mar 17, 2016 at 20:36
  • Hey, that's interesting. Learning something new all the time, thanks.
    – TARKUS
    Commented Mar 17, 2016 at 20:40
  • Giving you best answer, not just for your answer, but also because of your very instructive jsfiddle examples.
    – TARKUS
    Commented Mar 17, 2016 at 20:43
6

Another small piece of advice is you can combine all of your "centered" css rules into one line if you want. So instead of

h1.centered{
    text-align:center;
}
h2.centered{
    text-align:center;
}
h3.centered{
    text-align:center;
}

Do something like this:

h1.centered, h2.centered, h3.centered{
    text-align: center;
}
1
  • 1
    Cheers bud, I was having a brain fart moment and this answer really helped me out. Upvoted Commented Nov 24, 2020 at 20:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.