I'm trying to get the buttons on my webpage to be side by side in the middle of the page, these are the 2 bootstrap 5 classes I've attempted and they aren't as I want them to look:
For reference, some questions will have answers with multiple buttons (up to 10: ), I would also like them to be equally spaced in a row, something like this:
I'm not sure if this changes anything but the buttons are added using a for loop in javascript.
Any tips would be much appreciated, I've been quite stuck on this
I'm trying to get the buttons on my webpage to be side by side in the middle of the page, these are the 2 bootstrap 5 classes I've attempted and they aren't as I want them to look:
- https://gyazo./c23f2eade4614380aec547b11e61387a
- https://gyazo./e40a678b02c9f641f746b1cfbe83d03f
For reference, some questions will have answers with multiple buttons (up to 10: https://gyazo./15d810f8c0f79f23d12463db9ba50e2a), I would also like them to be equally spaced in a row, something like this: https://gyazo./ca1ab61aa7fef54f1cf1a099cb56a5e0
I'm not sure if this changes anything but the buttons are added using a for loop in javascript.
Any tips would be much appreciated, I've been quite stuck on this
Share Improve this question edited Dec 5, 2021 at 16:44 Maik Lowrey 17.6k7 gold badges53 silver badges98 bronze badges asked Dec 5, 2021 at 11:17 Tester321Tester321 1432 gold badges2 silver badges9 bronze badges 07 Answers
Reset to default 7I have success with class="btn-group" Exemple:
<div class="btn-group">
<button type="button" class="btn btn-primary">Apple</button>
<button type="button" class="btn btn-primary">Samsung</button>
<button type="button" class="btn btn-primary">Sony</button>
</div>
View more detail in: https://www.w3schools./bootstrap/bootstrap_button_groups.asp
You could wrap the buttons in a flex
, and then control the width of your flex-div using custom css, or Bootstrap Grid System. JSFiddle
<div class="row">
<div class="col-6 mx-auto">
<div class="d-flex align-items-center justify-content-evenly">
<button class="btn btn-primary">Button 1</button>
<button class="btn btn-primary">Button 2</button>
</div>
</div>
</div>
Use the flex
CSS in the example, the counter
is just for numbering the buttons, it's not required.
.center {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
counter-reset: button;
}
.btn::before {
counter-increment: button;
content: counter(button);
}
<link href="https://maxcdn.bootstrapcdn./bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<main class='container'>
<section class='row'>
<div class='col center'>
<button class='btn btn-primary btn-lg'></button>
<button class='btn btn-primary btn-lg'></button>
<button class='btn btn-primary btn-lg'></button>
<button class='btn btn-primary btn-lg'></button>
<button class='btn btn-primary btn-lg'></button>
</div>
</section>
Add a element that fill have all the buttons, in your css folder, style that div with these styles:
{
display:flex; align-item:center;
justify-content: space-around;
}
If they are many buttons, let the display: flex;
, to be display: inline-flex;
.
You dont need any classes by default they will align themselfs in a row and we can use text-center on the container to make them centered horizontally
<script src="https://unpkg./@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container text-center">
<button class="btn btn-primary" type="submit">Button</button>
<button class="btn btn-primary" type="submit">Button</button>
<button class="btn btn-primary" type="submit">Button</button>
</div>
You dont need bootstrap for that. You can easily do alignment using flex layout. Bootstrap can be used for only styling elements.
Read More: a-guide-to-flexbox
.container{
margin-top:20px;
display: flex;
}
.container{
display: flex;
}
.cl-1{
justify-content: center;
}
.cl-1 button{
margin-right: 20px;
}
.cl-2{
justify-content: space-between;
}
.cl-3{
justify-content: space-evenly;
}
.cl-4{
justify-content: space-around;
}
<link href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<h2>Center align</h2>
<div class="container text-center cl-1">
<button class="btn btn-primary" type="submit">Cancel</button>
<button class="btn btn-primary" type="submit">Submit</button>
</div>
<h2>space-beetween</h2>
<div class="container text-center cl-2">
<button class="btn btn-primary" type="submit">Cancel</button>
<button class="btn btn-primary" type="submit">Submit</button>
</div>
<h2>space-evenly</h2>
<div class="container text-center cl-3">
<button class="btn btn-primary" type="submit">Cancel</button>
<button class="btn btn-primary" type="submit">Submit</button>
</div>
<h2>space-around</h2>
<div class="container text-center cl-4">
<button class="btn btn-primary" type="submit">Cancel</button>
<button class="btn btn-primary" type="submit">Submit</button>
</div>
If you want a row of buttons, but with a small gap in between them - unlike what you get with .btn-group
class, which hugs the buttons together - then you can use d-flex
and gap-[number]
classes in a surrounding div.
<div class="d-flex gap-2">
<button class="btn btn-primary" type="button">Button 1</button>
<button class="btn btn-primary" type="button">Button 2</button>
<button class="btn btn-primary" type="button">Button 3</button>
</div>
Or for a more 'responsive' variation you can use d-grid
, gap-[number]
and d-md-block
classes, again in a surrounding div:
<div class="d-grid gap-2 d-md-block">
<button class="btn btn-primary" type="button">Button 1</button>
<button class="btn btn-primary" type="button">Button 2</button>
<button class="btn btn-primary" type="button">Button 3</button>
</div>
As explained in the Bootstrap docs:
Here we create a responsive variation, starting with vertically stacked buttons until the md breakpoint, where
.d-md-block
replaces the.d-grid
class, thus nullifying thegap-2
utility. Resize your browser to see them change.
Obviously can change the breakpoint from md to something else, e.g. xs or sm.
Both of these approaches work in Bootstrap v5 or later.