html - 如何在 Bootstrap 4 中向我的 HTML 元素添加响应式居中文本?

标签 html css twitter-bootstrap

我想知道如何将响应居中的文本元素添加到我的 <a>元素?它不应受叠加颜色的影响。如果有额外覆盖 div 的干净替代方法,请告诉我。我正在使用 Bootstrap 4。

http://jsfiddle.net/or74zoy0/1

HTML

<div class="container">
  <div class="row">
    <div class="col-md-3">
      <a class="item" href="#">
        <div class="image">
          <img src="https://lorempixel.com/output/technics-q-c-640-480-4.jpg" alt="" />
          <div class="overlay"></div>
        </div>
      </a>
    </div>
  </div>
</div>

CSS

.image { position: relative;}
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; right:0; bottom:0; background-color: rgba(0,0,0,0.5);}

最佳答案

这是一种方法:

HTML

<div class="container">
  <div class="row">
    <div class="col-md-3">
      <a class="item" href="#">
        <div class="image">
          <img src="https://lorempixel.com/output/technics-q-c-640-480-4.jpg" alt="" />
          <div class="overlay"></div>
        </div>
        <span class="title">Centered Text</span>
      </a>
    </div>
  </div>
</div>

CSS

.image { position: relative;}
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; right:0; bottom:0; background-color: rgba(0,0,0,0.5);}
.title {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

或者用 Bootstrap 类解决:

HTML

<div class="container">
  <div class="row">
    <div class="col-md-3">
      <a class="item" href="#">
        <div class="image">
          <img src="https://lorempixel.com/output/technics-q-c-640-480-4.jpg" alt="" />
          <div class="overlay"></div>
        </div>

        <div class="title d-flex justify-content-center align-items-center">
            Centered Text
        </div>
      </a>
    </div>
  </div>
</div>

CSS

.image { position: relative;}
.image img { max-width: 100%; max-height: 100%; }
.overlay { position: absolute; top: 0; left: 0; right:0; bottom:0; background-color: rgba(0,0,0,0.5);}
.title { position: absolute; top: 0; left: 0; right:0; bottom:0;}

关于html - 如何在 Bootstrap 4 中向我的 HTML 元素添加响应式居中文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47463530/

相关文章:

javascript - 将元素数据传递给 Bootstrap 模态

javascript - 如何获取没有ID的div的数据值?

html - 如何使此布局在 Chrome 和 IE 11 中看起来相同?

javascript - 如何使用 Javascript 在 Html 页面上添加计时器

html - 悬停时使背景变暗但不使文本变暗

html - 如何在 Non-responsive Bootstrap 中使水平滚动在 Navbar 上工作

css - 插入背景图像

css - 如何嵌套此 scss 以使其仍按预期运行

html - 在 Bootstrap 中创建 App Store 按钮

html - Bootstrap 网格系统是流动的而不是静态的