Setting the height of a div inside another div to 100% while also utilizing margins

I am facing an issue with a nested div structure in my HTML/CSS code. The inner div is getting cropped due to the overflow hidden property of the outer div, and I need to find a solution to set the inner div's height to 100% without any overflow.

Unfortunately, I do not have the flexibility to modify the content or style of either divs directly. However, I can wrap the inner div within additional divs if necessary, but this needs to be achieved without using JavaScript.

In addition, it is crucial that this problem is solved in a way that is compatible with IE8.

The style of the outer div may vary slightly in terms of width, height, background color, and margin.

As for the inner div, it could have different border sizes, no border at all, or other unique attributes.

#outer
{
width: 200px;
height: 200px;
background-color: yellow;
margin: 20px;
overflow: hidden;
position: absolute;
}

#wrapper
{
  height: 100%;
}

#inner
{
border: 1px solid red;
height: 100%;
}
<div id="outer">
<div id="wrapper">
<div id="inner">
a
</div>
</div>
</div>

Answer №1

To style the #wrapper element, you can utilize the css flex property.

#wrapper {
    display: flex;
    flex-flow: column;
    height: 100%;
}

#outer
{
width: 200px;
height: 200px;
background-color: yellow;
margin: 20px;
overflow: hidden;
position: absolute;
}

#wrapper /* adding flex */
{
  display: flex;
flex-flow: column;
height: 100%;
}

#inner
{
border: 1px solid red;
height: 100%;
}
<div id="outer">
<div id="wrapper">
<div id="inner>
a
</div>
</div>
</div>

This styling technique is compatible with all modern browsers except for IE8.

Answer №2

Check out this JSFiddle

To fix the issue, simply adjust the height of #wrapper to 99%.

The reason for the cutoff inside is due to:

#inner {
    height: 100%; // This makes it equivalent to 200px
    border: 1px solid red; // The 1px top and bottom borders add up to the height making it 202px, leading to overflow:hidden causing the cutoff.
}

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

Bootstrap / Blazor - no action when clicking on an image link

Having issues with adding a simple HTML link around an image in my Bootstrap 4.5 / Blazor app. When I click on the link, nothing happens. How is this possible? I've tried using the <a href"..."><img ...> pattern, as well as NavL ...

How to make a sticky sidebar using JavaScript or jQuery

Hello everyone! I am just starting out in front end web development, so please bear with me if my question seems basic. I am trying to figure out how to create a sticky sidebar that only appears when the parent div is in view, and disappears when it' ...

The importance of understanding Req.Body in NODE.JS POST Requests

Currently, I am developing a Node.JS application that interacts with a MySQL database. The app retrieves data from the database and displays it in a table using app.get, which functions correctly. The issue I am facing is that when utilizing app.post, re ...

What are some creative ways to achieve a background effect without using images?

Is there a way to achieve a background gradient like the one in the image? I attempted to create it using a radial gradient, but my result appears circular. Would using an image be a better approach? https://i.sstatic.net/lccSX.png How can I create a clea ...

Generate a new element using CreateElement and proceed to add content using inner

After two days of relentless searching, I finally found the solution. What's the process for creating an element and then writing it in HTML with innerHTML? function dataPull() { // Connects to my server from which it pulls JSON data containin ...

Tips for concealing the "maxlength" attribute in HTML

Here is the code snippet I currently use on a signup and login form to restrict users from entering more than a specified number of characters: $( "#limit1" ).attr('maxlength','11').on('input', function() { if ($(this).val(). ...

MySQL automatically inserts a null record if no value is provided

Currently, I am working on a project that involves inserting, viewing, and deleting records. Although everything functions smoothly, an issue persists where a null record is automatically added for some reason. The deletion feature works perfectly for othe ...

Personalizing HTML input fields

I am looking to create a contact form that will display an image depending on whether the input entered is correct or incorrect. My goal is to achieve this using only HTML and CSS, without needing any JavaScript. While there are similar posts on this topic ...

Customizing HTML Select Elements

Can the HTML Select attribute be customized to have a flatter appearance? Although I can set the border to be 1px solid, the dropdown part still appears 3D and unattractive. ...

Issue with preventdefault() function in Internet Explorer 8

I've encountered a problem while working on a page that heavily utilizes ajax. Everything seems to be functioning well across different browsers, except for one final step that is broken in IE8 and below. You can view the issue on this demo page: To ...

Tips for using nested flexbox with images

I am facing a straightforward use case where I have an image and a div containing text. The current setting for the div is flex column. My goal is to change the flex setting to row so that the text and image appear side by side. Despite having the corre ...

Leveraging the combination of <Form>, jQuery, Sequelize, and SQL for authentication and navigation tasks

My objective is to extract the values from the IDs #username-l and #pwd-l in an HTML form upon the user clicking the submit button. I aim to compare these values with those stored in a SQL database, and if they match exactly, redirect the user to a specifi ...

Troubleshooting problem with Chrome's getRangeAt function following changes to innerHTML of a contenteditable div using code

Here is an example of my HTML code: <button onclick="testfunction()">test</button> <div id="divT" spellcheck="false" contenteditable="true"></div> This is the JavaScript portion: document.get ...

Tracking User Visits in Codeigniter

Below is the code breakdown: (Step by step) Place counter.txt in APPPATH . 'logs/counter.txt' Set up counter_helper.php in APPPATH . 'helpers/counter_helper.php'; Autoload the newly created helper in APPPATH . 'config/autoload.p ...

Utilizing node_modules CSS imports in your webpack-powered Angular 2 application

Imagine we kick things off with this nifty starter pack: https://github.com/angularclass/angular2-webpack-starter Once we execute npm install and npm run start, everything runs smoothly. Now, let's say I want to incorporate an external css module, l ...

Why don't media queries take effect

After thoroughly reading all the @media topics on this site, it appears that the code should be functioning correctly. body { background-color: yellow; } @media screen and (min-width: 1080px;) and (max-width: 2000px;) { body { backgrou ...

Determine the top margin of an element

Looking to position my text with a 100px top margin, similar to this: Any ideas on accomplishing this? The font size should be 24pt and bold. ...

Slideshow featuring cards with text overlay

I am working with a bootstrap carousel and trying to place a card with text on top of it. No matter what I try, the card is not appearing as desired. Here is an example of what I want to achieve: https://i.sstatic.net/tjW3w.png Below is the code snippet: ...

Utilizing JSP to trigger a servlet upon loading without the use of script

Is there a way to automatically call a servlet when my JSP page is loaded? Currently, I have a link like this: <a href="ContentServlet?action=userContents">Homepage</a> However, I would like the servlet to be called without having to click th ...

Insert information into the <div> element

Sorry if this is a repeated question, but I'm struggling to locate the solution. Is there a way to use jQuery to add content within a div, depending on its current class? For instance, adding "CODE" to a div such as: <div "CODE" class="class_one" ...