Each section must have at least 100% height - html

I've searched for a lot of answers and tricks for this but nothing worked for me.
Some briefing: The project has a homepage with 5 sections (about us,activities,contact etc...). Each section must have AT LEAST 100% height. That means that if the child's containt is "little", then the section must have height 100% (the screen resolution has effect here). But if the child's containt is "large", then the div with class bg-color and the section must expand to over than 100% height so it can contain all the content. Each section has a different background-image and i used bg-color to add a transparent color over the background image.
The html structure seems like this
<section class="each-page about-us">
<div class="bg-color">
<div class="container page-content">
...CONTENT...
</div>
</div>
</section>
<section class="each-page activities">
<div class="bg-color">
<div class="container page-content">
...CONTENT...
</div>
</div>
</section>
<section class="each-page work-with-us">
<div class="bg-color">
<div class="container page-content">
...CONTENT...
</div>
</div>
</section>
The css seems like this:
body, html {
height: 100%;
width: 100%;
}
.about-us {
background-image: url("../images/bb2.jpg");
}
.each-page {
border-top: 1px solid #fff;
height: 100%;
}
.bg-color {
background-color: rgba(35, 124, 170, 0.6);
height: 100%;
width: 100%;
}
Some divs with class container page-content have a lot of content. But since the parent divs have height:100%, this content overlays the section at the bottom and it's pretty ugly. Especially when i test it in low resolution screens, almost every section seems broken! i dont want to set overflow with scroll bars.
Any suggestions/solutions please? Since the project will be mobile friendly (bootstrap), a responsive solution would be the best option.
Thank you in advance.

You can achieve it by using view height unit vh , in the fiddle you can see that every .section div has at least full height, see sectionThree has lots of content so it has more height
JS Fiddle
.each-page {
border-top: 1px solid #fff;
min-height: 100vh;
}
----------
UPDATE 1:
In order to fix it for Safari versions less that 8 -because view units are supported in Safari 8+- all you need is to add this to your javascript:
var UA = navigator.userAgent,
Ver = parseInt(navigator.appVersion,10);
if (UA.indexOf("Safari")!=-1 && Ver < 8) {
// it is safari and version less than 8;
// use javascript to fix it.
$('.each-page').css({'min-height': $(window).height()});
}
And that's it JS Fiddle 2, tested on Safari 5.1.7
http://caniuse.com/#feat=viewport-units
https://css-tricks.com/viewport-sized-typography/
https://web-design-weekly.com/2014/11/18/viewport-units-vw-vh-vmin-vmax/
https://dev.opera.com/articles/css-viewport-units/
http://www.w3schools.com/cssref/css_units.asp

