Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide partially obscured element using CSS

Given:

<div class='row'>
  <div class='c1'>hello</div>
  <div class='c2'>bob</div>
</div>

<div class='row'>
  <div class='c1'>this is a very long test test bc a</div>
  <div class='c2'>bob</div>
</div>


<div class='row'>
  <div class='c1'>this is a very long test test test test test</div>
  <div class='c2'>bob</div>
</div>

CSS

.row .c1 {
  position: absolute;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
  z-index: 1;
  background-color: red;
}

.row .c2 {
  float: right;
  background-color: green;
}

.row {
  width: 200px;
  position: relative;
}

.row:after {
  clear: both;
  content: "";
  display: table;
}

Pen:

http://codepen.io/SamSaffron/pen/JtDHb

Is there some sort of fancy CSS (or HTML5 structure) that can cause c2 to hide itself as soon as the text in c1 starts overlapping?

Barring that, what is the best performing JavaScript hack to achieve this?

image

like image 657
Sam Saffron Avatar asked Aug 31 '14 23:08

Sam Saffron


People also ask

How do you hide part of an element in CSS?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden . display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.

How hide Li dot in CSS?

Adding the "list-style: none" CSS class to the unordered list (<ul>) or ordered list (<ol>) tag removes any bullet or number.


1 Answers

EDIT2: Changed the hiding mechanism to visibility:hidden, as per suggested by comments.

Here's a solution using jQuery. I'm working on a pure JS solution as well. The same logic should apply.

CodePen based on yours.

Basically if the width of c1 plus c2 is greater than the width of the row, hide c2. I added an overflow:hidden to the CSS for c2, and used .width(0) to hide it, since your c1 has position:absolute and so depends on c2 to provide the height for the row.

Hope that's what you needed.

EDIT: Here's the same solution in pure JavaScript. This assumes that every row has a c1 and a c2.

CodePen

like image 182
flowstoneknight Avatar answered Oct 07 '22 02:10

flowstoneknight