Arrange four divisions so that they are displayed in pairs on each row when the viewport width is smaller

I have a row of 4 div elements aligned horizontally from left to right. Each div takes up 25% of the screen width.

I am looking for a solution to make them wrap when the user resizes the screen instead of overlapping each other.

.MenuSett {
  margin-top: 10px;
  width: 100%;
  position: relative;
  height: 120px;
  color: #0ddF00;
  display: inline-block;
}

.m1 {
  width: 25%;
  margin: auto;
  text-align: center;
  float: left;
}
<div class="MenuSett">

  <div class="m1">
    <p>This content is valid</p>
    <div ngbDropdown class="d-inline-block">
      <button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown</button>
      <div ngbDropdownMenu aria-labelledby="dropdownBasic1">
        <button ngbDropdownItem>Action - 1</button>
        <button ngbDropdownItem>Another Action</button>
        <button ngbDropdownItem>Something else is here</button>
      </div>
    </div>
  </div>

  [repeat m1 div blocks]

</div>

Following that div element, there is

.belowDiv {   
   position: relative;  
   height:350px;
 }

Any suggestions on how to move it down when the above divs wrap?

Answer №1

To stack them vertically, use the CSS property display: flex; in the parent with flex-direction: row; and adjust the layout to show them horizontally by changing flex-direction: column; within media queries.

.MenuSett{  
  margin-top:10px ;
  width: 100%;
  position: relative; 
  height: 120px;  
  color: #0ddF00;
  display: flex;
  flex-direction: row; 
} 

.m1 { 
   width: 25%;   
   margin: auto;
   text-align: center;    
   float: left;
} 


@media only screen and (max-width: 600px) {
  .MenuSett{ 
    flex-direction: column; 
  }
}

Answer №2

If you want the children of .MenuSett to move onto a new line at smaller viewport widths, you can add Flexbox and use flex-wrap: wrap.

Keep in mind that setting an absolute value for the width, like 250px, is necessary. This is because with width: 25%, the children will always be 25% of the parent's width no matter the viewport size, causing them to stay on one row.

.MenuSett {
  margin-top: 10px;
  width: 100%;
  position: relative;
  height: 120px;
  color: #0ddF00;
  
  /* Implementing Flexbox to the parent */
  display: flex;
  
  /* Allowing children to wrap to the next line */
  flex-wrap: wrap;
}

.m1 {
  /* Children will be 25% of the parent at larger viewports */
  width: 25%;
  
  /* At viewports smaller than ~1000px, children will start wrapping due to the minimum width set */
  min-width: 250px;
  
  margin: auto;
  text-align: center;
  float: left;
}
<div class="MenuSett">

<div class="m1">
<p>This content is ok</p>
<div ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button ngbDropdownItem>Action - 1</button>
<button ngbDropdownItem>Another Action</button>
<button ngbDropdownItem>Something else is here</button>
</div>
</div>
</div>

<div class="m1">
<p>This content is ok</p>
<div ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button ngbDropdownItem>Action - 1</button>
<button ngbDropdownItem>Another Action</button>
<button ngbDropdownItem>Something else is here</button>
</div>
</div>
</div>

<div class="m1">
<p>This content is ok</p>
<div ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button ngbDropdownItem>Action - 1</button>
<button ngbDropdownItem>Another Action</button>
<button ngbDropdownItem>Something else is here</button>
</div>
</div>
</div>


<div class="m1">
<p>This content is ok</p>
<div ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Toggle dropdown</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
<button ngbDropdownItem>Action - 1</button>
<button ngbDropdownItem>Another Action</button>
<button ngbDropdownItem>Something else is here</button>
</div>
</div>
</div>

</div>

Answer №3

If I were to style this, I would opt for using flexbox:

.MenuSett{  
  margin-top:10px;
  width: 100%;
  position: relative; 
  height: 120px;  
  color: #0ddF00;
  display: inline-block;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
} 

.m1 { 
   flex: 1; 
   margin: auto;
   text-align: center;    
   float: left;
} 

The parent element .MenuSett would utilize properties like display: flex;, flex-direction: row;, and flex-wrap: wrap;, while the child elements with class .m1 would have flex: 1 without a specified width.

To ensure responsiveness, media queries could be implemented to adjust the layout based on screen size. For example:

@media screen and (max-width: 500px) {
  .m1 { 
   flex: 1 0 100%;
  }
}
@media screen and (min-width: 700px) {
  .m1 { 
    flex: 1 0 50%;
  }
}

@media screen and (min-width: 900px) {
  .m1 { 
    flex: 1 0 25%;
  }
}

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

HTML - one div's child element takes precedence over another div

My HTML page features two consecutive divs. The first div contains a child span that has a relative position, causing it to overlap the second div. I have implemented click events for both divs. However, when I click on the span that overlaps the second d ...

The v-for in Vue is not displaying the accurate text in the modal window and is only showing the data for the first item