You can also make section 100% height with js
$(window).on("resize", function () {
var fullHeight = $(window).height();
$('section').height(fullHeight);
}).resize();
.s-one {
background: blue;
}
.s-two {
background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="s-one"></section>
<section class="s-two"></section>

Related

html height for a percentage doesn't seem to work [duplicate]

This question already has answers here:
Percentage Height HTML 5/CSS
(7 answers)
Closed 3 months ago.
I expected <div style="height:50%;background-color:cyan;" to draw a blue box on the screen. But it doesn't. Only the area I fill with characters becomes blue.
So how I do draw a colored box of say 33% of the visible height of the parent?
I seem to remember it actually working at some time, but I cannot remember how it was done.
Poul
<html>
<style>
.worksOK {
background-color: cyan;
}
.DoesntWork {
background-color: lightblue;
}
</style>
<body>
<h1>Height % doesn't seem to work</h1>
<div class = "worksOK" style="height:75px;">
A fixed height, like <b>height:50px;</b> works fine
</div>
<div class = "DoesntWork" style="height:75%;">
But a height in percentage, like <b>height:50%;</b> doesn't seem to work.
</div>
<br>It does however work in this demo:w3schools<br><br>Unfortunately I cannot access the css code, so I do not understand why that is.<br>
And I cannot recall ever have been able to make height work with a percentage.
</body>
</html>```
add a height for the container, for example, here I am adding 100vh (the viewport height) to the body
body {
height: 100vh;
}
.perc50 {
height: 50%;
background: aliceblue;
}
.a {
background: teal;
}
.b {
background: red;
}
<body>
<div class="perc50"> content</div>
<div class="perc50 a"> content</div>
<div class="perc50 b"> content</div>
</body>

Parent bg shows/ child doesn't fill 100% depending on zoom factor

Sometimes the background of my parent-div is showing, and it's driving me crazy.
Setup:
Container-div with 4x4 divs inside (lets call them "outer"; blue bg).
Each of the blue "outer"-divs contains another div ("inner"; green bg).
Why I want it that way:
The green is supposed to cover the blue, because eventually the green will disappear (via jQuery - click) and the blue will show (it will have a bg-image instead of just the blue bg).
Problem:
Even though I tried different approaches (see codepen below), sometimes the blue bg will show.
screenshot of the problem
The point is -sometimes-. If the browser is zoomed to a convenient zoomfactor, it will display exactly as intended: the green covers the blue 100%. If, however, the browser thinks it's an unconvenient zoomfactor, the blue bg shows through. It also varies depending on the browser. If you view the below codepen-example in firefox rather than chrome, opera or edge, you might not even see that error (I didn't test it in safari).
Question:
How can I ensure that the green will consistently cover the blue, no matter the zoomfactor (or browser)?
css:
<style>
#outer-container {
width: 600px;
/* height: 600px; */
display: flex;
flex-flow: row wrap;
}
.outer-box {
width: 150px;
height: 150px;
background-color: blue;
border: 1px solid black;
box-sizing: border-box;
}
.inner-box {
width: 100%;
height: 100%;
background-color: green;
}
</style>
html:
<div id="outer-container">
<div class="outer-box" id="outer-box01">
<div class="inner-box"></div>
</div>
<div class="outer-box" id="outer-box02">
<div class="inner-box"></div>
</div>
<div class="outer-box" id="outer-box03">
<div class="inner-box"></div>
</div>
<!-- there are 16 outer-divs in total (see codepen) -->
</div>
codepen-link
Additional info:
If I open that codepen with firefox, it behaves as intended, not in chrome or edge. If I open my visual studio code with firefox, I get the same background-showing-display-glitch(?) (depending on the zoomfactor).
I tried floats, flexbox and grid (see codepen); I used height/width in pixels, height: 100%, stretch (flexbox); pos: rel on outer and pos: abs, top,right,bottom,left: 0 on inner; display: block won't help, because the div is a block-element already (just to be sure I assigned it manually anyways); I use box-sizing: border-box - and I'm running out of ideas.
Yes, when I assign the border to the inner-divs the outer-bg doesn't show; but it's the outer-divs that are supposed to have the border, because once the green get's clicked away the border must still be there.
Thanks in advance for any suggestions!
I found a workaround to achieve my goal by now (which was to not let the blue show through) by just putting another container-div on top of the first one.
My original question, though, remains unanswered with that solution.
But just in case anyone was trying something similar:
html:
<div id="outer-container">
<div id="inner-container">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner"></div>
<!-- 16 of those inner-divs here -->
</div>
<div class="bg"></div>
<div class="bg"></div>
<div class="bg"></div>
<!-- 16 of those bg-divs here -->
</div>
css:
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#outer-container {
width: 600px;
position: relative;
overflow: hidden;
}
#inner-container{
/* width: 600px; */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: hidden;
}
.bg, .inner{
width: 150px;
height: 150px;
border: 1px solid black;
float: left;
}
.bg{
background-color: blue;
}
.inner{
background-color: green;
}
</style>
codepen link
I had a look at this and it's puzzling. My best guess at this point is that it's a browser quirk/bug. With HTML and CSS there are so many ways to achieve the same thing so your workaround isn't necessarily bad. I managed to make it behave by putting the border on the inner-box's instead of the outer.
The reason I think it may be a bug is that it isn't consistent, as you have noticed yourself. In Chrome it happens at zoom level 125% (on a standard 1080 screen). In FireFox it doesn't happen at all on any zoomlevels and Edge has it at various zoom levels.
It looks a bit like the outer div at some levels ends up extending half a pixel wider than it's supposed to but it's hard to be sure.

Safari doesn't make a div 100% of it's parent's height

I'm working on a website, where the structure of a section is like this:
<div class="col-md-6">
<section class="right" data-type="background" data-speed="30" style="background: url(<?php echo get_field('book_image')['url']; ?>);">
</section>
</div>
The parent div in this situation has (in safari) a height 2976px. However, the child won't size itself to the same height, even though I've applied the height property to it.
I tried this in Chrome & Safari, both on macOS: Chrome worked, Safari didn't.
Is there any was to get this to work, preferably without any JS? Thanks :)
-- Edit
I'm using Bootstrap4, however, the right class only contains properties to style the div. Contents below
.right {
max-width: 50vw;
height: 100%;
background-size: 60vh !important;
background-repeat: no-repeat;
}
-- Edit #2.
Here's a snippet demonstrating the problem:
.left {
padding: 0;
background: #000; /*debugging*/
height: 200px; /*debugging*/
}
.right {
height: 100%;
background-size: 60vh !important;
background-repeat: no-repeat;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-6 left">
</div>
<div class="col-6">
<section class="right" data-type="background" data-speed="30" style="background: red;"></section>
</div>
</div>
</div>
(or in JSFiddle: Click me
As an advice you can use webpage CANIUSE
,you can check which css property does browser support . In my opnion the best solution is to use jquery or javascript to set height of div , when page will be resize or will be load. You can't find absolute perfect solution to solve crossbrowser support . If you need help with jquery do not hesitate ask when you need .
The problem is your parent div doesn't have an height property. Even though Chrome is able to resolve the containers height, Safari (and maybe some other browsers) isn't. In order to fix this you've to specify a height for the 2nd div in your .row (the parent of .right) as well.
Styling
.right-parent {
height: 200px;
}
Markup
<div class="col-6 right-parent">
<section class="right" data-type="background" data-speed="30" style="background: red;"></section>
</div>
Demo

Almost full screen header problems

I've recently tried out to make a website with a bar on top and with an image that cover the rest of the screen, much like this X-theme demo. I've managed to get it right in proportion (with a lot of help from this thread).
<body>
<div id="block">
<p>logo</p>
</div>
<header>
<h1>jumbotron</h1>
</header>
<div id="page">
<p>content</p>
</div>
*{
margin: 0;
}
#block {
height: 10vh;
width: 100%;
background: red;
background-size:cover;
}
body{
width: 100%;
height: 100%;
}
header {
height:90vh;
width:100%;
background: green;
background-size:cover;
}
This solution is however not so practical. When you shrink down your browser window, the bar on top (naturally) shrinks as well. Adding a logo would then be impossible, so I wonder if there is any way to make the bars size constant, while the header still takes up the rest of the screen?
I would really appreciate some help! :)
Because vh and px are two different units, it is not possible to do some math with them like 100vh - 30px, but it's possible using some jQuery/JavaScript. I'll give you an example using jQuery (Be sure to add a fixed height to the header).
On the end of this answer there is a CSS-only solution for this problem. Found out about it later.
This is the html code:
<header class="header">YOUR LOGO HERE</header>
<section class="jumbotron">CONTENT HERE</section>
<section class="content">SOME OTHER CONTENT HERE</section>
And this should be the javascript one:
var resizePage = function() {
var headerHeight = $('.header').outerHeight() // Getting the height of the header
, pageHeight = $(window).height(); // Getting the height of the window
$('.jumbotron').css('height', (pageHeight-headerHeight));
}
$(window).on('resize', resizePage()); // Fire every time the page resizes
resizePage(); // Fire once the site is running
Here is a JSFiddle working with the snippets I told you: CLICK ME
UPDATE: Just found out that CSS is providing a calc() method. Here is another JSfiddle with this method: CLICK ME, TOO. So it is possible using pure CSS, but be aware of the browser support for this.
.jumbotron {
/* Viewport height 100 minus 50px calculated using pure css */
height: calc(100vh - 50px);
}

issue with css full screen in different resolutions

I need your help. I have been googling around but i can not find good tips I am looking for. So I decide to write here. Pardon my English which is not my first language.
I am struggling to manage css layout with full screen with different resolution of browsers (big / small laptop screen). HTML Body works perfectly with full screen without scroll but wrapper (pink) seems out of control in different resolutions : the wrapper (pink) fits in a big screen (1920 x 1200), but it doesn't fit in a small screen (1280 x 800) that causes scroll. I dont want scroll. I need everything to fit in different resolutions without scroll.
You can look at my simple html and css codes with colour layout. You can just copy them and paste in your markup so that you can see what a problem is. Focus on the pink one.
CSS:
<style type="text/css">
*{
margin:0;
padding:0;
}
html{
/* This image will be displayed fullscreen */
background:url('name.jpg') no-repeat fixed;
/* Ensure the html element always takes up the full height of the browser window */
min-height:100%;
/* The Magic */
background-size:cover;
}
body{
text-align: center;
/* Workaround for some mobile browsers */
min-height:100%;
}
section, footer, header{
display: block;
}
footer{
background-color: #111111;
bottom: 0;
box-shadow: 0 -1px 2px rgba(0,0,0,0.4);
height: 45px;
left: 0;
position: fixed;
width: 100%;
z-index: 100000;
}
</style>
HTML MARKUP
<body>
<div class="header_container" style="background-color:orange; height: 150px;overflow: hidden;">
</div> <!-- end of header_container container_12-->
<div id="wrapper" style="background-color:pink; height:100%">
<div class="featured_container1" style="height: 280px;width:100%;background-color:grey;">
</div>
<div class="featured_container2" style="background-color:red; width:300px; height:700px">
</div>
</div> <!-- wrapper End -->
<footer style="background-color:green">
</footer>
</body>
</html>
Your help would be highly appreciated.Looking forward to seeing how to be solved by you. Hope it is worth to write on this forum :)
Regards
To stop the pink wrapper scrolling change <div id="wrapper" style="background-color:pink; height:100%;"> to <div id="wrapper" style="background-color:pink; height:auto;">
I would also recommend not using inline css, and putting it all in your stylesheet too :).