I am trying to get 2 button in the middle of my screen. However these buttons are overlapping with the code that I have now. I actually don't use that much CSS, so this is probably a newbie question.
This is my html:
.wrapper {
text-align: center;
}
.button {
position: absolute;
top: 50%;
}
.button1 {
background-color: Transparent;
background-repeat:no-repeat;
padding:20px;
color: white;
border: 3px solid #fff;
}
.button1:hover{
border: 3px solid #000;
}
<div id="particles-js"></div>
<div class="wrapper">
<button class="button button1">button</button>
<button class="button button1">button2</button>
</div>
I have no idea on how to align these buttons to the middle and leave a little space in between the buttons. If someone can help me, I'd really appreciate that!
Using
text-align: center
to horizontally center nested elementsYou can achieve this by declaring
text-align: center
on the containing parent element (.wrapper
) and declaringdisplay: inline-block
on nested children elements.position: absolute
must be removed since we want to keep elements in the natural document flow - in order for this method to work, elements must be eitherstatic
or positionedrelative
.Note: for horizontal alignment, using
text-align: center
to work as expected, a few requirements should be met:display: block
)display: inline-block
)float
rules should be declared on the nested elements you are intending to horizontally align,float
rules will negate any effort to align elements in this wayOr... you could Just use Flex...
Using
flex-box
to horizontally center nested elementsBy declaring
display: flex
on the containing parent element (.wrapper
), thenjustify-content: center
to specify the horizontal alignment, you get the intended result.flex-box
leverages the browser's built-in power to do all the "heavy lifting" and calculations for precision alignment - which, incidentally, also makes it a popular responsive solution.Heads up!
flex-box
has poor or limited support for legacy browsers, so if this is going to be a concern for you, it's probably better not to use it in production.IE <= 9 - Not Supported
IE 10,11 - Partial Support
See more: https://caniuse.com/#feat=flexbox
Edit
Vertical & Horizontal Alignment using
flex-box
&position: absolute
If you remove absolute position from button that it automatically center
Updated Answer using your screen short. For that just wrap the both div in one parent div and set position relative to it. and add position absolute in
.wrapper
.