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!!

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

jQuery and HTML: Need help enabling an <input> or <select> element?

Currently, I am using Mozilla Firefox 14.0.1 and Google Chrome 20.0.1132.57 (which I believe is the latest version). The code I am working with looks like this: http://jsfiddle.net/SVtDj/ Here is what I am trying to accomplish: Type something into inpu ...

JQuery is having trouble with playing multiple sound files or causing delays with events

I've been working on a project that involves playing sounds for each letter in a list of text. However, I'm encountering an issue where only the last sound file is played instead of looping through every element. I've attempted to delay the ...

The use of script src with vue.js is causing issues

I am looking to move my Vue code into an external .js file. Since I am new to Vue, I am trying to keep it simple without using webpack. However, I have encountered an issue where Vue does not work when using the script src tag in the html file. For instanc ...

Filtering, cleaning, and confirming the validity of data input allowed for HTML tags

While there is a lot of information available on sanitizing, filtering, and validating forms for simple inputs like email addresses, phone numbers, and addresses, the security of your application ultimately relies on its weakest link. What if your form inc ...

Resizing Your WordPress Header Logo

Hi there! I am struggling to resize the logo in the header of my website www.cookingvinyls.com. Despite my efforts, I can't seem to override the hardcoded version. I attempted to modify the width and height lines in header.php, but unfortunately, it ...

text/x-handlebars always missing in action

I'm currently working on my first app and I'm facing an issue with displaying handlebars scripts in the browser. Below is the HTML code: <!doctype html> <html> <head> <title>Random Presents</title> ...

having trouble transferring the password field in PHP to phpMyAdmin

My HTML form collects the user's first name, last name, username, and password. I am trying to upload this data to my local phpMyAdmin, but I'm facing an issue with storing the password in the database. Below is my HTML code: <input type="te ...

What is the process for incorporating the !important declaration into a CSS-in-JS (JSS) class attribute?

I'm currently exploring the use of CSS-in-JS classes from this specific response in conjunction with a Material UI component within my React project. In order to override the CSS set by Bootstrap, I've decided to utilize the !important modifier. ...

The absence of a meta tag in the Wordpress head section

I keep getting an error from Lighthouse saying that I am missing both the viewport meta tag and description meta tag Does not have a <meta name="viewport"> tag with width or initial-scaleNo `<meta name="viewport">` tag found ...

Scrolling behavior in two columns with sticky positioning

Both columns have varying heights, with the left sometimes higher than the right. I want them to start scrolling down simultaneously, but when the shorter column's content ends, it should stick to the bottom of the viewport and "wait" until the taller ...

Bootstrap image with simplistic arrow indicators

I'm facing a minor issue that I need help solving. I want to have two simple arrows (one pointing left and one right) vertically aligned in the center of an image. Additionally, these arrows should resize proportionally when the screen size changes. ...

The functionality of CSS isn't up to snuff on Mozilla Firefox

I am having an issue with CSS styling on a <div> element in my PHP page. The CSS style is working perfectly in Google Chrome, but when I test my code in Firefox, the styling is not being applied. .due-money { background-color: #09C; color: whi ...

What is the best way to create a toggle button that can show more or show less of a text snippet with animation?

Is it possible for someone to assist me with showing a long text partially and providing a "show more" button to reveal the rest, along with a "show less" option, all with some CSS animation? I was thinking of using a font awesome arrow down icon for expan ...

Issue with the top margin of the body

Recently, I've encountered a peculiar problem with a standard website layout that I use. The issue seems to be centered around the space between the top and the first div: navbar. No matter what I try, I just can't seem to remove it, as the reas ...

Utilizing interpolation in Angular to apply CSS styling to specific sections of a TypeScript variable

Suppose I have a variable called data in the app.component.ts file of type :string. In the app.component.html file, I am displaying the value of data on the UI using string interpolation like {{data}}. My question is, how can I apply some css to specific ...

Changing the background color using jQuery switch case statement

I am attempting to utilize jquery to dynamically change the background image of a webpage based on different cases in a JavaScript switch statement. I have completed three steps: 1) Included this script tag in my HTML document: <script src="http://co ...

Expanding size on hover without affecting the alignment of surrounding elements

Imagine there are 10 divs on the page styled as squares, collectively known as the home page. Among these 10: 3 divs belong to the .event1 class, 5 divs belong to the .event2 class, and 2 divs belong to the .event3 class. <div class="boxes event1"> ...

Is it normal for e.target.result to only work after two or three tries?

Attempting to resize an image on the client side before sending it to the server has been challenging for me. Sometimes, the image does not align correctly with the canvas used for resizing. I have noticed that I need to send the resized image at least tw ...

Trouble with the display:none attribute in Firefox and Chrome

<tr style="height:5px" id="TRRSHeaderTrialBar" name="TRRSHeaderTrialBar" style='display:none'> <tr id="TREmail" name="TREmail" style="height:1px;" nowrap style='display:none'> Using the code snippet above to hide the bar w ...

Using Selenium and Python to scrape text from a continuously refreshing webpage after each scroll

Currently, I am utilizing Selenium/python to automatically scroll down a social media platform and extract posts. At the moment, I am gathering all the text in one go after scrolling a set number of times (see code below), but my objective is to only gathe ...