【问题标题】:how to put text over image without absolute positioning如何在没有绝对定位的情况下将文本放在图像上
【发布时间】:2018-10-14 07:56:09
【问题描述】:

我正在使用引导程序进行布局,我遇到的困难之一是如何将文本放在图像上。我已经尝试了所有可以做的事情,例如使用不起作用的背景图像。其中一种方法是将图像放置为相对位置并将文本放置在绝对位置。

但是对于我的问题,我不能使用绝对位置,因为我有太多的图像和文本的长度是不同的。所以我必须通过使用 align-center 来解决这个问题..

如果有人熟悉这个问题,你能给这个问题一些建议吗?我必须将文本垂直和水平地放在图像的中心。

<div class="row justify-content-center">
      <div class="col-sm-auto">
        <img src="img/1.jpg">
      </div>
      <div class="col-sm-auto">
        <img src="img/2.jpg">
      </div>
      <div class="col-sm-auto">
        <img src="img/3.jpg">
      </div>
 </div>

【问题讨论】:

  • “因为我的图片太多,文字的长度不一”,你可以使用相对长度来让你的文字居中。
  • 没有绝对和相对是不可能的。建议:在 col-sm-auto 附近添加唯一类并将其用作相对类。然后将节点内的文本设为绝对。
  • 如果图像源没有变化,那么你可以尝试在 CSS 中设置background: url(img/1.jpg) 并使用background-position 调整其位置。
  • @IshantSolanki 文字呢?
  • 你知道为什么 background-image 或 background 是不可能的吗? .x { 背景: url("img/1.jpg"); },它永远不会起作用。

标签: css twitter-bootstrap


【解决方案1】:

如果图像源没有变化,那么你可以尝试从 CSS 设置 background: url(img/1.jpg) 并使用 background-position 调整它的位置

在这里查看小提琴。 http://jsfiddle.net/yktvod54/2/

html

<div class="row justify-content-center">
  <div class="col-sm-auto image-1">
  text1
  </div>
  <div class="col-sm-auto image-2">
  text2
  </div>
  <div class="col-sm-auto image-3">
  text3 with longer text
  </div>

css

.col-sm-auto {

  color: lightgreen;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.col-sm-auto.image-1 {
  width: 200px;
  height: 150px;
  background: 
  url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
  background-size: cover;
}

.col-sm-auto.image-2 {
    width: 200px;
    height: 200px;
    background: url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
    background-size: cover;
}

.col-sm-auto.image-3 {
    width: 250px;
    height: 250px;
    background: 
    url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
    background-size: cover;
}

【讨论】:

  • 每个div的图片都不一样。但你的都是一样的。
  • 这是一个很好的解决静态HTML页面的方法,当页面变成动态的时候就会出现问题。
  • 取决于页面的动态程度。如果 url 开始来自服务器而不是从一开始就知道它,那么这个解决方案将不起作用。然而,这完全是一个不同的问题。提问者在 html 中提到了 url,因此可以安全地假设我们从一开始就知道图像列表及其 URL。我们可以将这些 URL 绑定到每个 css 类名。并将该类动态注入到我们希望显示图像的任何位置。
  • 我没有考虑过使用flex display,这似乎是个好主意,我会尝试一下。非常感谢。
  • 不客气!很想知道这个解决方案是否真的适合你:)
【解决方案2】:

将图像设置为 div 的背景,将该 div 设为 display:flex; justify-content: center; align-items: center; 并为其指定宽度/高度。 flex 对齐会将所有内容推到 div 的中心,但它需要一个高度/宽度,否则 div 将只是文本的大小。添加flex-wrap: wrap;,文本会环绕,但是没有像word-break这样的断点

【讨论】:

    【解决方案3】:

    我不能使用绝对位置,因为我有太多的图像和文本的长度是不同的

    您可以使用相对长度来定位文本,这样无论它们的大小如何,它们都会居中。
    您还需要translate CSS function

    .image-wrapper {
      width: 400px;
      position: relative;
    }
    
    .image-wrapper img {
      width: 100%;
      height: auto;
    }
    
    .image-wrapper figcaption {
      width: 100%;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      text-align: center;
      font-size: 2em;
      color: white;
    }
    <figure class="image-wrapper">
      <img src="https://image.ibb.co/n5wsKU/bg3.jpg" alt="A nice pic!">
      <figcaption>A text that fits on one line</figcaption>
    </figure>

    如果您仍然不想使用绝对定位,这里有另一种解决方案,使用 background-image 而不是 img 标签:

    .image-wrapper {
      width: 400px;
      height: 300px;
      display: flex;
    }
    
    .image-wrapper.image-01 {
      background: url('https://image.ibb.co/n5wsKU/bg3.jpg') center center / cover;
    }
    
    .image-wrapper p {
      margin: auto;
      font-size: 2em;
      color: white;
    }
    <div class="image-wrapper image-01">
      <p>Some text</p>
    </div>

    【讨论】:

    • 提问者想要“没有绝对定位”
    • Quentin 的解决方案成功地解决了我的问题,我现在必须解决的一件事是即使文本可以放入一行,但它会转到下一行。当然短文本写在一行中,但是文本有点长,它会转到下一行。所以我把 padding-left 和 padding-right 设置为 0px,它仍然超过两行。如果您有一些解决方案,请给我建议?
    • 只需将 width:100%text-align:center 添加到文本标签!它应该工作。我将用更长的文字编辑我的答案。
    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多