Harness the power of responsive divs with Bootstrap 4!

I have a total of 7 div elements.

Given that the column width can only go up to 12, I am unable to evenly divide them using the .col-md-2 class. Instead, I am opting to use the .col class for equal distribution.

To ensure responsiveness, I aim to set the width of all divs to 100% in the mobile view. However, using col-xs-12 does not work properly if I add the class="col col-xs-12" to the same div. Additionally, I cannot use class="col-md-2 col-xs-12" as it would break one of the div elements due to having 7 in total.

If anyone has suggestions on how to achieve this, I would greatly appreciate it.

Answer №1

If you're working with Bootstrap 4, remember to prioritize a mobile-first approach. Start by setting the default class to col-12 and then adjust for desktop using col-md or col-lg as needed. For a more detailed explanation, refer to the Bootstrap documentation available at https://getbootstrap.com/docs/4.3/layout/grid/

Therefore, your divs should be structured like so:

<div class="col-12 col-md"></div>
.

I hope this explanation has been beneficial to you!

Answer №2

Although this question already has a marked answer, there are still some points that need clarification.

When you use the class col-md (without specifying col-md-[1-12]) with the aim of creating 7 equal columns in Bootstrap 4, the default behavior is to apply flex-grow: 1;. This may lead to the misconception that flex-grow: 1; will evenly distribute the parent width into 7 equal parts. However, this will not be the case if the content within any div exceeds the width of a column.

For flex width, it's important to remember the formula:

content —> width —> flex-basis (constrained by max|min-width)

An informative article detailing this concept can be found here.

In your scenario, if you have a div with a width of 500px, all 7 divs will not have the same width. To address this, you should define a custom column grid as shown below:

<div class="container">
  <div class="row">
    <div class="col col-12 custom-col-7">Data</div>
    ...
  </div>
</div>
@media screen and (min-width: 768px) {
  .col.custom-col-7 {
    flex: 0 0 auto;
    width: calc(100%/7);
  }
}

I have created a simple example to illustrate this concept, I hope this clarifies the issue for you.

.col {
  background: lightgreen;
  margin-bottom: 50px;
}
@media screen and (min-width: 768px) {
  .col.custom-col-7 {
    flex: 0 0 auto;
    width: calc(100%/7);
  }
}

