Hiding the last row of floated elements only if it is not an even row can be achieved using CSS

I am trying to find a quick and easy solution (either using CSS or jQuery) to hide the last row of floated elements if they are not even.

Take a look at this JS Fiddle example: http://jsfiddle.net/cohhvfjn/

There is an issue when resizing the HTML container. Sometimes, the last row of floated elements may have 3 items while the row above it has 4, and the same thing happens when it's resized further down so the last row has 1 item when the row above it has 2.

In essence, I want to be able to remove/hide that last row only if the number of items in it does not match the row above.

Could someone provide some assistance with this?

Below is the basic HTML code included in the JSFiddle:

<style>
    .content {width:100%;}
    .content .block {float:left;width:20%}

    @media all and (max-width: 600px) {
        .content .block {width:25%}
    }
    @media all and (max-width: 500px) {
        .content .block {width:33.3%}
    }
    @media all and (max-width: 400px) {
        .content .block {width:50%}
    }
</style>
<div class="content">
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
    <div class="block">Text</div>
</div>

Answer №1

To achieve the desired layout, you can utilize a mix of nth-child and nth-last-child().

If you aim for 4 items per row, you can select every 4th element starting from the 1st one (e.g., 1, 5, 9, ...) which is also the last child in that row, or the 2nd to last child (when only two items are left in the row), or even the 3rd to last child (if there are three elements remaining).

This technique is adaptable to rows of different lengths as well. For more insights on this topic, I recommend reading this helpful article by Heydon Pickering.

Note: I have made adjustments to your media queries for demonstration purposes and included an ordered list (<ol>) to enumerate the items for clarity.

  1. Text
  2. Text
  3. Text
  4. Text
  5. Text
  6. Text
  7. Text
  8. Text
  9. Text
  10. Text
  11. Text
  12. Text
  13. Text
  14. Text
  15. Text
  16. Text

Answer №2

If you're looking to achieve a similar effect, consider the following approach:

function hideOddItems() {
    var containers = $(".container");
    containers.show();

    var itemsPerRow = Math.round($(".content").width() / containers.width());

    var itemsToHide = containers.length % itemsPerRow;

    for (var i = 0; i < itemsToHide; ++i) {
        containers.eq(containers.length - i - 1).hide();
    }
}

$(document).ready(function () {
    $(window).on("resize", hideOddItems);
    hideOddItems();
});

Check out the updated demonstration here: http://jsfiddle.net/cohhvfjn/1/

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

Methods for anchoring an element at the bottom of a square container div

* { box-sizing: border-box; } .publication { display: flex; width: 100%; margin-top: 6%; } .bottom1, .bottom2 { display: flex; flex-direction: column; ...

Is it possible to showcase D3 charts on an .epub file?

For my research project, I am exploring the possibilities of .epub files and experimenting with embedding JavaScript code to display data visualizations. I am currently using calibre to convert an HTML file containing D3 scatterplots into an .epub. The s ...

Fixing an erroneous value that has been dragged into the drop function with Jquery

I am encountering an issue with my codes and need some assistance in identifying the problem. The data is being dynamically loaded from the database, and I am using a foreach loop to display all items in a draggable div. The issue arises when I drag an it ...

Delete chosen border from photo in html

I have a logo carousel on my website and when I click on one of the logos, a black outline appears indicating that it is selected. I've tried using img::selection { background: transparent; } but it didn't work as expected. I want to get rid of ...

Animate in Angular using transform without requiring absolute positioning after the animation is completed

Attempting to incorporate some fancy animations into my project, but running into layout issues when using position: absolute for the animation with transform. export function SlideLeft() { return trigger('slideLeft', [ state('void&a ...

Using Angular's [ngIf], [ngIfElse], and [ngIfElseIf] functionalities enables dynamic content rendering

Currently, I have the following code snippet: <ng-container *ngIf="someCondition"> <ng-template [ngIf]="cd.typedType === 'first'" [ngIfElse]="Second"> <div class="row"> fir ...

Tips for Utilizing the Accurate Google Map Code within Jquery Tabs

Looking for assistance in properly integrating Google maps into jQuery tabs. I am trying to incorporate two maps into the project. One map to display the location and another map to provide directions. Encountering an issue with the code, as Firebug rep ...

Using jQuery to target the element before

Is there a way to determine the width of elements located before an element when it is hovered over? I attempted to achieve this using the following code: $('ul li').hover(function() { $(this).prevAll().each(function() { var margin = $(this ...

Learn the process of assigning the present date using the jQuery UI date picker tool

I am looking to implement the feature of setting the current date using Angular/Jquery UI date picker. I have created a directive specifically for this purpose which is shown below: app.directive('basicpicker', function() { return { rest ...

Issues with the navigation and logo design in bootstrap mode

1: How can I adjust the size of the logo and name in my Bootstrap navigation? 2: My navigation menu is not showing up correctly. Can anyone help me troubleshoot? https://i.sstatic.net/0pXya.jpg Navbar: <nav class="navbar navbar-expand-lg py-4 f ...

"Utilize jQuery to implement toggling, pagination, and handling multiple

I am new to jquery and I need help figuring out the best approach to achieve what I want. It seems like it involves using a combination of jquery functions: When the list of items exceeds 5, a 'more' link should appear. Clicking on this link ...

"Optimizing reload time for DIV content with jQuery's .load function is proving to be

I am currently facing an issue with a div that displays data from a database and a form that updates item quantities. After submitting the form, the div refreshes while a modal with a bootstrap spinner pops up to indicate it is loading. The problem arises ...

Launch a bootstrap modal using jQuery, showcasing a designated HIDDEN section (excluding nav-tabs)

This unique modal box does not have a visible tab. Instead, it utilizes the href attribute for navigating to other pages. <div id="bootmodal" class="modal fade" tabindex="-1" data-width="370" data-backdrop="static" data-keyboard="false" style="display: ...

The sub-menu options are constantly shifting... How can I keep them in place?

Here we go again... I'm having trouble with my sub-menu elements moving when I hover over them. I've searched for a solution but haven't found anything helpful. The previous advice I received about matching padding and adding transparent bo ...

Tips for modifying the text color of radio button choices in HTML and CSS

I have set a black background image for my HTML page. How can I change the radio button labels/texts to white, similar to the questions? This is the code snippet that shows how it currently looks on the page: View Image <hr&g ...

JavaScript Autocomplete - retrieving the value of a label

I am looking for a way to utilize the autocomplete feature in my form box to extract a specific value from the displayed label. When a user selects an option, I want to include part of the label (client_id) in the form submission process. JS: <script ...

How can I detect a change in user input using jQuery?

When utilizing jquery .oninput with an element, the event will trigger instantly whenever there is a change in the input value. In my scenario, it is necessary for me to validate the input value by calling the service immediately after any changes occur. ...

Tips for aligning controls in a column using Bootstrap

My coding issue: <div class="col-lg-4"> <label>Expiration ID</label> <div class="row "> <div class="col-lg-5"> <input type="text" class="form-control input-sm" id="TextIDExpire" /> </div> < ...

Easily manipulate textboxes by dynamically adding or deleting them and then sending the data to a

Can you provide a simple example of how to dynamically add and remove textboxes and then display the results? I envision it like this: [Empty Textbox][Add button] When 'Value1' is entered into the textbox and 'add' is clicked, it s ...

li rel=identifier

Can someone assist with choosing rel="28700" in order to update the text within the <li> tag? <li id="pen_li_8" rel="28700"><span class="linkselectValue">Text to be changed</span></li> I have attempted the obvious method: $ ...