Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7581679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:15:13+00:00 2026-05-30T18:15:13+00:00

What happens when an element has multiple class attributes? <div id=test class=one two three

  • 0

What happens when an element has multiple class attributes?

<div id="test" class="one two three" class="four">

I’m trying to add a class to the output of post_class(); in a WordPress plugin, but the function itself is creating the entire part class="one two three"

Is it equivalent to class="one two three four"?
Or does the first or second win?
Or is it undefined behaviour, in which case what do the major browsers do?

If you know the correct way of adding a class to this snippet (WordPress plugin), then that would also be appreciated!

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:15:15+00:00Added an answer on May 30, 2026 at 6:15 pm

    What happens when an element has multiple class attributes?

    When an attribute is declared multiple times for a single element (which is invalid HTML, by the way), behavior-wise the first value will override all subsequent values for the same attribute. So in this case, your element will only have the classes one two three.

    This behavior is explained in the HTML5 spec, 8.2.4.35 Attribute name state, “… if there is already an attribute on the [element] with the exact same name, then this is a parse error and the new attribute must be removed…”

    If you know the correct way of adding a class to this snippet (WordPress plugin), then that would also be appreciated!

    Typically, if you need to add custom classes dynamically to your WordPress posts, you hook onto the post_class filter and manipulate the $classes array as necessary. Here’s what it roughly looks like in my themes:

    function nv_post_class( $classes ) {
        // Most recent post on the front page
        global $count;
        if ( is_home() && 1 == $count )
            $classes[] = 'latest-post';
    
        return $classes;
    }
    
    add_filter( 'post_class', 'nv_post_class' );
    

    If you only need to add one or more static classes, pass them as a space-delimited string directly to post_class():

    <div id="post-<?php the_ID(); ?>" <?php post_class( 'myclass1 myclass2' ); ?>>
    

    More on this in the WordPress Codex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a table row with ajax/jquery that has a form element
what happens if an user trying to read HttpContext.Current.Cache[key] while the other one trying
My xml document has a element that can contains multiple child elements. In my
I'm trying to scrape a span element that has mixed content <span id=span-id> <!--starts
I've always been told that adding an element to an array happens like this:
What happens in the memory when a class instantiates the following object? public class
What happens on a Windows box once you add more drives than can fit
My method fades a div box out and has a callback that calls a
I have an element in my document that has a background color and image
I am receiving this XML error: Only one top level element is allowed in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.