The layout consists of two divs: one with a fixed height, while the other dynamically adjusts to occupy the

I am facing an issue with two div elements within a parent container. One div has a fixed height, and the other should fill up the remaining space in the parent container. The height of the parent container is dynamic and can vary between 100px, 200px, or 300px. However, the section displayed in green spills over the boundary of the blue parent section. I have added some margin to illustrate this overflow. My goal is to have the green section only fill up until the bounds of the parent without displaying any scroll bars. I am unsure how to eliminate the extra 15px that extend beyond the parent's boundaries. Any assistance would be greatly appreciated.

.container {
  width: 100%;
  height: 100px;
  background: blue;
  position: relative;
}

.child1 {
  position: absolute;
  top: 0;
  height: 15px;
  width: 100%;
  background: red;
}

.child2 {
  position: absolute;
  top: 15px;
  height: 100%;
  width: 100%;
  margin-left: 5px;
  background: green;
}
<div class="container">
  <div class="child1"></div>
  <div class="child2"></div>
</div>

https://i.sstatic.net/rHM0G.png

Answer №1

It is recommended to implement flexbox for styling

.container {
  display: flex;
  flex-direction: column;
  min-height: 150px;
  border: 1px solid red;
}

.child1 {
  height: 15px;
  background-color: yellow;
}

.child2 {
  flex-grow: 1;
  background-color: green;
}
<div class='container'>
    <div class=child1></div>
    <div class=child2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error officia, perferendis sed ipsam quae illo voluptas voluptate, obcaecati pariatuLorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
</div>

Answer №2

describe the height and width of child2 as follows:

height: calc(100% - 15px);
width: calc(100% - 5px);

.container {
  width: 100%;
  height: 100px;
  background: blue;
  position: relative;
}

.child1 {
  position: absolute;
  top: 0;
  height: 15px;
  width: 100%;
  background: red;
}

.child2 {
  position: absolute;
  top: 15px;
  height: calc(100% - 15px);
  width: calc(100% - 5px);
  margin-left: 5px;
  background: green;
}
<div class="container">
  <div class="child1"></div>
  <div class="child2"></div>
</div>

Answer №3

Utilize the top/left/right/bottom values to ensure the box fills the parent, rather than relying on

width: 100%; height: 100%; margin-left: 5px

<div style="width: 100%; height: 100px; background: blue; position: relative;">
  <div style="position: absolute; top: 0px; height: 15px; width: 100%; background: red"></div>
  <div style="position: absolute; top: 15px; bottom: 0; left: 5px; right: 0; background: green"></div>
</div>

Answer №4

It seems like you're facing a problem, so let me take a shot at it:

<div style="width: 100%; height: 150px; background: orange; position: relative; overflow:hidden">
    <div style="position: absolute; top: 0px; height: 25px; width: 100%; background: purple"></div>
    <div style="position: absolute; top: 25px; height: 100%; width: 100%; margin-left: 10px;background: yellow"></div>
</div>

Answer №5

Hopefully this information proves beneficial

.container {
  width: 100%;
  height: 100px;
  background: blue;
  position: relative;
}

.child1 {
  position: absolute;
  top: 0;
  height: 15px;
  width: 100%;
  background: red;
}

.child2 {
  position: static; //default position for browsers is static, no need to specify
  top: 15px;
  height: 100%;
  width: 100%;
  margin-left: 5px;
  background: green;
}
<div class="container">
  <div class="child1"></div>
  <div class="child2"></div>
</div>

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

Display the chosen date from the datepicker in the input field

Utilizing a JQuery DatePicker to be constantly displayed, I have assigned it to a div. Despite wanting it to display the selected date in the input box like in this example, it just isn't functioning as desired for me. Is there a way to have the selec ...

What could be the reason for the child elements not being centered in a bootstrap navbar in this basic code example

Take a look at the display output by clicking here -> http://www.bootply.com/VtPTgKeCLO Below is the code snippet: <nav class="navbar navbar-fixed-bottom top-main-navbar"> <div class="container"> < ...

Is there a way to switch the main image by clicking on different thumbnails in a sidebar using javascript/jQuery

Currently on my page, I have a large plot created with jqplot along with a sidebar containing several smaller plots. I am trying to find a way to dynamically change the large plot based on which of the smaller plots the user clicks on, without needing to ...

