Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Show/Hide html elements based on container size or wrap functionality

How would you dynamically hide multiple elements based on container size so it doesn't wrap ?

Let me explain with some screenshots :

non wrapped tags

wrapped tags

I have a non predictible number of tags with random size and basically I would like to add a d-none class to all latest elements until the container is not wrapped. The last element would be a toggle button to show/hide hidden elements. The second screenshot shows what I want to avoid, container must not wrap, it must always be on one line.

It's a React project, I tried multiple things like using useRef to get the size of each tag but I can't manage to only hide latest elements.

Here is a sample of the code :

<div class="row">
  <div class="col">
    <span class="badge bg-white">zetze</span>
    <span class="badge bg-white">fsdfdsfsd</span>
    <span class="badge bg-white">fdsfdsfsd</span>
    <span class="badge bg-white">gfgfgdfgdf</span>
    <span class="badge bg-white d-none">gfdgdfn</span>
    <span class="badge bg-white d-none">rtetret</span>
    <button type="button" class="btn show-more"><i class="fe fe-plus"></i></button>
  </div>
</div>

To explain it with the code above, I would like my div.col element to never wrap on a second line and add d-none classes (hide them) to all child elements that exceed except the button.

Do you have an idea how to achieve this ? Any leads will be appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well, there is no way to do CSS dependant on the extent of a span in current browsers (that i know of). At least you can prevent it from wrapping with white-space: nowrap;

So you will need JS for this, using sth like offsetWidth. I'm using Vue, so I can't give any details, but I would render the span-Elements from an array of objects that have a hidden-property, and after calculating how much space there is left you switch off the spans, which go beyond the parent div. Of course you will want data-binding to the show-more-button too, as it is not always visible. Be careful to do calculation when DOM has finally rendered. I'm sure there is a hook for that in the React-Lifecycle.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!