Moving a div below another div on small screens with Bootstrap 4: A step-by-step guide

add description for image In my layout, div1 contains div2 I am looking for a way to reposition div1 below div2 on smaller screens while using bootstrap4

I attempted using row and col classes but with no success

( (div2) )<---- div2 inside div1 desired outcome: (div1) (div2) stacked on top of each other

Edit:

<div id="div1" style="background:url("sdic/sdv.jpg") center no-repeat cover;">
 <div id="div2">
  Some content
 </div>
</div>

The goal is for div2 to be positioned underneath div1 while keeping the background image intact

Answer №1

To achieve the desired layout, a new div needs to be nested within div1. When the screen size exceeds 425px, the ordering will remain as is. For smaller screens, the order will be reversed.

Update: I have revised the solution so that it does not rely on custom CSS. Instead, Bootstrap 4 classes such as d-flex and order can be used to accomplish the same outcome, taking advantage of Bootstrap's built-in breakpoints.

(If not using Bootstrap, refer to the provided CSS)

.div1 { display: flex; flex-flow: column; }
.div3 { order: 1; }
.div2 { order: 0; }

@media (min-width: 425px) {
  .div3 { order: 0; }
  .div2 { order: 1; }
}
<div class="div1 d-flex">
  <div class="div3 order-2 order-sm-1"><h3>Div3</h3></div>
  <div class="div2 order-1 order-sm-2"><h2>Div2</h2></div>
</div>

.div1 { display: flex; }
.div2 { order: 2; }
.div3 { order: 1; }

Answer №2

To effectively accomplish this task, it is recommended to create a parent container with two direct children elements: one for displaying appropriately sized images using the picture element and another for block-level content (such as a div).

Here is a basic markup example that demonstrates this concept (try resizing your browser to see the effect):

* {
  box-sizing: border-box;
  max-width: 100%;
}

.splash {
  height: 20em;
  position: relative;
}

.splash img {
  height: 100%;
  object-fit: cover;
  width: 100%;
}

@media (min-width: 40.1em) {
  .splash {
    height: 100vh;
    overflow: hidden;
  }
  
  .splash > img {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left; 0;
    width: 100%;
  }

  .splash > .modal {
    background-color: hsla(0,0%,0%,.2);
    height: 100vh;
    position: absolute;
    top: 0;
    left: 20%;
    width: 30%;
  }
}
<div class="splash">
  <picture>
    <img src="//unsplash.it/1600x900" />
  </picture>
  <div class="modal">
    <p>Form goes here/</p>
  </div>
</div>

While the mockup may seem simple at first glance, on larger viewports, the modal overlays the image element but not on smaller screens. It is advisable to customize and adjust the sizing based on your specific needs.

For loading images suitable for different screen sizes, consider adding more image sources within the picture element. Unfortunately, I am unable to provide further details as I have a meeting to attend. This framework-agnostic approach may not have a built-in solution in Bootstrap due to its less common nature, but feel free to explore other options.

Answer №3

I gave this a shot and it actually worked like a charm!

Css:

  /****HEADER START****/
  #header .header-background{
   background: url("../images/header.jpg");
   background-repeat:no-repeat;
   background-size: cover;
   background-position: center;
 }
 @media only screen and (max-width:1024px) {
  .header-background{
    background: none !important;
  }
 .header-img{
    display:block !important;

 }
  .form-container{
    display:none;
  }
  .form-container2{
    display: block !important;
  }
}
/****HEADER END****/

html:

   <div class="container-fluid header-background">
    <div class="row">
      <div>
        <img class="d-none img-fluid header-img" src="images/header.jpg"/>
         <div class="form-container">
          <img class="img-fluid"src="images/unkownbrand.png" />
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo, iure.</p> 
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fuga, eveniet?</p>
           <form>
           <input type="text" class="form-control" />
           <input type="text" class="form-control" />
           </form>
    </div>
  </div>
</div>
<div class="form-container2 d-none">
  <img class="img-fluid"src="images/unkownbrand.png" />
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo, iure.</p>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fuga, eveniet?</p>
  <form>
  <input type="text" class="form-control" />
  <input type="text" class="form-control" />
  </form>
</div>

Answer №4

Have you considered utilizing a media query specifically for smaller screens?

@media (max-width: 801px) {

   div1 {position: relative;}
   div2 {position: absolute; top: -80px (please adjust as needed) };

}

Give this a try and see if it solves the issue.

Answer №5

For mobile view, the simplest solution is to apply a top-margin to the child div.

Check out this code snippet for reference

<style>
.div1{
  border: 1px red solid;
  height: 100px;
  width: 100px;
}

.div2{
  border: 1px black solid;
  height: 50px;
  width: 50px;

}

@media only screen and (max-width: 400px) {
  .div2{
      margin-top: 100%;
  }
}
</style>
<div class="div1">
   <div class="div2">    
   </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

Tips on utilizing the data-filter-control-container feature in Bootstrap-table

