html - 无法将边框与图像内部的文本居中

标签 html css

<分区>

正如标题所述,我已经尝试搜索,但我无法获得带有文本的边框,使其位于背景图像的中心。截至目前,它只是居中但坚持顶部(如果这有意义的话)。

.banner {
  background-image: url("http://www.hdwallpapers.in/walls/early_snowfall-wide.jpg");
  background-size: cover;
  background-position: bottom center;
  height: 400px;
  width: 100%;
}

#banner-content {
  border: 1px solid #FFF;
  margin: auto;
  text-align: center;
  width: 40%;
}
  <div class="banner">
    <div id="banner-content">
      <h1>Random misc text for the purpose of practicing</h1>
    </div>
  </div>

提前感谢您的帮助!

最佳答案

添加display: flex;banner类;并将 display: inline-flex 添加到您的 banner-content ID 中。如这里:

.banner {
  background-image: url("http://www.hdwallpapers.in/walls/early_snowfall-wide.jpg");
  background-size: cover;
  background-position: bottom center;
  height: 400px;
  width: 100%;
  display: flex;
}

#banner-content {
  border: 1px solid #FFF;
  margin: auto;
  text-align: center;
  width: 40%;
  display: inline-flex;
}
  <div class="banner">
    <div id="banner-content">
      <h1>Random misc text for the purpose of practicing</h1>
    </div>
  </div>

已编辑

如果您不希望文字在屏幕较小时超出边框,请将此代码添加到您的样式中:

.banner h1{
  overflow-wrap: break-word;
  word-wrap: break-word;
  -ms-word-break: break-all;
  word-break: break-all;
  word-break: break-word;
}

关于html - 无法将边框与图像内部的文本居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41417279/

上一篇:html - 较小屏幕尺寸的配置问题

下一篇:html - Bootstrap 2列嵌套列

相关文章:

jquery - 如何为jquery中的所有子节点添加单个父元素

html - 不相等的 Html 文本框和下拉宽度与 XHTML 1.0 strict

jQuery 悬停不透明度闪烁

html - 为什么 Internet Explorer 8 会打印不可见的内容?

html - 在生成的 HTML 中更改函数签名的 doxygen 格式

python - 使用 xpath 单击按钮

css - 宽度不扩大?

javascript - 请帮我修复 javascript 中的 html div 宽度

css - Angular 6,如何根据变量(连续变化)更改 img 的样式(旋转、颜色、比例)

html - 删除手机屏幕中的图片链接