How to Assign Multiple Classes to an HTML Container

Adding more than one class html

Yes, it is possible, but you can only declare the class attribute once per HTML element. Just separate the classes you want to apply by a space.

<a href="#" class="class1 class2">My Text</a>

If you declare the class attribute more than once, all definitions beyond the first will be ignored, so in your code .class2 will not be applied to your link.

How to apply two CSS classes to a single element

1) Use multiple classes inside the class attribute, separated by whitespace (ref):

<a class="c1 c2">aa</a>

2) To target elements that contain all of the specified classes, use this CSS selector (no space) (ref):

.c1.c2 {
}

How to add multiple classes for ul element

In HTML, classes are separated by space. So,

<ul class="sub-menu insert">

Can a div have multiple classes (Twitter Bootstrap)

Sure, a div can have as many classes as you want (this is both regarding to bootstrap and HTML in general):

<div class="active dropdown-toggle"></div>

Just separate the classes by space.

Also: Keep in mind some bootstrap classes are supposed to be used for the same stuff but in different cases (for example alignment classes, you might want something aligned left, right or center, but it has to be only one of them) and you shouldn't use them together, or you'd get an unexpected result, basically what will happen is that the class with the highest specificity will be the one applied (or if they have the same then it'll be the one that's defined last on the CSS). So you better avoid doing stuff like this:

<p class="text-center text-left">Some text</p>

How to add multiple classes in Material UI using the classes props?

you can use string interpolation:

<div className={`${this.props.classes.container} ${this.props.classes.spacious}`}>

How to add multiple classes to a ReactJS Component?

I use classnames when there is a fair amount of logic required for deciding the classes to (not) use. An overly simple example:

...
var liClasses = classNames({
'main-class': true,
'activeClass': self.state.focused === index
});

return (<li className={liClasses}>{data.name}</li>);
...

That said, if you don't want to include a dependency then there are better answers below.



Related Topics



Leave a reply



Submit