Managing the order of rows in Bootstrap specifically for mobile devices

Take a look at this simple HTML snippet on this fiddle that illustrates a specific layout on wide desktop screens:

Here is the code responsible for rendering the above layout:

<div class="container">
<div class="row">
<div class="col-md-4"><img class="img-responsive" src="http://placehold.it/800x400"></div>
<div class="col-md-8>
        <h1>Random decisions for emacs, with functions and a minor mode.</h1>
        <p>Random decisions for emacs, with functions and a minor mode.
    Roll various types of dice, generate random numbers from
    </p>
</div>
</div>

On mobile devices (when the page width is reduced), the layout changes to display as follows:

My goal is to reposition the header ("Random decisions..") above the image in mobile view only (without changing anything on desktop). It seems like rearranging rows in mobile view may achieve this, but I'm looking for guidance on achieving this in an organized manner.

Answer №1

Flexbox is the solution for this.

html {
  box-sizing: border-box;
}
*,
*::before,
*::after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
}
@media (max-width: 768px) {
  .row {
    display: flex;
    flex-direction: column;
  }
  .top {
    order: 2;
  }
  .bottom {
    order: 1;
  }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col-md-4 top">
      <img class="img-responsive" src="http://placehold.it/800x400">
    </div>
    <div class="col-md-8 bottom">
      <h1>Random decisions for emacs, with functions and a minor mode.</h1>
      <p>Random decisions for emacs, with functions and a minor mode. Roll various types of dice, generate random numbers from
      </p>
    </div>
  </div>

Alternatively, you can achieve the same effect without specifying order values by simply reversing the order using:

@media (max-width: 768px) {
  .row {
    display: flex;
    flex-direction: column-reverse;

  }
}

Check out the Codepen Demo for reference

Answer №2

To achieve a responsive layout using Bootstrap, you can utilize the Push/Pull classes. Rearrange your content so that your image comes second in the sequence to ensure it shifts below the header on mobile devices and moves to the left as the viewport expands (following Bootstrap's mobile-first approach).

For more information, refer to Column Reordering

Example:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">

    <div class="col-md-8 col-md-push-4">
      <h1>Random decisions for emacs, with functions and a minor mode.</h1>
      <p>Random decisions for emacs, with functions and a minor mode. Roll various types of dice, generate random numbers from
      </p>
    </div>


    <div class="col-md-4 col-md-pull-8">
      <img class="img-responsive" src="http://placehold.it/800x400">
    </div>

  </div>
</div>

Answer №3

To achieve the desired layout using bootstrap, utilize the col-**-push-* and col-**-pull-* classes. By reversing the order of elements and using these classes, you can create a mobile-friendly design where the first element is pushed ahead and the second element is pulled back by the appropriate number of columns. For example, placing the col-md-8 div at the top with a col-md-push-4 class, while adding a col-md-pull-4 to the second div.

Answer №4

After trying a few options, I decided to go with the suggestion from @vanburen as it ultimately guided me towards this solution.

<div class="container">
<div class="row">

<div class="col-md-8 col-md-push-4">
    <h1>Creating random choices for emacs, including functions and a minor mode.</h1>
</div>

<div class="col-md-4 col-md-pull-8">
    <img class="img-responsive" src="http://placehold.it/800x400">
</div>

<div class="row">
    <div class="col-md-4">
    </div>
    <div class="col-md-8">
    <p>Random decisions for emacs, with functions and a minor mode. Roll various types of dice, generate random numbers from</p>
    </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

An empty space separating the header and the navigation bar

As a beginner in HTML and CSS, I decided to create a basic website. However, I encountered an issue with the layout - there seems to be a gap between my header image and navigation bar, but I can't figure out what's causing it. HTML <!DOCTYPE ...

Ensuring the Accuracy of User Input within Bootstrap's jQuery Modal Popup

What is the best way to validate an input control within a Bootstrap jQuery modal dialog? Check out some examples of modal dialogs in this link http://getbootstrap.com/javascript/#modals The Bootstrap Modal examples typically contain this type of Input: ...

Creating and implementing a HTML template from scratch, devoid of any frameworks

Is there a way to create a quiz where all questions follow the same format and only one question is displayed at a time, without duplicating code? Perhaps using a template would be the ideal solution in this scenario. I appreciate your help. ...

How can I display data saved from step 2 in a multi-step form with 4 steps, when I return from step 3 to step 2?

Let's consider this scenario: Imagine the user is at step 2 and types their name into <input type="text" class="form-control input-xl" ngModel name="firstName"> They proceed to step 3 but then decide to return to step 2. The information entere ...

Resetting the check status in html can be accomplished by using the checked

I am currently working on a popup feature in HTML <div id="term_flags" class="term_flags"> <div class="modal-users-content flagsContent"> <div class="modal-users-header"> <span class="close" ng-clic ...

Page returning causing tabs to refresh improperly

Today I want to discuss two different pages: VelocityMicro.com/Cruz.php and VelocityMicro.com/PC.php, which I am currently viewing in Firefox. The issue I am experiencing is with Cruz.php. When you click on a tab like "R100 Series" and then select an acce ...

Display an HTML image within a span tag and ensure it is aligned to the bottom

I am facing a layout issue with 2 images that are placed side by side within their tag . The sizes of the images are different, but they are both anchored at the top of their parent element. <span style="position: relative; left: 0; top: 0; vertical- ...

Learn how to iterate over an array and display items with a specific class when clicked using jQuery

I have an array with values that I want to display one by one on the screen when the background div is clicked. However, I also want each element to fade out when clicked and then a new element to appear. Currently, the elements are being produced but th ...

How can I retrieve information from an HTML or JavaScript object?

Imagine a scenario where you have an HTML table consisting of 5,000 rows and 50 columns, all generated from a JavaScript object. Now, suppose you want to send 50 checked rows (checkbox) from the client to the server using HTTP in JSON format. The question ...

Utilizing AJAX and JavaScript to generate a table using the AJAX response and placing it within a <div> element

I am currently passing the response of this action in text form, but I would like to display it in a table format. Is there a way to do this? function loadAditivos(){ $('#aditivoAbertoInformacoesTexto').html('<div id="loaderMaior ...

The separator falls short of spanning the entire width of the page

For some reason, I can't seem to make the divider extend to the full length of the page. <TableRow> <TableCell className={classes.tableCell} colSpan={6}> <Box display="grid" gridTemplateColumn ...

How can I set a default radio button selection when the page is loaded?

Input your radio button code below: <ion-radio ng-model="data.gender" ng-required="true" ng-value="'male'">Male</ion-radio> <ion-radio ng-model="data.gender" ng-required="true" ng-value="'female'">Female</ion-rad ...

Bootstrap 3's Clear:Left Media Query Feature

Bootstrap is being used, and there is a div with col-xs-12, col-sm-6, col-md-4 classes. Task : To add a clear on every 1 item for col-xs-12, add a clear on every 2 items for col-sm-6, and clear on every 3rd item for col-md-4. The current code only s ...

Retrieving all options from a dropdown list in HTML using ASP.net C#

I am currently working with a select list that looks like this: <select name="Section" id="Section" size="5"> <option>Section1</option> <option>Section2</option> <option>Section3</option> <option>Section4< ...

How can I use JavaScript to create a drop-down menu that only appears after selecting the previous one?

<div class="row"> <div class="col-md-4"> <label for="selectCustomers">Customer Select</label> <select class="form-control pointer" name="tableCustomers" id=" ...

Off-center rotation of an element - seeking solution [closed

This question has been closed. It requires additional details for debugging. Answer submissions are currently not accepted. ...

The Material UI dialog box popped up against an unexpected gray backdrop

Currently, I am using Material UI in conjunction with React to create a dialog that appears when a button is tapped. This button is located within a table, which is displayed over a Paper component. The problem arises when I utilize the dialog with its def ...

Switch the appearance between two elements

In my HTML table, I have different levels of content - from main "contents" to nested "sub-contents" and even deeper "sub-sub-content". My goal is to hide all sub-content within the content cell that I click on. <table> <tr class=' ...

Set the default value for a form control in a select dropdown using Angular

I've been struggling to figure out how to mark an option as selected in my select element, but I haven't had any luck. I've tried multiple solutions from the internet, but none of them seem to be working for me. Does anyone out there have ...

Utilizing CSS to create dynamic 3D cube transformations

Exploring the realm of CSS 3D transforms to showcase cubes and cuboids featuring cross-sections has been my current venture. In this endeavor, I'm aiming at compatibility with Chrome, Firefox, and MSIE 11. It has come to my attention that in order to ...