如何将文本放在图像卡头引导程序4上

tes*_*321 4 html css twitter-bootstrap

我尝试将标题放在图像上,我希望我的卡片标题超出我的图像,不确定我需要CSS吗?

这是我的HTML

<div class="card card-blog">
        <div class="card">
            <a href="#pablo">
                <img class="img" src="assets/img/backgound.jpg">
            </a>
        </div>
        <div class="card-body text-center">
            <h4 class="card-title">
                TEXT OVER IMAGE
            </h4>
            <div class="card-description">
                <div class="table-responsive">

                </div>
            </div>
            <div class="card-footer">
                <a href="#pablo" class="btn btn-danger btn-round">View Article</a>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

输出:我可以将卡片标题移到图像上吗?

在此输入图像描述

Rob*_*ert 8

Bootstrap 4没有任何本机工具可以在card组件中实现这一点,但是使用一些额外的CSS可以使它工作:

.card-img-caption {
  border-top-left-radius: calc(.25rem - 1px);
  border-top-right-radius: calc(.25rem - 1px);
}
  
.card-img-caption .card-img-top {
  z-index: 0;
}
    
.card-img-caption .card-text {
  text-align: center;
  width: 100%;
  margin: 33% 0;
  position: absolute;
  z-index: 1;
}
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">

<div class="card" style="width: 20rem;">
  <div class="card-img-caption">
    <p class="card-text">Test</p>
    <img class="card-img-top" src="http://placehold.it/130x100" alt="Card image cap">
  </div>
    
  <div class="card-body">
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我们将.card-img-top图像封装在一个新的包装器中.card-img-caption,然后应用一些定位,z-index以便图像始终位于文本下方.

你可能想要使用marginon,.card-img-caption .card-text因为33%这里使用的更多是一个神奇的数字而不是更优雅的东西.

如果你知道你用作背景的图像的尺寸; 一个更聪明的举动可能是做到这一点...使用一个background-image.


Lad*_*y_A 8

我知道这已经很旧了,但是对于那些使用 Bootstrap 4 的人来说,有一种使用该类的本机方法来执行此操作card-img-overlay

<div class="card bg-dark text-white">
  <img class="card-img" src="..." alt="Card image">
  <div class="card-img-overlay">
    <h5 class="card-title">Card title</h5>
    <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
    <p class="card-text">Last updated 3 mins ago</p>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

https://getbootstrap.com/docs/4.0/components/card/#image-overlays


小智 3

尝试这样:

#container {
  height: 400px;
  width: 400px;
  position: relative;
}
#image {
  position: absolute;
  left: 0;
  top: 0;
}
#text {
  z-index: 100;
  position: absolute;
  color: white;
  font-size: 24px;
  font-weight: bold;
  left: 150px;
  top: 350px;
}
Run Code Online (Sandbox Code Playgroud)
<div id="container">
  <img id="image" src="http://www.noao.edu/image_gallery/images/d4/androa.jpg" />
  <p id="text">
    Hello World!
  </p>
</div>
Run Code Online (Sandbox Code Playgroud)