有没有办法在不知道ID的情况下定位或设置divs
?
<!DOCTYPE html>
<html>
<head>
<style>
#main div {
background-color:coral;
width: 40%;
}
#main div div {
background-color: lightblue;
width: 60%;
}
</style>
</head>
<body>
<div id="main">
<div >Red DIV</div>
<div >Blue DIV</div>
</div>
</body>
</html>
答案 0 :(得分:0)
定位元素的最佳方法是使用ID或类,但您可以使用:nth-child
#main > div {
background-color:coral;
width: 40%;
float: left;
}
#main > div:nth-child(2) {
background-color: lightblue;
width: 60%;
float: right;
}
<div id="main">
<div >Red DIV</div>
<div >Blue DIV</div>
</div>
答案 1 :(得分:0)
#main div:nth-of-type(1) { background-color: red; }
#main div:nth-of-type(2) { background-color: blue; }