Aligning Divs in a Responsive Browser

In the following illustration, I am facing an issue with my responsive code. I have three sections named square1, 2, and 3, each containing a white div with text on top and an icon positioned absolutely. Everything works fine when the browser width is 920px or higher. However, between 720 to 940 pixels, I am trying to make these three elements take up 50% of the width - two displayed on top and one at the bottom, all centered. So far, it has turned into a mess. Can anyone help me understand what I am doing wrong? Here is a link to my current DEMO

HTML:

<div id="section2">

        <div id="centralize2">
            <div id="square1">
                <div id="white1">
                    <img src="images/hexagon1.png" class="hexagon" />
                    <h2 class="title1">Ipsum Dolor Consectetur1</h2>
                    <h3 class="description2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a dolor ac sapien semper maximus vel vel arcu. Praesent venenatis semper ornare. Donec blandit feugiat tellus. </h3>
                </div>
            </div>

            <div id="square2">
                <div id="white1">
                    <img src="images/hexagon2.png" class="hexagon" />
                    <h2 class="title1">Ipsum Dolor Consectetur2</h2>
                    <h3 class="description2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a dolor ac sapien semper maximus vel vel arcu. Praesent venenatis semper ornare. Donec blandit feugiat tellus. </h3>
                </div>

            </div>
            <div id="square3">

                <div id="white1">
                    <img src="images/hexagon3.png" class="hexagon" />
                    <h2 class="title1">Ipsum Dolor Consectetur3</h2>
                    <h3 class="description2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a dolor ac sapien semper maximus vel vel arcu. Praesent venenatis semper ornare. Donec blandit feugiat tellus. </h3>
                </div>
            </div>
        </div>

    </div>

Thanks!!

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

Answer №1

Utilizing the power of flexbox can help you achieve your desired layout without relying on absolute positioning unnecessarily. When the width exceeds 940px, the div elements will naturally arrange themselves side by side. Feel free to experiment with the complete code snippet below.

Additionally, I have made some improvements to the code by replacing duplicate id attributes with a more appropriate class. This ensures valid markup according to web standards.

.section2 {
position: relative;
background-color: #112e4c;
overflow: hidden;
}

.square1, .square2, .square3 {
margin-top: 59px;
}

.white1 {
  margin: 20px;
background-color: #fff;
text-align: center;
}

.hexagon {
position: absolute;
left: 50%;
}

.title1 {
font-size:18px;
margin-top: 90px;
font-family: 'Ubuntu', sans-serif;
color: #112e4c;
font-weight: 600;
}
.description2 {
font-size: 14px;
line-height: 16px;
margin-top:20px;
padding-left: 20px;
padding-right: 20px;
text-align: center;
color: #8da0b4;

}

@media all and (min-width:720px) {
  
  .centralize2 {
    display: flex;
    flex-flow: row wrap;
  }

  .square1, .square2, .square3 {
  flex-basis: calc(50%);
  }
  
}


@media all and (min-width:940px) {
  
  .square1, .square2, .square3 {
  flex-basis: calc(100%/3);
  }
  
}
<body>
<div class="section2">


      <div class="centralize2">
<div class="square1">
<div class="white1">
<h2 class="title1">Ipsum Dolor Consectetur1</h2>
<h3 class="description2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a dolor ac sapien semper maximus vel vel arcu. Praesent venenatis semper ornare. Donec blandit feugiat tellus. </h3>
</div>
</div>

<div class="square2">
<div class="white1">
<h2 class="title1">Ipsum Dolor Consectetur2</h2>
<h3 class="description2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a dolor ac sapien semper maximus vel vel arcu. Praesent venenatis semper ornare. Donec blandit feugiat tellus. </h3>
</div>

</div>
<div class="square3">

<div class="white1">
<h2 class="title1">Ipsum Dolor Consectetur3</h2>
<h3 class="description2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a dolor ac sapien semper maximus vel vel arcu. Praesent venenatis semper ornare. Donec blandit feugiat tellus. </h3>
</div>
</div>
</div>

</div>

</body>

Answer №2

When dealing with your layout, the issue seems to be related to absolute positioning. To resolve this, consider removing absolute positioning and instead floating the blocks left while adjusting the margins accordingly. I will work on putting together a functioning demo for you promptly.

I had the opportunity to modify your demo, and it appears that you may be seeking something along these lines: https://jsfiddle.net/ah5zz8qq/

#section2 {
width: 100%;
background-color: #112e4c;
overflow: hidden;
}

#centralize2 {
  width: 880px;
  height: 310px;
  margin: 40px auto;
  text-align: center;
}

#square1 {
  display: block;
float: left;
width: 33.3%;
  position: relative;
}

#square2 {
  display: block;
float: left;
width: 33.3%;
position: relative;
}

#square3 {
  display: block;
float: left;
width: 33.3%;
  position: relative;
}

#white1 {
width: 280px;
height: 250px;
background-color: #fff;
text-align: center;
  position: relative;
}

.hexagon {
position: absolute;
left: 50%;
  top: 0;
margin-left: -52px;
margin-top: -59px;
}

.title1 {
  display: block;
font-size:18px;
font-family: 'Ubuntu', sans-serif;
color: #112e4c;
font-weight: 600;
}
.description2 {
font-size: 14px;
line-height: 16px;
margin-top:20px;
padding-left: 20px;
padding-right: 20px;
text-align: center;
color: #8da0b4;
}


@media all and (max-width:940px) {
  
#centralize2 {
width: 90%;
} 

#section2 {
height: 720px;
}

#square1 {
  display: block;
width: 50%;
float: left;
}

