What causes my columns to wrap when the window is resized?

I am having trouble implementing a four-column layout on my webpage that maintains its structure when the browser window is resized. I followed a tutorial to create it, but now I can't seem to find it. The columns look fine initially, but as soon as I resize the browser window, the third and fourth columns start wrapping under the first two. Can someone help me identify why this is happening?

#ColumnMain {
  width: 100%;
  height: auto;
  float: left;
  margin-left: auto;
  margin-right: auto;
  min-width: 44%
}

.col1 {
  float: left;
  padding: 10px 20px 15px 20px;
  margin: 0;
  width: 350px;
  height: 100px;
  border-left: solid 1px #35488C;
}

.col2 {
  float: left;
  padding: 10px 20px 15px 20px;
  margin-left: 10px;
  width: 22%;
  height: 100px;
  border-left: solid 1px #35488C;
}

.col3 {
  float: right;
  padding: 10px 20px 15px 20px;
  margin: 0;
  width: 22%;
  height: 100px;
  border-left: solid 1px #35488C;
  overflow: hidden;
}

.col4 {
  float: right;
  padding: 10px 20px 15px 20px;
  margin-right: 10px;
  width: 22%;
  height: 100px;
  border-left: solid 1px #35488C;
  Overflow: hidden;
}
<div class="columns" id="ColumnMain">
  <div class="col1">content-1</div>
  <div class="col2">content-2</div>
  <div class="col3">content-3</div>
  <div class="col4">content-4</div>
</div>

Answer №1

When you set a fixed width for the first columns, it can disrupt the flow of the layout and cause the last divs to be pushed back. To maintain a fixed width for the first div while keeping all divs inline, you can utilize the display: table; layout. This ensures that the first div maintains its size while allowing the other divs to adjust dynamically.

#ColumnMain {
  display: block;
  width: 100%;
  height: auto;
  margin-left: auto;
  margin-right: auto;
  min-width: 44%
}

.col {
  display: table-cell;
  padding: 10px 20px 15px 20px;
  height: 100px;
  border-left: solid 1px #35488C;
}

.col1 {
  margin: 0;
  width: 350px;
}

.col2 {
  margin-left: 10px;
  width: 22%;
}

.col3 {
  margin: 0;
  width: 22%;
  overflow: hidden;
}

.col4 {
  margin-right: 10px;
  width: 22%;
  overflow: hidden;
}
<div class="columns" id="ColumnMain">
  <div class="col col1">content</div>
  <div class="col col2">content</div>
  <div class="col col3">content</div>
  <div class="col col4">content</div>
</div>

Answer №2

When setting different widths for the first column and other columns, in order to make them resizable they must be given percentage width or alternatively you can use bootstrap CSS.

Additionally, margins will not work well with the same percentage width. In such cases, you need to calculate each column's width after applying margin/padding relative to the window/document width, and then apply it in the width style. Also, ensure that this calculation is done on the window resize event as well.

To resolve this issue, remove margin from your column style and add the following property:

box-sizing:border-box;

For testing purposes, I have adjusted the first column width to 200px. It would be better to assign a 25% width to each column for testing.

#ColumnMain {
      width: 100%;
      height: auto;
      float: left;
      margin-left: auto;
      margin-right: auto;
      min-width: 44%
    }

    .col1 {
      float: left;
      padding: 10px 20px 15px 20px;      
      width: 200px;
      height: 100px;
      border-left: solid 1px #35488C;
      box-sizing:border-box;
    }

    .col2 {
      float: left;
      padding: 10px 20px 15px 20px;      
      width: 22%;
      height: 100px;
      border-left: solid 1px #35488C;
      box-sizing:border-box;
    }

    .col3 {
      float: left;
      padding: 10px 20px 15px 20px;     
      width: 22%;
      height: 100px;
      border-left: solid 1px #35488C;
      overflow: hidden;
      box-sizing:border-box;
    }

    .col4 {
      float: left;
      padding: 10px 20px 15px 20px;     
      width: 22%;
      height: 100px;
      border-left: solid 1px #35488C;
      Overflow: hidden;
      box-sizing:border-box;
    }
<div class="columns" id="ColumnMain">
  <div class="col1">content</div>
  <div class="col2">content</div>
  <div class="col3">content</div>
  <div class="col4">content</div>
</div>

Answer №3

The col1 element has a fixed width of 350px. When it reaches a specific size, adding 22% + 22% + 22% + 350px surpasses the width of ColumnMain. This causes some columns to be pushed down in order for all elements to fit within the given space.