Tips for initiating a function at a designated scroll point on a webpage

Currently, I am testing out a code snippet that allows images to flip through in a canvas element. I'm curious if it's possible to delay the image flipping effect until the viewer scrolls down to a specific section of the page where the canvas i ...

Retrieve data from a table with XPath expressions

Looking to gather details from this specific website link: Focusing on extracting the number of bedrooms from a table. <div class="panel panel-primary"> <div class="panel-heading">Property Details</div> <div class="panel- ...

Utilizing extra CSS for a full-width Wordpress featured image makes for a visually stunning design

Is there a way to customize the CSS of a featured image in Wordpress Version 5.9.2 to appear full width? Currently, the image has a large margin and does not fill the entire width as seen on the page below: I attempted to achieve this by adding code to th ...

Capture microphone and audio in a SIP call with sip.js

Greetings Stack Overflow community! I am in need of assistance with a project involving sip.js and VoIP for making real phone calls. The Objective I aim to enable users to record audio from both parties during a call and save the data on a server (either ...

Is there a way to enable multiple selections in a particular section?

Currently, I am in the process of working on my step by step order and I want to express my gratitude for all the assistance I have received so far. Despite the help, I still have some unanswered questions! The steps in my project are categorized into dif ...

The image fails to load once the AI-generated image is complete and the response process has concluded

After a long hiatus from development, I am diving back in and acknowledging the rustiness of my skills. Any syntax or formatting errors encountered are preemptively apologized for. Currently, I am immersed in a small web app project that involves taking a ...

When the URL is modified, triggering an event to load

Is there a way to make a page load directly to a specific section and automatically open an accordion? For instance, I have a page with multiple accordions displayed vertically. I already know that using id's in the URL like page#accordion1 will scro ...

Tips on how to horizontally center align a div when using the flex direction column technique

In my design, there is a container that consists of both a table and a div. The div has its display property set to flex with the flex-direction as column. I am trying to horizontally center align the div within the container. Below is the code snippet: ...

Adding a new entry to a database that involves many-to-many relationships

I recently inquired about executing a fetch query on a database with a many-to-many relationship. As I delve deeper into learning SQL + PHP, I encountered a rather intricate scenario: Within my database, I have the following tables: Songs Link ...

Using Bootstrap 4, you can create nested rows that will automatically expand to fill their parent container, which in turn has been set

In my current setup, I have a div with the class "d-flex flex-column" that contains a navbar and a container. Within this container, there is another div with the class "d-flex flex-column" which then contains two rows. I am using flex-grow to make the con ...

TemplateUrl malfunctioning

While experimenting with basic Angular code, I encountered an issue where everything was functioning correctly when using template, but not when switching to templateUrl. I did not provide the previous code as it was working fine except for this specific c ...

The application has encountered an unexpected error and is currently unavailable

This is the code snippet for my MainActivity.java file. I am developing a simple LoginApp. However, when I try to run the app, it displays an "unfortunately stopped working ERROR..." message. package com.example.loginapp; import android.os.Bundle; import ...

There appears to be an empty void on the website

If you could kindly click on this link & use CTRL + F to search for the text "Info", you will see a visual representation similar to the image below. There seems to be quite a bit of empty space below these texts. I experimented with padding, display: ...

Incorporating a custom background image for Google Maps

Can a live Google map achieve the same overlay effect as a static image? Discover more here Many thanks, Greg Update: Check out this Fiddle that was created based on another Fiddle by @SinistraD ...

Position fixed iframe neglects right and bottom positioning

Here is the code snippet I am working with: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head></head> <body> <iframe src="htt ...

Trouble aligning content in CSS? Try using justify-content center for perfect centering!

I am facing an issue with centralizing the content properly. Despite using justify-content:center, the layout breaks. When I apply justify-content:center, the layout appears like this: Here is the HTML / JSX code snippet: <div className={styles.co ...

Effortlessly Styling Children Elements in Emotion JS When Parent Element is Hovered

This particular query seems to have been posed and resolved in various ways, but the solutions I've come across either do not pertain to Emotion or the responses related to Emotion haven't yielded results for me. I am currently using @emtion/[ema ...