skip to Main Content

So if I have the code:

<h1>Here is some filler code</h1>
<h1>Here is some more filler code</h1>

Is there some way I can use CSS to make every element have a class?

For example, if I have a class called "text":

.text{
  color: white;
}

can I use a CSS declaration for every h1 to be in the "text" class?

I couldnt find any declarations using CSS like "class:", so I was just wondering.

2

Answers


  1. If you want to add a class you will have to manually add it

    <h1 class="text">Here is some filler code</h1>
    <h1 class="text">Here is some more filler code</h1>
    

    But you can target the h1 with css without using a class. Just by using the tag name

    so in your css

    h1 {
      color: white;
    }
    

    Read more about CSS at https://developer.mozilla.org/en-US/docs/Web/CSS


    Finally, if you need to add a class to the h1 but cannot modify the actual html, you can add it with javascript

    document
      .querySelectorAll('h1')
      .forEach(function(element) {
        element.classList.add('text')
      });
    
    Login or Signup to reply.
  2. If you really need to select all h1’s you can use, element selector itself..

    Eg:

    h1 {
      color: white;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search