Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

centering vertically and horizontally several buttons css

I wanted to center both vertically and horizontally my buttons so I got inspired by this tutorial. However, I think, because of position: fixed; my three buttons are on top of each other, and I would like them to be next to each other (but still centered horizontally). I sort of guess why this wouldn’t work (because I place them all at the same place) but don’t really know how to fix this. Adding display: inline; didn’t help.

I also saw this stackoverflow post so I tried a second .css but this still didn’t work (this time the buttons were on the top left, but not on top of each other).

My HTML code is the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<body>
    <button name="button1" class="buttonsWelcome" type="submit" value="HaploSFHI">1</button>
    <button name="button2" class="buttonsWelcome" type="submit" value="tool2">2</button>
    <button name="button3" class="buttonsWelcome" type="submit" value="tool3">3</button>
</body>

and my first css:

.buttonsWelcome {
    width: 200px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: inline;
    position: fixed;
}

and my second css:

.buttonsWelcome {
    width: 200px;
    vertical-align: middle;
    text-align:center;
    display: inline-block;
}

>Solution :

Just try this!

.container{   
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
.buttonsWelcome {
    width: 50px;
}
<div class="container">
    <button name="button1" class="buttonsWelcome" type="submit" value="HaploSFHI">1</button>
    <button name="button2" class="buttonsWelcome" type="submit" value="tool2">2</button>
    <button name="button3" class="buttonsWelcome" type="submit" value="tool3">3</button>
</div>

If you want you can also change the .container to body in CSS and remove the above div in HTML code but it is good to use a separate block to add them otherwise all of your code will be centered when you use it with your body tag.

Moreover, there are a lot of ways to do this and this is only a one method of them. Look at this W3School CSS Layout page if you want to know more about how to align items to center

Thanks and best regards!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading