Delete any spaces before or after a floating element

Float elements are typically meant to be displayed side by side in a row. However, in certain cases where float elements need to be pushed below others, it can result in whitespace. An example of this structure is:

<div class="row clearfix">
   <div class="col"></div>
   <div class="col"></div>
   <div class="col"></div>
   <div class="col"></div>
   <div class="col"></div>
</div>

When the following CSS is considered:

@media screen and (max-width: 991px) {
   .col { 
      width: 50%;
 }

}

In this scenario (screen < 992px), extra space will be present:

I want to eliminate this additional gap without altering the HTML structure. How can I achieve this?

View Fiddle demo

Answer №1

While playing around with the fiddle, I noticed that the width was set to 50% using percentages. If needed, you can change it to a specific number of pixels for better compatibility.

I decided to make the following switch:

@media screen and (max-width: 991px) {
   .col { 
      width: 50%;
 }

to

@media screen and (max-width: 991px) {
  .col {
    width: 250px;
    height: 250px;
  }

Check out the edits here

Answer №2

Here is a JQuery script that can be utilized when the page loads and when the window is resized:

$('.col').each(function(index, element) {

  if (index % 2 === 0) {

    if ( $(element).height() > $(element).next().height() ) {

      $(element).next().height( $(element).height() );

    } else {

      $(element).height($(element).next().height());

    }

  }

});

Check out this JSFiddle link for a live demonstration.

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

What steps can I take to create a slideshow with dynamic animations?

I am trying to create a unique category display by turning it into a slider. When the user clicks on any category, I want it to move to the left and become larger. To see an example of how the sliding effect works, check out this link: slideshow https://i ...

JavaScript - Receiving alert - AC_RunActiveContent.js needed for this page to function

When I attempt to open the HTML file in my application, a pop-up message appears stating that "This page requires AC_RunActiveContent.js." However, I have already imported and referenced the AC_RunActiveContent.js file in my package. Can someone assist m ...

Struggling to format boxes or display images on your WordPress website?

When working on this website, I encountered an issue with adding links to Google+ and Facebook. Despite my efforts to style the div boxes using CSS, I was unsuccessful. I also attempted to insert images using an img tag, but the pictures did not load even ...

Newbie Question: What is the process to upload a website that consists of only HTML files to a web hosting provider

I'm embarking on creating a basic four-page website using HTML, CSS, and images. It's my maiden voyage into web design. When I finish developing the HTML files, what steps should I take to upload them and launch the website? ...

Changing Fixed Navigation Bar - Cascading Style Sheets

I currently have a fixed navigation bar that utilizes the affix() function from Twitter Bootstrap 3. The navigation bar is functioning properly, but I am interested in adding a transition effect to its appearance. As of now, when the user scrolls down th ...

Accessing a website to retrieve data using javascript

I have developed a small application that retrieves data by scraping a URL. Now, I want to replicate this process for another website. However, this new site uses JavaScript and the desired results are not included in the HTML. After some research, I disco ...

Tips for aligning Submenu in the middle using CSS

Is there a way to center align the Submenu container? I attempted using margin-left but it didn't work. #moz_page .moz_menu_items ul { position: absolute; top: -9999px; display: block; width: 220px; background-color: #F6D9E8; background: rgba(246,2 ...

"Incorporating input boxes for MySQL updates using PHP, MySQL, and HTML

I'm looking to make my input box dynamic, where it can read the current value from the database and allow users to change that value. Currently, when I enter a number like 1000 into the input box, it posts fine. The PHP results show: Data updated: c ...

One simple fix for removing default focus styles across all browsers

Is there a universal method to disable the default styling of focused elements across all browsers? If not, what are some of the common styles implemented by the most popular browsers? ...

The integration of Angular CLI with SCSS is no longer a separate process -

It seems like I might be overlooking something very straightforward here. After a fresh installation of angular-cli, I created a new website with SCSS. I input the SCSS in the global style.scss as well as some in a component SCSS file. However, when I se ...

Using Jquery to locate a specific data element

I have the code snippet below: <div id="mainMenuInternalMyProfile" class="mainMenuInternalTab" data-pagename="myprofile"> <div class="mainMenuInternalTabClickable">&nbsp;</div> <h4 class="mainMenuInternalTitle">MY PROFILE ...

Checking and locating elements using Jquery's distinctive style

Within my code, I am searching for the DIV element that has the class .se and also has the attribute display: block. Once found, I need to retrieve the ID of this particular DIV. Unfortunately, it seems like my current approach is not correct. var usedse ...

How can I create an accordion panel that can be toggled using

I've been working on creating an accordion panel using CSS with a touch of JavaScript. Everything seems to be functioning correctly except for the toggling of the panels. Essentially, when one panel is clicked, I want all other open panels to close. ...

What could be the reason behind the issue of my HTML draggable feature not functioning properly on touch

I've set up a draggable div with headers and p tags, but I'm encountering an issue when trying to run it on a touch screen. The div tag isn't draggable in this scenario. Can anyone suggest the best solution for making this page touch screen ...

Where can I find the location of the WooCommerce $product->get_price_html method or function?

I have thoroughly reviewed all inquiries on this matter, but none of them have been helpful. I am in need of the location for the $product->get_price_html method or function. The reason for my search is that, inexplicably, all prices are now being disp ...

What is the HTML and CSS code to create a webpage with 3 separate columns?

Looking to create 3 vertical columns on an HTML page: #navbar {width:15%} #content {width:70%} #right {width:15%} Implemented this stylesheet for the layout: #container { position: fixed; float: none; overflow: visible; height: auto; widt ...

Executing a function on each page within the head tag in Nuxt.js

I successfully made my function accessible by using the plugin attribute in the nuxt.config.js file, allowing me to call the function under mounted on every page. I am looking for a more efficient way to run this function within the head tag and have it b ...

Select2 bootstrap component not expanding properly as anticipated

I'm currently working on setting up the layout of my webpage using Bootstrap, incorporating elements such as input-group and Select2. In particular, I am facing an issue with aligning the Select2 element correctly within the input group. Below is a s ...

Failing to thrive is like a child shadowing its parent

Could you please provide some ideas on how I can make the first button (Sections) on the left overlap with the navigation bar in this specific scenario? The current issue is that the extreme left part of the button is not fully visible. I attempted to use ...

Enhanced Custom Code Highlighting for Aptana Studio 3 with support for .less files

Looking to enhance syntax highlighting for .less files in Aptana Studio 3 but struggling to find a solution. XText only works with Eclipse, and the forums offer limited guidance. Has anyone successfully implemented custom syntax highlighting for .less fi ...