让两个div并排显示

时间:2017-11-12 00:31:21

标签: html css

如何让两个div中包含的两个图像并排显示? 我已经尝试将container中的变量更改为display: inline-block;float: left;,正如其他一些主题所建议的那样,但这些变量并不像我尝试的那样工作。

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #008CBA;
  overflow: hidden;
  width: 100%;
  height: 0;
  transition: .5s ease;
}

.container:hover .overlay {
  height: 100%;
}

.text {
  white-space: nowrap; 
  color: white;
  font-size: 20px;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}
</style>
</head>
<body>

<h2>Slide in Overlay from the Bottom</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

</body>
</html>

外观如何:

enter image description here

我希望这些可以并排显示,而不是在另一个之上。

2 个答案:

答案 0 :(得分:2)

如果你将两个容器包装在一个div设置为display: flex,你会没事的。

<div class="wrapper">
  <div class="container">
    <!-- your content -->
  </div>
  <div class="container">
    <!-- your content -->
  </div>
</div>

.wrapper {
  display: flex;
}

我希望它有所帮助:)

答案 1 :(得分:1)

只需将float:left添加到容器类。

结果:

enter image description here