The positioning attribute and flexible height in CSS are essential for creating dynamic

My goal is to create a layout with 3 DIVs inside a container, similar to table rows.

top {height = 100px} / middle {height = dynamic} / bottom {height = 100px}

I am trying to figure out the best approach to make the height of the middle div dynamic while maintaining the correct structure.

HTML

<div id="notification">
<div id="n-top">
    top
</div>
<div id="n-middle">
    middle<br /><br /><br /><br /><br />middle
</div>
<div id="n-bottom">
    bottom
</div>
</div>

CSS

#notification {
    position:absolute;
    left:10px;
    top:10px;
    width:175px;
    background: yellow;
}

#n-top {
    position:absolute;
    left:0px;
    top:0px;
    width:175px;
    height:50px;
    background: blue;
}

#n-middle {
    position:absolute;
    left:0px;
    top:14px;
    width:175px;
    background: red;
}

#n-bottom {
    position:absolute;
    display:block;
    left:0px;
    bottom:0px;
    width:175px;
    height:50px;
    background: green;
}

Answer №1

I found a solution that worked perfectly in my case.

#notification {
    position:absolute;
    left:10px;
    top:10px;
    width:175px;
    background: yellow;
}

#n-top {
    position:relative;
    left:0px;
    top:0px;
    width:175px;
    height:50px;
    background: blue;
}

#n-middle {
    position:relative;
    left:0px;
    width:175px;
    background: red;
}

#n-bottom {
    position:relative;
    display:block;
    left:0px;
    bottom:0px;
    width:175px;
    height:50px;
    background: green;
}

Keep in mind that using absolute positioning removes the element from the normal flow of the page. By changing the absolute positioning to relative, the elements are now able to maintain their position on the page instead of being displaced. This adjustment allowed the elements to be correctly placed one after the other.

I hope this explanation clarifies things for you.

Answer №2

To create space at the top and bottom of your middle section, simply apply margin to those areas. Surround your middle section with a wrapper that has position:relative, and place the top and bottom sections within the wrapper. Set the top and bottom sections to position:absolute and ensure their height matches the margin you set.

Answer №3

If you have fixed heights for the top and bottom <div>s, one solution is to set the middle <div> to position 0,0 with a height of 100%. Add a top margin to match the height of the top <div> and a bottom margin to match the height of the bottom <div>.

Answer №4

It appears to me that removing the positioning attributes (absolute, top, left, bottom) for the inner divs in your CSS solves the issue of maintaining the correct appearance regardless of the middle div's size. Can you clarify if there is a specific requirement for absolute positioning on those inner elements?

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

Struggling to locate the CSS needed to perfectly center my Wordpress lightboxes

There seems to be an issue with the alignment of images on my website when opened in a lightbox (using the Lightbox Plus Colorbox plugin on Wordpress). Instead of appearing in the center, the images stubbornly show up at the top left-hand side of the scree ...

I cannot seem to alter the background color of my image through the use of external CSS

