Adjust the CSS to set the size of the element's height to a percentage of the screen

I'm curious if there's a way to set the CSS with an x (unknown) percentage amount, like height: *%;. If it's not possible, how can I make it take the size of the rest of the screen?

I experimented in http://jsfiddle.net/omarjuvera/xnau2wsL/ and http://jsfiddle.net/omarjuvera/xnau2wsL/1/, but in this fiddle, these div elements aren't taking the desired screen height size or using the whole screen together.

HTML

<div id="hello">Hello</div>
<div id="world">world</div>
<div id="shout">!</div>

CSS

#hello {
    border: solid black 2px;
    min-height: 10%;
}
#shout {
    border: solid brown 2px;
    /*inherit height from content*/
}
#world {
    border: solid blue 4px;
    height: *;
}

I prefer not to set a height with pixels and overflow since I want to have a mobile-friendly (Responsive Web Design) CSS.

Notes

  • Height size for #hello is known as 10% of media (screen) size
  • Height size for #shout is unknown (inherited from the content)
  • Height size for #world is the remaining percentage (x%) of the media
  • Can this be achieved without changing the semantics? -No container or changes, just CSS

Answer №1

is using display:table the best approach here?

html,body {
  height:100%;
  width:100%;
  }
body {
  display:table;
  border-collapse:collapse;
  margin:0;
  box-sizing:border-box;
  }
body>div {
display:table-row;
  }
#hello {
  height:10%;
  border:green solid;
  }
#world {
  border:solid purple;  
  }
#shout:hover {
  height:15%;
  }
#shout {
  height:1%;
  border:red solid;
  }
<div id="hello">Hello</div>
<div id="world">world</div>
<div id="shout">!,hover me to make me grow</div>

would it be better with display:flex instead?

html,body {
  height:100%;
  }
body {
  display:flex;
  flex-direction:column;
  margin:0;
  }
#hello {
  height:10%;
  border:green solid;
  }
#world {
  border:solid purple;  
  flex:1
  }
#shout { 
;
  border:red solid;
  }
#shout:hover {
  height:25%;
  }
<div id="hello">Hello</div>
<div id="world">world</div>
<div id="shout">!, hover me to make me grow</div>

Answer №2

To make sure the divs reach a specific height using percentage, it is important for the container enclosing the divs to have a defined height in the css: http://jsfiddle.net/xwyg89jk/

If the container happens to be the body tag, you can specify:

html {
height: 100%;
}

body {
height: 100%;
}

Answer №3

The percentage is calculated based on the height of the parent, so you must have a parent element with a specified height.

To ensure proper sizing, you should include

height: 100%; position: absolute;
in the wrapper's CSS:

#hello {
    border: solid black 2px;
    min-height: 10%;
}
#shout {
    border: solid brown 2px;
}
#world {
    border: solid blue 4px;
    height: 70%;

}
#wrap{
    height: 100%;
    position: absolute;
}
<div id="wrap">
    <h1>Size and unit % wildcard</h1>
    <div id="hello">Hello</div>
    <div id="world">world</div>
    <div id="shout">!</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

What is the reason behind HTML5Boilerplate and other frameworks opting for a CDN to host their jQuery files

When it comes to loading jQuery, HTML5Boilerplate and other sources[citation needed] have a standard process that many are familiar with: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script>window. ...

Show concealed content for individuals who do not have javascript enabled

One of the challenges I faced was creating a form with a hidden div section that only appears when a specific element is selected from a list. To achieve this, I utilized CSS to set the display property of the div to 'none' and then used jQuery t ...

Problem with Jquery hover or mouse enter show/hide functionality

I am currently experimenting with displaying hidden text when the mouse enters a div and hiding it when it leaves. Here is the progress I've made on my JSFiddle: $(document).ready(function() { $(".image").mouseover(function(){ $(".hover").show ...

Troubleshooting problem with printing iframes on Firefox

When attempting to print an iframe with a large amount of data using the code below, I'm encountering an issue in Firefox where only the first page is printed. The rest of the pages are not being included in the printout. window.frames[frameID]. ...

