【问题标题】:Place a text on top of an image [SOLVED] [duplicate]在图像顶部放置文本 [已解决] [重复]
【发布时间】:2021-01-07 19:31:48
【问题描述】:

我想在我的白页的中心放置一个图像并在其上添加一个标题(标题会大于图像 || 并且标题中心必须是图像中心)。问题是标题不会出现在图片之上。

我使用 Django 和引导程序 ^^

代码如下:

HTML:

<div class="col-md-7 white nopadding text-center">
            <div class="brand">
                Brand
            </div>
            <div class="heading">
                <img src="{% static '/eyemeet/hero.jpg' %}" class="hero">
                <div class="title ctr">Title</div>
</div>

CSS:

.hero {
    width: 65%;
    height: auto;
}

.title {
    position: absolute;
    background-color: cadetblue;
    top: 50%;
    left: 50%;
}

.ctr {
    position: absolute;
    transform: translate(-50%, -50%);
}

目前发生了什么:

编辑:

我找到了答案!我希望它可以帮助那些仍然想知道如何做的人:) Center text over an image in flexbox

【问题讨论】:

  • 我认为您需要添加更多 CSS,因为您提供的代码运行良好:jsfiddle.net/17bofkqa
  • 我会将图片用作标题的背景图片

标签: html css django flexbox


【解决方案1】:

父元素必须是position:relative,然后定位将只包含在这个父元素内部

.heading{
  position:relative;
}
.title{
  position:absolute;
  top:50%;
  right:50%;
}

【讨论】:

    【解决方案2】:

    .hero {
        width: 65%;
        height: auto;
    }
    
    .title {
        position:absolute;
        top:55%;
        left:50%;
        transform:translate(-50%,-50%);
        background-color: cadetblue;
        width:100px;
        font-size:40px;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="col-md-7 white nopadding py-3 text-center">
                <div class="brand py-2">
                    Brand
                </div>
                <div class="heading">
     
                    <img src="https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fgetwallpapers.com%2Fwallpaper%2Ffull%2Ff%2F3%2Fa%2F807159-download-funny-cats-wallpapers-1920x1200-meizu.jpg&f=1&nofb=1" class="hero">
      <div class="title text-center ml-auto mr-auto ">Title</div>               
    </div>

    【讨论】:

    • 您好,谢谢您的回答。它有效,但标题没有居中。是否可以使用 % 代替 px ?这样标题就可以距离图片顶部50%
    • @Bonsaï 检查我编辑了我的答案
    猜你喜欢
    • 2011-02-24
    • 2021-12-23
    • 1970-01-01
    • 2011-02-15
    • 2013-09-16
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多