html - 为什么我的文字不能对齐到图像的中心?

标签 html css

我正在尝试将页面中间的页眉文本与图像对齐。图片居中对齐,但文本位于页面左侧。

Here is how it looks.

我试图复制 this解决方案,但我显然没有做正确的事情。任何帮助将非常感激。 HTML:

    <header>
        <div class="tag">
            <h1>Some text here</h1>
            <h3>And more text here</h3>
        </div>
        <img src="http://www.placehold.it/900x300">
    </header>

CSS:

    header {
      height: 200px;
      line-height: 50px;
      text-align: center;
      background: #303e49;
      position: relative;
    }

    .tag {
         text-align: center;
         background: #303e49;
         position: absolute;

    }

最佳答案

1st 你不应该标记 jquery 因为它的 css

2nd 将 left 0 和 right 0 添加到绝对元素以获得包含 div 的完整宽度

.tag {
         text-align: center;
         background: #303e49;
         position: absolute;
         left: 0;
         right: 0;
         z-index : 1;
    }

关于html - 为什么我的文字不能对齐到图像的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780761/

相关文章:

html - 从 <ul> 导航中删除左侧空间

html - 使用 CSS1 如何将鼠标悬停在图像上并将其与不同的图像交换?

html - 当兄弟的 child 出现时的选择器

css - 在 div 内居中 3 个框

jquery - 如果单击除第一个链接以外的所有链接,我想让一个对象有一个类处于事件状态,如果单击第一个链接,则应将其删除(如果处于事件状态)

html - 悬停不适用于 SVG 路径的未填充区域

jquery - <IMG/> LowSrc 属性+事件?

html - 我的导航栏不会与页面中心对齐?

php - 为每个 WordPress 页面自定义背景图片?

html - 在 float div 之前定义最小可用高度?