html - 将鼠标悬停在右侧时,左侧 div 确实会减小宽度

标签 html css css-selectors

所以我试图创建这个动画,当我将鼠标悬停在左侧 div 上时,它会将宽度更改为 100%,同时右侧 div 会将宽度设置为0%。当我将鼠标悬停在右侧 div 上时,反之亦然。

但是当我将鼠标悬停在右侧 div 上时,只有右侧 div 标签的宽度变为 100%,左侧 div 的宽度不变这使得右边的 div 位于左边,所以任何人都可以帮我解决这个问题。

我把整个代码放在下面

body {
  margin: 0;
  background-color: #fcfcfc;
  font-family: 'Roboto', 'open-sans', sans-serif;
  font-weight: 400;
  text-decoration: none;
}

.navbarli {
  display: inline-block;
  padding-bottom: 15px;
  padding-top: 15px;
  padding-right: 20px;
  padding-left: 20px;
  color:
}

.navbarui {
  margin: 0;
  padding: 0;
  list-style: none;
}

.navbardiv {
  background-color: #282828;
  align-content: center;
  text-align: center;
  color: #e0e0e0;
}

nav div a {
  text-decoration: none;
  color: #e0e0e0;
}

nav a: hover {
  color: #d8d8d8;
}

.mycontainer {
  margin-left: 10%;
  margin-right: 10%;
  background-color: ;
}

.footer1 {
  text-align: center;
  align-content: center;
  min-height: 200px;
  background-color: #282828;
  color: #a3a3a3;
  clear: both;
}

.divsecl {
  height: 200px;
  width: 50%;
  text-align: center;
  align-content: center;
  float: left;
}

.divsecr {
  height: 200px;
  width: 50%;
  text-align: center;
  align-content: center;
  float: right;
}

.divsecl:hover {
  width: 100%;
}

.divsecl:hover+.divsecr {
  width: 0%;
}

.divsecr:hover {
  width: 100%;
}

.divsecr:hover+.divsecl {
  width: 0%;
}

.resultitem {
  background: #48e;
  -webkit-transition: 0.5s;
  transition: 0.5s;
}

.resultitem:hover {
  background: #59f;
}
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<div>
  <div class="divsecl resultitem">
    <h1>Skills</h1>
  </div>
  <div class="divsecr resultitem">
    <h1>Portfolio</h1>
  </div>
</div>

最佳答案

+ 选择器在两个方向上都不像您期望的那样工作。此选择器表示选择紧跟在之后的元素,因此仅当您将鼠标悬停在第一个元素上时才起作用。

如果你想实现你想要的,这是另一个想法(我简化了你的代码以只保留相关部分)

.a {
  height: 200px;
  width: 50%;
  text-align: center;
  float: left;
  background: red;
  transition: 1s;
  overflow:hidden
}

.b {
  height: 200px;
  width: 50%;
  text-align: center;
  float: right;
  background: blue;
  transition: 1s;
  overflow:hidden
}

.container:hover .b{
  width: 100%;
}

.container:hover .a {
  width: 0%;
}
.a:hover {
  width: 100%!important;
}

.a:hover+.b {
  width: 0%;
}
<div class="container">
  <div class="a">
    <h1>Skills</h1>
  </div>
  <div class="b">
    <h1>Portfolio</h1>
  </div>
</div>

关于html - 将鼠标悬停在右侧时,左侧 div 确实会减小宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48630014/

相关文章:

html - Angular 2 CLI 元素中的 Bootstrap 4 按钮间距

css - 如何将 Angular.js 表单加载到 jsFiddle?

html - 我怎样才能用 CSS 专门针对这个元素 id?

CSS 选择器应用不正确

css - 在 IE6 CSS 中使用双类?

html - 如何在页面加载时将图像居中

java - 面板主体 Bootstrap 的一致显示

javascript - 如何通过 ajax 获取一个标签而不是整页的值 - jQuery

css hacks 和重写它们

css - 纯 CSS 添加链接 :before content