Answer №4

The issue causing your div elements to stack on top of each other is primarily due to the predefined widths set on the individual .col classes. When the browser resizes and reaches those specific widths, it becomes unable to accommodate all the elements within a single row, hence resulting in the divs wrapping underneath each other.

Answer №5

To simplify and streamline your CSS code, consider rewriting it to only require one selector for columns. This approach makes maintenance and updates much easier. By making changes in one location, such as adjusting the border or padding, it will automatically apply to all elements with the specified class. Below is a condensed version of the code to achieve your desired layout:

.col {
  float: left;
  padding: 10px 20px 15px 20px;
  margin: 0;
  width: 25%;
  height: 100px;
  border-left: solid 1px #35488C;
  box-sizing: border-box;
}
<div>
  <div class="col">content</div>
  <div class="col">content</div>
  <div class="col">content</div>
  <div class="col">content</div>
</div>

For a visual representation of the output, refer to this gifv link: http://i.imgur.com/VgBByqf.gifv

If you decide to remove the border line before the first column (left-most column), simply add this pseudo-class below your .col selector in the CSS code:

.col:first-child {
  border-left: none;
}

Answer №6

<html>
        <head>    
        <style> 
        .cols_ex {     
         -webkit-columns: 4 10px;     
         -moz-columns: 4 10px;    
          columns: 4 10px;     
         -webkit-column-gap: 2em;     
         -moz-column-gap: 2em;     
          column-gap: 2em;     
         -webkit-column-rule: 2px dashed gray;    
         -moz-column-rule: 2px dashed gray;    
          column-rule: 2px dashed gray; 
       }  
       </style> 
       </head> 
       <body>    
       <div class="cols_ex">         
        <p>At that instant, her head made contact with the hall's ceiling: she had grown beyond nine feet tall, quickly grabbing the little golden key and rushing to the garden door.</p>        
        <p>Poor Alice! It was a struggle for her to lie on one side and peek into the garden with just one eye; however, crossing over seemed more impossible than ever: she slumped down, tears streaming once again.</p>        
        <p>"You should feel ashamed," scolded Alice, "a big girl like yourself" (she had every right to say this), "to keep sobbing in such a manner! Stop this instance, I demand you!"</p>         
        <p>Nevertheless, she continued weeping, shedding gallons of tears until there formed a sizable pool around her, about four inches deep, stretching halfway through the hall.</p>     
       </div>
       </body> 


</html>

Answer №7

Like many have mentioned before, the width of .col1 is fixed, so when combined with percentage-based widths on other columns, the total width may exceed 100% and cause wrapping.

A more contemporary approach to creating a row of columns like this is by utilizing display: flex, which will align them in a row without wrapping (unless specified via flex-wrap).

Below is an illustration of using a @media query to adjust the layout of columns/rows at different screen sizes.

.columns {
  min-width: 44%;
  margin: auto;
  display: flex;
}

.col {
  padding: 10px 20px 15px 20px;
}

.col1 {
  width: 350px;
  height: 100px;
  border-left: solid 1px #35488C;
}

.col2 {
  margin-left: 10px;
  width: 22%;
  height: 100px;
  border-left: solid 1px #35488C;
}

.col3 {
  width: 22%;
  height: 100px;
  border-left: solid 1px #35488C;
}

.col4 {
  margin-right: 10px;
  width: 22%;
  height: 100px;
  border-left: solid 1px #35488C;
}

@media (max-width: 420px) {
  .columns {
    flex-direction: column;
  }
  .col {
    margin: 0;
    width: auto;
  }
}
<div class="columns" id="ColumnMain">
  <div class="col1 col">content</div>
  <div class="col2 col">content</div>
  <div class="col3 col">content</div>
  <div class="col4 col">content</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

Stopping JavaScript from executing upon the loading of new content with Ajax - a comprehensive guide

During the development of a website, I have implemented Ajax to load content dynamically along with the necessary JavaScript. The reason behind this choice is that all pages share the same layout but have different content. An issue I encountered was that ...

When touch-triggered on IOS, HTML5 Web SQL Transactions were smoothly bypassed without any errors

I'm encountering issues with database transactions on IOS devices. When the user does not interact with the phone, everything functions as expected. However, if the user taps, scrolls, or touches the screen, some transactions bypass the actual transac ...

Stop working to $(this) in jquery when clicking outside

