0

How can I put the h1 with the background to the center? I searched for it but I think because I'm too new at coding, I couldn't find where to find the mistake here.

.main-header, h1{
  background-color: black;
  color: white;
text-align:center;
position: relative;
background-size: 100px;
display: inline;
margin: auto
width: auto;
}
body{
  background-color: purple;
  margin: 0;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Lei's Gym Wiki</title>
  <link rel="stylesheet" href="css/styles.css">
</head>

<body>

  <div class="main-header">
    <h1>Welcome To Leindras's Gym Wiki</h1>


  </div>

</body>

</html>
Leindras
  • 1
  • 2

2 Answers2

1

All you need is text-align: center; for the .main-header. Probably you over loaded it with styles.

.main-header {
  text-align: center;
}

h1 {
  background-color: black;
  color: white;
  text-align: center;
  position: relative;
  background-size: 100px;
  display: inline;
  margin: auto;
  width: auto;
}

body {
  background-color: purple;
  margin: 0;
}
<div class="main-header">
  <h1>Welcome To Leindras's Gym Wiki</h1>
</div>
IT goldman
  • 14,885
  • 2
  • 14
  • 28
1

Reduce/delete unnecessary CSS settings and use seperate rules for the .main-header element and the h1 inside it:

.main-header {
  text-align: center;
}

.main-header>h1 {
  background-color: black;
  color: white;
  display: inline;
}

body {
  background-color: purple;
  margin: 0;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Lei's Gym Wiki</title>
  <link rel="stylesheet" href="css/styles.css">
</head>

<body>

  <div class="main-header">
    <h1>Welcome To Leindras's Gym Wiki</h1>


  </div>

</body>

</html>
Johannes
  • 64,305
  • 18
  • 73
  • 130