具有相同id的多个div应用css风格的工作方式与类相同? [英] Multiple divs with the same id applying css style works same like class?
本文介绍了具有相同id的多个div应用css风格的工作方式与类相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有多个具有相同ID更改ID的div,则对css的类和id选择感到困惑。类对结果没有任何影响。
I'm confused about the class and id selection with css if i have multiple divs with same ID changeing ID to Class does not have any effect on the result
style
<style>
#main-div
{
width:300px;
height:300px;
background-color:red;
margin:10px;
}
</style>
<div id="main-div"></div>
<div id="main-div"></div>
<div id="main-div"></div>
<div id="main-div"></div>
推荐答案
首先,使用多次相同的ID无效。不要这样做。
First of all, using multiple times the same id is invalid. Never do it.
然后,在css选择器中,您需要使用
Then, in css selectors, you need to use
这是一个例子。
<!-- in HTML -->
<div id="identifier1"></div>
<div class="class1"></div>
<div class="class1"></div>
/* In css */
#identifier1 { /* This targets the first div */ };
.class1 { /* This targets the second and third div */ };
这篇关于具有相同id的多个div应用css风格的工作方式与类相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文