html - 让两个 div 并排显示

标签 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

我希望这些并排显示,而不是彼此叠加显示。

最佳答案

如果将两个容器包装在一个 div 中并设置为 display: flex 就没问题了。

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

.wrapper {
  display: flex;
}

希望对你有帮助:)

关于html - 让两个 div 并排显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244292/

相关文章:

html/css定位三个文本框

jquery - 如何使用 Jquery 对图像进行平滑的自动放大?

css - 如何选择每两个元素交替?

jquery - 有没有办法将边框放在元素内部而不是外部?

css - 使布局在 IE8 怪癖模式下工作

html - iOS WebView : open single hyperlink in external browser

javascript - 即使站点进行多次重定向,也可以看到网络流量吗?

html - 具有中心偏移的 css calc

css - 日期选择器部分被页脚遮挡

css - CSS "not working"中的简单 div 当其中有文本时?