#square2 {
  display: block;
width: 50%;
float: left;
  margin-bottom: 10px;
}

#square3 {
  display: block;
  clear: both;
width: 50%;
float: none;
  margin: 0 auto;
}

#white1 {
width:300px;


}

@media all and (max-width: 720px) {
  
#section2 {
height: 1070px;
}

#buttons {
margin-top:25px;
}

#square1 {
width: 100%;
}

#square2 {
width: 100%;
margin-top:340px;
}

#square3 {
width: 100%;
margin-top:340px;
}

#white1 {
width: 100%;
position: absolute;

} 
}
}

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 jQuery's toggleClass method to implement CSS transformations

I have a question about this situation Each time the button is clicked, jQuery toggles the class: .transition { background-color: #CBA; transform: translateX(100px) scale(0.5) rotate(180deg); } Within the <div class="container2"> And upon ...

Position the Crossmark to align with text using CSS styling

How can I properly align a crossmark and a checkmark with the text behind them? The checkmark looks aligned well, but the crossmark appears to be slightly off to the top-right. I'm hesitant to adjust margins and paddings as it may cause issues when th ...

Issues arise when implementing ReactJS transitions for progress bars

As a newcomer to the world of ReactJS, I have been exploring the FB documentations and online tutorials to kickstart my test project. Within this project, I wanted to implement a progress bar that visually represents the user's progression through fi ...

Showing a notification that is persistent and not dismissible

Hello everyone! I've been struggling with a problem on the RPS project for quite some time now. The issue arises when I try to display a message for a tie in the game, but I can't seem to find a solution to make it disappear when the round is not ...

What is the best way to prompt users to submit comments with a popup textarea box?

Within my project, I have incorporated a dropdown list: <div class="dropdown"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select subject <span class="caret"></span> </but ...

Conceal a division based on its numerical position

I'm having trouble figuring out how to hide multiple divs based on an index that I receive. The code snippet below hides only the first div with the id "medicalCard", but there could be more than one div with this id. document.getElementById("medical ...

What is the most efficient way to populate a dynamic table in a form with a user's past responses using PHP?

Explaining my thought process through a complex title might be challenging, but let me provide an example to clarify: Imagine I am requesting the user's favorite Rock albums, including albumName and albumArtist. Initially, there is a table with one r ...

Using Phonegap alongside ons-scroller and ons-button

Recently, I have been using Phonegap with the Onsen UI system on iOS devices. I encountered an issue where buttons included within an ons-scroller were not clickable when running on an iPad or iPhone. Here is the code snippet that caused the problem: < ...

What is the best way to use PHP in order to retrieve the element containing the visit ID from the specific row within an HTML table?

I am working on a project that involves populating an HTML table with data from a database. The table includes an approval button for administrators to approve visits and change their status to APPROVED. I am trying to figure out how to select the element ...

Retrieve the value from another select element

Is there a way to create a function in pure JavaScript that changes the selected options in two select tags based on each other? For example, if I choose a French word in one select tag, the English equivalent automatically changes in the other select tag ...

in tailwind, the position transition does not work as expected

I have a React component that I'm working on: function DivModal() { const [isOpen, setIsOpen] = useState(true) return ( <div> <button onClick={() => setIsOpen(prev => !prev)} className={buttonStyle}>Open</button> ...

Search for "conditions" in a basic HTML DOM parser

I encountered an issue when trying to parse information from a website using simple HTML DOM parser. My code looks something like this: <li> <p class="x"></p><p>..</p> <p>..</p> <p>..</p> <p class ...

Attempting to embed an image within the datepicker input

After successfully implementing the datepicker to select a date, I decided to add a buttonImage option for opening the datepicker on image click. However, I encountered an issue when trying to place my calendar image inside my input element, floated to th ...

I have developed a web page using asp.net that cannot be resized

Hey there! I'm interested in building a web page that cannot be resized or minimized. Is there a way to do this? Additionally, I've noticed some advertising pages that are fixed on the screen - how can I create something similar? ...

AngularJS - Oops! Looks like we encountered an error: [$injector:modulerr]

I have a client-server application and I am facing an issue with this error message: "Uncaught Error: [$injector:modulerr]". Despite searching for solutions, none of them seem to be fixing the problem. Here is my client-side code: angular.module('m ...

The color of the text on the Homepage appears differently on iOS Safari

At the top of my homepage, I have displayed the company phone number with white text color. It appears correctly as white on my desktop browsers (Firefox and Chrome), also on my Droid phone, but shows up as dark gray on my iOS phone using Safari. Why is th ...

Implementing a keypress function that can handle duplicate names

HTML <input name="pm" type="text" value="0"/> <input name="pm" type="text" value="0"/> <input name="pm" type="text" value="0"/> <input name="total" type="text" value="0" disabled="disabled"/> Javascript $('[name="pm"]' ...

Adjust the styling with jQuery according to the selected value from the dropdown menu

Check out this Fiddle I have a jQuery script that is supposed to change the style of an input box to display:block if the selected value is "<>" (Between). However, currently it is changing the css for all selected items instead of just the target i ...

AngularJS is used to vertically collapse three different div elements. This feature

I am facing an issue with the design of my page that contains three panels. Two of these panels have tables, and when viewing the page on a smaller display, it disrupts the layout. I am wondering if there is a way to add collapsible buttons to the panels s ...

Modify css with php instead of using FTP

As I work on constructing a website, I wonder how WordPress allows its admin users to edit CSS styles through the website instead of accessing the css file directly via FTP. How exactly does WordPress load the CSS file? My assumption is that it somehow re ...