Leverage Bootstrap 4 for achieving flexible layouts and centering content with two elements using flex and justify-content

By using the d-flex, justify-content-center, and flex-grow-1 classes, I was able to achieve my desired layout with just 3 divs.

<div class="row bg-primary d-flex justify-content-center">
  <div class="col-auto">
      LEFT
  </div>
  <div class="col-auto flex-grow-1 text-center">
      Middle
  </div>
  <div class="col-auto">
      Right
  </div>
</div>

https://i.stack.imgur.com/frVcQ.png

Now, I am trying to achieve a similar effect with only 2 divs. I want the middle one to be perfectly centered. However, removing the 'Right' element causes the center calculation to be off.

<div class="row bg-primary d-flex justify-content-center">
  <div class="col-auto">
      LEFT
  </div>
  <div class="col-auto flex-grow-1 text-center">
      Middle
  </div>
</div>

https://i.stack.imgur.com/UG2ud.png

Is there a way to make sure "Middle" is perfectly centered while allowing the Left or Right divs to expand as needed? Similar to having the positions of Left and Middle match the original image but without the 'Right' div.

Check out the Fiddle here: http://jsfiddle.net/4a7tLf0m/


I have found that I can achieve the same effect by specifying column sizes (col-3 instead of col-auto). However, I prefer not to be so explicit. Is this the only solution?

Answer №1

Make sure to conceal the left/right div when it is not needed. By doing this, the middle div will always maintain its position as if the other two divs were not present.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<div class="container">
  <div class="row bg-primary d-flex justify-content-center">
    <div class="col-auto">
      LEFT
    </div>
    <div class="col-auto flex-grow-1 text-center">
      Middle
    </div>
    <div class="col-auto">
      Right
    </div>
  </div>

  <hr/>

  <div class="row bg-primary d-flex justify-content-center">
    <div class="col-auto">
      LEFT
    </div>
    <div class="col-auto flex-grow-1 text-center">
      Middle
    </div>
    <div class="col-auto invisible">
      Right
    </div>
  </div>

  <hr/>

  <div class="row bg-primary d-flex justify-content-center">
    <div class="col-auto invisible">
      LEFT
    </div>
    <div class="col-auto flex-grow-1 text-center">
      Middle
    </div>
    <div class="col-auto">
      Right
    </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

Count the amount of exposed items in each row within a specific container size and dimensions

I created a code sample available at demo code to showcase li elements within a ul displayed in a flow-like manner with the use of display:inline. The challenge I am facing is determining the number of complete li items that are not wrapping for each row ...

What occurs when an image is unable to be rendered on the screen?

When an image cannot be displayed, what is the expected behavior of browsers? The information provided by official sources regarding this matter is intentionally vague. They mention that alt text should be utilized, but fail to clarify how it should be us ...

Unable to get spacing correct on loading page

My attempt at creating a loading page using CSS and HTML has hit a roadblock. I'm trying to show a loading bar that ranges from 0% to 100%. Despite my use of justify-content: space-between, I can't seem to get it right. I've searched through ...

Encountering a predicament when attempting to retrieve an image from an alternative

[index.jsp][5] style.css [problem][6] [folder hierarchy][7] Although everything runs smoothly when the file is located in the css directory, it fails to show any images if placed in the images folder. Kindly provide assistance. ...

Incorporating text sections into a div container and adjusting the width

Currently facing an issue with the canvas element on my project. <div id="app-container"> <div id="canvas-container"> <div id="canvas"></div> </div> </div> In the CSS stylesheet, the following styles ar ...

The issue of ExpressionChangedAfterItHasBeenCheckedError is a common problem faced by Angular

I have implemented a component loading and an interceptor to handle all requests in my application. The loading component is displayed on the screen until the request is completed. However, I am encountering an error whenever the component inside my router ...

Trouble with the functionality of the Bootstrap collapse button

I am facing an issue where clicking on the collapse button does not trigger any action. I have tested it on both of my laptops, ruling out any problems with the web browser. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

My webpage is experiencing issues with function calls not functioning as expected

I have created a select menu that is integrated with the Google Font API. To demonstrate how it works, I have set up a working version on JSBIN which you can view here. However, when I tried to replicate the code in an HTML page, I encountered some issues ...

Remove the concluding statement from the HTML string if it exceeds the capacity of the iPhone's webView

I have an array of content with certain sections in bold. The bold parts can be located anywhere within the text, so I am utilizing a webView to display an HTML string with appropriate bold tags. However, I am encountering issues where the webViews do not ...

What are the steps to storing numerical inputs as variables using jQuery and then using them to perform calculations in order to update the input numbers

Currently, I am in the process of creating a BMI calculator using HTML and jQuery. <form id="bmiCalculator"> <fieldset> <legend>BMI Calculator:</legend> <p> <label>Height: ...

What is causing my 100% height to be excessively large?

I'm facing an issue with my background containers, "background-overlay" and "background-gradient", as they are extending the height of the document beyond the actual content displayed. I am unsure of what could be causing this problem. It might be som ...

Organizing DIVs upon website initialization

My website features a layout with three columns: <div id="column1"></div> <div id="column2"></div> <div id="column3"></div> I currently have 3 divs on the webpage: <div id="1">aaa</div> <div id="2">b ...

Utilizing multiple address parameters within a single-page application

Apologies for the lengthy post. I have encountered an issue with my code after running it through a bot that I can't seem to resolve. In my code, I aim to create functionality where you can visit the address #two, followed by two separate parameters ...

Issues with PHP not retrieving selected radio button values

Currently, I am using a PHP webpage to generate a list of all files with a .264 extension in a specific folder. Once the file is selected, it is then sent to a command for video playback on a display connected to the computer. I have encountered some diff ...

What is the best way to personalize Material UI elements, such as getting rid of the blue outline on the Select component?

Embarking on my journey of developing a React app, I made the decision to incorporate Material UI for its array of pre-built components. However, delving into the customization of these components and their styles has proven to be quite challenging for me ...

Implement a responsive CSS division that simulates a two-column table

In my layout, I have three row divs: header, content, and footer. My goal is to create a table-like format within the content div to display information. Here's an example: .itemwrapper{width:600px;} .picture{width:150px;float:left;} .item{width:45 ...

Issue with CSS 3 gradient compatibility in Internet Explorer 8

My CSS3 gradient button is working perfectly on all browsers except for IE8. To make it work on IE8, I am utilizing CSS3 Pie. You can see the search button in action by visiting: Upon inspecting my console, I noticed an error on this line: PIE.htc, lin ...

The jQuery pop-up fails to activate on the initial click

I have multiple "Buy Now" buttons for different products. If the button is labeled as "sold-out," it should not do anything, but if it's available, it should trigger a jQuery Magnific Popup. Currently, the popup only opens after the second click becau ...

Solution to trigger CSS :hover to refresh following a transition (such as expanding a menu)

While there are existing discussions on this topic, I am presenting my query for two specific reasons: It introduces a potential alternative solution The demo code could be helpful to individuals looking to emulate a menu Following a CSS transition, the ...

Creating a default homepage across various HTML files using Google Apps Script and deploying it as a WebApp

Is there a way to set a default main page on multiple HTML and GS files in Google AppScript? I plan to publish it as a Web App. Have you attempted using the doGet function? ...