Are there alternative methods to create space between two adjacent Divs and ensure they align properly when stacked on top of each other?

After trying different methods, I realized that adding margins to the left divs instead of the right ones achieved the desired spacing. Yet, I can't help but feel like this might not be the most ideal solution. If you have any other suggestions on how to achieve this layout, please let me know.

Here is the issue at hand:

Below is the code snippet that currently works for me:

(I made adjustments by adding right and bottom margins)

<div style="float:left;width:300px;text-align:center;margin-right:20px;margin-bottom:20px">
<div style="background-color:red">Hello Hi. One Two</div>
<div style="background-color:green">Hello Hi. One Two</div>
</div>

<div style="float:left;width:300px;text-align:center;">
<div style="background-color:aqua">Hello Hi. One Two</div>
<div style="background-color:pink">Hello Hi. One Two</div>
</div>

When resizing the screen horizontally to mimic a phone size, the items on the right stack under the left set, as intended.

For larger screens like computers, I want to create space between the sets while ensuring they align properly when resized. This proves to be challenging for me. Currently, the alignment looks something like this upon resizing:

_____
   ______ 

The first line represents the first set of divs, and the second line represents the second set. The challenge lies in getting the second set to align with the first after creating the desired space.

My goal is to achieve the following alignment upon resizing:

______ 

______

This follows the format where the first set of divs has a space break followed by the second set that was originally on the right side.

Answer №1

To make sure your layout works on both small and large screens, you need to utilize @media queries and adjust the margin property accordingly.

body > div {
  float: left;
  width: 300px;
  text-align: center;
  margin-right: 20px;
  margin-bottom: 20px;
}
@media screen and (max-width: 600px) {
  body > div {
    float: none;
    margin: 0;
  }
}

Fiddle:

Answer №2

Check out this example utilizing the inline-block property.

body {
  text-align: center;
}
.first, .second {
  display: inline-block;
  width: calc(50% - 20px);
  min-width: 300px;
  text-align:center;
  margin: 10px;
}

@media screen and (max-width: 658px) {
  .first, .second {
    width: calc(100% - 20px);
  }
}
  <div class="first">
    <div style="background-color:red">Hello Hi. One Two</div>
    <div style="background-color:green">Hello Hi. One Two</div>
  </div><div class="second">
    <div style="background-color:aqua">Hello Hi. One Two</div>
    <div style="background-color:pink">Hello Hi. One Two</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

Using an inline-block display and pseudo-elements for creating vertically aligned content

Currently, I am utilizing display:inline-block and pseudo-elements (::before, ::after) to achieve vertical alignment in the middle, but unfortunately, it seems to be ineffective. The pseudo-element ends up taking a whole row of space even with a width set ...

Two Ajax Requests Simultaneously

I am currently faced with the challenge of handling two requests simultaneously. The first request involves executing a lengthy PHP script that takes 10 minutes to complete (I cannot modify it using JavaScript, so that's not an option). The second ...

Creating a responsive single webpage using HTML/CSS

How can I make some div or img responsive for mobile format? <meta name="viewport" content="width=device-width, initial-scale=1" /> <div class="container"> <h1 class="title"> ...

Inquiring about the best method to determine the intersection point between two lines in Draw i.o

Can someone please explain how to find the coordinate point where two lines intersect in draw i.o? I am looking for a Javascript function to solve this issue and get the coordinate of the point only. For example: ...

The currency exchange script is malfunctioning and not functioning properly

Is there a solution for accessing the JSON value that is currently eluding me? If anyone has any suggestions, I would greatly appreciate it. function forex() { var to = document.getElementById("to").value; alert(to); var from = document.getE ...

HTML5 - the enigmatic header and outline procedure

While revisiting the HTML5 spec, I came across a question about nesting a nav element within a header. The description for the header element seemed a bit peculiar to me, mainly because I couldn't fully grasp it. In the given example, the page inc ...

Adjusting the labels in Datatable.net for optimal viewing size

I have integrated data table.net into my website and encountered an issue with the labels in the status column. The labels for 'in progress', 'assigned', and 'not assigned' are not wrapping properly. I have tried using text-w ...

Having trouble with .animate() function?

Struggling to animate the position of my background image $(function() { $('#nav1').bind('click',function(event){ $('ul.nav').stop().animate({backgroundPosition: 'right top'}, 1000); }); $(function() { ...

Too much gap between the span and the border's bottom

Occasionally, there seems to be an odd gap between the span and the bottom border. This issue is particularly noticeable on mobile devices. You can view a screenshot of the problem here: Code .heading { text-align: center; border-bottom: 2px solid ...

Monitor the change in values upon pressing the submit button on Angular

I am currently working with an edit form that contains data in the input fields. <ng-form #infoForm="ngForm" novalidate> <div> <label for="firstName">First Name :</label> <input type=" ...

Modifying the page title for a Facebook application

I'm curious if it's possible to modify the title of my page for my Facebook application. Currently, the title reads "CoolApplicationName on Facebook", but I would like to insert an additional word before the application name. For instance, "(Adde ...

Hold off on scrolling up while hovering over the screen

I've been attempting to implement a scroll-up behavior in WordPress, but I'm facing difficulties in pausing the animation when hovering over it. I've experimented with both jQuery and CSS methods, but have not been successful so far. Any ass ...

What's the best way to show floating point numbers in a concise format while also maintaining the ability to perform calculations within this Vue app?

I'm currently developing a compact calculator application using Vue 3 and implementing some custom CSS. For the most part, everything seems to be functioning correctly, except for when the results are long numbers that extend beyond the display limit ...

Sending an HTML POST request in Android is a powerful

Just started exploring android development and set up an http server that works locally. Here is the html form I am using: static final String HTML_FORM= "<form action=\"http://localhost:5000/doSearch\" enctype=\"multipart/form-d ...

Exploring Vue: Creating smooth transitions for dropdown menus and dynamically toggling classes on different elements

Here's a simplified version of what I'm trying to achieve: <div class="dropdownMenuWrapper"> <ul class="dropdownMenu"> <li class="dropdownMenuItem"&rt; Menu 1 </li> <button class=&quo ...

What is the best way to toggle between tabs using a checkbox that is checked or unchecked?

I have implemented a checkbox with two sets of tabs that are meant to be displayed or hidden based on the checked state of the checkbox. Currently, the checkbox switches tabs when checked, but does not switch back when unchecked. What modifications ca ...

Using Html.BeginForm to route to a Web Api endpoint

I need help figuring out how to make my page post to my Web API controller instead of my Area/Controller/Action. I've tried using both Html.BeginForm and Ajax.BeginForm, but haven't had any success so far. Here's what I've attempted: @ ...

Logo-enhanced Navigation Bar in Bootstrap

Hey everyone, I've been experimenting with Bootstrap headers that include a logo before the "Brand" text. I'm facing an issue where the navbar doesn't resize properly, as shown in the screenshot below: - The navbar doesn't adjust to t ...

PHP Lightweight HTML Content Scraper

I have been experimenting with a basic web crawler and here is the simple HTML code I used for learning purposes: input.php <ul id="nav"> <li> <a href="www.google.com">Google</a> <ul> <li&g ...

Guide on displaying a paragraph with jQuery depending on two separate select options

When a user selects "Option 1" and "Option a," I want to display one thing, and for "Option 2" and "Option c," show something else...and so on. I've been experimenting with this functionality on https://jsfiddle.net/xw6t3gL4/8/ Thank you! $(&apos ...