I've been working on implementing the new filterControl feature, but I seem to be having some trouble understanding how it functions. The documentation states: Set to e.g. #filter to allow custom input filter in an element with the id filter. Each f ...

Deciphering HTML on android

I am experiencing confusion with decoding HTML text before displaying it to the user. This is the code I have tried: result= Html.fromHtml(temp).toString(); In this case, 'temp' contains something like: "B \u0026 M Collision Repair". But s ...

Display or conceal a div element depending on its CSS class

I need a way to dynamically show or hide a div named "contact-form" on my website based on the CSS class of another div, which indicates whether a product is in stock or out of stock. The goal is to display the div if the class is "in-stock" and hide it i ...

switching the image source by selecting different options from a dropdown menu and leveraging JavaScript

I have been attempting to change the src of an image using JavaScript's addEventListener, but for some reason it is not working. Here is an example to illustrate my issue: let bulbImage = $.querySelector(".bulb-image"); let turnOnOption = $.querySele ...

prevent scrolling to a specific div

Hey, I'm facing a slight issue and need to prevent scrolling to the div after clicking on a link that targets the left div. <div class="kafelki"> <div class="kafelki-img"> <div class="target"> <div id="m1">dasdasdasd m1</di ...

The slider causing conflicts with widgets and content positioned below it in an HTML layout

When implementing a slider with a fade effect, there seems to be an issue with the placement of widgets on the homepage. The problem arises from the margin-top setting as when images change in the slider, the widgets clash with the slider. During the trans ...

Unable to transmit information to a different page

My goal is to transmit both hidden data and user input data to another page. Initially, I attempted to have just one user input field, which allowed the system to transmit the data successfully. <h1><strong>Please scan barcode on JIG</stron ...

"Upon expanding the row in the bootstrap table, make sure to initialize the

While working on a table using bootstrap-table.com, I encountered an issue with expandable rows using expand-row.bs.table. After expanding the rows, I found myself unable to manipulate any elements within the expanded area. For instance, when trying to se ...

Eliminate HTML field based on checkbox status

I'm looking to dynamically remove HTML fields based on a Yes/No condition. I've shared the code below for better understanding. If Yes is selected, I want to hide the No Field/Input/Box and vice versa. function AutoCheck() { if (document.getEl ...

`The Bootstrap modal fails to display`

I have a navigation bar with a button that is supposed to open a modal when clicked. I've tried following the guidance from Bootstrap's documentation and even checked out this question: bootstrap modal not working at all Unfortunately, nothing s ...

Unable to align two overlapping images centrally

https://i.sstatic.net/5Szut.jpg Two identical images are displayed correctly with a semi-transparent 'logo' on top of a base green image. However, they are positioned on the left and I need them both to be centered on the screen. I can't se ...

If the user confirms it with a bootstrap dialog box, they will be redirected to the corresponding links

Firstly, I am not interested in using javascript confirm for this purpose. Please read on. For example, adding an onClick attribute to each link with a function that triggers a system dialog box is not what I want. Instead of the standard system dialog bo ...

CSS styling not functioning properly

After spending a considerable amount of time on this issue... <div id="pager" style="top: 5px; position: relative; " class="pager" > <form> <img src="http://tablesorter.com/addons/pager/icons/first.png" alt="first" class="first" ...

What steps can I take to expand this on a grander level?

I have a code snippet for a personality quiz, but I'm facing an issue with increasing its size. Here's the HTML code: <h3>1. What is your favorite color?</h3> <input type="radio" name="q1" value="A" /> A. Red. <input type="r ...

Currently utilizing Yii framework to customize the appearance of a DIV element by specifying the CSS properties within the View, and subsequently storing the CSS file

I am looking for a way to allow users to customize the properties of a parent div, such as color, background, and other CSS styles, directly from the WordPress theming dashboard interface. Can someone please guide me in the right direction? ...

Accordion Element using HTML and CSS

Seeking assistance with a website project! I am in need of help in implementing the following feature: I would like the "About" link to expand into a panel when clicked, and retract back when the user clicks "hide" within the panel. Check out the attached ...

Tips on managing URL in jQuery Mobile that appears in the navigation bar

I am currently working on a jQuery Mobile application that is designed to function on a single static URL, also known as a single page application. Using $.mobile.changePage.defaults.changeHash = false within the mobileinit event has been successful in ma ...

Exciting new animation feature in ng-repeat

I have implemented ng-repeat with custom styling and plan to expand the array by adding new items. Here is my code snippet: // Custom JavaScript code var _app = angular.module("userApp", []) _app.controller("usrController", function($scope) { $scope ...

Is there a way for me to determine if a user has engaged with a form?

I'm surprised by how difficult it was to find information on this topic through Google. Currently, my struggle lies in wanting my form fields to change color based on validity only after the user has interacted with the form. I don't want invali ...

JavaScript for rotating an element upon button click

My webpage design <div id="yabanner"> <img src="img/ok.jpg"> </div> <button>Click Me</button> <button>Click Me</button> <button>Click Me</button> My script code var button = document.getElementsBy ...