I’m trying to put two or more buttons in a column, I tried inline, inline-block, block, but none worked. I need one button at the top and the others below, not in a row, not in the first line.
<!DOCTYPE html>
<html>
<head>
<style>
.item1 {
grid-area: header;
}
.item2 {
grid-area: menu;
}
.item3 {
grid-area: main;
}
.item4 {
grid-area: right;
}
.item5 {
grid-area: footer;
}
.grid-container {
display: grid;
grid-template-areas: 'header header header header header header' 'menu main main main right right' 'menu footer footer footer footer footer';
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container>div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.item2 {
display: block;
}
</style>
</head>
<body>
<h1>Grid Layout</h1>
<div class="grid-container">
<div class="item1">Title</div>
<div class="item2">
<button>Hello</button>
<button>Bye-Bye</button>
</div>
<div class="item3">Main</div>
<div class="item4">Right</div>
<div class="item5">Footer</div>
</div>
</body>
</html>
I try how to do this and I failing. Maybe you can help me with this. Stackexchange is telling me that most of my question is code and I don’t know how to fix it.
2
Answers
According to code provided above, to get the required result of putting buttons vertically, the CSS rule "display: block" should be applied to them as follows:
You can put two or more buttons in a column using CSS to use the flex property. The flex property is a shorthand for
flex-grow
,flex-shrink
, andflex-basis
, which control how the items grow, shrink, and take up space in a flex container. By setting the flex property to1
0
auto
, you can make the buttons take up the available space in the column and wrap to the next line if needed.To use the flex property, you need to make the parent element a flex container by setting its display property to flex. You also need to set its
flex-direction
property tocolumn
to make the items stack vertically. You can also use thealign-items
property tocenter
the items horizontally.PS::
The
flex: 5 5 auto
property means:flex-grow
value is5
, which means the flex item will grow five times faster than other flex items with lowerflex-grow
values when there is extra space in the flex container.flex-shrink
value is 5, which means the flex item will shrink five times faster than other flex items with lowerflex-shrink
values when there is not enough space in the flex container.flex-basis
value is auto, which means the flex item will use its width (in horizontal writing mode) or height (in vertical writing mode) as the preferred size unless it has a content value.Here is a sample code snippet that illustrates the effect of
flex: 5 5 auto
:As you can see, the first flex item takes up more space than the second flex item, because it has a higher flex-grow value. If you resize the browser window, you will also notice that the first flex item shrinks more than the second flex item, because it has a higher flex-shrink value.