body { background-color: #f6f7d4; } h1, h3, hr { color: #68b8ab; } hr { width: 100px; border-style: dotted none none; border-color: gray; border-width: 5px; } img { background-color: black; } Although the external CSS code a ...

Trigger fixed element to appear/disappear when scrolling using JavaScript

I've tried multiple solutions, but I can't seem to get this functionality to work properly on my website. I'm attempting to create a "schedule a demo" button similar to the one on www.bamboohr.com Currently, I have the following html, css, ...

Why does the video cover extend past its lower boundary in HTML?

I'm facing an issue where a purple cover over the <video> element extends slightly beyond the video's bottom border. Here's the code I'm using: .media-cover { display: inline-block; position: relative; } .media-cover:after ...

Refreshing a specific div within an HTML page

I am facing an issue with the navigation on my website. Currently, I have a div on the left side of the page containing a nav bar. However, when a user clicks a link in the nav bar, only the content on the right side of the page changes. Is there a way t ...

Ways to prevent the hover state from activating when the mouse is in the corner of a circular QPushButton

I'm working on customizing a QPushButton that has round corners: https://i.stack.imgur.com/g8zfs.png When the mouse hovers over the button, I want to change its style: https://i.stack.imgur.com/SDm79.png To achieve this, I utilized setStyleSheet f ...

Guide on adding dual horizontal scroll bars to a v-data-table (Vue / vuetify)

Currently, I am working on Vue / Vuetify and experimenting with the v-data-table component. While creating a table, I noticed that it has a horizontal scroll bar at the bottom. https://i.stack.imgur.com/noOzN.png My goal now is to implement two horizontal ...

My Javascript file is not being recognized by MVC

Initially, I created an MVC application without fully understanding that MVC is more backend-oriented than frontend-focused. I ended up building a simple website with just HTML, CSS, and Javascript files, which worked perfectly. However, when I tried to in ...

"Rotated element in Opera does not maintain event functionality when positioned outside its

Make sure to test this example on Opera (version 12.02) by clicking this link. In Opera, try clicking the red box inside the blue box (event is triggered), then click the red box outside the blue box (event isn't triggered). The container must have p ...

Numerous sections of content

Struggling to align these two pieces of content side by side. While I've had success displaying content like this in the past, I'm hitting a roadblock this time around. Any assistance would be greatly appreciated. HTML <div class="block-one" ...

What are some methods for toggling text visibility in PHP?

I am facing confusion regarding the functionality of this code. Here is the snippet in question : //start date to end date <?php if($show5 < $show6) { ?> <a>show content</a> <?php }?> If both 'start da ...

Tips for adjusting custom spacing margins within Material UI Grid

Is there a way to adjust the spacing between Grid items in Material UI? Here's the code I currently have: <Grid container spacing={1}> <Grid item xs={6}>Node 1</Grid> <Grid item xs={6}>Node 2</Grid> ...and so ...

The Opera browser displays a horizontal orientation for vertical menus

Having some trouble with my vertical menu rendering correctly in different browsers. It's appearing horizontal in Opera, but looks good in Firefox and Chrome. I'm pretty sure it's just a small tweak needed in the code, but I can't quite ...

Adjust the height of a <div> element to fit the remaining space in the viewport or window automatically

I am working with the following HTML code: <html> <BODY> <DIV ID="holder"> <DIV ID="head_area">HEAD CONTENT GOES HERE....</DIV> <DIV ID="main_area">BODY CONTENT GOES HERE</DIV> ...

utilizing Javascript to insert a class with a pseudo-element

Witness the mesmerizing shining effect crafted solely with CSS: html, body{ width: 100%; height: 100%; margin: 0px; display: table; background: #2f2f2f; } .body-inner{ display: table-cell; width: 100%; height: 100%; ...

The navigation bar struggles to display list elements without proper line breaks

Recently, I've been immersed in a web development project for a company. For the navigation bar, I opted to use the FlexNav JQuery plugin. While referencing the example on , I encountered an issue when attempting to add more than 5 list elements; they ...

Scroll Bars do not appear on mobile devices until they are scrolled

On this page, I have included a table with a maximum height. If the content exceeds this height, a custom scrollbar should appear. However, there is an issue on mobile devices where the scrollbar does not display until the content is scrolled. Click here ...

Could someone provide a detailed explanation on the functionality of multiple inline nth-child() in CSS?

In this scenario, there is an example provided: html>body>#wrapper>div>div>div:nth-child(1)>div:nth-child(1)>nav { I am curious about the functionality of this code. Could someone dissect each div, ">", and nth-child()? Your ins ...

Reducing the size of the website does not necessarily mean the body will be at 100% width

While inspecting the site, I noticed that the body does not extend to 100% width of the screen when I reduce the size of the site. After removing the Bootstrap classes, the issue seems to be resolved. I even tried adding body{width: 100%} but it did not h ...

The process of overriding CSS applied through JavaScript

I am using a third-party multi-select dropdown. Similar to Select2, the multi-select dropdown is created using JQuery with select2.min.js and the width of the dropdown is automatically calculated. Is there any way to apply static width to it, as I believe ...