Make the Height of the Parent Div Based on the Position of its Children

If you're looking to design a card with three child divs, where only one is visible at a time and transitions smoothly as the user progresses through steps 1, 2, and 3, then you've come to the right place.

In the code snippet provided below, the parent container's height should be dynamically set to span only 30px, matching the height of the children divs. This ensures a neat overlap effect when transitioning from one child div to another.

.container {
  border-style: solid;
  border-width: 2px;
  width: 30px; /* Dynamically set */
}

.childOne {
  position: relative;
  height: 30px;
  background-color: red;
}

.childTwo {
  position: relative;
  top: -30px;
  height: 30px;
  background-color: blue;
}
<div class='container'>
  <div class='childOne'></div>
  <div class='childTwo'></div>
</div>

Answer №1

Utilize CSS grid to ensure that the child elements are placed in the same area:

.container {
  border-style: solid;
  border-width: 2px;
  width: 30px;
  display: grid;
}

.container>* {
  grid-area: 1/1;
  height: 30px;
}

.childOne {
  background-color: red;
}

.childTwo {
  background-color: blue;
}
<div class='container'>
  <div class='childOne'></div>
  <div class='childTwo'></div>
</div>

Answer №2

In my view, the best approach would be to utilize JavaScript for this task. I've put together a sample code snippet for you below. I started by adjusting your CSS and adding comments for the second page (slide) top property:

.container {
        border-style: solid;
        border-width: 2px;
    }

    .childOne {
        position: relative;
        height: 200px;
        background-color: red;
        display: block;
    }

    .childTwo {
        position: relative;
        /*top: -200px;*/
        height: 200px;
        background-color: blue;
        display: none;
    }

I've included two simple buttons to trigger the action, but there are multiple ways to achieve this:

<div class='container'>
    <div class='childOne'></div>
    <div class='childTwo'></div>
</div>

<button onclick="slideTo(1)">Slide One</button>
<button onclick="slideTo(2)">Slide Two</button>

Below is a straightforward function to modify the display property of each page (slide). Since we only have 2 pages, I kept it simple:

<script>
    let childs = document.querySelector(".container");
    let totalPages = childs.children.length;

    function slideTo(page){
        // Convert to integer
        var page = parseInt(page);
        // Pages start at 0
        page -= 1;
        // Invalid page...
        if (page >= totalPages || page < 0){
            return;
        }else{
            if (page === 0){
                childs.children[page].style.display = "block";
                childs.children[page+1].style.display = "none";
            }else{
                childs.children[page-1].style.display = "none";
                childs.children[page].style.display = "block";
            }
        }
    }
</script>

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

The Enigma of the Empty Gap When Employing "display:table/table-cell"

Struggling to understand why there is a blank space between the image and the menu. Here is the HTML and CSS related to it: <header class="header_nav"> <img src="img/logo.png" class="logo"/> <nav class="nav_bar"> <u ...

Is there a way to specifically target the parent input div using event delegation with jQuery?

I'm having trouble inserting values into the input fields for Item Name and Description. See the HTML code below. <div class="input_fields_wrap"> <div class="input_fields_subwrap"><!-- this div is repeatable(dynamic form)--> ...

Ways to have the right sidebar seamlessly blend in with the page content by hovering over it rather than occupying a separate area

While working on the design of my website, I encountered a challenge with the sidebar. I aim to have a sidebar that hovers from the right side to the left, displaying the full content when the cursor is placed over it, much like the one featured on . Altho ...

Tips for creating td/th elements with a consistent width?

I am currently working on a website where I need to create a table with fixed width td tags, rather than the entire table having a fixed width. This is necessary because I will be dynamically showing and hiding columns using jQuery, and also using a jQuery ...

Tips for modifying the font style of a Bootstrap form input using a custom font

While developing my website using Bootstrap 4, I encountered an issue with the custom font (GeosansLight) I applied to the entire website through the body's CSS attribute. The problem arose when the default Bootstrap form inputs appeared too light to ...

