Home >Web Front-end >HTML Tutorial >Using multiple CSS classes for one element in HTML

Using multiple CSS classes for one element in HTML

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2023-09-09 08:45:03892browse

Using multiple CSS classes for one element in HTML

To create CSS faster, we can assign multiple classes to a single HTML element and style each class separately. This approach allows us to manage redundancy in style application. We can apply common styles to many classes and category-specific styles to specific categories.

grammar

<tag_name class="class_1 class_2">

Example

In the following example, we use the style of class "Varma" to apply to two paragraphs, and the second paragraph has the style of secondclass varma1 applied.

<!DOCTYPE html>
<html>
   <style>
      .varma {
         font-size: larger;
         margin-bottom: 35px;
         background-color: lightgreen;
      }
      .varma1 {
         color: red;
      }
   </style>
<body>
   <p class="varma">
      Hello Everyone.
   </p>
   <p class="varma varma1">
      Welcome to Turorialspoint.
   </p>
</body>
</html>

The results will be generated after executing the above script. It is a combination of the text "Welcome to tutorialspoint" with two CSS styles and the text "Hello everyone" with a single CSS style.

Example: Using javascript

In the following example we are adding two style to an single text by declaring the .bg-blue style and .text-white style.

<!DOCTYPE html>
<html>
   <style>
      .bg-blue {
         background-color: lightgreen;
      }
      .text-white {
         color: red;
      }
   </style>
<body>
   <div id="varma">Welcome To Tutorialspoint</div>
   <script>
      const box = document.getElementById('varma');
      box.classList.add('bg-blue', 'text-white');
   </script>
</body>
</html>

When executed, the script generates a text output with two CSS styles applied: "Welcome to Tutorial Point".

The above is the detailed content of Using multiple CSS classes for one element in HTML. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete