【问题标题】:HTML5 & CSS Header banner image and text alignmentHTML5 & CSS 标题横幅图像和文本对齐
【发布时间】:2019-02-20 01:14:52
【问题描述】:

我一直在尝试将横幅图像放在顶部标题中的文本后面,但格式似乎并不奏效。 我还在样式表中尝试了background-image: url(/imgs/header_pic.jpg);,但这样它甚至都没有出现。目前它显示,在文本后面,图像无法居中或调整最大高度,并且导航(列表项)也与图像重叠。

div#header-background {
  position: absolute;
  max-height: 4em;
  z-index: -1;
  border-radius: 10%;
  opacity: 0.25;
}

h1#header-text {
  position: relative;
  top: 0;
  text-align: center;
  font-size: 3em;
}
<body>
  <header>
    <div id="header-background"><img src="imgs/header_pic.jpg"></div>
    <h1 id="header-text">John Doe</h1>
  </header>
  <nav>
    <ul style="list-style-type: none">
      <li><a href="index.html">Main</a></li>
      <li><a href="about.html">about</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
  </nav>
  <main></main>
  <footer></footer>
</body>

更新:检查答案后(谢谢!)。我做了一些研究并将其修改为以下代码。现在我只需要将文本垂直移动到中心,因为奇怪的是它不是完全居中而是偏离了一点。 大家觉得呢?

header{
    width: 100%;
    height: 12em;
    position: relative;
  }

h1#header-text{
    position: absolute;
    z-index:999;
    text-align: center;
    font-size: 3em;
    width: 100%;
    height: 100%;
  }
  
header:after{
    content: "";
    position: absolute;
    top: 0px;
    width: 100%;
    height: 100%;
    background-image: url(imgs/header.jpg);
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
    opacity: 0.25;
  }

【问题讨论】:

    标签: css html http-headers background-image


    【解决方案1】:

    按照您目前的做法,您可以通过将其添加到您的 CSS 来隐藏溢出:

    div#header-background{
        position: absolute;
        max-height: 4em;
        z-index: -1;
        border-radius: 10%;
        opacity: 0.25;
        overflow: hidden;
    }
    

    https://jsfiddle.net/f0xwbq73/2/

    要应用背景图像,您可以这样做:

    header{
        background-image: url('https://www.gettyimages.com/gi-resources/images/CreativeLandingPage/HP_Sept_24_2018/CR3_GettyImages-159018836.jpg');
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
        position: relative;
    }
    
    h1#header-text{ 
        position: relative;
        top: 0;
        text-align: center;
        font-size: 3em;
    }
    <body>
    <header>
        <h1 id="header-text">John Doe</h1>
    </header>
        <nav>
           <ul style="list-style-type: none">
              <li><a href="index.html">Main</a></li>
              <li><a href="about.html">about</a></li>
              <li><a href="contact.html">Contact</a></li>
           </ul>
        </nav>
        <main></main>
        <footer></footer>
    </body>

    但是,您无法通过 CSS 控制图像的不透明度,因此您必须通过 Photoshop 或其他图像编辑器来控制。

    https://jsfiddle.net/5g1zjLhw/

    您可以查看https://www.w3schools.com/cssref/pr_background-image.asp 了解有关定位/尺寸的更多信息。

    【讨论】:

      【解决方案2】:

      我是网页设计的新手,但如果可以的话,我想帮助您解决您的问题。我相信你的问题是你没有定义图像的大小。我复制了您的代码并输入了图像的宽度和高度值,它对我有用。

      【讨论】:

        【解决方案3】:
        div#header-background {
          background-image:url('imgs/header_pic.jpg');
          background-size:cover;
          opacity: 0.25;
        }
        h1#header-text{
          text-align:center;
        }
        ui li{
          text-decoration:non;
          display:block;
        }
        
        <body>
          <header id="header-background">
            <h1 id="header-text">John Doe</h1>
          </header>
          <nav>
            <ul>
              <li><a href="index.html">Main</a></li>
              <li><a href="about.html">about</a></li>
              <li><a href="contact.html">Contact</a></li>
            </ul>
          </nav>
          <main></main>
          <footer></footer>
        </body>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-04
          • 1970-01-01
          相关资源
          最近更新 更多