1

I am trying to make a button that goes in the centre of the screen I have tried lots of solutions which havent worked html:

      <button align="center" type="button" onclick="location.href='https://discord.gg/AsA7P9B5Mv'" class="button">
      <span align="center" class="button__test"> Join Server </span>
      </button>

css:

  .button {
  text-align: center;
  align: center;
  justify-content: center;  
  align-items: center;  
  height: 10vw;
  width: 25vw;
  margin: 0;
  padding: 0;
  background: #10e321;
  border: none;
  outline: none;
  border-radius: 1vw;
  overflow: hidden;
  font-size: 4vw;
  cursor: pointer;
}
.button:hover{
  background: #11d120;
}
.button:active{
  background: #0eb31b;
}

Help would be greatly appreciated :)

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
Mushroom idiot
  • 39
  • 3
  • 10

3 Answers3

2

There is a couple of options to center element, e.g.

  1. CSS property position - always in the center of the screen
.button {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

}
  1. Flexbox - children element will be always in the center of the parent. You can make the parent full screen.
.parent-element {
    display: flex;
    justify-content: center;
    align-items: center;
}
Matthew
  • 369
  • 1
  • 12
1

You should use something like this:

<div style="text-align:center">  
    <input type="submit" />  
</div>  

Or you could use something like this. By giving the element a width and specifying auto for the left and right margins the element will center itself in its parent.

<input type="submit" style="width: 300px; margin: 0 auto;" />
jahantaila
  • 840
  • 5
  • 26
0

.class {
  display:flex;
  justify-content:center;
  align-items:center;
  height:100vh;
}
<div class="class">
  <button>html</button>
</div>
Abhishek Pakhare
  • 476
  • 1
  • 4
  • 15