[英]CSS position of divs without id
有沒有一種方法可以在不知道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>
定位元素的最佳方法是使用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>
#main div:nth-of-type(1) { background-color: red; } #main div:nth-of-type(2) { background-color: blue; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.