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

Is there a way for me to choose every element excluding those contained within a specific div?

Check out my code snippet: <div class="car"> <div class="make">NISSAN</div> <div class="model">MICRA</div> </div> <div class="discontinued"> <div class="car"> <div class="make">FOR ...

Including CSS in an HTML file does not function properly

I've been attempting to import a CSS file into an HTML document, but for some reason it's not working. I have also tried linking the path directly, but that hasn't worked either. <!DOCTYPE html> <html lang="en"> <head> < ...

Using Angular 2 to trigger an event when a native DOM element is loaded

I am working towards my main objective of having a textarea element automatically focused upon creation. I recently came up with an idea to use e.target.focus() on the onload event. It would look something like this: <textarea rows="8" col="60" (load)= ...

What is the best way to set up a condition that only runs if an element does not contain a certain class, such as "active"?

Click here for the picture I have a list of items: a, b, c, d, e, f. Whenever I click on item b, it becomes active and the other elements lose the "active" class. When an element has the "active" class, I want the background to change accordingly. If it ...

Is your WordPress one-page scroll JQuery script failing to function properly?

Currently attempting to develop a single page scroll feature. Within WordPress, my navigation contains anchor tags structured as follows: <a href="#contact">Contact</a> <a href="#about">About</a> The corresponding divs for the li ...

What is the best way to save an integer in HTML's localStorage instead of a string?

I've encountered an issue while using the localStorage feature in a game I'm developing. Specifically, the money variable should be increasing by 1 every second. Here's a snippet of my code: var money = 0; window.onload = function () { ...

Aligning text inputs inline within a Bootstrap form for a cleaner and organized

I am currently working on designing a form using Bootstrap, and I have a specific requirement where the first name and last name fields need to be displayed inline while the rest of the form remains unchanged. Although I have managed to make the inline fu ...

Having trouble retrieving the URL from JSON data - every time I attempt to access it, it just shows as undefined. Any suggestions

Having trouble extracting the URL from JSON, as it shows undefined. Any suggestions? <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" ></meta> <script language="JavaScript" type="text/javascript" ...

Using JQuery to reverse the action of hiding an image

Having some trouble with my Javascript and JQuery skills, so bear with me. I've set up a gallery of images where users can drag and drop them into a designated drop zone. Everything works fine when dragging an image from the gallery to the drop zone a ...

My draggable item seems stuck in place. Any ideas on how to get it moving again?

I have been trying to implement some code I found on this link. Despite adding all the necessary file links, the code is not functioning as expected. It should be able to move within the bounds of a container, but it's not working properly. var max ...

How should attributes be passed to the li element properly?

I am trying to transfer values from the database to the <li> tag. Can someone confirm if the approach below is valid and the proper way to do it? <li id='$current_row["RA_SUB_ID"]' component_name='$current_row["RA_SUB_NAME"]' ...

Tips for closing a popup without exiting the entire webpage

I'm facing an issue while trying to create a video playlist using two HTML files (index.html and video.html). In my video.html file, I have set up a popup for playing videos. However, whenever I close the popup or click anywhere on the page, it redire ...

Choose a specific <div> element from an external page using Ajax/PHP

I'm encountering a small issue. I am currently utilizing Ajax to dynamically load content from another page into a <div> element after detecting a change in a <select>. However, my specific requirement is to only load a particular <div& ...

Instructions for implementing tooltips on a pie chart slice when hovering with the mouse pointer, using the canvas

var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var cw = canvas.width; var ch = canvas.height; ctx.lineWidth = 2; ctx.font = '14px verdana'; var PI2 = Math.PI * 2; var myColor = ["Gr ...

A comprehensive guide to effectively formatting Recharts in React: addressing alignment and size management!

While attempting to style two graphs as floating cards, I am encountering difficulties in controlling the sizing and centering of the graphs. Specifically, I am having trouble with the pie chart in this example. To address this issue, I am passing paramete ...

Achieving a Side-Along Sidebar with CSS, Fully Embracing

I'm encountering an issue with achieving 100% height using CSS. Despite my extensive search for tutorials and solutions, I haven't been able to achieve the desired results. My layout consists of a sidebar and a main column where the content will ...

Share the hyperlink to a webpage with someone else

In my SQL command, I have a unique feature that retrieves the complete URL value of the current page: [##cms.request.rawurl##]. This code returns the entire URL. I need to send this address to another page and load it into a special div called "load-fac". ...

Issue with showing multiple images on HTML page

I'm currently working on enhancing my webpage by enabling the upload of multiple images. However, I'm facing challenges in figuring out how to obtain a valid URL for the image source and to verify if the correct number of files have been uploaded ...

Using jQuery and Bootstrap in an ASP.NET Core project

I am encountering an issue with the configuration of bootstrap and jquery within my project, causing these tools to fail to load properly. The problem seems to be that bootstrap is loading before jquery, resulting in error messages appearing when I check ...

Tips for adjusting the color of multiple classes that have a common class using a toggle feature

I am working on a simple website using bootstrap 4 and SCSS. I want to include a toggler that switches between dark and light modes on the webpage. However, I'm facing an issue with changing the background color of 6 Bootstrap cards, footer, and the t ...