How can you modify the Bootstrap class of a grid item only when the grid collapses in Bootstrap 4?

Is there a way to dynamically adjust the bootstrap class of a grid element from col-md-9 to col-md-12 only when the grid breaks due to resizing the browser window?

For example, consider the following code snippet:

https://jsfiddle.net/kp41m0xq/

<body>
<div class="row">
  <div class="col-md-3">
    <!-- Something, graph or so -->
  </div>
  <div class="col-md-9">
    <!-- Something, graph or so -->
  </div>
</div>
</body>

When the window size is large, it looks like this:

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

But when the window size is small, it looks like this:

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

To achieve the desired effect of having the class as col-md-9 when the window is big and col-md-12 when the window is small, is there a way to do it using bootstrap itself? If not, would using jquery be a viable solution?

Answer №1

To optimize for smaller screens, it's recommended to utilize a combination of specific classes:

<body>
<div class="row">
  <div class="col-md-3 col-sm-3">
    <!-- Content, graph, etc. -->
  </div>
  <div class="col-md-9 col-sm-9">
    <!-- Content, graph, etc. -->
  </div>
</div>
</body>

Alternatively, you can also make use of general classes:

<body>
<div class="row">
  <div class="col-3">
    <!-- Content, graph, etc. -->
  </div>
  <div class="col-9">
    <!-- Content, graph, etc. -->
  </div>
</div>
</body>

Answer №2

In order to achieve the desired layout, you must incorporate the 'col-sm-12' class in addition to the 'col-md-9' class like this

<body>
<div class="row">
  <div class="col-md-3">
    <!-- Insert content or graph here -->
  </div>
  <div class="col-md-9 col-sm-12">
    <!-- Insert content or graph here -->
  </div>
</div>
</body>

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

Exploring Next.js Font Styling and Utilizing CSS Variables

I'm attempting to implement the "next" method for adding fonts, but I find the process described quite complex just to preload a font. I experimented with exporting a function to create the font and then using a variable tag to generate a CSS variabl ...

Wait for the canvas to fully load before locating the base64 data in HTML5

Wait until the full canvas is loaded before finding the base64 of that canvas, rather than relying on a fixed time interval. function make_base(bg_img, width, height) { return new Promise(function(resolve, reject) { base_image = new Image(); base_imag ...

HTML: Enhancing User Experience by Updating Page Content Dynamically

Looking for assistance with creating HTML code that will dynamically generate a table on the same page after a user submits a form. Rather than redirecting to a new page with the output values, I want the results to display at the bottom of the current p ...

Error displayed inline

I am facing an issue with a hidden textbox that I need to make visible based on a certain condition. Despite checking that the control is being triggered by the change event, I am unable to get it to display. I have experimented with different methods with ...

Paragraph opacity adjustments can alter both the text and background opacities

Is there a way to change the opacity of only the background of a paragraph without affecting the text? When I try to adjust the opacity, it changes both the text and the background. How can I resolve this issue? HTML <div id="opener"> <p id="in ...

Applying CSS to both pop-up and desktop interfaces can be achieved through the use of media queries or alternative solutions

I am facing a dilemma where I need to display the same content on both a pop-up and desktop view. While I have already implemented media queries to adjust the content for desktop and mobile views, I am struggling with displaying the same content on a pop ...

Tallying the Number of Accordion Toggle Clicks

Let me present a scenario where I have some accordions and would like to keep track of how many times the user expands each one. Could you guide me on how to implement this particular feature? Appreciate your support, Kevin ...

Assign a class while directing visitors away from a particular webpage

Can a div have a class added on page load, but only when redirected from a specific link or page? I currently have an <a href="marketing.php" id="openMyMaterialTab"><button class="secondaryAction">See all</button></a> link on my la ...

There seems to be a glitch with the Flask flash messaging feature, as

My Flask application includes login and register routes where I aim to display flash messages when username passwords are not matched. Specifically, this issue arises in my index and login routes: @app.route('/') @login_required def index(): ...

Prevent auto-clicking actions by the browser

I'm having an issue with a link that I want to automatically click when the page loads. I have the following jQuery code: $('#links').click(); However, when I test this, the browser prevents the popup. How can I fix this issue? ...

Access a local website path using JavaScript upon opening the browser

I need a Javascript function that can determine the day of the week when my html file is accessed, allowing me to automatically open a specific sub-page within my project. ...

The vertical stacking of Bootstrap 4 columns is causing them to appear in a column instead

I have set up a Bootstrap container with a row containing two columns. The column on the right is supposed to hold an image and disappear on screens smaller than medium size. It should occupy 7 grid columns on medium screens and above. The left column sho ...

Is it feasible to place an image on top of a box?

Hello everyone, I have been trying to figure out how to achieve a specific layout in React. I am using MUI so there is no need for any hard-coded CSS. However, my attempts have not been successful so far. The layout I am aiming for consists of an image on ...

The 'Bfrip' button is missing from the DOM

Having some issues with displaying table content using DataTables. Everything seems to be working smoothly except for the Buttons, which are not appearing as expected. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.1 ...

Using CSS to place the h1 text on top of an image

I need some advice on how to place a heading above an image. I have tried using negative margin and adjusting z-index, but the image still covers the heading. If you have a better solution, please let me know! Thank you in advance for your help! PS: Anot ...

Guide to changing the color of SVG images on a live webpage

I'm having trouble changing the color of a specific part within an svg image (created in Inkscape). I believe CSS is the solution, but I can't seem to select the id from the particular SVG file. The object in the SVG has the id='ToChange&apo ...

The function dataTable is not recognized as a valid command

I am encountering an issue while attempting to utilize the datatables plugin. Whenever I call the function dataTable(), I receive an error. Here is a snippet of my code: @Scripts.Render("~/Scripts/DataTables-1.9.4/media/js/jquery.js") @Scripts.Render("~/S ...

Avoiding the default action to submit AJAX form data won't result in any changes to the front end?

Currently, I am working with Flask and have utilized JavaScript to prevent default behavior in order to send all the necessary data through an AJAX request. However, I am facing an issue where although my view contains all the data (verified by console out ...

Sending a URL request to Ajax that is too lengthy

I'm encountering an issue with my Ajax function when calling my controller with an array of ID's. It seems that the problem lies in the URL being generated, which exceeds 2000 characters. $.ajax({ url: "/DistributionRule/GetCalculatedRes ...

What are the techniques for implementing an if statement in CSS or resolving it through JavaScript?

Demo available at the bottom of the page Within my code, there is a div called #myDiv that defaults to an opacity of 0. Upon hovering over #myDiv, the opacity changes to 1. See the CSS snippet below: #myDiv { opacity: 0; } #myDiv:hover { opacity: 1 ...