.box {
  background: red;
  width: 300px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<h4>Using `col-md`</h4>
<div class="container">
  <div class="row">
    <div class="col col-md col-12">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Itaque officia, culpa, nemo numquam illum totam odio dolorem nisi ad in animi! Sequi illo voluptatem dolores.</div>
    <div class="col col-md col-12">Col Data</div>
    <div class="col col-md col-12">Col Data</div>
    <div class="col col-md col-12">Col Data</div>
    <div class="col col-md col-12">
      <div class="box">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore, numquam et obcaecati, optio rerum animi aspernatur in similique id</div>
    </div>
    <div class="col col-md col-12">Col Data</div>
    <div class="col col-md col-12">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore, numquam et obcaecati, optio rerum animi aspernatur in similique id eaque iusto mollitia. Excepturi ea id cumque delectus facilis accusantium cum, ut itaque incidunt mollitia aspernatur, voluptates eligendi quibusdam iste facere sint soluta corporis et laborum porro, molestiae eaque sed molestias?</div>
  </div>
</div>

<h4>Using custom column class</h4>
<div class="container">
  <div class="row">
    <div class="col custom-col-7 col-12">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Veritatis inventore aspernatur architecto consectetur nostrum consequatur, esse amet molestias labore fuga quis reiciendis nulla reprehenderit assumenda?a</div>
    <div class="col custom-col-7 col-12">Col Data</div>
    <div class="col custom-col-7 col-12">Col Data</div>
    <div class="col custom-col-7 col-12">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Tempora, nam.</div>
    <div class="col custom-col-7 col-12"><div class="box">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore, numquam et obcaecati, optio rerum animi aspernatur in similique id</div></div>
    <div class="col custom-col-7 col-12">Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod eveniet, mollitia accusamus nam consequuntur velit.</div>
    <div class="col custom-col-7 col-12">Col Data</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

When resizing the browser window, this single horizontal page website does not re-center

I am in the process of creating my very first one-page horizontal website using jQuery and the scrollTo plugin. This site consists of 3 screens and initially starts in the center (displaying the middle screen). To achieve this, I decided to set the width o ...

"Combining multiple DIVs into a single DIV with jQuery - a step-by-step guide

I have multiple DIVs with the same class, and I am looking to group them into a single DIV using either jQuery or pure JS. <div class="a"> <div>1</div> </div> <div class="a"> <div>2</div> ...

I require a breakdown of the JavaScript code, please

Are you struggling with a code snippet that involves CSS, JavaScript, and HTML? Let's take a look at the complete code: <!doctype html> <html> <head> <link rel="stylesheet" type="text/css" href="http://snipplicious.com/css/bo ...

Utilizing a combination of PHP and jQuery, certain checkbox options were dynamically deactivated

<input type="checkbox" name="pref[]" value="red//fuji" />Fuji <input type="checkbox" name="pref[]" value="red//pinklady" />Pink Lady <input type="checkbox" name="pref[]" value="red//reddelicious" />Red Delicious <input type="checkbox" ...

Conceal a single column on mobile devices using Bootstrap 4

Take a look at this code snippet: <div class="row"> <div class="col-lg-3"> <div class="ava-block"> </div> </div> <div class="col-lg-6 ...

What are the steps to generate interactive hotspots in a 3D setting with threejs?

I'm new to working with three.js and I'm attempting to create a project similar to the example found at . I have everything set up using three.js, but I'm struggling to figure out how to implement a hotspot (like the red dot in the provided ...

Arranging items in a vertical column using CSS based on the content

I am facing an issue with aligning the values in a column under each other without percentages using my own html code, css, and bootstrap classes: <div class="d-flex"> <p class="table-string">91.86</p> <span ...

Is there a way to create a dynamic associative array using jquery?

I am looking to create an array structured like the following:- arr[0][from] = value arr[0][to] = value arr[1][from] = value arr[1][to] = value . . And so forth. I have input array html elements for the from & to fields. <input type="text" name ...

After sending a post request, the form in HTML with Node.js is returning an undefined response

Upon creating an HTML <form></form> object to capture a username, email, and password, I encountered an issue where the value returned is always undefined when accessing req.body.name or any other field. It seems like the form is not functionin ...

Design a sophisticated background using CSS

https://i.sstatic.net/yILwL.png I am wondering if there is a way to create a CSS3 background similar to the one shown in the image link above. The gradient should smoothly transition from white to black, spanning across a header div, and remain consistent ...

Adding an image to a jQuery class name on the fly

I am attempting to add an image before a div by using its className with jQuery. function insertImage(obj) { var dynamicClass = $(obj).prop("className"); After retrieving the classname, I now encapsulate it in single quotes and add a dot to access t ...

Unneeded return is necessary when utilizing recursion and restarting the application

Recently, I successfully recreated the 2048 game in Java, which was a pleasant surprise to me as I didn't expect to create something this "advanced." During the game, when tiles are moved, a new square/tile/number needs to be generated. To find an av ...

Use jQuery to delete the parent div when the element inside does not contain a specific class

I'm dealing with a grid that has 3 columns, each sharing the same div class. Inside each column is a span with either the class of class="simplefavorite-button" or class="simplefavorite-button.active". Here's how the markup looks: <div class= ...

The RGB values of a dynamically loaded image being drawn to a canvas in JavaScript are slightly off

After hosting some .PNG images on my NGINX webserver, I noticed a discrepancy in the RGB values when loading and drawing them on a canvas using context.drawImage(img, 0, 0); and retrieving the image data using context.getImageData(x, y, 1, 1).data. Interes ...

Excessive text in HTML div element

Whenever I maximize the window in any browser, I type a long string in a div or span tag. It perfectly fits within the div tag. However, when I minimize or compress the browser width, the text spills out of the div area. I am looking for a solution to set ...

Injecting PHP variables into CSS code right from a PHP file

$a = "ABCD"; ?> <link rel='stylesheet' type='text/css' href='others.php' /> <style> .page-title { text-indent: -9999px; line-height: 0; } .page-title:afte ...

What is the most effective method to extract an ID from a URL that is written in various formats?

Does anyone know of a reliable method to extract an ID from various URL formats using javascript or jquery? https://plus.google.com/115025207826515678661/posts https://plus.google.com/115025207826515678661/ https://plus.google.com/115025207826515678661 ht ...

Tips for verifying that one of the two input fields is filled in Bootstrap 5 validation

I have implemented Bootstrap validation for the other input fields in this form by using the 'required' attribute. However, for these two specific fields, if at least one is not empty, then the form should be submitted. <form class="needs ...

Removing dropdown lists from input forms can be achieved without using jQuery by utilizing vanilla JavaScript

Looking to build a custom HTML/JavaScript terminal or shell. Interested in utilizing an input box and form to interact with JavaScript functions/commands. Unsure of how to eliminate the drop-down menu that appears after previous inputs. Pictured terminal: ...

Is it possible to swap images by clicking on them?

Let's say I'm working with 3 images. Image A, B and C. A is the main image initially. If I click on image B, it will become the main image and image A will take its place in the old position. The same condition applies when B is the main image a ...