How to hide nested element when parents width is 0?

halfacreyum

Im trying to hide a nested p tag when its parents width is 0. essentially I want the nested p tag to follow the parent as its width approaches 0, as well as when it expands. I'm sure its something simple, thanks for any help in advance.

Fiddle

HTML:

<div class="container">
  <div class="floater1">
    <p id="icon">X</p>
  </div>
  <div class="floater2">
    <p>This is a test</p>
  </div>  
</div>
<button>click</button>

CSS:

.container {
  width: 100%;
  height: 35px;
  border: 1px solid #000;
}
.floater1 {
  float: left;
  width: 0px;
  height: inherit;
  background: red;
  text-align: center;
  transition: width 200ms ease-in-out;
}
.show {
  width: 30px;
}
.floater2 {
  height: inherit;
  background: yellow;
  overflow: hidden;
  padding-left: 15px;
}
p {
  margin: 0;
  line-height: 35px;
}

JS:

var trigger = $('button');
var deleteBtn = $('.floater1');
trigger.click(function(){
    console.log("hello")
    deleteBtn.toggleClass('show');
})
David Chelliah

You have to set the overflow:hidden; property to ".floater1" class. So that the p tag will be hidden when the parent width is 0.

var trigger = $('button');
var deleteBtn = $('.floater1');
trigger.click(function(){
	console.log("hello")
	deleteBtn.toggleClass('show');
})
.container {
  width: 100%;
  height: 35px;
  border: 1px solid #000;
}
.floater1 {
  float: left;
  width: 0px;
  height: inherit;
  background: red;
  text-align: center;
  transition: width 200ms ease-in-out;
  overflow:hidden;
}
.show {
  width: 30px;
}
.floater2 {
  height: inherit;
  background: yellow;
  overflow: hidden;
  padding-left: 15px;
}
p {
  margin: 0;
  line-height: 35px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="floater1">
    <p>X</p>
  </div>
  <div class="floater2">
    <p>This is a test</p>
  </div>  
</div>
<button>click</button>

See the working fiddle here

https://jsfiddle.net/davidsekar/7uk9n0ev/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to remove parents in firebase by its nested data?

When not more nested lists hide an element

Fixed width element taking it's fluid parents size?

How to hide element when it on first section of page

How do I set a text element to 0px width?

How to hide an element when a certain div is present?

Hide element when overflow

Get all parents of an element in nested object

IOS set element width constraint to 0 when in located inside UITableViewCell

How to access Parents width in Extjs?

How to Set width of div to 100% when second div hide by jQuery

How to hide image when resizing using max-device-width?

How to merge nested objects into their parents in Javascript?

Want to hide other parents child when click on any one parent element

How to show an element and hide when click on another

How to hide a nested table?

ng-pluralize hide element when value is 0 (zero)

how to hide timer when it is at 0 seconds left

Hide div when it's affected with parents 'overflow:hidden;'

Hide elements when width change

Limit Jscrollpanes width to Parents width when dynamically adding content

jQuery hide parents that have 0 children after filtering

How to hide an element when I hover on an element

Why is the width of this element not 0?

How do you hide an element when another element has a left position of more than 0?

Css problem how to apply changes when i hover a parents child element to another parents child element?

How to reduce width of container to children width when "left:0" and "right:0" are used?

How to hide the parent component data when going nested in the component?

How can I add the correct depth value to a nested json object when children have multiple parents