In the process of learning Vue and Bootstrap, I am creating a blog where I want a modal window to pop up with the post information when the user clicks on the comment button. However, the issue I'm facing is that the modal only displays information fr ...

The layout of my website is perfect when my laptop's zoom is set to 90%, but at 100% it starts overflowing

Currently, I am in the process of building a website on my laptop while following an instructor's video tutorial. It has been important for me to set the same pixel measurements as the instructor in order to replicate the design they provided. However ...

The Masonry Grid is leaving empty spaces unfilled

I've been experimenting with Sveltekit and SCSS to create a Masonry grid. However, I'm facing an issue where the items in my grid are not filling in the empty space as expected. Instead, they seem to be following the size of the largest image, le ...

Can you explain the mechanics behind the animation of the upvote button on steemit.com?

Behold the upvote button of steemit.com: <span class="Icon chevron-up-circle" style="display: inline-block; width: 1.12rem; height: 1.12rem;"> <svg enable-background="new 0 0 33 33" version="1.1" viewBox="0 0 33 33" xml:space="preserve" xmlns=" ...

Issue with scrollTop not functioning when using onclick on an <a> tag within a div

After various attempts and research online, I am still experiencing erratic scrolling behavior with the scrollTop function. My goal is to scroll within a specific div when clicking on links. The content of my div: <div id="test" style="height:400px; o ...

Integrate CSS and JavaScript files into Node Express

I am facing an issue including my CSS file and JavaScript file in a node-express application, as I keep getting a 404 not found error. Here is the code snippet that I am using: 1. In server.js var http = require('http'); var app = require(' ...

What is the best way to link various stylesheets to a Rails project?

In my project, I have a main stylesheet called application.css included in the layout file layouts/application.html.erb: <%= stylesheet_link_tag "application" %> However, there is a specific section of the website where I want to use a different st ...

What is the best way to align my content alongside my sidebar?

I am facing some challenges with my website layout because I used Bootstrap to integrate a sidebar. The sidebar has caused issues with the positioning of my content, and I'm struggling to align it properly next to the sidebar on the left side. Please ...

Using setTime in JavaScript allows for customizing and adjusting the

I'm having trouble getting this code to display the time. I thought it would work, but it's not showing the time. Can someone please help me figure out what's going wrong? function startTime() { var currentTime = new Date(); ...

Can 'fr' units be used in the `calc()` function of CSS?

In this particular scenario, the query arises whether the CSS calc() function extends its support to the fr unit as presented in the below example? .sample-grid { --main-fr: 60fr; grid-template-columns: var(--main-fr) 1rem calc(100 - var(--main-fr ...

Increased spacing HTML

Trying to create a footer in HTML, but each time I attempt it ends up with an extra margin on the left side. Here is my current footer code: <div id="footer"> Tester </div> #footer { background-image: url(images/backgroundrpatternf ...

Is there a more efficient approach to applying a basic function to multiple elements within a DIV and having it activate only upon a click event using jQuery?

This solution works, but I am looking for a more professional approach in order to adhere to the principle of "don't repeat yourself." $("document").ready(function(){ $(".child").each(function(index){ $(this).click(func ...

What is the best way to close the space between a box-shadow and a div background when incorporating semi-transparent rgba() values?

There seems to be an issue arising when applying partially transparent rgba() background-color and box-shadow color values to an element with a non-fixed (percent-based) border-radius. This combination results in a minuscule transparent gap appearing at th ...

a fixed width layout with two columns aligned at the top

I've been struggling to design a two-column, top-aligned layout with fixed width for data that will be inserted into text in a WebView for an iPhone app. The first column needs to have a set width so the items in the second column can align perfectly. ...

How to handle Component binding change events in AngularJS

I have developed a component in AngularJS that displays data. However, when the customer binding changes, I need to call a service in the component controller, but it is not updating. Below is the code snippet: In my Test1.html file: <tab-customer tit ...

Using dynamic jquery to target specific elements. How can we apply jquery to selected elements only?

Hello everyone! I have been working on a simple hover color change effect using jQuery, but I noticed that I am repeating the code for different buttons and service icons. Is there a way to achieve the same result so that when a button is hovered, the co ...

Integrating fresh models into Django and creating dedicated pages for each

I have successfully set up a model in my Django project where I can create and display posts. However, when I created a new Model, I encountered issues replicating the same process. I tried setting up html files, modifying urls.py, and views.py similar to ...

What are the benefits of sticking with Express versus transitioning to a combination of JavaScript and HTML?

Recently, I've been developing a web application that involves taking user input to make modifications to files on the server side. The main logic for this project is built in node.js and operates via command line, with the rest of the site being deve ...

What is the best way to ensure email address validation is done perfectly every time?

Can someone assist me with validating email addresses correctly? I am able to handle empty fields, but now I need a way to identify and display an error message for invalid email addresses. How can I modify my validation process to check for improper email ...