Tips for creating clickable hyperlinks within a chat platform

I'm currently working on a chat feature for my website that pulls messages from a JSON file and displays them using Vue.js. The issue I'm facing is that when a user includes a link in their message, it does not automatically become clickable beca ...

HTML5 video player with secondary playlist

Looking for a videoplayer setup where there are two playlists, but only one can play at a time. When choosing a video from the first list initially, nothing happens. However, after selecting a video from the second list, the first list starts working. HTM ...

Utilize ajaxFileUpload.js to upload 3 images from various input type files in CodeIgniter effortlessly

I'm looking to enhance my code by enabling the upload of three images from different input types along with a text field using a single submit button function. The current code works fine when uploading only one file, but issues arise when attempting ...

Exploring ways to create a dynamic background image that allows the rest of the page to float over seamlessly

Currently working on a responsive website and almost done with the build. However, I came across a site where an image appears to be floating behind the rest of the webpage content. Tried searching for a solution using w3.css without any luck. Any sugge ...

Failure occurred when attempting to link and display on the page container

I have created a simple app using jQuery Mobile. At some point in the app, I include the following code: <a href="test_es.html" data-role="button">Start!</a> This code snippet loads a new HTML file that contains several jQuery Mobile page ...

Switch on and activate a button using AngularJS

I have a set of four buttons that I want to toggle and activate upon clicking them. Currently, the buttons toggle when double-clicked. My desired solution is for the button current btn to be highlighted and display data when clicked, and for the previous ...

Create a JavaScript/jQuery function that causes an element to fade in when the mouse is

I am currently developing a PHP/MySQL project that involves creating a function to display a brief description when a user hovers over an image. However, I seem to be encountering an issue where the function crashes after a few uses. Below is a snippet of ...

Updating the text of one option within a selected option using jQuery while iterating through each element in a function

Hey there, I have multiple rooms each with two select dropdowns. My problem is that when I choose the option with a value of 4 in the number of persons dropdown, I only want the text of the next option (the dinner select) to change under the room I selecte ...

Opting for css3 translate over using position: left for positioning elements

Why Using translate() for Element Movement is Superior to Pos: abs Top/left Read the full article Lisa Anderson I struggled with implementing a CSS3 translate animation, so I opted for a jQuery solution that achieves the same effect. Click on the X icon . ...

The border is not displaying for the Child Div element

I am struggling with a webpage that has two nested divs. Despite trying all the suggestions I have found, I still cannot get the inner div to display its border. On the other hand, I have noticed that the parent div's border shows up without any issu ...

Revamp the appearance of angular material inputs

I have been working on customizing the style of an Angular Material input. So far, I successfully altered the background-color with the following code: md-input-container { padding-bottom: 5px; background-color: #222; } I also changed the placeh ...

Utilizing Bootstrap 4 columns to control the height of a separate column

Is there a way in CSS to make one column in a bootstrap grid determine the height of another column within the same row? For example, consider the following scenario: <div class="row"> <div class="col col-sm-6" id="column1"> <!-- an e ...

Having trouble getting the sorting/search function to work with a click event to display content. It seems to only work with a single item - possibly an issue with the

Creating a function that involves multiple boxes with a search function. The search function is working for the most part, but when clicking on a result, certain content should display. function filterFunction() { var input, filter, ul, li, a, i; ...

Undisclosed iFrame technique for uploading files - issue with nested forms

This question seems to be more related to JavaScript/jQuery/DOM rather than Rails, but it's all linked to how I integrated it in Rails. I'm currently working on allowing users to upload multiple photos while creating a new object (auction). The c ...

Uncertainty surrounding the extent of a button's scope within an Angular application

(click) event in one instance of a component is causing the function in another instance to be triggered unexpectedly. I am having trouble describing this issue. For reference, I have included a working example on stackblitz. When clicking on both buttons ...