I'm facing an issue with my code and would like some help. I have a button that toggles the visibility of a filter element, which is working fine. However, I now want to implement a "click outside" effect to close the filter when clicking on any body ...

Challenges with converting JSON into PHP code

I am attempting to output the JSON response below to HTML using PHP. Here is the JSON response : { "data": { "onward": [ { "origin": "LHR", "id": "SB15", "rating": 0, "destination": "FKK", "PricingSolut ...

Eliminate character strings from several columns using R

I am currently working with a large dataset that contains questionnaire data collected at multiple time points. The questionnaire used was the same for each wave, resulting in variables labeled by time in the format "w#variablename" (for example, "w1age", ...

Struggling to show the AJAX response text in the HTML console

I am troubleshooting an issue with my ajax response text, but it keeps coming back as undefined. Here is the code I am using: $(document).ready(function() { var responseData = $.ajax({ type: "GET", url: "https://raw.githubusercontent.com/Theru ...

What is the best way to implement a fixed header within a table?

Greetings everyone! I am looking to keep the thead header fixed at the top and allow only the tbody values to scroll within the table. Currently, when scrolling the page, everything in the table scrolls, but I aim to have only the tbody values scroll. Chec ...

Distinguish between individuals who are sharing login credentials

At the moment, I am distinguishing users by using $_SESSION[id]. However, I have noticed that some users are sharing their login information on multiple devices at the same time. This could potentially create issues in the system. If there is a method to ...

Triggering a modal window with unique content by clicking on various images

Is there a way to trigger a modal window through clicking on an image? Additionally, can different images open different content upon clicking? I am planning to showcase a portfolio where clicking on an image will activate a modal that displays other image ...

How to retrieve text content from a span element with Selenium WebDriver and C#

I am at a mental roadblock and can't figure out this puzzle. My goal is to retrieve the number "9" from the span with id = "lookupCount". No matter how much effort I put in, I just can't seem to make it work. Please lend me a hand, here is the pr ...

problem with overlapping elements

Hey there! I'm facing a CSS issue where an error message appears below the contact us box if the user doesn't fill the text box properly. However, there's an overlap issue with the image below the contact us box. Any suggestions on how to s ...

Prevent content overlap in Tumblr's sidebars by using CSS positioning techniques

I'm facing an issue with my blog on Tumblr where the sidebar keeps overlapping the main content. I have modified a premade template and am trying to position the sidebar in the top right corner using absolute or fixed positioning: #sidebar{ position: ...

Using jquery to insert a new row into a table

Here is the HTML and Javascript code I have been working on. My goal is to clone a row in a table and update the ids of each input element. While debugging the script, I can see the row being cloned and added to the table temporarily, but it disappears f ...

Should I implement this practice when developing an AJAX website? Is it recommended to enable PHP code within .html files, or should I switch to using .php files instead?

Query: I am interested in executing PHP within HTML documents to include HTML using PHP include();. Question: Would it be more beneficial to change .php to .txt for my AJAX-loaded pages and switch my .html files to .php? This approach might resolve the ...

Struggling to implement a vertical scroll bar in HTML code?

<body ng-app="myApp" ng-controller="myCtrl"> <div ng-show = "dataFromRest" ng-repeat = "x in dataFromRest.posts" > <div class="tittle" style="width: 25%;"> <a href="" ng-click="showDi ...

Create a dynamic webpage where two div elements glide in opposite directions

My apologies for this seemingly basic question, but I am wondering if anyone knows how to create an animation where a div moves up and down while scrolling, similar to the effect on this website: On that site, near the footer, there are 2 sections with 2 ...

Update the dropdown options based on the radio button selection using PHP and MySQL

<form action="naam.php" method="POST"> <input type="radio" name="gender"value="female">Female</input> <input type="radio" name="gender"value="male">Male</input> <label>First Name: </label> <select name="owner" ...

integrating live html content into dompdf with php

Hey there, I'm currently working on a script that dynamically loads HTML using the file_get_contents function and then generates and saves the PDF on the server. However, I'm running into a problem where my PHP file is throwing a 500 internal ser ...

<div> issues with background display

I have created two different sections with distinct backgrounds. However, I am facing an issue where these two divs are not appearing on the webpage. My intention is to position the Navbar at the top and have another section below it that is not linked to ...

Selecting the dropdown option reveals a blank white box on the screen

I have encountered an issue with my navigation bar code. Specifically, when I click on the Users dropdown in the top navigation bar, I am not able to see any items in the sub-menu: <nav> <ul> <li><img style="marg ...