html - 两个带有底层图像的独立 DIV 不会 float 到右侧

标签 html css

我想将页面分成两半。一侧是带有蓝色透明覆盖层的计算机显示器图像。将鼠标悬停在上面时,它会变得更亮。同样在右侧有不同的图像(音乐手稿)。然而,这在实践中行不通,只会折叠成左侧的一个 DIV block 。

代码:

html, body {
  background-color: black;
  height: 100%;
  width: 100%;
  margin: 0%;
}

div.leftImage {
  /*Set up positioning:*/
  display: block;
  float: left;
  width: 50%;
  height: 100%;
  z-index: 0;
  position: absolute;
  /*Set up the background:*/
  background-image: url("https://images.pexels.com/photos/270360/pexels-photo-270360.jpeg?w=940&h=650&dpr=2&auto=compress&cs=tinysrgb");
  background-position: 45%;
}

div.rightImage {
  /*Set up positioning:*/
  display: block;
  float: right;
  width: 50%;
  height: 100%;
  position: absolute;
  z-index: 0;
  /*Set up the background:*/
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/9/9d/BWV605.png");
  background-position: center;
}

div.left {
  /*Set up positioning:*/
  display: block;
  float: left;
  width: 50%;
  height: 100%;
  position: absolute;
  z-index: 1;
  /*Set up the background:*/
  background-color: rgba(0, 150, 255, 0.5);
  transition: background-color 0.25s ease;
}

div.left:hover {
  background-color: rgba(0, 150, 255, 0.8);
}

div.right {
  /*Set up positioning:*/
  display: block;
  float: right;
  width: 50%;
  height: 100%;
  position: absolute;
  z-index: 1;
  /*Set up the background:*/
  background-color: rgba(204, 0, 0, 0.5);
  transition: background-color 0.25s ease;
}

div.right:hover {
  background-color: rgba(204, 0, 0, 0.8);
}
<!DOCTYPE html>
<html>

<body>
  <div class="leftImage">
  </div>
  <div class="rightImage">
  </div>

  <div class="right">
    <p>Test.</p>
  </div>
  <div class="left">
    <p>Test.</p>
  </div>
</body>

</html>

提前致谢。

最佳答案

如果您使用position: absolutefloat: left 等布局属性将不会有任何效果。

相反,使用 topleftrightbottom 属性来控制绝对定位的元素:

html, body {
  background-color: black;
  height: 100%;
  width: 100%;
  margin: 0%;
}

div.leftImage {
  /*Set up positioning:*/
  z-index: 0;
  position: absolute;
  top: 0;
  left: 0;
  right: 50%;
  bottom: 0;
  /*Set up the background:*/
  background-image: url("https://images.pexels.com/photos/270360/pexels-photo-270360.jpeg?w=940&h=650&dpr=2&auto=compress&cs=tinysrgb");
  background-position: 45%;
}

div.rightImage {
  /*Set up positioning:*/
  z-index: 0;
  position: absolute;
  top: 0;
  left: 50%;
  right: 0;
  bottom: 0;
  /*Set up the background:*/
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/9/9d/BWV605.png");
  background-position: center;
}

div.left {
  /*Set up positioning:*/
  z-index: 1;
  position: absolute;
  top: 0;
  left: 0;
  right: 50%;
  bottom: 0;
  /*Set up the background:*/
  background-color: rgba(0, 150, 255, 0.5);
  transition: background-color 0.25s ease;
}

div.left:hover {
  background-color: rgba(0, 150, 255, 0.8);
}

div.right {
  /*Set up positioning:*/
  z-index: 1;
  position: absolute;
  top: 0;
  left: 50%;
  right: 0;
  bottom: 0;
  /*Set up the background:*/
  background-color: rgba(204, 0, 0, 0.5);
  transition: background-color 0.25s ease;
}

div.right:hover {
  background-color: rgba(204, 0, 0, 0.8);
}
<!DOCTYPE html>
<html>

<body>
  <div class="leftImage">
  </div>
  <div class="rightImage">
  </div>

  <div class="right">
    <p>Test.</p>
  </div>
  <div class="left">
    <p>Test.</p>
  </div>
</body>

</html>

关于html - 两个带有底层图像的独立 DIV 不会 float 到右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47877404/

相关文章:

css - 我怎样才能停止向左浮动?

javascript - 是否可以在没有 CSS3 的情况下旋转 HTML 对象?

javascript - 替换 "Choose file"的文本栏

javascript - 调整窗口大小时扩展的字体大小

java - 如何创建动态拖放模板?

php - 两个相互影响的div的CSS

php - 将占位符属性添加到 Zend 2 中的日期输入

javascript - 如何使用jquery将base64图像路径转换为真实路径。

javascript - 如何为文本字符串自定义 css 椭圆

javascript - 如何将类添加到下一个 DIV 并使用动态元素数淡出当前 - 多个实例