Mixing float and nth-child for versatile box hiding functionality

I am attempting to create a list of elements that float and clear at the beginning of each new row because the height of the boxes is unknown. We want to be able to hide certain boxes based on category, like so:

$('nav a').on('click', function(e) {
    e.preventDefault();
    var show = $(this).data('show');
    if(show == 'all') {
        $('div').show();
    } else {
        $('div').hide();
        $('.' + show).show();
    }
});
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
article {
    width: 400px;
}
div {
    width: 100px;
    border: 1px solid black;
    float: left;
    padding: 10px;
    margin-bottom: 10px;
}
.category-b {
    background-color: #eee;
}
div:nth-child(4n+5) {
    clear: both;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
    <a data-show="all" href="#">All</a> /
    <a data-show="category-a" href="#">Category A</a> /
    <a data-show="category-b" href="#">Category B</a>
</nav>
<article>
    <div class="category-b">hello</div>
    <div class="category-a">hello world folk</div>
    <div class="category-a">lorem ipsum dolores</div>
    <div class="category-a">bonjour Mr</div>
    <div class="category-b">Katarina vs Alistar</div>
    <div class="category-a">hello</div>
    ...

http://jsfiddle.net/Pik_at/Lwyte7sm/

The issue arises when selecting a category as the nth-child selector also counts hidden boxes with display:none, causing the layout to break. Any suggested CSS solutions would be greatly appreciated.

Answer №1

For a quick CSS fix:

Simply assign a set height to each DIV and then float them left without using clear:both.

div { height:80px; }

Check out the DEMO here

Alternatively, you can utilize jQuery to determine the tallest height among all DIVs and achieve the same layout with floating left.

/====================================================/

Have you considered using display:inline-block; instead of float:left; ?

Explore DEMO 2 for reference

*Keep in mind that when using display:inline-block;, the width % may need adjustment due to the spacing it generates between the DIVs.

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

Showcasing both text and image side by side

https://i.sstatic.net/zR1G1.png Our current method for displaying text in one line and an image below it is shown below. I attempted to achieve this using position : relative: top: xxpx; bottom: xxpx;, but I believe there may be a more efficient way to c ...

Position a Bootstrap 3 division within a central division for ultimate centering

I have written a code snippet that looks like this- html, body, .container-table { height: calc(100% - 50px); } /*Centering a div*/ .container-table { display: table; } .vertical-center-row { display: table-cell; vertical-align: middle; } < ...

Bootstrap accordion collapse feature not functioning properly

I'm just starting out with HTML/CSS and as I follow a few tutorials, I've encountered an issue when attempting to implement an accordion example from https://getbootstrap.com/docs/4.0/components/collapse/. It seems like the problem stems from the ...

I'm having trouble changing the background color on my HTML Bootstrap website. Is there a way to easily add social network buttons as well?

I'm encountering a problem here. One of my friends assisted me with Bootstrap elements for my website, but after switching my site to Bootstrap, I am unable to change the background color on my website. Can someone lend a hand? Much appreciated! Also, ...

Creating a basic image carousel with JavaScript, CSS, and HTML

After running the code, I encountered an issue where the first image displays but then after one second, only a white blank screen appears with no further action. It seems like there may be an error in the JavaScript code. Below is the code that was attemp ...

Upgrade to bootstrap 4.1 for access to the advanced grid system features

Hey there! Wondering if it's possible to tailor Bootstrap 4.1 to create a personalized version with just the Grid system and Responsive utilities. I found a link for customizing Bootstrap 3.3, but my final compilation doesn't seem to be working: ...

What is the best method to retrieve the value from a chosen list item?

I decided to incorporate a horizontal selection menu from CodePen into my project, and you can find the source code here Here is the outcome: var btn = document.querySelector("button") var dropdown = document.querySelector(".dropdown-options") var opti ...

Steps to creating a proper cascade using the label statement

I am currently customizing the appearance of a checkbox. The HTML code in the file called fin1.cfm is as follows: <td>Master Event:</td> <td> <input type = "checkbox" id = "cb_e" name = "datesumm" value = "" > <label f ...

The drop-down box options on the web page are not visible to me, but I am able to see them in the element

Having a peculiar issue with two dropdowns on my webpage. The first dropdown works perfectly fine, but when I click on the second dropdown, the options don't show up on the webpage, even though they are visible in the element window. Both dropdowns ha ...

Emphasizing the content of the text file with the inclusion of span tags

I am relatively new to working with angular js and javascript. I have a document or text file that looks something like this: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dumm ...

Modify the design of the button in the CSS code

I am facing an issue with the layout of the Visible Columns button and unable to standardize it like the buttons above using CSS enter image description here The code snippet for the Visible Columns button is as follows: import React, { useState, useEffe ...

Tips for addressing the issue of button flickering due to CSS transitions

Is there a solution to prevent flickering without compromising the intended design? The issue arises when hovering over a moving or animated element and accidentally un-hovering because it moves beneath the cursor. <!DOCTYPE html> <html> &l ...

What is the best way to use jQuery to adjust the size of a slider image based on a percentage of the browser window

I've been searching all over for a solution to this issue, but so far I haven't had any luck. Basically, I have an image that is 1800 pixels wide by 500 pixels high. I need this image to adapt to all screen resolutions. Instead of having a fixed ...

Looking to access all parent elements of the <li> tag when it is clicked?

Currently, I am working on designing dynatree and the nodes are structured within ul and li tags. However, I am encountering an issue where I am unable to retrieve the parents of the clicked li element. Below is a snippet of my HTML code: <ul class="b ...

Changing colors in the rows of a table

Here is a fiddle I created to demonstrate my issue. https://jsfiddle.net/7w3c384f/8/ In the fiddle, you can see that my numbered list has alternating colors achieved through the following jQuery code: $(document).ready(function(){ $("tr:even").css("ba ...

How can I alter the color of the menu text when scrolling to the top of a webpage?

As I navigate my website, I encounter a menu with a transparent background at the top that changes to black as I scroll down. On a specific page where the background is white, this presents an issue: when the menu reaches the top, the white background clas ...

Media queries for the header background image display

I am currently working on a small web project and I want to change the background image in the header section while also making it responsive. I attempted to use a class called "header_accueil" in my header to distinguish different sections of the site, bu ...

Can you provide guidance on how to showcase the chat date and time for each message in a webchat created with the bot framework SDKV4 using C#

I've successfully created a chatBot using bot framework SDKV4 in C# with the WebChannel as the channel. Everything is functioning properly, but I have a specific goal in mind: For each message that the user types or the Bot replies to, I want a time ...

Entering destinations into the Google Maps search bar

I am currently working on a project that involves creating a Google Maps window. The goal is for the user to input their location (marker A) and receive directions to a specified destination (marker B), ideally using an input box and submit button. So far, ...

Error in points calculation and verification of colors

Welcome to the Color Challenge, where you must match the target color from a grid of colors to earn points. However, there seems to be an issue with the color checking and point tracking system. Initially, everything works fine, but as you click on more co ...