Home > Blockchain >  How can i get my H1 text to appear in the center of an image?
How can i get my H1 text to appear in the center of an image?

Time:12-16

I have an image on my webpage that I'm trying to display the text "Welcome to Washington State" in the center of the image but have failed so far. I tried using a flexbox and display functions and the text will align on the image but I cannot get it to center on the image. How can i get it to where the text can align center over the image?

-Thanks for the help!

* {
  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 >
    <div >
      <img src="C:\Users\\Documents\Washington State Project\Images\Seal_of_Washington.svg.png">
    </div>
    <!--Tabs-->
    <div >
      <ul>
        <li>Things to do</li>
        <li>History</li>
        <li>Education</li>
        <li>Sports</li>
      </ul>
    </div>
  </div>

  <!--Welcome Page-->
  <div >
    <img src="C:\Users\Roger Garcia\Documents\Washington State Project\Images\1094997.jpg">
    <h1>Welcome to Washington State</h1>
  </div>
</body>

</html>

CodePudding user response:

Try wrapping the img and h1 with a div and then use that div to put them together. The <div > uses flex-box to center its contents.

You can edit

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

to reposition h1 vertically.

* {
  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 >
    <div >
      <img src="C:\Users\\Documents\Washington State Project\Images\Seal_of_Washington.svg.png">
    </div>
    <!--Tabs-->
    <div >
      <ul>
        <li>Things to do</li>
        <li>History</li>
        <li>Education</li>
        <li>Sports</li>
      </ul>
    </div>
  </div>

  <!--Welcome Page-->
  <div >
    <div >
      <img src="C:\Users\Roger Garcia\Documents\Washington State Project\Images\1094997.jpg">
      <h1>Welcome to Washington State</h1>
    </div>
  </div>
</body>

</html>

CodePudding user response:

Like this:

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>

CodePudding user response:

you could try this :

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 >
    <div >
      <img src="C:\Users\\Documents\Washington State Project\Images\Seal_of_Washington.svg.png">
    </div>
    <!--Tabs-->
    <div >
      <ul>
        <li>Things to do</li>
        <li>History</li>
        <li>Education</li>
        <li>Sports</li>
      </ul>
    </div>
  </div>

  <!--Welcome Page-->
  <div >
    <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 : I use dummy image just to show you how its done.

  • Related