html - 他们是如何制作 a :hover full background color

标签 html css

<分区>


关闭 3 年前

我正在使用 a:hover 创建布局侧边栏。我如何将背景颜色设置为充满div?为什么这段代码不起作用?

<style>
#header{
background-color: cornflowerblue;
    height: 10%;
    font-size: 1.5em;
    text-align: center;
    padding-top: 20px;
}

#sidebar{
background-color: deepskyblue;
    height: 5%;
    width: 100%;
    font-size: 12px;
    padding-top: 2px;
}

#content{
background-color: dodgerblue;
    width: 100%;
    height: 400px;
    font-size: 1.5em;
    text-align: center;
    padding-top: 20px;}

#footer{background-color: darkblue;
    clear: both;
    font-size: 1.5em;
    text-align: center;
}

a {text-decoration: none;
    color: white;}
a:active {font-size: 10px;}




li{display: inline;
    margin-right: 10px;}
</style>

<div id="header">
    HEADER
</div>
<div id="sidebar">
<ul>
    <a href=""> <li>Home</li></a>
    <a href=""><li>Annoucement</li></a>
    <a href=""><li>ReportList</li></a>
    <a href=""><li>Settings</li></a>
</ul>
</div>
<div id="content">
    CONTET
</div>
<div id="footer">
    FOOTER
</div>

这个脚本有什么问题吗,当我们将鼠标指向列表(悬停)时,它会显示背景颜色穿过侧边栏div

enter image description here

最佳答案

ul 有默认边距。将 margin 更改为 0 并提供 height

ul {
  margin:0;
  height :40px;
  display :flex;
}

解决方法:

#header{
    background-color: cornflowerblue;
    height: 10%;
    font-size: 1.5em;
    text-align: center;
    padding-top: 20px;
}

#sidebar{
    background-color: deepskyblue;
    height: 5%;
    width: 100%;
    font-size: 12px;
}

#content{
    background-color: dodgerblue;
    width: 100%;
    height: 400px;
    font-size: 1.5em;
    text-align: center;
    padding-top: 20px;}

#footer{
    background-color: darkblue;
    clear: both;
    font-size: 1.5em;
    text-align: center;
}

a {
    text-decoration: none;
    color: white;
    height:100%;
    display: flex;
    align-items: center;
}

a:active {font-size: 10px;}

li{
  display: inline;    
  margin-right: 10px;
}

a:hover {
  color: black;
  background: dimgray;
}

ul {
  margin:0;
  height :40px;
  display :flex;
}
<div id="header">
    HEADER
</div>
<div id="sidebar">
<ul>
    <a href=""> <li>Home</li></a>
    <a href=""><li>Annoucement</li></a>
    <a href=""><li>ReportList</li></a>
    <a href=""><li>Settings</li></a>
</ul>
</div>
<div id="content">
    CONTET
</div>
<div id="footer">
    FOOTER
</div>

关于html - 他们是如何制作 a :hover full background color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57679807/

上一篇:html - 无法让图像响应?

下一篇:html - 如何修复 Angular Materials 不起作用的错误

相关文章:

html - 如何将 wordpress 博客页面拆分为 2 列响应式布局?

javascript - 如何使用 Jquery 修复游戏中自动生成的食物

css - 正确使用section article和heading

javascript - 如何防止固定 div 后面的内容滚动?

javascript - 查询选择器具有父id和子类混合的元素

html - 面板标题 Bootstrap 中的定位下拉列表

php - 有没有办法根据其格式在表格行中动态显示图像或视频?

javascript - 工具提示动态更新在 Firefox 下不起作用的内容

javascript - 使用 javascript 在每个文件中使用 <TITLE></TITLE> 重命名多个 HTML 文件

javascript - 单击jquery加载打字动画