How can you conceal nested elements within a layout that adjusts to various screen sizes or is percentage-based

Contemplate this HTML:

<body>
  <div id="parent">
    <div id="child">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras dapibus sapien congue, fringilla justo quis, aliquam tellus. Maecenas semper consectetur metus eget varius. Curabitur imperdiet sagittis leo, a mollis dui lobortis in. In ac facilisis tortor. Nam vitae pellentesque lorem, non commodo lacus.
    <div>
  </div>
</body>

The objective here is to conceal the child div within the parent div using overflow:hidden. However, the goal is to eventually modify/animate the width of the parent div to 'uncover' the content of the child. I am not seeking the code for the animation/change, but rather a foundation of HTML/CSS that allows for runtime modification while adhering to these initial rules:

  • the parent should have a percentage width relative to the screen (to enable width modulation for hiding/revealing the child).
  • the child should also have a percentage width relative to the screen.
  • the child's content must not wrap according to the parent's width.
  • the parent should reveal only as much of the child's content as possible given its width.
  • Avoid absolute pixel widths.
  • No use of CSS3 features.
  • No inclusion of JavaScript.

Illustration:

Here is an example of the desired outcome:

In this demo, only the portion of the child div enclosed by the parent div should be visible. The child's content should not wrap, and both elements should occupy percentage widths of the screen.


Previous Attempts:

I've experimented with the following CSS so far:

html, body{

  height:100%;

}

#parent{

  position:relative;

  width:15%;
  height:100%;

  overflow:hidden;

}

#child{

  position:absolute;

  width:200%;

}

The issue with this CSS snippet is that the child div constantly adjusts its width based on the parent div, making it nearly impossible to gradually 'reveal' the child div by increasing the parent's percentage width until the child's content fully "unwraps."


Potential Questions:

  • Why not remove position:relative from the parent to maintain consistent child width?

This adjustment is necessary for concealing an absolutely positioned child element, as the parent needs to have a relative position.

  • Why not set the child element as static or relative?

If so, the child's content would wrap according to the parent, impeding any attempts at discreetly 'hiding' content.

  • Why is the child dimension set to 200%?

To maintain a relative width of 30% (in relation to the screen). Given the parent occupies 15%, the child must span twice the parent's width to fill 30% of the screen.

  • Can we employ fixed pixel widths?

No.

  • Can we implement CSS3 techniques?

No.

  • Can we utilize JavaScript?

No. If JavaScript were permissible, I wouldn't be pondering this query—I'd be enjoying some drinks at the pub. :-)

Answer №1

After some experimenting, I've devised a potential workaround:

You'll require an additional wrapper:

<div class="parent">
<div class="parent2">
<div class="child">
</div>
</div>
</div>

Next, apply this CSS:

.parent {
    position: relative;
    height: 90%;
    width: 100%;
    left: -70%;
    border-right: 1px solid red;
    overflow: hidden;
}
  
.parent2 {
    position: absolute;
    width: 100%;
    height: 99%;
    left: 70%;
    border: 1px solid red;
    overflow: hidden;
}

.parent, .parent2 {
    -webkit-transition: all 2s;
}

.child {
    position: absolute;
    width: 50%;
    height: 80%;
    border: 2px solid green;
    background: url("http://placekitten.com/200/300");
    background-size: cover;
}

.parent:hover {
    left: -40%;
}

.parent:hover .parent2 {
    left: 40%;
}

This technique creates a "sliding window" effect. The parent containers adjust position rather than size to reveal more or less of the child.

Fiddle Demo

The hover transition is already set up. Additionally, this eliminates the need for calculating the child's width artificially.

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

Issue with scrollspy causing navigation items to not highlight correctly

If you want to track my progress, you can view it here - . Although clicking on the links in the navigation bar scrolls the page to the correct location, the link itself is not being highlighted. To address this, I had to incorporate an offset. var offset ...

How can we use Jquery to add animation effects to our styling

