Aligning dynamic width columns in Bootstrap 4 with Internet Explorer

I'm facing an issue with Bootstrap 4 columns in a row, where I have variable widths on the columns causing slight discrepancies in how they are displayed. While Chrome handles this well, Internet Explorer seems to align the last two columns based on "n times min-width" rather than their actual max-width size, resulting in misalignment. I've tried using margin-left: auto and margin-right: auto to center the boxes, but it ends up pushing the last box outside the row entirely. Splitting the columns into separate rows isn't an option due to varying screen widths displaying different numbers of items in each row. Is there a CSS solution to properly center these columns in Internet Explorer?

* {
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 1px dotted yellow;
}

.row {
  width: 100%;
  border: 1px dotted red;
  justify-content: center;
  display: flex;
  flex-wrap: wrap;
}

.col {
  min-width: 25%;
  max-width: 33.3%;
  border: 1px dotted blue;
  height: 100px;
  flex-basis: 0;
  flex-grow: 1;
  margin-bottom: 5px;
}
<div class="container">
  <div class="row">
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
  </div>
</div>

Using fixed widths for the columns works fine, but requires manual adjustment for affected columns or post-calculation with JavaScript. This is not ideal. I haven't found a CSS selector that targets columns without a full width, except by precalculating a modulo value and storing it in a data attribute, which isn't satisfactory either. I'm hoping for a pure CSS solution within the existing structure.

* {
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 1px dotted yellow;
}

.row {
  width: 100%;
  border: 1px dotted red;
  justify-content: center;
  display: flex;
  flex-wrap: wrap;
}

.col {
  min-width: 25%;
  max-width: 33.3%;
  border: 1px dotted blue;
  height: 100px;
  flex-basis: 0;
  flex-grow: 1;
  margin-bottom: 5px;
}

.col.bigger {
  min-width: 33.3%;
}
<div class="container">
  <div class="row">
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col bigger"></div>
    <div class="col bigger"></div>
  </div>
</div>

Answer №1

After exploring various options, I resorted to implementing a small snippet of javascript to address this particular issue as it appeared that resolving it solely with css was not feasible. The workaround involved configuring the min-width and max-width properties to identical values, enabling Internet Explorer 11 to correctly position the elements once more. This approach can be easily replicated for different layouts without significant difficulty, and the resultant css code remains largely uncomplicated even when applying fixes for varying widths.

(($) => {
  $(() => {
    const cols = $('.col');

    const count = cols.length;
    const offset = Math.floor(count % 4);

    for (let i = count - offset; i < count; i++) {
      cols.eq(i).addClass('bigger');
    }
  });
})(jQuery);
* {
  box-sizing: border-box;
}

.container {
  width: 100%;
  border: 1px dotted yellow;
}

.row {
  width: 100%;
  border: 1px dotted red;
  justify-content: center;
  display: flex;
  flex-wrap: wrap;
}

.col {
  min-width: 25%;
  max-width: 33.3%;
  border: 1px dotted blue;
  height: 100px;
  flex-basis: 0;
  flex-grow: 1;
  margin-bottom: 5px;
}

