简体   繁体   中英

Make content min-height fill the screen

As an example, I've made a fiddle: https://jsfiddle.net/L7mwpzux/3/

How do I make the div .container minimally fill the screen? So when there is almost no content, it still fills the screen.

It's for a page that is shown when the checkout cart is empty. The content is too thin, so the screen is not fully filled with content.


Ps I am not looking for an answer that assumes that the header or footer has a static height. I want to be able to use it also in situations where the height of the header or footer is variable.

Also, I would love a CSS solution, so no JavaScript or jQuery

You can use calc() and set 100vh - height of header , also add box-sizing: border-box to keep padding inside.

 * { box-sizing: border-box; } body, html { margin: 0; padding: 0; } header { height: 200px; background-color: blue; width: 100%; } .container { padding: 50px; min-height: calc(100vh - 200px); } footer { width: 100%; height: 200px; background-color: #333; } 
 <header> </header> <div class="container"> small text </div> <footer> </footer> 

Other approach is to use Flexbox and set display: flex on body which is parent element in this case with min-height: 100vh and then just set flex: 1 on .container so it takes rest of free height.

 * { box-sizing: border-box; } body, html { margin: 0; padding: 0; } body { display: flex; flex-direction: column; min-height: 100vh; } header { height: 100px; background-color: blue; } .container { padding: 50px; flex: 1; } footer { height: 100px; background-color: #333; } 
 <header> </header> <div class="container"> small text </div> <footer> </footer> 

try this

min-height: calc(100vh - 400px);

here is the fiddle https://jsfiddle.net/L7mwpzux/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM