Html 一个 div 可以有多个类吗(Twitter Bootstrap)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14884523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:37:50  来源:igfitidea点击:

Can a div have multiple classes (Twitter Bootstrap)

htmlcsstwitter-bootstrap

提问by FluxEngine

Can a div tag have two classes?

一个 div 标签可以有两个类吗?

I am using twitter bootstrap, and there are two predefined classes that I would like to use.

我正在使用 twitter bootstrap,并且有两个我想使用的预定义类。

One is an activeclass that I would like to use on a dropdown-togglewithin a nav bar.

一个是active我想dropdown-toggle在导航栏中使用的类。

What is the best way of approaching this in the html, with out overriding the css.

在不覆盖 css 的情况下,在 html 中处理此问题的最佳方法是什么。

回答by DarkAjax

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

当然,一个 div 可以拥有任意数量的类(这通常与引导程序和 HTML 相关):

<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 orcenter, but it has to be only oneof 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 specificitywill be the one applied (or if they have the same then it'll be the one that's defined last on the CSS). So keep in mind you should avoid doing stuff like this:

另外:请记住,一些引导程序类应该用于相同的东西,但在不同的情况下(例如对齐类,您可能希望某些东西向左、向右居中对齐,但它必须只是其中之一)并且您不应该一起使用它们,否则你会得到一个意想不到的结果,基本上会发生的是具有最高特异性的类将被应用(或者如果它们相同,那么它将是最后定义的那个在 CSS 上)。所以请记住,你应该避免做这样的事情:

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

回答by OliverP

Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well. All you have to do is separate each class with a space.

当然,div 可以有多个类,并且对于一些 Bootstrap 组件,您通常需要有多个类才能让它们按照您的意愿运行。当然,在引导程序之外也可以应用多个类。您所要做的就是用空格分隔每个类。

Example below:

下面的例子:

<label class="checkbox inline">
   <input type="checkbox" id="inlineCheckbox1" value="option1"> 1
</label>

回答by Eric Goncalves

separate the classes with a space.

用空格分隔类。

<button class="btn btn-success dropdown-toggle active" data-toggle="dropdown">Success <span class="caret"></span></button>

demo: http://jsfiddle.net/wNfcg/

演示:http: //jsfiddle.net/wNfcg/

回答by Ekin

You can add as many classes to an element, but you can add only one id per element.

您可以向一个元素添加任意多个类,但每个元素只能添加一个 id。

<div id="unique" class="class1 class2 class3 class4"></div>

回答by sellmeadog

I'm not sure if you're asking specifically about bootstrap, i.e., whether or not those two classes can be used together or if you're just asking in general?

我不确定您是否专门询问引导程序,即这两个类是否可以一起使用,或者您只是一般询问?

You can apply as many classes to an element as you want, just separate class names with a space

您可以根据需要为元素应用任意数量的类,只需用空格分隔类名

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

回答by Ravi Varma

Yes, div can take as many classes as you need. Use space to separate one from another.

是的,div 可以根据需要选择多个类。使用空间将一个与另一个分开。

 <div class="active dropdown-toggle custom-class">Example of multiple classses</div>

回答by Rezaul

A div can can hold more than one classes either using bootstrap or not

一个 div 可以包含多个类,无论是否使用引导程序

<div class="active dropdown-toggle my-class">Multiple Classes</div>
For applying multiple classes just separate the classes by space.

<div class="active dropdown-toggle my-class">Multiple Classes</div>
对于应用多个类,只需按空格分隔类。

Take a look at this links you will find many examples
http://getbootstrap.com/css/
http://css-tricks.com/un-bloat-css-by-using-multiple-classes/

看看这个链接,你会发现很多例子
http://getbootstrap.com/css/
http://css-tricks.com/un-bloat-css-by-using-multiple-classes/

回答by Huzaifa Saifuddin

You can have multiple css classselectors:

您可以有多个 cssclass选择器:

For e.g.:

例如:

<div class="col-md-4 a-class-name">
</div>

but only a single id:

但只有一个id

<div id = "btn1 btn">  <!-- this is wrong -->
</div>

回答by for

space is used to make multiple classes:

空间用于创建多个类:

<div class="One Two Three">

</div>