Is there a way to completely remove an element from the dom once the ajax call has returned

I've been struggling to completely remove LI elements and their content, including the checkbox input, from the DOM without success. After an ajax callback, I am calling the following function, but it only removes the contents and not the element its ...

Show the name of the current user in a WordPress form using a readonly input field

How can I display the logged-in WordPress username (display name) in a form input field that is set to readonly? I have already checked out the Function Reference/wp get current user, but haven't had any success with it. Take a look at the code snip ...

What could be causing the issue with the width: 100% not rendering correctly in this

I'm currently working on my footer, but I'm having trouble getting the width: 100% tag to work correctly. Here's a screenshot of what I mean: http://gyazo.com/9c23897cd7b9a74ca55ee9fb977f810c This is my CSS code: /*..Portfolio - Lars Beut ...

Having Trouble with jQuery toggleClass

I'm currently working on a project where I have implemented a button that displays a menu when clicked. I am attempting to change the color of the button when it is active, however, the toggleClass function is not behaving as expected. Below is the co ...

Causes for the display of the text within the alt attribute

Recently, I attempted to view an HTML page in text browsers and noticed that the alt attribute value of the img tag was visible. I decided to omit the alt attribute and instead utilized CSS: .headerImg:after, .headerImg::after { conte ...

The value returned when using $.css('transform') is incorrect and displays as "rotate"

Possible Duplicate: Retrieve -moz-transform:rotate value using jQuery Firefox 15 - Chrome: $('#img2').css('transform'); return => "rotate(90deg)" Firefox 16 $('#img2').css('transform'); return => mat ...

Error in Javascript programming | tracking progress bar

After stumbling upon this code snippet at this link, I was eager to delve deeper into the world of JavaScript and jQuery. However, upon implementing these codes, I encountered a perplexing issue. The progress bar and continue buttons seem to be non-funct ...

Utilize the conditional GET method when including scripts through tags in an HTML webpage

Is it possible to benefit from HTTP conditional requests when including a script in the head section of a page? My goal is to cache dynamically downloaded JavaScript files that are added to the head section using script tags. If this approach isn't fe ...

Horizontal scroll box content is being truncated

I've been trying to insert code into my HTML using JavaScript, but I'm facing a problem where the code is getting truncated or cut off. Here's the snippet of code causing the issue: function feedbackDiv(feedback_id, feedback_title, feedb ...

The applet failed to load on the HTML webpage

I'm having trouble getting this applet to load on an HTML page. I've already added full permissions in the java.policy file and I'm using the default HTML file from NetBeans Applet's output. /* Hearts Cards Game with AI*/ import java.a ...

Quick and hassle-free form editing is a breeze with our user-friendly platform

Currently, the forms I have require certain files to be filled out before the submit button can be clicked. What I need is for the 'Title' field at the top of the list to also be marked as required. I know how to accomplish this for text fields, ...

Displaying information from a database in an HTML form using PHP

Seeking assistance with managing data and populating: I'm in the process of creating an Employee database with two pages: 1. Add Employee 2. Modify Employee The "Add Employee" page requires input fields for EMP Name, EMP ID, Manager name (from a dro ...

store user settings in local storage

After writing some code with a link that toggles text visibility upon click, I now want to incorporate saving this state in web storage so that it persists upon page reload. As a beginner in JavaScript and HTML, this task has proven challenging for me. Th ...

Using the concept of a while loop in PHP, you can easily retrieve the values of Radio Buttons on one page and

Is there a way to retrieve and store radio button values in a MySQL database using PHP looping concepts? $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_row($result)) { ?> ...

Please submit information that is not already filled out in the text fields

I am looking to create a table with text fields and buttons as shown below: <table border="1"> <tr> <td>name</td> <td>id</td> <td>icon</td> <td> </td> & ...

Protecting against overflow for text inside a container that changes when hovered over

There is text displayed on top of an image within a div container that functions as a link. <a href="https://link.com" style="color:white"> <div class="thumbnail"> <img src="image.jpg" clas ...