Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple class attributes in HTML

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(); ?>>
like image 642
RichardTheKiwi Avatar asked Mar 01 '12 07:03

RichardTheKiwi


People also ask

How do you give multiple classes in HTML?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Can an attribute have multiple classes?

While an element can only have a single ID attribute, you can give an element several classes and, in some cases, doing so will make your page easier to style and much more flexible. If you need to assign several classes to an element, add the additional classes and simply separate them with a space in your attribute.

How do you add multiple attributes in HTML?

Definition and Usage When present, it specifies that the user is allowed to enter more than one value in the <input> element. Note: The multiple attribute works with the following input types: email, and file. Tip: For <input type="file"> : To select multiple files, hold down the CTRL or SHIFT key while selecting.

Can an HTML element have multiple attributes?

The multiple attribute in HTML allows user to enter more than one value. It is a Boolean attribute and can be used on <input> as well as <select> element, To allow multiple file uploads in HTML forms, use the multiple attribute. The multiple attribute works with email and file input types.


2 Answers

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.

like image 98
BoltClock Avatar answered Oct 22 '22 05:10

BoltClock


Then the document will be invalid and the browser will attempt to perform error recovery.

From the HTML 5 specification:

If the attribute's name is already in attribute list, then return to the step labeled attributes.

So, if an HTML 5 parser is being used, the first attribute should apply.

like image 34
Quentin Avatar answered Oct 22 '22 06:10

Quentin