html - 图片内的文字

标签 html css

我只想像这样在图像中添加我的 h2 文本和小段落。

enter image description here

当我添加 h1 和文本时,它位于图像下方。

enter image description here

这是因为错误的 css 定位吗?

这是我的代码

<div className="committeeBackground">
                  <img src="/images/conference.jpg" alt=""/>
                  <h1>Who</h1>
                  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
                    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
                  </p>
                </div>

.committeeBackground{
img{
  width: 100%;
 }
}

最佳答案

这样做,使用 flex 居中,使用 position: absolute 居中<​​/p>

.wrapper {
  position: relative;
  display: inline-block;
}
.ontop {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.ontop > div {
  text-align: center;
  color: white;
  font-size: 24px;
}
<div class="wrapper">
  <img src="http://lorempixel.com/400/300/animals/6" alt="">
  <div class="ontop">
    <div>
      <h2>Test Text</h2>
      <p><i>Dummy Text Under H2</i></p>
    </div>
  </div>
</div>

关于html - 图片内的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37818957/

相关文章:

javascript - 如果内部有其他标签,带有 <p> 标签的 jQuery 分页会在单个页面中显示所有页面

php - WordPress 自定义下拉菜单

css - Chrome 开发人员工具样式选项卡显示褪色的 CSS 定义,为什么?

javascript - 在不同的 div 中插入图像

javascript - 如何更改 <meter> 值?

php - 从 .html 页面重定向未登录的用户?

html - 显示垂直和水平彼此相关的图像

javascript - 将多个值传递到另一页

css - 如果标签元素中有 anchor 元素,则复选框不起作用

html - 始终将列表的前两项保留在顶部