html - 使用 display flex 在图像顶部垂直对齐 bootstrap btn

标签 html css twitter-bootstrap flexbox

正如我的标题所述,我正在尝试垂直对齐我的 btn,使其位于我的图像的中心。我没有任何运气,因为我的 btn 位于我的图像下方而不是顶部。我已经尝试了很多,但似乎没有任何效果。我已经发布了我的 HTML 和 CSS,也许有人可以指导我从哪里开始。

HTML

<div class="row">
    <div class="col-md-4">
        <div class="imgAbout">
          <img src="img/team/neil580x410.jpg" class="img-responsive" alt="Neil Elmouchi Bio">
          <a class="btn btn-sm btn-primary" href="bios/teamBioNeil.html" role="button">View More</a>
        </div>
      <h1>Neil Elmouchi</h1>
      <h3>Chairman &amp; CEO<br> 
      Senior Wealth Advisor</h3>
    </div>
</div>

CSS

#about img {
  margin: 0 auto;
}

.imgAbout {
  background: #d5d5d5;
  text-align: center;
}

.imgAbout img {
  opacity: 1;
  transition: opacity .25s ease-in-out;
  -moz-transition: opacity .25s ease-in-out;
  -webkit-transition: opacity .25s ease-in-out;
}

.imgAbout img:hover {
   opacity: 0.4;
}

最佳答案

您很困惑,因为您忘记了元素之间通常不能重叠。因此,为了让按钮位于较大图像的顶部,我们通常使用叠加(position:absolute)技术来提高图像尺寸的灵 active 。这是您修改后的示例

.imgAbout {
  position: relative;
}

/* overlay to inherit dimension from imgAbout and centralize button */

.center-container{
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.imgAbout img {
  width: 100%;
  height: auto;
  display: block;
  opacity: 1;
  transition: opacity .25s ease-in-out;
  -moz-transition: opacity .25s ease-in-out;
  -webkit-transition: opacity .25s ease-in-out;
}

.imgAbout:hover img {
   opacity: 0.4;
}

https://jsfiddle.net/0bt4q9vk/

关于html - 使用 display flex 在图像顶部垂直对齐 bootstrap btn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38418218/

相关文章:

css - 导航栏切换器按钮不显示?

html - 圆 Angular ,1 个 Angular 仍然非常尖

html - 如何使我的 HTML 页面灵活/响应式,适用于 iPhone 6s

php - 在mysql中插入数据后重定向到php页面

具有不同字体系列和行高的 CSS Bulma

php - Bootstrap 输入组添加到谷歌浏览器

html - 如何在不丢失宽度的情况下使用负边距

php - 同一页面上的联系表单错误消息

javascript - 如何使元素与屏幕大小相同但不再重新调整大小?

css - bootstrap 4主体滚动问题