What is the proper way to conceal overflow content effectively?

$('.child_1').click(function(){
  $(this).toggleClass('left').siblings().toggleClass('left');
});
* {box-sizing: border-box;}
.parent {
  width: 500px;
  position: relative;
  border: 1px solid #ccc;
  overflow: hidden;
}
.child_1 {
  background-color: rgba(255,255,0,.6);
  width: 100%;
  float: left;
  height: 100px;
  z-index: 1;
  transition: all .5s;
}
.child_1.left {
  width: 30%;
}
.child_2 {
  width: 70%;
  padding-left: 15px;
  left: 100%;
  transition: all .5s;
}
.child_2.left {
  left: 30%;
}
.child_2 > div {
  background-color: rgba(0,255,255,.6);
  height: 200px;
}  

.child_2 > div > div {
  overflow: hidden;
}
.relative {
  position: relative;
}
.absolute {
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<p>Relative</p>
<div class="parent">
  <div class="child_1"></div>
  <div class="child_2 relative">
    <div>Content of child_2<div></div></div>
  </div>
</div>
<hr />
<p>Absolute</p>
<div class="parent">
  <div class="child_1"></div>
  <div class="child_2 absolute">
    <div>Content of child_2<div></div></div>
  </div>
</div>
I am facing an issue with this code structure:

  1. The parent div needs to have overflow set to hidden in order to conceal child_2 when it extends outside the parent boundary;

  2. The parent div cannot have a specified height because the exact height of child_2 is unknown (even though it's provided in the example).

To hide the child_2 div, I have experimented with two approaches:

  1. The first method involves relatively positioning child_2, but as you can observe, the content inside child_2 ends up in an incorrect position;

  2. The second approach uses absolute positioning for child_2, however, an absolutely positioned element is unable to determine the parent element's height, resulting in the content within child_2 getting cut off when the parent div has hidden overflow.

Answer №1

$(document).ready(function() {
   $('.child_1').click(function(){
  $(this).toggleClass('left').siblings().toggleClass('left');
});
});
* {box-sizing: border-box;}
.parent {
  width: 500px;
  position: relative;
  border: 1px solid #ccc;
  overflow: hidden;
  height: 100px; /* Adding height to parent div */
}
.child_1 {
  background-color: rgba(255,255,0,.6);
  width: 100%;
  float: left;
  height: 100px;
  z-index: 1;
  transition: all .5s;
}
.child_1.left {
  width: 30%;
}
.child_2 {
  width: 70%;
  padding-left: 15px;
  left: 100%;
  transition: all .5s;
}
.child_2.left {
  left: 30%;
}
.child_2 > div {
  background-color: rgba(0,255,255,.6);
  height: 200px;
}  

.child_2 > div > div {
  overflow: hidden;
}
.relative {
  position: relative;
}
.absolute {
  position: absolute;
}
<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" type="text/css" href="sample.css">
</head>

<body style="display:table">
    <p>Relative</p>
    <div class="parent">
        <div class="child_1"></div>
        <div class="child_2 relative">
            <div>1
                <div></div>
            </div>
        </div>
    </div>
    <hr />
    <p>Absolute</p>
    <div class="parent">
        <div class="child_1"></div>
        <div class="child_2 absolute">
            <div>1
            </div>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</body>

</html>

I have added height to parent div, then overflow works fine

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

Issues with undesirable spacing in CSS using the display property and table-cell display type

Having trouble grasping the concept of table and table-cell in css? See this example: http://jsfiddle.net/dd7h7/3/. HTML <div class="widget"> <div class="button"> <div class="content"> <div class="icon">A</div> ...

Ways to update the HTML layout for certain components using the Selenium driver

Even though I am still learning about selenium, it appears that the driver initially caches the HTML, causing any subsequent modifications to go unnoticed (unless the page is refreshed). This is a snippet of what my HTML code looks like: <div class="q ...

Tips for creating two HTML buttons to switch between images

I have a pair of robot images, one displaying the front and the other displaying the back. I am looking for HTML or CSS code to switch between the two images by clicking on them, without using JavaScript or jQuery. I came across a helpful tutorial on this ...

Is it possible to use Sass properties as arguments for my function?

My attempt to use SASS properties as arguments for my function seems to be failing miserably. Is there a way I can achieve this, or is there a better solution to what I'm trying to do? I am looking to dynamically change the values of variables based ...

What is the best way to position the label above the input text in a Material UI TextField component with a Start Adornment?

Struggling to figure out the right Material UI CSS class to style a reusable TextField component? You're not alone. Despite tinkering with InputLabelProps (specifically the shrink class), I can't seem to get it right. Here's the code snippet ...

How can I use the store command to retrieve the href value of a link using a css selector in Selenium?

Is there a way to store a URL from a link using CSS instead of xpath? store //tr[td[contains(.,'6 Day')]][1]/td[8]/a@href my_var open $my_var If so, how can I achieve this goal with css? I managed to use the following locator: store css= ...

Can anyone offer guidance on how to properly parse the " " character after consolidating an array into a

When working with Vue.JS, I encountered a situation where I had an array in an object that was parsed as a string like this: destination: item[1].destination.join('\n') Upon checking console.log(obj), the output appeared as follows: destin ...

Unable to incorporate CSS background image

https://i.sstatic.net/3ZLq3.jpg[ I've been working on designing the main page using Bootstrap, but I'm facing an issue with adding a background image. I've tried multiple methods such as adding it to the CSS body and directly in the HTML co ...

Using Javascript to dynamically change the data type to align with a specific keyword

I need to create an input box for a promo code that triggers the appearance of additional radio input options when a specific promo code is entered. For example, if the user enters "springchicken" in the input box, I want a set of radio inputs to show up ...

RangeError: The React application has surpassed the maximum stack size limit, causing an error to be thrown

Hey there, I could use a hand. I'm fairly new to React and attempting to develop an application for managing contacts by adding them to Local Storage and deleting them. Below is the code snippet from my App.js file: import React, {useState, useEffect} ...

Using CSS to disable an image map in conjunction with dynamically adjusting CSS styling

On the desktop version of the website, there is an imagemap implemented. However, when the site transitions into tablet or mobile view, the imagemap should be disabled to allow for a simpler navigation area to be displayed. I attempted to use the following ...

An automated solution designed to launch external website links in a new tab or window

I currently have a website where links open in the same window if they lead to pages on my site, and in a new window if the domain is different. However, I'm using manual code like this: <a href="http://www.example.com" target="<%=checkdomain(" ...

Transform your CSS3 rotateY into PHP Imagick::distortImage

I'm trying to rotate an image by rotateY(-54deg) on the front-end, and now I need to achieve the same rotation in PHP using Imagick::distortImage. $image->distortImage(Imagick::DISTORTION_PERSPECTIVE, $controlPoints, true); Is there a straightfor ...

In Firefox, the scaling of svg masks is not functioning properly

Trying to utilize an svg for masking an image, but encountering scaling issues in Firefox. Safari and Chrome seem to display correctly, with proper fallbacks for IE. Website: SVG File: CSS: -webkit-mask: url("../img/feelfilms-logo.svg#logo_mask"); mask ...

Using HTML in conjunction with Javascript, the window.prompt function can be

As someone who is brand new to javascript and html, I wanted to create a simple example that takes user input and displays it in a table. However, I'm struggling to get the window prompt to show up. I have a feeling that I am making a very obvious mis ...

CSS Tricks: Placing a Panel Just Off the Page Edge

Having difficulty adjusting a panel slightly off the page like this: click here for image Attempted using width: 120% While it does work, resizing causes the image to move from its original position. I want it to remain consistent on different screens. ...

Can you explain the distinction between place-items and align-items?

Wondering about the distinction between using display: flex; align-items: center; versus display: flex; place-items: center; Although they look similar visually, it's worth noting that place-items has 90% browser support while align-items has 92%. ...

How can I import HTML code containing JS from an external source in just one line of code?

I am looking to add external functionality to my web page using a single line .js import that works similar to a plugin. Here is what I currently have: <!--index.html--> <head> </head> <body> <!-- Here is the one-line scrip ...

Making a page jump with Javascript

Welcome! Let's dive into our question. Below you'll find my script and HTML code: My Script: const resultEl = document.querySelector('.allquotes'); const pageSize = document.querySelector('select[name="page-size"]') ...

What is preventing the container from occupying the full height?

Currently, I am working on creating a survey form page. I have successfully created a div with the id "container" which holds all the elements on my site. For the code snippet and reference, you can check out this link CODE * { margin: 0; padding ...