添加多个类 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31412923/
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 11:46:31  来源:igfitidea点击:

Adding more than one class html

htmlclass

提问by Stefan

Is it possible to add more than one class in html? Here is what i've tried:

是否可以在 html 中添加多个类?这是我尝试过的:

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

Thanks! :)

谢谢!:)

回答by connexo

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

是的,这是可能的,但您只能为class每个 HTML 元素声明一次该属性。只需用空格分隔您要应用的类。

<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 .class2will not be applied to your link.

如果多次声明 class 属性,则第一个以外的所有定义都将被忽略,因此在您的代码.class2中将不会应用于您的链接。

回答by emcas88

Yes of course you can! But you do it all inside a single class attribute:

是的,你当然可以!但是您可以在单个类属性中完成所有操作:

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

回答by coderfin

You can read about the global class attribute here.

您可以在此处阅读有关全局类属性的信息

The class global attribute is a space-separated list of the classes of the element.

class 全局属性是以空格分隔的元素类列表。

Use a single class attribute and separate additional classes with a space.

使用单个类属性并用空格分隔其他类。

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

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

回答by HarrisJT

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