Take the available div height without knowing its height or being able to set the height to 100%

I would like to center the Dasboard h1 in the attached markup. It is impossible to know the size of the div or it is possible to set the height: 100% for each previous div for this.

I would think that this is possible with flexbox, but every example I see has a height of 100% or a height: 100vh in all the parent elements. I could just use display: table or dispaly: table-cell if that were the case.

html { min-height: 100%; position: relative; } body { height: 100%; background-color: #f7f7f4; } #root { height: 100%; padding-bottom: 170px; } .wrapper { max-width: 750px; } .grid { list-style: none; margin: 0; padding: 0; margin-left: -30px; } .grid__item { display: inline-block; padding-left: 30px; vertical-align: top; width: 100%; -webkit-box-sizing: border-box; box-sizing: border-box; } .one-half { width: 50%; } .private-banner__container { height: 98px; } .private-banner__layout { width: 100%; height: 100%; display: table; } .private-banner__right, .private-banner__left { display: table-cell; vertical-align: middle; } .footer__footer { padding-top: 48px; padding-bottom: 48px; } ul { margin-left: 38px; margin-left: 2.375em; } .footer__footer ul li:not(last-of-type) { margin-right: 48px; } .footer__footer li { display: inline; } 
 <html lang="en"> <body> <div id="root"> <header role="banner"> <div class="wrapper private-banner__container"> <div class="grid private-banner__layout"> <div class="grid__item one-half private-banner__left"><h1>Logo</h1></div> <div class="grid__item one-half private-banner__right"><a href="/business/dashboard">Home</a><a class="link__default" tabindex="0">Log Out</a></div> </div> </div> </header> <div> <div class="wrapper"> <div class="grid"> <div class="grid__item "> <h1>Dashboard</h1></div> </div> </div> </div> <footer class="footer__footer"> <div class="wrapper"> <div class="grid"> <div class="grid__item "> <ul> <li><a target="_blank" href="/static/about">About</a></li> <li><a target="_blank" href="/static/accessibility">Accessibility</a></li> <li><a target="_blank" href="/static/cookies">Cookies</a></li> <li><a target="_blank" href="/static/privacy">Privacy</a></li> </ul> </div> </div> </div> </footer> </div> </body> </html> 
+5
source share
3 answers

If I'm right, you are trying to achieve something similar.

 <header id="header" height="120px"></header> <div id="content" height="fill the rest of the height"></div> <footer id="footer" height="120px"></footer> 

In this case, you will need to use CSS flex . Flex works normally horizontally without any corrections, because by default any element of the block level has width=100% ie fills all the available width of the parent. But it does not have a default height . The height of a block element is calculated as the total height of all its children. The default height of the div is the height of the content . Even the <html> , <body> tags do not cover the entire height of the window; it covers the height of the content.

So, in order to do the flexible work vertically and split your screen into headers and footers and automatically adjust the content, you need to set the height starting from the top, set <html> , <body> and #root to 100%

 html, body, #root { height: 100%; margin: 0; paddding: 0; } 

Now your #root div takes up the entire height of the screen, so you can set the height of the title, footer and content using flex.

 #root { display: flex; flex-direction: column; } header { height: 120px; } #content { flex: 1; overflow-y: auto; /* scrollbar for content */ } footer { height: 120px; } 

The same flex job can be done using javascript

We run javascript code that runs when the page loads, calculates the height of the screen and sets the ideal height for the content of the div, which solves it beautifully and neatly.

 window.onload = function () { var headerHeight = document.getElementById('header').clientHeight; var footerHeight = document.getElementById('footer').clientHeight; var contentheight = screen.availHeight - headerHeight - footerHeight; document.getElementById('content').style.height = contentheight + 'px'; } 

And to center the <h1> header, just use the text-align property, enough.

 h1 { text-align: center; } 

You do not need to set vertical-align to the middle if the <h1> does not have a specified height.

+7
source

vertical-align:middle CSS property along with text-align:center . I always try to keep it simple, in other words, if you should not add a shell element, do not do this. So, that was the last thing I did was delete the unnecessary div attachment you had there.

In particular, all that I changed is what I added:

 h1{ vertical-align:middle; text-align:center; } 

and then remove the grid-item div and grid .

 html { min-height: 100%; position: relative; } body { height: 100%; background-color: #f7f7f4; } #root { height: 100%; padding-bottom: 170px; } .wrapper { max-width: 750px; } .grid { list-style: none; margin: 0; padding: 0; margin-left: -30px; } .grid__item { display: inline-block; padding-left: 30px; vertical-align: top; width: 100%; -webkit-box-sizing: border-box; box-sizing: border-box; } .one-half { width: 50%; } .private-banner__container { height: 98px; } .private-banner__layout { width: 100%; height: 100%; display: table; } .private-banner__right, .private-banner__left { display: table-cell; vertical-align: middle; } .footer__footer { padding-top: 48px; padding-bottom: 48px; } ul { margin-left: 38px; margin-left: 2.375em; } .footer__footer ul li:not(last-of-type) { margin-right: 48px; } .footer__footer li { display: inline; } h1{ vertical-align:middle; text-align:center; } 
 <html lang="en"> <body> <div id="root"> <header role="banner"> <div class="wrapper private-banner__container"> <div class="grid private-banner__layout"> <div class="grid__item one-half private-banner__left"><h1>Logo</h1></div> <div class="grid__item one-half private-banner__right"><a href="/business/dashboard">Home</a><a class="link__default" tabindex="0">Log Out</a></div> </div> </div> </header> <div> <h1>Dashboard</h1> </div> <footer class="footer__footer"> <div class="wrapper"> <div class="grid"> <div class="grid__item "> <ul> <li><a target="_blank" href="/static/about">About</a></li> <li><a target="_blank" href="/static/accessibility">Accessibility</a></li> <li><a target="_blank" href="/static/cookies">Cookies</a></li> <li><a target="_blank" href="/static/privacy">Privacy</a></li> </ul> </div> </div> </div> </footer> </div> </body> </html> 
0
source

 html { min-height: 100%; position: relative; } body { height: 100%; background-color: #f7f7f4; } #root { height: 100%; padding-bottom: 170px; } .wrapper { max-width: 750px; } .grid { list-style: none; margin: 0; padding: 0; margin-left: -30px; } .grid__item { display: inline-block; padding-left: 30px; vertical-align: top; width: 100%; -webkit-box-sizing: border-box; box-sizing: border-box; } .one-half { width: 50%; } .private-banner__container { height: 98px; } .private-banner__layout { width: 100%; height: 100%; display: table; } .private-banner__right, .private-banner__left { display: table-cell; vertical-align: middle; } .footer__footer { padding-top: 48px; padding-bottom: 48px; } ul { margin-left: 38px; margin-left: 2.375em; } .footer__footer ul li:not(last-of-type) { margin-right: 48px; } .footer__footer li { display: inline; } .grid__item.db { display: flex; align-items: center; justify-content: center; } 
 <html lang="en"> <body> <div id="root"> <header role="banner"> <div class="wrapper private-banner__container"> <div class="grid private-banner__layout"> <div class="grid__item one-half private-banner__left"><h1>Logo</h1></div> <div class="grid__item one-half private-banner__right"><a href="/business/dashboard">Home</a><a class="link__default" tabindex="0">Log Out</a></div> </div> </div> </header> <div> <div class="wrapper"> <div class="grid"> <div class="grid__item db"> <h1>Dashboard</h1></div> </div> </div> </div> <footer class="footer__footer"> <div class="wrapper"> <div class="grid"> <div class="grid__item"> <ul> <li><a target="_blank" href="/static/about">About</a></li> <li><a target="_blank" href="/static/accessibility">Accessibility</a></li> <li><a target="_blank" href="/static/cookies">Cookies</a></li> <li><a target="_blank" href="/static/privacy">Privacy</a></li> </ul> </div> </div> </div> </footer> </div> </body> </html> 
-1
source

Source: https://habr.com/ru/post/1275060/


All Articles