Sony
Sony

Reputation: 92

Button not going underneath header

I have been trying to make a game where the user has to press a button that redirects to the game page.

I am new to HTML/CSS and I want the button to go underneath a header, but the button goes to the side of the header instead.

I've tried using the <br> tag after the header, tried using position:fixed and specifying top and bottom heights (this worked, but for different screen sizes, the button shifted out of place), and I have tried changing display:block but nothing worked.

Here is my code:

h {
  color: rgb(0, 0, 0);
  text-shadow: 1px 2px 5px black;
  text-align: center;
  font-family: 'Times New Roman', Times, serif;
  font-size: 30pt;
}

div {
  text-align: center;
  height: 100%;
  width: 100%;
  display: flex;
  position: fixed;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}

.button {
  float: left;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.2), 0 2px 5px 0 rgba(0, 0, 0, 0.19);
  -webkit-transition-duration: 0.4s;
  /* Safari */
  transition-duration: 0.4s;
  background-color: #97d9f3;
  border-color: rgb(0, 131, 192);
  border-style: solid;
  color: black;
  padding: 15px 32px;
  display: block;
  font-size: 16px;
}

.button:hover {
  background-color: rgb(90, 190, 236);
}

body {
  text-align: center;
  height: 100%;
  width: 100%;
  display: flex;
  position: fixed;
  align-items: center;
  justify-content: center;
  background-color: #84ceeb;
  margin: 0;
  padding: 0;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  </div2>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>repl.it</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <h>
    Un Jeu de Veux Tu Danser
  </h>
  <button class="button" onclick="window.location.href='game.html'">Commencer</button>
  <script src="script.js"></script>
</body>

</html>
Have a great day, thanks for all your help!

Upvotes: 1

Views: 346

Answers (1)

Niloct
Niloct

Reputation: 10015

When you use flex-direction: column; on a flex-based parent element, the children each become rendered along the y-axis.

h {
  color: rgb(0, 0, 0);
  text-shadow: 1px 2px 5px black;
  text-align: center;
  font-family: 'Times New Roman', Times, serif;
  font-size: 30pt;
}

div {
  text-align: center;
  height: 100%;
  width: 100%;
  display: flex;
  position: fixed;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}

.button {
  float: left;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.2), 0 2px 5px 0 rgba(0, 0, 0, 0.19);
  -webkit-transition-duration: 0.4s;
  /* Safari */
  transition-duration: 0.4s;
  background-color: #97d9f3;
  border-color: rgb(0, 131, 192);
  border-style: solid;
  color: black;
  padding: 15px 32px;
  display: block;
  font-size: 16px;
}

.button:hover {
  background-color: rgb(90, 190, 236);
}

body {
  text-align: center;
  height: 100%;
  width: 100%;
  display: flex;
  position: fixed;
  align-items: center;
  justify-content: center;
  background-color: #84ceeb;
  margin: 0;
  padding: 0;
  flex-direction: column; /* added */ 
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  </div2>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>repl.it</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <h>
    Un Jeu de Veux Tu Danser
  </h>
  <button class="button" onclick="window.location.href='game.html'">Commencer</button>
  <script src="script.js"></script>
</body>

</html>

Upvotes: 1

Related Questions