gpt4 book ai didi

html - 将 HTML id 更改为类会扰乱 CSS 样式

转载 作者:太空宇宙 更新时间:2023-11-04 11:36:29 24 4
gpt4 key购买 nike

我想将我使用 id 标记的一些元素更改为一个类,以便在我的 CSS 中我可以使用 .sortsubmenu 而不是 #sortsongmenu#sortartistmenu

问题是,当我将其中一个从 id 更改为 class 时,它弄乱了格式。下图中,songsubmenu和artistsubmenu完全一样,只是songsubmenu用id标识,artistsubmenu用class标识。

enter image description here

#topbar {
background-color: #222;
}

#topbar_wrapper {
width: 100%;
margin: 0 auto;
text-align: left;
}

#mainmenu {
list-style-type: none;
padding: 0px;
margin: 0px;
position: relative;
min-width: 200px;
}

#mainmenu li {
display: inline-block;
width: 200px;
}

#mainmenu li:hover {
background-color: #333;
}

#mainmenu li a {
color: #CCC;
display: block;
padding: 15px;
text-decoration: none;
}

#mainmenu li:hover > ul {
display: block;
}

#sortmenu {
display: none;
position: absolute;
background-color: #333;
border: 5px solid #222;
border-top: 0;
margin-left: -5px;
}

#sortmenu > li {
display: block;
position: relative;
}

#sortmenu li a:hover {
color: #699;
}

#sortmenu li:hover ul {
display: inline-block;
}

#sortsongmenu, .sortsubmenu {
display: none;
position: absolute;
background-color: #333;
border: 5px solid #222;
border-left: 0px;
text-align: right;
top: 0;
left: 100%;
width: 100px;
}

#sortsongmenu li, .sortsubmenu li {
display: inline;
}

#sortsongmenu li a:hover, .sortsubmenu li a:hover {
color: #DB7093;
}
<div id="topbar">
<div id="topbar_wrapper">
<ul id="mainmenu">
<li><a href="#">Home</a>
</li>
<li>
<a href="#">Search</a>
</li>
<li>
<a href="#">Sort By &#9660</a>
<ul id="sortmenu">
<li><a href='#'>Song</a>
<ul id="sortsongmenu">
<li><a href='#'>A to Z</a>
</li>
<li>
<a href='#'>Z to A</a>
</li>
</ul>
</li>
<li>
<a href='#'>Artist</a>
<ul class="sortsubmenu">
<li><a href='#'>A to Z</a>
</li>
<li>
<a href='#'>Z to A</a>
</li>
</ul>
</li>
<li>
<a href='#'>Album</a>
</li>
<li>
<a href='#'>Genre</a>
</li>
<li>
<a href='#'>BPM</a>
</li>
<li>
<a href='#'>Release Date</a>
</li>
</ul>
</li>
<li>
<a href="#">Add Song</a>
</li>
<li>
<a href="#">Contact Us</a>
</li>
</ul>
</div>
</div>

最佳答案

#mainmenu li 规则妨碍了您。它不会被基于类的选择器覆盖,因为它被基于 id 的选择器覆盖。

仅保留直接后代的定位/大小,即更改:

#mainmenu li {

#mainmenu > li {

一切都很好。

#topbar {

background-color: #222;

}

#topbar_wrapper {

width: 100%;

margin: 0 auto;

text-align: left;

}

#mainmenu {

list-style-type: none;

padding: 0px;

margin: 0px;

position: relative;

min-width: 200px;

}

#mainmenu > li {

display: inline-block;

width: 200px;

}

#mainmenu li:hover {

background-color: #333;

}

#mainmenu li a {

color: #CCC;

display: block;

padding: 15px;

text-decoration: none;

}

#mainmenu li:hover > ul {

display: block;

}

#sortmenu {

display: none;

position: absolute;

background-color: #333;

border: 5px solid #222;

border-top: 0;

margin-left: -5px;

}

#sortmenu > li {

display: block;

position: relative;

}

#sortmenu li a:hover {

color: #699;

}

#sortmenu li:hover ul {

display: inline-block;

}

#sortsongmenu,

.sortsubmenu {

display: none;

position: absolute;

background-color: #333;

border: 5px solid #222;

border-left: 0px;

text-align: right;

top: 0;

left: 100%;

width: 100px;

}

#sortsongmenu li,

.sortsubmenu li {

display: inline;

}

#sortsongmenu li a:hover,

.sortsubmenu li a:hover {

color: #DB7093;

}
<div id="topbar">
<div id="topbar_wrapper">
<ul id="mainmenu">
<li><a href="#">Home</a>
</li>
<li>
<a href="#">Search</a>
</li>
<li>
<a href="#">Sort By &#9660</a>
<ul id="sortmenu">
<li><a href='#'>Song</a>
<ul id="sortsongmenu">
<li><a href='#'>A to Z</a>
</li>
<li>
<a href='#'>Z to A</a>
</li>
</ul>
</li>
<li>
<a href='#'>Artist</a>
<ul class="sortsubmenu">
<li><a href='#'>A to Z</a>
</li>
<li>
<a href='#'>Z to A</a>
</li>
</ul>
</li>
<li>
<a href='#'>Album</a>
</li>
<li>
<a href='#'>Genre</a>
</li>
<li>
<a href='#'>BPM</a>
</li>
<li>
<a href='#'>Release Date</a>
</li>
</ul>
</li>
<li>
<a href="#">Add Song</a>
</li>
<li>
<a href="#">Contact Us</a>
</li>
</ul>
</div>
</div>

关于html - 将 HTML id 更改为类会扰乱 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31701973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com