What is the best way to conceal a nested element if its parent's width is set to

I am attempting to dynamically hide a nested paragraph tag based on its parent's width. I want the nested p tag to adjust its visibility as the parent's width changes, either expanding or contracting. I believe there is a simple solution for this, and I appreciate any assistance in advance.

Check out the demo here

HTML Structure:

<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 Styling:

.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 Interaction:

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

Answer №1

In order to hide the p tag when the parent width is 0, you need to apply the overflow:hidden; property to the ".floater1" class.

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>

Check out the live demo on this fiddle link

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

Answer №3

Include the following CSS styles in your code:

.floating-text p {
    opacity: 0;
    transition: opacity 250ms ease;
}
.floating-text.display {
    width: 30px;
}
.display p {
    opacity: 1;
}

I trust this solution will be beneficial!

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

Create a unique HTML id dynamically using AngularJS

Hello, I am looking to create a unique identifier in html in the format 'a1', 'a2', etc. based on the values in the table. I am thinking of achieving this in the following way: <div ng-controller="ddController" > ...

Executing JavaScript in HttpClient or HtmlUnitHow to trigger javascript in HttpClient or HtmlUnit

Currently, I am using the HttpClient POST method to perform a specific action on a website. This involves using Javascript for an ajax connection which generates a unique requestID in the form of var reqID = Math.floor(Math.random()*1000001);. I need to ac ...

Creating dynamic drawer animations with jQuery in an Angular application

Concealed behind a hidden panel using absolute positioning and z-indexing is a drawer. Triggered by a button on the panel, the drawer is revealed through jQuery animation that adjusts the top margin. This approach with jQuery is specifically chosen to ensu ...

Don't forget to keep track of the number of tabs in AngularJS

In my mobile application, I am utilizing AngularJS to allow users to add products to their orders. Different tabs are used so that users can switch between categories, with the corresponding products displayed for each category. Users can add these product ...

Set the height of the vertical scroll at a fixed 100% floatValue

Check out my creation at http://jsfiddle.net/ZygnV/ html, body { margin: 0; padding: 0; height: 100%; } .main-content-wrapper { height: 100%; overflow-y: hidden; white-space: nowrap; } .main-sidebar { display: inline-block; height: 1 ...

What is the best way to update the img src attribute of a different element when a specific image is clicked?

Whenever an HTML element is clicked, I need the data attribute of that element to be displayed correctly, specifically an image. For example, when "Ken" is clicked, the image in his data-fighter attribute should replace the current image in div.portrait ...

Javascript promises executing in a mixed-up sequence

Utilizing Javascript's native Promise, I created a modified version of fs.readFile called readFileAsync. This function reads and parses a JSON file, returning the object when resolving. function readFileAsync(file, options) { return new Promise(fun ...

Is there a way to access multiple values from my function array in Javascript instead of just the first one?

function findDistinctElements(inputArray) { let distinctItems = []; for (let i = 0; i < inputArray.length; i++) { const currentItem = inputArray[i]; if (!distinctItems.includes(currentItem)) { console.log(`Argument ${i+1}: $ ...

Make a line break occur automatically after every 7 divs are displayed

Looking to create a grid of equal height and width divs using AngularJS to form a 7x5 square: ---------------------- | | | | | | | | ---------------------- | | | | | | | | ---------------------- | | | | | | | | ---------------------- ...

The Render function in ReactJS is not getting refreshed

My goal is to implement a chat feature using material UI. I have set up a function where users can submit new chat messages, which then go through the reducer and are stored in the redux store. The process seems to be working fine, except for the fact that ...

The JSON request object has not been defined

I am currently facing an issue while trying to make a JSON request from the open weather API example. When I attempt to access the key value in the $.each method, it keeps showing that the value for the key is undefined. Can someone please review the code ...

Creating a query string in MongoDB using [Object] as a field

Currently, I am in the process of constructing a string within Meteor to delve deeper into my MongoDB data. The structure of my data can be seen here: Data In my JavaScript code for Meteor projects, I have formulated the string as shown below: const co ...

Steps to substituting characters within a date string

Create a function called normalize that changes '-' to '/' in a given date string. For example, normalize('20-05-2017') should output '20/05/2017'. This is my attempt: let d = new Date('27-11-2021'); fun ...

InvalidType: entity does not function as a function

I'm not sure where this issue is originating from, as version 4.0 of CO and version 0.11.14 of Node are installed. The code itself is very simple and worked fine with version 3.1 (just realized the earlier version of co was different) "use strict" co ...

What causes the variation in width when I hover over this particular element?

#gallery { padding: 2rem 7%; } #gallery .row { display: flex; align-items: center; justify-content: center; } #gallery .row .col-md-3 { position: relative; overflow: hidden; } #gallery .row .col-md-3 img { height: 100%; width: 100%; ob ...

Unexpected error in JavaScript, Curl, and Node interactions

After sending some JSON to a Node server using CURL, I faced an unexpected issue. Even though the data was received successfully and printed using console.log(...), attempting to run JSON.parse(...) resulted in an error: undefined:1 '{command:print ...

Having trouble keeping the HTML text centered within the div on a responsive layout

Having an issue with keeping text centered in the middle of a div that overlaps an image. It looks fine on desktop but when I try to make it mobile-friendly, things go haywire. I believe the problem lies in using fixed heights for the div, as setting heigh ...

Why does my ball defy the laws of geometry and refuse to be perfectly round with

I'm attempting to create a simple game in javascript, but I've run into an issue where my circle isn't perfectly round. Despite searching for solutions online for 20 minutes, I haven't been able to resolve the problem. I'm hoping s ...

Tips for resolving the error "In vue.js, a default export should be at the top level of a file or module declaration"

Help! I'm encountering an error while attempting to shuffle a word in vue.js using the "shuffle" function from the lodash library. The problem seems to be originating from this piece of code: import { shuffle } from 'lodash' // Get word of ...

Angular Form Group without a name

Struggling to loop through my formgroups to identify which ones are valid and add the invalid ones to a new array. I can't seem to grab the name value for each formgroup. this.applicationFormArray = new FormGroup({ selectAppFormGroup:th ...