Adjust the height of an element using percentages

Over at this JSFiddle link, they demonstrated splitting a page into 3 parts.

However, when I adjust the width of #wrapper to 100%, everything works perfectly. So why does changing height:400px to height:100% cause it to fail?

#wrapper {
    width: 400px;
    height:400px;
}

What is the solution for covering the entire page using percentage (%)?

Answer №1

Avoid using percentage heights in CSS.

Referring to this answer on Stack Overflow:

In order for a height set in percentage to work, the parent element(*) must have a specific height defined. If the parent's height is left as auto, the block will adjust its height based on its content... but if the content itself has a height specified in percentage relative to the parent, it creates a bit of a dilemma. The browser then defaults to the content height.

Therefore, the parent of the div must have an explicit height property. Even if that height is also in percentage, it simply shifts the problem to the next level.

If you want the div's height to be a percentage of the viewport height, every ancestor of the div, including and , needs to have height: 100%, creating a hierarchy of explicitly defined percentage heights leading down to the div.

This means that the parent element must have a specific height (which is not the case here). This is because the default behavior for the height of a block element is to match the content's height, while the width defaults to 100%. That's why setting width: 100% works.

Therefore, to achieve a similar effect as width:100%, all parent elements must be set to height: 100% (including the body and html 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

Ways to control image width without distorting the image dimensions

Can anyone help me with writing an img tag for images of varying sizes? I'm looking to scale down larger images to fit the container at width: 100%, but I do not want smaller images to be scaled up. I want them to maintain their natural size when view ...

Troubleshooting problem with CSS aligning <dl> and <dd> tags

Utilizing <dt> and <dd> typically assumes a single line definition for dd. However, in my scenario, I have multiple entries for the definition that I need to correctly display (the exact method will be clarified after reviewing the attached ima ...

PHP mail function not working properly when ajax is used

On my MAC, I am attempting to utilize php mail with ajax implementation. I have simplified my code to just strings: In my .js file : function sendMail() { $('#sending').show(); $.ajax({ type:'POST', ...

Can JQuery be used to specifically target attributes with vendor prefixes?

Is there a way to change the Thumb color for my slider without needing to select the thumb with a vendor prefix? I want to be able to dynamically pick the color without simply appending a class. $('.button').on('click', function (e) { ...

What are the best practices for preventing duplicate IDs in JavaScript-based web applications?

On my heavily JavaScript-based webpage, I have sections that consist of lists of HTML elements categorized as lists A and B. When the first item in list A (A1) is clicked, an action is triggered on the first item in list B (B1). To simplify the process, e ...

Tips for expanding full screen slider dimensions

I am currently working on a website project using ASP.NET MVC Core 1.1.2. I want to create a full-screen slider for the homepage that appears stretched both on desktop and mobile browsers. However, when I view the slider on my browser, there is an annoying ...

Dynamically taking input in Vue JS while using v-for loop

Hey there! I'm trying to get input in this specific manner: itemPrices: [{regionId: "A", price: "200"},{regionId: "B", price: "100"}] Whenever the user clicks on the add button, new input fields should be added ...

Having trouble displaying ES6 promise value in AngularJS view

Currently, I am working on a project where I am utilizing the Pinterest JavaScript SDK to retrieve some pin data. In order to achieve this, I have created a Pinterest service with a method that is invoked in my HomeController. To fetch the response and d ...

What is preventing window.scrollTo() from being executed?

I have implemented Bootstrap's Buttons plugin to toggle the state of a custom checkbox. When the button is toggled on, a hidden element should appear at the top of the page and then the page should scroll to the top. Similarly, when the button is togg ...

Modifying HTML files in a Node.js environment with the fs module

Struggling with learning node.js and back-end development, I encountered a problem that I can't solve. Can anyone lend a hand? I've been working on changing my HTML page from h1.html to h2.html, but I keep encountering the following error: (Error ...

CSS Padding Hover Transition solution for Eliminating Flickering Text issue

Is it possible to achieve this? I'm attempting to animate the padding around a centered text link. When the text link is clicked, the padding changes from 20% to 100%, which works correctly. The text is horizontally and vertically centered using CSS ...

Transform a 3D text rotation JavaScript file into an Angular component tailored TypeScript file

I have a javascript file that rotates text in 3D format, and I need help converting it into an Angular component specific TypeScript file. You can find the codepen for the original JavaScript code here. Below are my Angular files: index.html <!doctyp ...

Objects and strings cannot be inserted into a JavaScript array

For hours, I've been attempting to troubleshoot this code. Currently, it successfully finds the number of anchors, and I have an array that is of undetermined size. Within the for loop, it retrieves the anchor and extracts the .href. I've confirm ...

align the button to the left using bootsrap configuration

Is there a way to align a button to the left using bootswatch/Litera, which is a bootstrap 4 beta theme? I attempted to use the float-left class on the button and place it inside a div, but the outcome remained the same. https://i.sstatic.net/qs4zU.png ...

Attempting to apply custom styles to jQuery components using CSS

Looking to restyle the black bar on my test page located at The black bar with 3 tabs about halfway down the page needs a new color scheme. By inspecting with FireBug, I found these elements: <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ...

Alignment of checkboxes and labels in a vertical manner

I have been browsing through various posts on Stack Overflow regarding this issue, but I haven't found a solution that works for me yet. I've tried using the following CSS code to vertically align checkboxes and their labels: body { font-fam ...

prevent scrolling to a specific div

Hey, I'm facing a slight issue and need to prevent scrolling to the div after clicking on a link that targets the left div. <div class="kafelki"> <div class="kafelki-img"> <div class="target"> <div id="m1">dasdasdasd m1</di ...

displaying the information when we mouse over a specific row in the table

I need to display a pop-up with data from a table row, similar to the image at this URL for CS WHOLESALE GROCERS. I've implemented the following AngularJS code in my controller: app.directive('tooltip', function(){ return { res ...

Press the "share" buttons for social media on the YouTube video embedded on my site

As I work on developing my website which features a list of YouTube videos, one of the key requirements is to include share buttons below each video for easy social media sharing. Despite trying different options, I haven't been able to figure out how ...

positioned absolutely with a margin of 0 auto

Struggling to find a way to center the text in the wrapper? I have a responsive slideshow that requires text to be on top of it and centered. ul.bjqs { position:relative; list-style:none; padding:0; margin:0; z-index: 1; overflow ...