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

Using multiple CSS classes for one element in HTML

WBOY
Release: 2023-09-09 08:45:03
forward
851 people have browsed it

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">
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template