html - 如何使徽标的可点击区域与导航栏的徽标大小相同?
问题描述
我的导航栏中的徽标有问题。当我将鼠标悬停在我的徽标上时,它让我悬停的次数超出了应有的范围。更喜欢一些像素。但是当我降低分辨率时,就像手机一样,徽标最终占据了导航右侧汉堡包之前的所有空间。
* {
margin: 0;
box-sizing: border-box;
}
.wrapper {
margin: 0 auto;
width: 100%;
}
nav {
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px, rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
}
nav img {
width: 50%;
padding: 10px 0;
}
nav a {}
.fa-bars {
margin-right: 20px;
}
<!-- FontAwesome -->
<script src="https://kit.fontawesome.com/aaefe49bc9.js" crossorigin="anonymous"></script>
<!-- Body -->
<div class="wrapper">
<nav>
<a href="index.html"><img src="https://via.placeholder.com/100.png"></a>
<i class="fas fa-bars"></i>
</nav>
</div>
解决方案
因为锚点比图像大,原因有 3 个。(申请a { border: 1px solid red;
证明):
- CSS 行
nav img { width: 50%; }
让图像只包含锚点宽度的 50%。意味着锚的宽度是必要的两倍。 - 这条线
nav img { padding: 10px 0; }
还在锚点内增加了额外的空间。 - 图像是一个内联元素,例如内联减速空间。用于
img { display: block; }
删除它。
/* changes */
nav img {
display: block
}
/* apply this instead of the images padding */
nav a {
margin: 10px 0;
}
/* Original CSS */
* {
margin: 0;
box-sizing: border-box;
}
.wrapper {
margin: 0 auto;
width: 100%;
}
nav {
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px, rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
}
/* remove this part
nav img {
width: 50%;
padding: 10px 0;
}
*/
nav a {}
.fa-bars {
margin-right: 20px;
}
<!-- FontAwesome -->
<script src="https://kit.fontawesome.com/aaefe49bc9.js" crossorigin="anonymous"></script>
<!-- Body -->
<div class="wrapper">
<nav>
<a href="index.html"><img src="https://via.placeholder.com/100.png"></a>
<i class="fas fa-bars"></i>
</nav>
</div>
推荐阅读
- xml - 使用 XSLT 生成特定元素的 XPath
- angular - 有什么方法可以从指令中监听插值变化?
- python - 如何绘制由训练有素的 kNN 决定的决策边界?
- javascript - 如何从二维数组中获取唯一值,同时删除包含另一个数组中元素的重复项
- c# - 如何简化 lambda 表达式中的多个空检查
- asp.net-core - 在常规 MVC 视图表中,呈现多个 Blazor 组件会触发与服务器的断开连接
- python - selenium python中的send()函数不起作用
- reactjs - 在钩子中使用异步数据初始化 useReducer
- css - 如何在 Visual Studio 2019 中将左侧垂直边栏添加到 .Net Core Web 应用程序的默认模板
- java - 在 jar 中加载外部库