【问题标题】:How can i get my H1 text to appear in the center of an image?如何让我的 H1 文本出现在图像的中心?
【发布时间】:2023-02-04 04:33:08
【问题描述】:

我的网页上有一张图片,我试图在图片中央显示文本“欢迎来到华盛顿州”,但到目前为止都失败了。我尝试使用 flexbox 和显示功能,文本将在图像上对齐,但我无法将其置于图像中心。我怎样才能让它到达文本可以对齐图像中心的位置?

-谢谢您的帮助!

* {
  margin: 0;
  padding: 0;
}


/*------------------------------HEADER--------------------*/

.header {
  background-color: #00843D;
  height: 125px;
  position: relative;
}

.logo img {
  height: 200px;
  width: 200px;
  position: relative;
  z-index: 1;
}

.logo {
  position: absolute;
  left: 5%;
  top: 15%;
}

.nav-links {
  text-align: right;
}

.nav-links ul {
  color: white;
  padding: 25px;
}

.nav-links ul li {
  display: inline-block;
  font-size: 35px;
  padding: 20px;
}


/*--------------------------WELCOME PAGE----------------*/

.welcome-page {
  position: relative;
}

.welcome-page img {
  width: 100%;
  height: 100%;
  position: relative;
}

.welcome-page h1 {
  position: absolute;
  color: black;
  text-align: center;
  top: 20%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Homepage</title>
  <link rel="stylesheet" href="homepage.css">
</head>

<body>
  <!--Navigation Link-->
  <div class="header">
    <div class="logo">
      <img src="C:\Users\\Documents\Washington State Project\Images\Seal_of_Washington.svg.png">
    </div>
    <!--Tabs-->
    <div class="nav-links">
      <ul>
        <li>Things to do</li>
        <li>History</li>
        <li>Education</li>
        <li>Sports</li>
      </ul>
    </div>
  </div>

  <!--Welcome Page-->
  <div class="welcome-page">
    <img src="C:\Users\Roger Garcia\Documents\Washington State Project\">
    <h1>Welcome to Washington State</h1>
  </div>
</body>

</html>

【问题讨论】:

  • 为什么不在 css 中将图像设置为背景?否则你可以使用绝对定位和 z-index

标签: html css


【解决方案1】:

尝试用 div 包装 imgh1,然后使用该 div 将它们放在一起。 &lt;div class="welcome-sign"&gt; 使用 flex-box 使其内容居中。

您可以编辑

.welcome-sign h1 {
    position: absolute;
    top: 0; /* Edit this value*/
}

垂直重新定位h1

* {
  margin: 0;
  padding: 0;
}


/*------------------------------HEADER--------------------*/

.header {
  background-color: #00843D;
  height: 125px;
  position: relative;
}

.logo img {
  height: 200px;
  width: 200px;
  position: relative;
  z-index: 1;
}

.logo {
  position: absolute;
  left: 5%;
  top: 15%;
}

.nav-links {
  text-align: right;
}

.nav-links ul {
  color: white;
  padding: 25px;
}

.nav-links ul li {
  display: inline-block;
  font-size: 35px;
  padding: 20px;
}


/*--------------------------WELCOME PAGE----------------*/

.welcome-sign {
  position: relative;
  display: flex;
  flex-flow: column nowrap;
  align-items: center;
  border: 1px solid red;
}

.welcome-sign h1 {
  position: absolute;
  top: 0; /* Edit this value */
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Homepage</title>
  <link rel="stylesheet" href="homepage.css">
</head>

<body>
  <!--Navigation Link-->
  <div class="header">
    <div class="logo">
      <img src="C:Users\DocumentsWashington State ProjectImagesSeal_of_Washington.svg.png">
    </div>
    <!--Tabs-->
    <div class="nav-links">
      <ul>
        <li>Things to do</li>
        <li>History</li>
        <li>Education</li>
        <li>Sports</li>
      </ul>
    </div>
  </div>

  <!--Welcome Page-->
  <div class="welcome-page">
    <div class="welcome-sign">
      <img src="C:UsersRoger GarciaDocumentsWashington State ProjectImages94997.jpg">
      <h1>Welcome to Washington State</h1>
    </div>
  </div>
</body>

</html>

【讨论】:

    【解决方案2】:

    像这样:

    div {
      background-image: url("https://upload.wikimedia.org/wikipedia/commons/a/ad/King_County_District_Court_Seal.png"); 
      height: 200px;
      width: 200px;
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      outline: 1px dotten black;
      display: flex;
      justify-content: center;
    }
    h1 {
      align-self: center;
    }
    <div>
      <h1>Hello World!</h1>
    </div>

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      HTML

      <!DOCTYPE html>
      <html lang="en">
      
      <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Homepage</title>
        <link rel="stylesheet" href="homepage.css">
      </head>
      
      <body>
        <!--Navigation Link-->
        <div class="header">
          <div class="logo">
            <img src="C:Users\DocumentsWashington State ProjectImagesSeal_of_Washington.svg.png">
          </div>
          <!--Tabs-->
          <div class="nav-links">
            <ul>
              <li>Things to do</li>
              <li>History</li>
              <li>Education</li>
              <li>Sports</li>
            </ul>
          </div>
        </div>
      
        <!--Welcome Page-->
        <div class="welcome-page">
          <img src="https://cdn3.goldtag.net/web/uploads/pictures/736x536/1668352521_1667803503__750-5449.jpg">
          <h1 id="theWelcome">Welcome to Washington State</h1>
        </div>
      </body>
      
      </html>
      

      CSS

      * {
        margin: 0;
        padding: 0;
      }
      
      
      /*------------------------------HEADER--------------------*/
      
      .header {
        background-color: #00843D;
        height: 125px;
        position: relative;
      }
      
      .logo img {
        height: 200px;
        width: 200px;
        position: relative;
        z-index: 1;
      }
      
      .logo {
        position: absolute;
        left: 5%;
        top: 15%;
      }
      
      .nav-links {
        text-align: right;
      }
      
      .nav-links ul {
        color: white;
        padding: 25px;
      }
      
      .nav-links ul li {
        display: inline-block;
        font-size: 35px;
        padding: 20px;
      }
      .welcome-page{ 
        position: relative;
        background: red;
        width: max-content;
      }
      
      #theWelcome {
          position: absolute;
          top: 50%;
          background: red;
          left: 0;
          right: 0;
          display: flex;
          justify-content: center;
          align-items: center;
      }
      

      P.S:我使用虚拟图像只是为了向您展示它是如何完成的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-12
        • 1970-01-01
        • 1970-01-01
        • 2019-11-15
        相关资源
        最近更新 更多