Here you go:
*, *::before, *::after {
box-sizing: border-box;
font-family: Gotham Rounded, sans-serif;
font-weight: 400;
font-size: 16px;
}
body {
padding: 0;
margin: 0;
background: linear-gradient(to right, #00fffb, #25ff29);
}
#calculator-grid {
--_button-width: 70px;
--_gap-size: 8px;
display: grid;
justify-content: center;
align-content: center;
gap: var(--_gap-size);
grid-template-columns: repeat(4, var(--_button-width));
grid-template-rows: repeat(16, 1fr);
width: fit-content;
margin-inline: auto;
margin-block: 2rem;
}
#calculator-grid > button {
cursor: pointer;
border: 1px solid black;
outline: none;
font-size: 14pt;
}
#calculator-grid > button:hover {
background-color: rgb(191, 183, 183, .9);
}
#output {
grid-column: 1 / -1;
grid-row: 1 / 2;
height: 50px;
background-color: red;
}
.buttons {
background-color: rgb(227, 197, 197);
}
.buttons2 {
width: calc((var(--_button-width) * 3 + var(--_gap-size)) / 2);
font-weight: 600;
background-color: green;
}
#calculator-grid button:nth-of-type(1) {
grid-column: 1 / 2;
justify-self: start;
}
#calculator-grid button:nth-of-type(2) {
grid-column: 2 / 4;
justify-self: end;
}
#calculator-grid button:last-of-type {
grid-column: 3 / 5;
justify-self: stretch;
width: auto;
}
.myConsole {
background-color: black;
color: white;
min-height: 100px;
}
I've removed fix sizes and allowed the
grid
to set the sizing.
Notes:
* Elements are positioned by using
grid
syntax.
* The buttons will fit the grid cell.
* For the output, I have set it to the top row.
* The first 2 oversized buttons I have set to 1.5 of 3 cells (including the gaps) using
calc(..)
. So if you change the
--_button-width
or
--_gap-size
variables, the button will resize perfectly
* Lastly, for aesthetics, I've made the last button fit 2 cells. No calc required, simply tell it to stretch to fit.
Working example on CodePen:
Grid Formatting[
^]