html 如何让H1文本显示在图像的中心?

zf2sa74q  于 2022-12-16  发布在  其他
关注(0)|答案(3)|浏览(170)

我的网页上有一张图片,我试图在图片的中心显示文本“欢迎来到华盛顿州”,但迄今为止一直失败。我尝试使用弹性框和显示功能,文本将在图片上对齐,但我无法使其在图片上居中。我如何才能使其在文本可以在图片上居中对齐的位置?

  • 谢谢你的帮助!
* {
  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\Images\1094997.jpg">
    <h1>Welcome to Washington State</h1>
  </div>
</body>

</html>
0lvr5msh

0lvr5msh1#

试着用一个div将imgh1 Package 起来,然后用这个div将它们放在一起,<div class="welcome-sign">使用flex-box将内容居中。
您可以编辑

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

以垂直地重新定位h1
x一个一个一个一个x一个一个二个x

cngwdvgl

cngwdvgl2#

就像这样:

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>
i2loujxw

i2loujxw3#

你可以试试这个
超文本标记语言

<!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="https://cdn3.goldtag.net/web/uploads/pictures/736x536/1668352521_1667803503__750-5449.jpg">
    <h1 id="theWelcome">Welcome to Washington State</h1>
  </div>
</body>

</html>

中央支助系统

* {
  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;
}

我使用虚拟图像只是为了告诉你它是如何做的。

相关问题