I want to style these 4 buttons into a cross form like a D-Pad. I tried it and it somehow didn’t look like I expected it to do. I’m a bit new to the whole html and css stuff.
For context I am trying to build a 2048 game as a webapp and I thought I would do a D-pad for it to work on mobile.
div#btn-container {
width: 400px;
height: 400px;
align-items: center;
display: flex;
justify-content: center;
margin-top: 600px;
margin-left: auto;
margin-right: auto;
background-color: black;
}
button#left {
width: 100px;
height: 100px;
margin-left: auto;
margin-right: auto;
margin-top: 200px;
}
button#up {
width: 100px;
height: 100px;
margin-right: auto;
margin-left: auto;
}
button#down {
width: 100px;
height: 100px;
margin-left: auto;
margin-right: auto;
margin-top: 400px;
}
button#right {
width: 100px;
height: 100px;
margin-left: auto;
margin-right: auto;
margin-top: 200px;
}
<div id="btn-container">
<button id="left"></button>
<button id="up"></button>
<button id="down"></button>
<button id="right"></button>
</div>
2
Answers
As @Justinas said, you’ll probably want to use CSS grid to achieve this styling. Here is an example to get you started. Notice specifically that a 3×3 grid was defined, and then
grid-xxx-start
was used to put each button in the appropriate location.Layout
For a flexbox (FBL) approach it is easiest to create a main FBL container containing three FBL child containers for the buttons.
justify-content: space-between
Sizing
rem
:calc(20.834vmin + 1.25rem)
calc(0.556vmin + 0.625rem)
Snippet