WikiGalaxy

Personalize

HTML Classes and CSS

Defining Classes in HTML

Understanding Classes:

HTML classes are used to define a group of elements that can be styled in the same way using CSS. This allows for efficient styling and maintenance of web pages.


<div class="container">
  <p class="text-primary">This is a paragraph.</p>
</div>
    

Applying Styles with CSS:

Once a class is defined in HTML, CSS can be used to apply styles to all elements with that class name, ensuring a consistent look and feel across the website.


.text-primary {
  color: blue;
  font-size: 16px;
}
    

Multiple Classes in HTML

Combining Multiple Classes:

HTML elements can have multiple classes, allowing for more flexible and complex styling. Each class is separated by a space in the class attribute.


<div class="container text-primary">
  <p class="text-primary highlight">Highlighted text.</p>
</div>
    

CSS for Multiple Classes:

CSS can target elements with multiple classes to apply specific styles, enhancing the design and functionality of the webpage.


.highlight {
  background-color: yellow;
}
    

CSS Specificity and Classes

Understanding Specificity:

CSS specificity determines which styles are applied to an element when multiple rules could apply. Classes have a lower specificity compared to IDs but higher than element selectors.


#unique-element {
  color: red;
}
.text-primary {
  color: blue;
}
p {
  color: black;
}
    

Applying Specificity:

In the example above, an element with the ID "unique-element" will have a red color due to the higher specificity of ID selectors over class and element selectors.

Using Classes for Responsive Design

Responsive Web Design:

Classes can be used in conjunction with media queries to create responsive designs that adapt to different screen sizes, ensuring a good user experience on all devices.


@media (max-width: 600px) {
  .text-primary {
    font-size: 14px;
  }
}
    

Implementing Media Queries:

In the above example, the font size of elements with the class "text-primary" is adjusted for screens smaller than 600px, enhancing readability on mobile devices.

Utility-First CSS Frameworks

Introduction to Utility-First:

Utility-first CSS frameworks, like Tailwind CSS, use classes for styling directly in the HTML, promoting rapid development and consistent design patterns.


<div class="bg-gray-100 p-4">
  <p class="text-blue-500 font-bold">Hello, World!</p>
</div>
    

Benefits of Utility-First:

This approach allows developers to build custom designs without writing custom CSS, reducing the time spent on styling and increasing productivity.

BEM Methodology

Understanding BEM:

BEM (Block, Element, Modifier) is a naming convention for classes in HTML and CSS, aiming to create reusable components and code sharing in front-end development.


<div class="block">
  <div class="block__element block__element--modifier"></div>
</div>
    

Advantages of BEM:

BEM helps in maintaining a clear structure and avoiding conflicts in CSS, making it easier to understand and scale large projects.

Scoped CSS with Classes

What is Scoped CSS?

Scoped CSS refers to CSS rules that apply only to a specific section of a document, often achieved through the use of classes or component-based frameworks.


<style scoped>
  .scoped-class {
    color: teal;
  }
</style>
<div class="scoped-class">This text is teal.</div>
    

Benefits of Scoped CSS:

This technique prevents styles from leaking into other parts of the document, ensuring modular and maintainable code.

CSS Grid and Flexbox with Classes

Utilizing Grid and Flexbox:

CSS Grid and Flexbox are powerful layout models that use classes to create complex and responsive layouts with minimal code.


.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
.flex-container {
  display: flex;
  justify-content: space-between;
}
    

Enhancing Layouts:

These models allow for efficient alignment, distribution, and resizing of elements, making them essential tools for modern web design.

Syntax and Basic Example

  • Assign a class to an HTML element using the class attribute.
  • Use the class in CSS or JavaScript for styling or manipulation.
  • 
    <!DOCTYPE html>
    <html>
    <head>
      <title>Classes Example</title>
      <style>
        .highlight {
          color: white;
          background-color: blue;
          padding: 5px;
        }
      </style>
    </head>
    <body>
      <p>This is a normal paragraph.</p>
      <p class="highlight">This paragraph has a class applied.</p>
    </body>
    </html>
    

    Output:

  • The first paragraph remains unchanged.
  • The second paragraph appears with white text on a blue background and has padding applied.
  • Combining Multiple Classes

    An element can have multiple classes by listing them, separated by spaces, in the class attribute.

    
    <!DOCTYPE html>
    <html>
    <head>
      <title>Multiple Classes</title>
      <style>
        .bold {
          font-weight: bold;
        }
        .italic {
          font-style: italic;
        }
      </style>
    </head>
    <body>
      <p class="bold italic">This text is bold and italicized.</p>
    </body>
    </html>
    

    Output:

    • The text appears bold and italicized.

    Classes in JavaScript

    Classes are often used with JavaScript to manipulate elements dynamically.

    
    <!DOCTYPE html>
    <html>
    <head>
      <title>JavaScript and Classes</title>
      <style>
        .hidden {
          display: none;
        }
      </style>
      <script>
        function toggleVisibility() {
          const element = document.getElementById('myElement');
          element.classList.toggle('hidden');
        }
      </script>
    </head>
    <body>
      <button onclick="toggleVisibility()">Toggle Visibility</button>
      <p id="myElement">This paragraph can be hidden or shown.</p>
    </body>
    </html>
    

    Output:

    • Clicking the button toggles the visibility of the paragraph.

    Aspect

    Classes

    ID

    Usage

    Applied to multiple elements.

    Unique to a single element.

    Syntax

    .classname in CSS.

    #idname in CSS.

    JavaScript

    Accessed via getElementsByClassName.

    Accessed via getElementById.

    HTML classes provide a flexible way to style and manipulate groups of elements consistently. They are integral to CSS and JavaScript, enabling dynamic and reusable designs. Understanding and utilizing classes effectively enhances the modularity and maintainability of web projects.

    logo of wikigalaxy

    Newsletter

    Subscribe to our newsletter for weekly updates and promotions.

    Privacy Policy

     • 

    Terms of Service

    Copyright © WikiGalaxy 2025