Utilizing CSS flex to perfectly align buttons

Struggling to master the art of aligning buttons using flex.

On desktop, everything looks perfect:

https://i.sstatic.net/vpdXn.png

Even in portrait mode on mobile:

https://i.sstatic.net/jAAAk.png

But when it comes to landscape view, things get a little messy:

https://i.sstatic.net/iShn3.png

I wish there was a way for them to look more aligned.

Here's the code snippet I'm using for this particular section:

.row {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

.col {
  -ms-flex-preferred-size: 0;
  flex-basis: 0;
  -ms-flex-positive: 1;
  flex-grow: 1;
}
<div>
  <div class="row">
    <div class="btnWrap col" style="">
      <button type="button" id="TopNames" class="btn btn-secondary btn-top active" onclick="topButtonClick(this)">Most Common Names</button>
    </div>
    <div class="btnWrap col" style="">
      <button type="button" id="TopBabyNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Newborn Names</button>
    </div>
    <div class="btnWrap col" style="">
      <button type="button" id="TopDisappearingNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Disappearing Names</button>
    </div>
    <div class="btnWrap col" style="">
      <button type="button" id="TopModernNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Modern Names</button>
    </div>
    <div class="btnWrap col" style="">
      <button type="button" id="TopLastnames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Common Last Names</button>
    </div>
  </div>
</div>

Answer №1

Here is an example of how your HTML should look:

<div class="button-wrapper">
   <button>Name</button>
   <button>Name</button>
   <button>Name</button>
   <button>Name</button>
   <button>Name</button>
</div>

This is the CSS for desktop display:

.button-wrapper{
  display:flex;
  align-items:center;
  justify-content: space-between;
  flex-direction: row;
 }

And here is the CSS for mobile display:

.button-wrapper{
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
 }

Answer №2

Avoid including additional CSS properties and instead utilize bootstrap classes for styling.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<div>
    <div class="row justify-content-center text-center">
        <div class="btnWrap col-12 col-md-6 col-lg" style="">
            <button type="button" id="TopNames" class="btn btn-secondary btn-top active" onclick="topButtonClick(this)">Most Common Names</button>
        </div>
        <div class="btnWrap col-12 col-md-6 col-lg" style="">
            <button type="button" id="TopBabyNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Newborn Names</button>
        </div>
        <div class="btnWrap col-12 col-md-4 col-lg" style="">
            <button type="button" id="TopDisappearingNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Vanishing Names</button>
        </div>
        <div class="btnWrap col-12 col-md-4 col-lg" style="">
            <button type="button" id="TopModernNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Modern Names</button>
        </div>
        <div class="btnWrap col-12 col-md-4 col-lg" style="">
            <button type="button" id="TopLastnames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Most Popular Last Names</button>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

Answer №3

Kindly review the snippet below and incorporate bootstrap classes to achieve the desired outcome

.btnWrap{
margin-top:10px
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="container-fluid">
    <div class="row justify-content-center text-center">
        <div class="btnWrap col-12 col-lg" >
            <button type="button" id="TopNames" class="btn btn-secondary btn-top active" onclick="topButtonClick(this)">Most Popular Names</button>
        </div>
        <div class="btnWrap col-12 col-lg" >
            <button type="button" id="TopBabyNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Baby Names</button>
        </div>
        <div class="btnWrap col-12 col-lg" >
            <button type="button" id="TopDisappearingNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Disappearing Names</button>
        </div>
        <div class="btnWrap col-12 col-lg" >
            <button type="button" id="TopModernNames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Modern Names</button>
        </div>
        <div class="btnWrap col-12 col-lg" >
            <button type="button" id="TopLastnames" class="btn btn-secondary btn-top" onclick="topButtonClick(this)">Most Common Last Names</button>
        </div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Animations in Angular fail to operate within mat-tabs after switching back when custom components are being utilized

I am facing a similar issue as Olafvv with my angular 16 project. The problem occurs in a mat-tab-group where the :enter animations do not work when navigating away from a tab and then returning to it. However, in my case, the issue lies within a custom su ...

Using the css function in JQuery may not always yield the desired results

These are the snippets of code I am working with: var topPix = $('#cc').css('top'); var leftPix = $('#cc').css('left'); $('#testFrame').css('top', topPix).css('left', leftPix); After ...

Can you please explain what shape-id denotes and provide guidance on locating it within a CSS file?

I have a question that may seem trivial to experienced web developers - what exactly is "shape-id"? For example, in the code <hr shape-id="2">. I'm currently attempting to change the color of the horizontal lines (hr) on my website, but I can& ...

What might be the reason behind the selection value not changing in the dropdown menu?

I have two buttons, one for adding an employee and one for editing. Submit Record $("#addNewEntry").click(function(){ departmentName = $("#selectEmployeeDepartment option:selected").text(); locate = $("#selectLocation o ...

Updating Select Options with Multiple Values using Javascript

My goal is to update the selected value of multiple select elements simultaneously using JavaScript. However, I am facing an issue where my script only updates one select element instead of all of them on the page. Unfortunately, I cannot modify the id att ...

Concealing the primary div within a Vue child component

Is there a way to conceal the primary div within a Vue application created using Vue-CLI? I attempted adding the display property, but it did not solve the problem. I am attempting to hide it within my Channels component. Here is how my main component lo ...

Is there a way to change the text (price) when I select an option?

<div class="single-pro-details"> <!--Customize in the CSS--> <h6>Home / Beats</h6> <h4>Unique Lil Tecca Type Beat - New Love</h4> <h2 id="price">$ ...

Ensure that columns are neatly arranged horizontally when they do not all fit in one row

I currently have a row with 6 columns, but when they can't fit next to each other on the screen, 2 of them get pushed below, resulting in a messy layout. I could use a media query to solve this issue, however, I am curious if there is a better way to ...

tips for choosing a specific dropdown menu item

I'm having an issue with a function that I want to apply to all my dropdown lists. The problem arises when I try to select an option from the second dropdown list - instead of displaying the correct value, it shows the value from the first group in th ...

Alter the color of textbox text using JavaScript

I have a text input field. When clicked, I want to change the text color style using JavaScript. Previously, I successfully achieved clearing the inner content of the textbox when clicked and reverting to the default version on blur. This code is currently ...

Each row will have two carousels

Having some trouble with Bootstrap 4 and trying to display two carousels next to each other on a laptop screen (col-md-6) and full width on mobile (col-sm-12). Can't seem to get it working, maybe I'm targeting the wrong elements? <div id="col ...

Utilizing jQuery to expand a collapsible section when another checkbox's event is triggered

For more details, please visit: https://jsfiddle.net/3msLwfu6/1/ The first tab is collapsible and contains nested collapsibles called "Items". Each "Item" can be collapsed or expanded using a checkbox on its header that utilizes bootstrap controls. Add ...

Can HTML be integrated into QwebKit using JavaScript?

Is it necessary to load JavaScript objects into QWebkit during the application loading process? I find using setHtml with formatted HTML and JavaScript strings difficult to maintain. So my question is, can I embed the HTML and JavaScript as resources int ...

ISP Blocks Flash Access

My innovative set of flash games has been adopted by numerous schools throughout the UK. However, I recently encountered an issue where one school was unable to load these Flash games... After reaching out to the school's Internet Service Provider (S ...

Ways to prevent onMouseDown from triggering when the element is exclusively clicked?

I'm currently working on developing a game where units (svg elements) are controlled using React. In this game, the units should be draggable, and clicking on a unit should open a form. However, when I click on the unit, only the mousedown event is t ...

Tips for streaming wma files in html (windows media files)

Is there a way to stream Windows Media files in HTML without downloading the file? I have tried using HTML audio and embedded tags, but WMA files are not supported. <audio controls="mycontrol" height="50" width="50"> <source src="music.mp3" type ...

Integrate Arduino's capabilities with Johnny-Five within an HTML JavaScript document

As a beginner in the world of JavaScript, I set out to embark on a project involving turning on a connected LED on an Arduino board by simply pressing a button on an HTML page. After doing some research, I came across a JavaScript library called "johnny- ...

Retrieve the chosen item from the autocomplete feature

Unfortunately, I have encountered an issue with this solution as I was unable to get it to work correctly in my code. The objective is to retrieve the selected item and then call another function to utilize it. The script is as follows: <script type=" ...

Ways to modify the data within a column for every row in a table without relying on props in Vue.js (specifically Element-ui)

I've been stuck on this issue for quite some time now. I've created a table with 3 columns, and for the first two columns, I can easily display the contents of each row using the prop property. However, for the third column, I need to display inf ...

Using JQuery to locate radio buttons that have been selected based on a variable

In my JavaScript code, I have created a variable called "thisRadio" to represent a dynamically generated div containing multiple radio buttons. Currently, one of these radio buttons may be checked and my goal is to find the checked radio button within this ...