.col.bigger {
  min-width: 33.3%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></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

Issue with iOS iframe scrolling - unable to scroll as expected

On iOS devices like the iPad and iPhone 6, scrolling doesn't seem to work as intended. Instead of the iframe scrolling, it's the 'body' that is moving. Javascript $(document).on(clickHandler, '#content a', function(){ href ...

Margin ambiguity in a vue.js application

I am facing an issue with my Vue.JS project setup. I want the App.vue to occupy the entire page and have different routes displayed within App.vue using router-view. But, when I try to add a margin to the content of my Game component, the margin seems to ...

Create a Bootstrap multi-bar component for visually displaying the daytime information

I am looking to create a horizontal bar with multiple intervals. Here is what I have tried so far: <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;"& ...

Dynamic row addition in Material Design Lite table causes format to break

Here's the HTML markup for creating a checkbox using material-design-lite: <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="checkbox"> <input type="checkbox" id="checkbox" class="mdl-checkbox__input" /> <span c ...

What causes the change in background when I apply CSS to a div element?

Can anyone explain why the background changes when I style the div element? Here is the code I am using. When I remove the CSS related to the div, the background instantly reverts back to its original state. Any insights would be greatly appreciated. Than ...

Difficulties in Adjusting the Size of Three Image Columns within a Website's Body

While working on my website layout, I encountered a problem with inserting three image columns in the body section. The images turned out to be too large and extended beyond the footer. Moreover, the third image was larger than expected, dominating the web ...

Ways to ensure that a div's height matches its width

Is there a way to make three divs, each with the CSS property display:inline-block, have a 1:1 ratio of width to height using only CSS? Currently, the width is set to 30% and the height to 75%, causing the width to be greater than the height. #our_servi ...

How can we add styles to text entered into a form input field in a targeted way?

Is there a way to apply styling to the second word entered into a form input text field after the user hits "enter" and the data is output? In this scenario, the text always follows the format of 3 numbers, followed by a space, and then a name. For examp ...

How can I make Material UI's grid spacing function properly in React?

I've been utilizing Material UI's Grid for my layout design. While the columns and rows are functioning properly, I've encountered an issue with the spacing attribute not working as expected. To import Grid, I have used the following code: ...

Internet Explorer displaying incorrect shadow effects (paper curl) with CSS

I am attempting to achieve a page curl effect similar to this one: https://i.stack.imgur.com/grS7p.png I have followed the example provided here: http://codepen.io/anon/pen/fpjoa When I create new PHP and CSS files and copy-paste the code from the afore ...

Phantom writing reminiscent of the ghostly prose found on a Tumblr registration

Is there a way to create a URL field similar to the one on ? It's the field where the text is faded and when you click on it to type, it automatically appends ".tumblr.com" that cannot be erased or highlighted. The JavaScript used for this feature is ...

Empty array returned when using wrapper.classes() with Vue-test-utils and CSS modules

Recently I started using Vue Test Utils along with CSS Modules. My Vue component is all set up with CSS Modules and it functions perfectly in the app as well as in Storybook. Take my BasicButton for example: <template> <button :class="[ ...

Display the title of an image beneath the image using CSS

Since upgrading my CMS, I've encountered an issue where the captions of images are not showing below the image for thousands of articles. For example: <img src="123.jpg" alt="Texttext" class="caption" style="disp ...

Can a list be transformed into a toolbar in a responsive design?

Here is my card with a nav section that includes multiple items (although I've only included one here for simplicity). <div class="col-md-3"> <div class="card"> <div class="card-header"> ...

centered logo in a flexbox header

Having trouble with Flex for the first time. Despite reading and experimenting, I still can't figure it out. Any suggestions on how to accomplish this? Check out an example ...

Versatile, stationary division - Looking to attach a different division at the bottom

Element A has a flexible and fixed position (at the top of the window) with a high z-index, allowing items below to pass underneath when scrolling. Element B is positioned below Element A, and I want it to remain attached to the bottom of Element A while ...

Options for Repeating and Full Width Custom Headers in WordPress

I have been exploring the WP codex extensively, but without success. I have enabled the customizer custom header and can upload images. I can then set some CSS to choose whether I want it to be full width or repeated. However, I would like to add an option ...

Is there a CSS3 Selector With Similar Functionality to jQuery's .click()?

For a few years now, I have been utilizing a pure CSS navigation system. However, with the recent increase in mobile site projects at my workplace, I am encountering issues with drop-down menus not functioning properly on mobile devices. Despite this chall ...

Stuffing a container with an image without considering the proportions

I am experimenting with filling a parent <div> with an image without concern for proportions. Despite knowing that it may not look great at all sizes, I just want to test its feasibility. Currently, the image is scaling instead of stretching to fit m ...

Ensuring Equal Padding on Both Sides of a Div by Setting its Width

In search of a div that houses multiple inner divs with equal padding on the left and right edges. This is crucial for maintaining a fluid layout where the distance between the inner divs and outer div border remains consistent even when the window size ch ...