Align elements to the left and right using Flexbox

I am having trouble arranging a list of buttons horizontally. My goal is to have each row display two buttons - one on the left side and one on the right side. Occasionally, the left button needs to be hidden using display: none, but I still want the right button to remain on the right-hand side of the row.

Although I am using Bootstrap, I couldn't find a suitable style for this layout. As a result, I tried creating my own style. The issue I'm facing is that when the left button is hidden, the right button takes its place and appears at the far left instead of staying on the right side. I attempted setting order: 0 for the left button and order: 1 for the right button, but it didn't solve the problem.

.align-horizontal {
    display: flex;
    flex-direction: row;
    justify-content: space-between; 
}

Here is an example of the HTML structure:

<tr>
  <td>

   <div class='align-horizontal'>
     <button>left</button>
     <button style='display: none'>right</button>
   </div>
  </td>
</tr>
<tr>
  <td>
   <div class='align-horizontal'>
     <button>left</button>
     <button>right</button>
   </div>
  </td>
</tr>
<tr>
  <td>
   <div class='align-horizontal'>
     <!-- This is where the issue occurs as the right button moves to the left -->
     <button style='display: none'>left</button>
     <button>right</button>
   </div>
  </td>
</tr>

If you have any suggestions or solutions on how I can ensure the right button always stays at the far right regardless of whether the left button is hidden or not, please let me know!

Answer №1

To achieve the desired effect with Flexbox, use justify-content:between when both elements are displayed. If one element must be hidden, consider using visibility:hidden instead of display:none...

<tr>
  <td>
   <div class='align-horizontal d-flex w-100 justify-content-between'>
     <button class="invisible">left</button>
     <button>right</button>
   </div>
  </td>
</tr>

If you still need to use display:none, you can apply left auto-margins (ml-auto) on the right button to shift it to the right.

<tr>
  <td>
   <div class='align-horizontal d-flex justify-content-between'>
     <button class="d-none">left</button>
     <button class="ml-auto">right</button>
   </div>
  </td>
</tr>

Check this link for a live example:

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

Display or conceal content based on checkbox status

i am trying to create a system where content is hidden by default and only displayed when a checkbox is checked. The goal is to show the content when the checkbox is checked and hide it when unchecked, so that other content can be shown as well upon checki ...

What is the method for implementing a distinct background in dark mode while utilizing Material UI Themes?

I am facing an issue with changing the background color when in dark mode. Here is my initial code snippet... export const myThemeOptions: ThemeOptions = { palette: { mode: "light" as PaletteMode, primary: { main: ...

What is preventing me from positioning my image on the left side of my Bootstrap website?

I've experimented with various classes like ml-auto, float: left, float-left, and fixed-left, but none have successfully aligned my image to the left as desired. I'm hesitant to use margin adjustments as they are not always responsive across diff ...

I am facing difficulty importing emotion js style using dynamic variables

I recently designed a webpage that has the following appearance: https://i.stack.imgur.com/AnIXl.jpg Here is the code from my _app.tsx file: import '../styles/globals.css' import type { AppProps } from 'next/app' import { createTheme ...

Struggling to Mask Password Input with Dots in HTML?

I am currently developing a User Registration System and I would like the password field to display as dots for security purposes. However, every time the page loads, the Email and Password fields are already filled in: Email: root Password: password ( ...

How can I load a URL without causing a page refresh?

While searching around, I stumbled upon this HTML 5 code snippet: window.history.pushState("object or string", "Title", "/new-url"); I started thinking: how can this be implemented? Is there a way to utilize this code to change the URL to without needin ...

Flexbox that adjusts to any browser height, ensuring full responsiveness

I am exploring the world of flexbox with the goal of creating a responsive layout consisting of 3 items that adjust seamlessly to different devices such as smartphones, tablets, and desktop computers. The layout should stack the items on top of each other ...

Page-header bootstrap class is malfunctioning

I just started learning bootstrap and currently I'm working with bootstrap v5.2.0. I attempted to create a header with a line using the class="page-header" but the header line is not showing up. Can someone please clarify why? <!doctype html> ...

"By clicking on the image, you can trigger the transfer of data to the view.py file

Check out my image in two.html: https://i.sstatic.net/FhKlz.png <td> <img src="http://127.0.0.1:8000/imageAll/101.png" width="100"; height="300"> </td> I need to pass the data value of 101 to my view.py wh ...

Tips on creating a dropdown menu with a fontawesome icon

I'm brand new to coding and struggling to create a drop-down menu similar to the one used in fonawsome fa fa-ellipsis-v While I'm attempting to code it here, I'm not sure why the class is being overridden. *{ margin: 0; padding : ...

display a loading spinner for number input fields in HTML5

In my HTML5 project, I am currently utilizing a numeric control (input type="number"). The default behavior displays the spinner (up and down arrows) only on hover. Is there a way to make the spinner permanently visible using CSS or another method? ...

Transforming a React Native application into a HTML5/Progressive Web App (P

Is there a way to convert a React Native app into a website format without the need to create a whole new frontend using HTML5 or PWA? Has anyone attempted this before or knows the process to do it? ...

String Replacement in JavaScript

Here's the scenario: I have a string var str="abc test test abc"; Is there a way to specifically replace the second occurrence of "abc" in the string using the str.replace() method? ...

AngularJS - Show Banner after two rows, then show two more rows

I'm attempting to showcase a Banner <div> after 2 rows of 4 x "col-md-3", succeeded by another 2 Rows - thus, resulting in the following markup: <div class="col-md-3">1</div> <div class="col-md-3">2</div> <div clas ...

Guide on integrating CSS into a Vue Bootstrap table design

I am having trouble adding a CSS to my bootstrap vue table template. <template slot="actions" slot-scope="data" :style="{min-width:'150px'}"> <b-button variant="info" @click="showViewModel(data.item)"> <i class="fa fa-e ...

How can I adjust iframe content to fit properly on a mobile device screen?

I am facing a challenge where I need to embed an iframe into an external website over which I have no control. The only thing I can specify is the size of the iframe on my own website. My goal is to ensure that the content within the iframe adjusts to fit ...

Sliding in the wrong direction on Bootstrap 5.2 carousel

I'm currently working on a carousel project. However, I encountered an issue where the images slide down initially and then jump up to the correct level as depicted in the image below. I've tried adjusting the height, width, and auto settings to ...

pop-up window that shows chosen choices using javascript or ajax

I have a specific HTML code that allows users to select multiple options. I would like these selected options to be displayed in a popup or a div in real-time as the user makes their selections. I have attempted using a selectbox with checkboxes, but extra ...

When using CSS text-indent on an input field, the caret position does not update until you begin typing in the field

When I apply the text-indent property in CSS, my expectation is that when the focus is on the text input area, the text cursor icon/caret will appear indented. However, it seems like the indentation does not take effect until you type the first character. ...

tips for accessing data attributes in ajax request

I am struggling to retrieve data-id from an anchor tag when clicked using AJAX, but it keeps returning undefined. Below is my code: $image_html .= '<a class="float-left " onclick="modal()" data-toggle="modal" data-targ ...