I am a beginner with JQuery and struggling to make the code below work correctly. $('.announcement_panel_button').click(function(e){ $('#site_logo').animate({ 'margin-top': '5px' }, 5000); e.pre ...

Ways to alter the color of individual pseudo elements using a CSS loop

While working on creating a typewriter animation effect for my website using only CSS that I discovered through some online research, a question popped into my mind - is it possible to set different colors for each text in the animation? I have included t ...

Items seem to vanish into thin air and become immovable when attempting to relocate them

I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence. I have hit ...

What are some techniques for managing scrolling within a particular element?

I am currently working with Vue.js and utilizing Element UI components. I want to incorporate a scroll management function to achieve infinite scrolling. To better understand, please refer to the screenshot in the Example section: Despite trying differen ...

Looking for assistance with CSS border animation

Below is my code snippet: .section-one { position: relative; background: #ffffff; } .section-one h2 { color: #000000; font-size: 32px; margin: 0px 0px 10px 0px; padding: 0px; font-family: "AzoSans-Medium"; } ... /* ...

What is the best way to organize and position numerous images and text elements within a flex container?

Currently learning the ropes of web development and working on a practice project. My goal is to utilize flexbox in order to achieve a layout similar to this design. Here's what I have so far: .flex-container { display: flex; align-items: cente ...

Element with a fixed position within an iScroll container

Has anyone encountered an issue with fixing an element with position: fixed inside a big Iscroll page? It seems like the element scrolls along with the content due to the transition imposed by Iscroll. I'm trying to fix the "FIXTHIS" element, but it ...

Updating and deleting Firebase data from an HTML table: A guide

I am struggling to make the onClick function work in order to update and delete already retrieved data from an HTML table view. Despite trying different approaches, I can't seem to get it right. var rootRef = firebase.database().ref().child("prod ...

Troubleshooting Problems with CSS Printing on iOS Devices

On desktop (both Windows and Mac), the print preview displays correctly. However, on iOS devices, the full width is not shown and there is some blank space above. The issue I'm facing is that I cannot debug this problem using Browserstack. I am restr ...

What is the most effective way to utilize a loop in order to generate a comma-separated list of values that does not contain an additional comma

Using @for to generate multiple box-shadow arguments: .myclass { $bxsw : ""; @for $i from 1 through 10 { $bxsw : $bxsw + " -" + $i + "px -" + $i + "px " + brown + ","; } box-shadow: #{$bxsw}; } This results in: .myclass { bo ...

Proper alignment of multiple elements with Bootstrap

I am currently incorporating Bootstrap panels into my project, and I have a design mockup that looks like this: The Profile text and progress bar need to be aligned to the left, while the collapse icon should be aligned to the right. Here is the code sni ...

Want to learn how to center a webpage only for a specific device width? For example, have your mobile device display the content in the center, while

Hello! I'm new to the world of @media queries and am currently trying to make my webpage responsive by centering it on smaller devices while keeping the display normal on larger screens. My website, mastercontrolunit.com, looks great in deskto ...

How can I achieve the functionality of an image changing when clicked and going back to its original state upon release with the help of HTML

I am facing an issue with styling an element to look like a button and function as a clickable link. My goal is to create a visual effect of a pressed button when it is clicked, while also loading the target page. For reference, here is a (non-working) J ...

Is it possible to generate various resolutions of an image simultaneously?

While trying to download wallpapers from a link on this website, I encountered an issue. Upon clicking the download button, a new page opened with the URL "https://images.wallpapersden.com/image/download/street-fighter-fortnite_bGtoaW6UmZqaraWkpJRmbmdlrW ...

Unable to override default styles with custom styles in React MUI 5

I've been attempting to replace the default MUI 5 Button styles with my custom styles using either withStyles or a className (created using makeStyles), but I'm running into an issue where the default styles take precedence over my custom ones. I ...

Loading an empty CSS file in Node.js

Just starting out with node.js, and I could really use some help with a small css and image issue that I'm facing. I've streamlined my html and .js for clarity. Despite trying everything, the css and image just won't load. My form and server ...

Eliminate the focus border in React-Select

I've been struggling to remove the border or outline (not really sure which one it is) from React Select when it's focused. Here is an image for reference. As you can see, I currently have no default border: https://i.stack.imgur.com/IubaN.png ...

Develop an onclick function with an href attribute

I am interested in transforming links into AJAX versions whenever possible. To achieve this, I plan to implement a function called replaceLinks that will add an onClick handler to each link on the page and trigger ajaxPageWSM(href). This is what I currentl ...

To view the following set of three images, simply click on the "load more" button

I'm looking to add a load more button to reveal additional images. Currently, the page loads with 3 images visible, and upon clicking the load more button, the next set of 3 images should be displayed on the screen. Unfortunately, the code I've ...