html - 应用于父元素而不是所选元素的效果

标签 html css

如您所见,如果将鼠标悬停在 HOME 链接上,边框效果将覆盖整个 .left div 而不仅仅是主页链接。我知道我忽略了一些简单的事情。

ul {
  z-index: 101;
  list-style: none;
  position: relative;
  top: 60px;
  margin: 0;
  padding: 0;
  opacity: 1;
}
li {
  display: inline;
}
.left {
  display: inline-block;
  position: relative;
  left: 50px;
}
.left li {
  padding: 0 1.8em;
}
.right {
  display: block;
  position: relative;
  top: -27px;
  left: 840px;
}
.right li {
  padding: 0 1.8em;
}
li {
  z-index: 101;
  text-decoration: none;
  font-family: 'Raleway';
  font-weight: 500;
  color: black;
  font-size: 1.4em;
  line-height: 30px;
}
a {
  text-decoration: none;
  color: black;
  padding: 15px;
}
a::before,
a::after {
  content: '';
  position: absolute;
  transition: transform .5s ease;
}
.hoverbox::before,
.hoverbox::after {
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border-style: solid;
  border-color: #f7941e;
}
.hoverbox::before {
  border-width: 2px 0 2px 0;
  transform: scaleX(0);
}
.hoverbox::after {
  border-width: 0 2px 0 2px;
  transform: scaleY(0);
}
.hoverbox:hover::before,
.hoverbox:hover::after {
  transform: scale(1, 1);
}
<ul id="navbar">
  <div class="left">
    <li><a href="index.html" id="home" class="hoverbox">home</a>
    </li>
    <li><a href="about.html" id="about" class="">about</a>
    </li>
    <li><a href="services.html" id="services" class="">services</a>
    </li>
  </div>
  <div class="right">
    <li><a href="work.html" id="work" class="">work</a>
    </li>
    <li><a href="process.html" id="process" class="">process</a>
    </li>
    <li><a href="contact.html" id="contact" class="">contact</a>
    </li>
  </div>
</ul>

最佳答案

问题是您的伪元素没有相对于包含它们的 a 定位。要修复,请进行以下更改:

  • position: relative;添加到a

ul {
  z-index: 101;
  list-style: none;
  position: relative;
  top: 60px;
  margin: 0;
  padding: 0;
  opacity: 1;
}
li {
  display: inline;
}
.left {
  display: inline-block;
  position: relative;
  left: 50px;
}
.left li {
  padding: 0 1.8em;
}
.right {
  display: block;
  position: relative;
  top: -27px;
  left: 840px;
}
.right li {
  padding: 0 1.8em;
}
li {
  z-index: 101;
  text-decoration: none;
  font-family: 'Raleway';
  font-weight: 500;
  color: black;
  font-size: 1.4em;
  line-height: 30px;
}
a {
  text-decoration: none;
  color: black;
  padding: 15px;
  position: relative;
}
a::before,
a::after {
  content: '';
  position: absolute;
  transition: transform .5s ease;
}
.hoverbox::before,
.hoverbox::after {
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border-style: solid;
  border-color: #f7941e;
}
.hoverbox::before {
  border-width: 2px 0 2px 0;
  transform: scaleX(0);
}
.hoverbox::after {
  border-width: 0 2px 0 2px;
  transform: scaleY(0);
}
.hoverbox:hover::before,
.hoverbox:hover::after {
  transform: scale(1, 1);
}
<div id="navbar">
  <ul class="left">
    <li><a href="index.html" id="home" class="hoverbox">home</a>
    </li>
    <li><a href="about.html" id="about" class="">about</a>
    </li>
    <li><a href="services.html" id="services" class="">services</a>
    </li>
  </ul>
  <ul class="right">
    <li><a href="work.html" id="work" class="">work</a>
    </li>
    <li><a href="process.html" id="process" class="">process</a>
    </li>
    <li><a href="contact.html" id="contact" class="">contact</a>
    </li>
  </ul>
</div>

顺便说一句,您的标记也不正确,因为 li 只能属于 ulol 元素。我修改了标记以将 div 切换为 ul(反之亦然)以使其成为有效的 HTML。

关于html - 应用于父元素而不是所选元素的效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31739940/

相关文章:

css - Vuetify 数据表组件 - 使用 CSS 截断单元格中的文本

javascript - Xamarin不允许加载本地资源文件

php - 使用 PHP 在动态创建表格中应用表格样式

html - 您如何使用 zurb foundation 设计表单文件字段的样式?

css - 如何防止 Primefaces 在子菜单文本和三 Angular 形图标之间换行?

internet-explorer - 内联 block : Firefox 4 vs IE 9

javascript - JQuery - 如何只对一个元素应用一次函数

html - 导航子菜单在悬停时切换到其他子菜单

html - 如何在 ionic 中创建这种类型的 View ?

javascript - 自动从 -9 旋转到 9 度