100% viewport vertical center with content following - html

I am looking to accomplish a few things-
Set a container to fit 100% of viewport height and width;
Center the h1 vertically and horizontally in that container;
Add a paragraph of text beneath the h1 without this paragraph pushing the h1 up or breaking the centering.
Here's a diagram
I've already tried many of the methods discussed here and elsewhere (tables, display: table/table-cell + vertical align, using an inline-block with vertical align, etc) but the problem is that all of them either center both the h1 and the paragraph, or adding the paragraph under the h1 breaks it entirely. As the site is responsive, the h1 will likely become multiple lines of text on smaller screens. Is there a way to keep the h1 at the vertical and horizontal center while still adding content beneath it?

Well, basically just give the p a height of zero. The max-width below is just for illustration, margin: 0 auto then centers that horizontally.
html, body {
height: 100%
}
.container {
display: table;
height: 100%;
width: 100%;
}
.v-center {
display: table-cell;
vertical-align: middle;
text-align: center
}
p {
height: 0;
max-width: 200px;
margin: 0 auto;
}
<div class="container">
<div class="v-center">
<h1>Heading</h1>
<p>
Paragraph paragraph paragraph paragraph paragraph paragraph
paragraph paragraph paragraph paragraph paragraph paragraph...
</p>
</div>
</div>

The use of flexbox is your best bet as it is very concise and has good browser support. Also, it's your best bet for future-thinking as it is forming the foundation of today's modern app layout infrastructure.
The <p> being not pushed down is just done by giving a 0 height so that its effects on its container is not realized.
HTML:
<div class="container">
<h1>
HI
</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras neque tortor, auctor ut consectetur non, posuere a justo. Morbi nisi eros, pellentesque eget ullamcorper eu, tristique at tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Praesent ornare odio lorem, vel fermentum est lacinia ut. Vivamus tincidunt augue scelerisque justo consectetur tincidunt. Phasellus lectus nibh, ultrices in dictum vel, pretium at nisl. Sed vehicula tortor sed facilisis accumsan. Sed cursus felis quis quam efficitur, id luctus mi aliquet. Morbi mattis gravida convallis. Sed non feugiat dolor, in gravida arcu. Morbi id dolor imperdiet, rhoncus ante convallis, varius lacus.
</p>
</div>
CSS:
.container {
align-items: center;
background: red;
display: flex;
flex-direction: column;
justify-content: center;
height: 100vh;
width: 100vw;
}
.container p {
height: 0;
}
Fiddle: https://jsfiddle.net/3ms3sggd/
A Great Flexbox Guide: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

You can put the p and h1 in a div, then give to the div margin-top equal to 50% viewport and margin-left/right auto
* {
margin: 0;
padding: 0
}
div {
background-color: #fff;
width: 400px;
height: 100px;
margin-top: 50vh;
margin-left:auto;
margin-right:auto;
text-align: center
}
h1{
margin-bottom: 20px
}
<div>
<h1>
header
</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
</p>
</div>

with flex, things can be easy, text underneath can be setted in absolute position and html will scroll if needed:
html {
display:flex;
height:100%;
/* see center */
background:linear-gradient(to left, transparent 50%, rgba(0,0,0,0.2) 50%),linear-gradient(to top, transparent 50%, rgba(0,0,0,0.2) 50%)
}
body {
margin:auto;/* will shrink and center body on both axis */
/* see me ? */
background:rgba(0,0,0,0.2)
}
p {
position:absolute;
left:0;
right:0;
width:80%;/* set a width eventually */
margin:auto;/* if you did set a width, then can be useful */
}
h1 {
/*margin reset ? */
margin:0;
}
<h1>HI !,test me full page too</h1>
<p>Lesquelles habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
Note, what works here is p {position:absolute;} and the fact that HTML will show a scroll bar if p goes beyond bottom. Flex makes it easy to center h1, but other centering technic within the flow will do (inline-block or table/table-cell ) with html {height:100%} as a basis ...
So you can a full site laying under your midle center h1 :
html {
display: flex;
padding: 0;
height: 100%;
box-sizing: border-box;
background: linear-gradient(to left, transparent 50%, rgba(0, 0, 0, 0.2) 50%), linear-gradient(to top, transparent 50%, rgba(0, 0, 0, 0.2) 50%) fixed
}
body {
margin: auto;
text-align: center;
}
.below {
position: absolute;
left: 0;
right: 0;
width: 80%;
margin: auto;
text-align: left;
}
<h1>HTML Ipsum Presents</h1>
<div class="below">
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris
placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis
tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis.</p>
<h2>Header Level 2</h2>
<ol>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
</ol>
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis
elit sit amet quam. Vivamus pretium ornare est.</p>
</blockquote>
<h3>Header Level 3</h3>
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
</ul>
<pre><code>
#header h1 a {
display: block;
width: 300px;
height: 80px;
}
</code></pre>
</div>

Related

Fit all elements within viewport without scrolling

I'm trying to design a section of HTML/CSS wherein I have a flexbox parent element that is the width and height of the starting viewport with 3 child text boxes.
My goal is to have all 3 elements on the page and visible without needing to scroll.
My goal is to have all 3 text boxes be legible and resize accordingly to the given viewport without getting shoved out.
Looking at the HTML, it seems the content is shoved off the viewport to the right despite trying to keep the width at 100%. Basically, how can I have 3 easily modifiable flexbox that stay legible and within the width and height of the viewport no matter what changes are made to them?
My code:
.intro_header {
display: flex;
width: 100%;
padding: 10% 10% 5% 10%;
background-position: center;
background-repeat: no-repeat;
height: 100vh;
flex-wrap: wrap;
justify-content: space-between;
}
/* Generic flexbox paragraph text div that can be used for one column display regardless of responsiveness */
/*can swap around elements using the order: style */
.oneColumnText {
width: 100%;
text-align: center;
}
<div class="intro_header" style="background-image: foo.jpg">
<div class="oneColumnText">
<p>Lorem ipsum dolor sit amet</p>
</div>
<div class="oneColumnText">
<p>Lorem ipsum dolor sit amet</p>
</div>
<div class="oneColumnText">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sodales nulla sed fermentum tempor. Maecenas eget posuere massa. Sed consequat, erat ac tincidunt porttitor, augue sapien feugiat ligula, id ultricies augue tortor id mauris. Duis mattis
felis non libero iaculis, nec varius turpis pharetra. Vivamus convallis nibh ac arcu condimentum porta. Ut tristique in erat quis lobortis. Etiam ut elit in sem placerat dapibus.
</p>
</div>
</div>
https://jsfiddle.net/eg14v3po/1/
Add this to your code:
* { box-sizing: border-box; }
body { margin: 0; }
.intro_header {
display: flex;
padding: 10% 10% 5% 10%;
background-position: center;
background-repeat: no-repeat;
height: 100vh;
flex-wrap: wrap;
justify-content: space-between;
}
.oneColumnText {
width: 100%;
text-align: center;
}
* { box-sizing: border-box; }
body { margin: 0; }
.oneColumnText {
border-bottom: 1px solid lightgray; /* for demo only */
}
<div class="intro_header" style="background-image: foo.jpg">
<div class="oneColumnText">
<p>Lorem ipsum dolor sit amet</p>
</div>
<div class="oneColumnText">
<p>Lorem ipsum dolor sit amet</p>
</div>
<div class="oneColumnText">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sodales nulla sed fermentum tempor. Maecenas eget posuere massa. Sed consequat, erat ac tincidunt porttitor, augue sapien feugiat ligula, id ultricies augue tortor id mauris. Duis mattis
felis non libero iaculis, nec varius turpis pharetra. Vivamus convallis nibh ac arcu condimentum porta. Ut tristique in erat quis lobortis. Etiam ut elit in sem placerat dapibus.
</p>
</div>
</div>
With the border-box value of the box-sizing property, the padding you have specified gets factored into the width / height calculations (more details).
With margin: 0 on the body element you are overriding the default margins set by the browser (more details).

How to make header fixed but not hiding the content via CSS and HTML?

I'm trying to make a page in HTML and CSS that looks something similar to this picture:
But whenever I try to do it, the content div goes behind the header div and I should do margin-top to get it down, but I think that it's a bad idea for responsive design...
This is the CSS code I tried:
.header{
width:83%;
height:62%;
background: url('bg.jpg');
position:fixed;
}
you may use flex for the positionning and scrolling.
The basic template could be made of 2 elements <header> and <main>.
example
html {
height:100%;
}
body {
display:flex;
flex-flow:column;
height:100%;
margin:0;
}
main {
flex:1;/* fills whole space left */
overflow:auto;/* i scroll if space too short */
background:yellow;/* see me */
}
<header> any content here , <br/>no matter my height</header>
<main> content all the way down, i'll be scrolling if necessary</main>
and with any content:
html {
height: 100%;
}
body {
display: flex;
flex-flow: column;
height: 100%;
margin: 0;
}
main {
flex: 1;
overflow: auto;
background: yellow;
}
/* extra demo purpose */
nav ul {
display: flex;
}
header {background:red;}
<header>
<h1>HTML Ipsum Presents</h1>
<nav>
<ul>
<li>Home
</li>
<li>About
</li>
<li>Clients
</li>
<li>Contact Us
</li>
</ul>
</nav>
</header>
<main>
<h1>HTML Ipsum Presents</h1>
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris
placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis
tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis.</p>
<h2>Header Level 2</h2>
<ol>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
</ol>
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis
elit sit amet quam. Vivamus pretium ornare est.</p>
</blockquote>
<h3>Header Level 3</h3>
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
</ul>
<pre><code>
#header h1 a {
display: block;
width: 300px;
height: 80px;
}
</code></pre>
</main>
By default, the body will be become scrollable when it overflows the window.
If you just want the portion with the text in it to be scrollable, specify it on the element and make sure it only occupies enough space to fit inside the window (so the body doesn't become scrollable).
html,
body {
height: 100%;
}
body {
margin: 0;
}
#header {
font-size: 26px;
}
#content {
overflow-y: scroll;
height: calc(100% - 36px); // accounts for height of header, occupies rest of available height
}
That's one way, I'd prefer giving the body display: flex though. It'll automatically account for the rest of the available space.
body {
display: flex;
flex-direction: column;
}
https://jsfiddle.net/JackHasaKeyboard/zhjsocfv/4/

Pure CSS solution blocks flowing out of grid

I've made an illustration of the situation:
The blue lines illustrate the grid the website is in. Let's assume a 960 grid for now, with a 300px left side (red part), a 20px gap and the remaining 640px for the right side (the black and green parts). I want to know if there's a solution for this problem that doesn't use calc() (due to older browsers) or background-image (because that's not really pretty).
Is there a pretty way to make this work, using just CSS while keeping the content centered within the grid, and the backgrounds flowing all the way to the borders of the screen?
http://codepen.io/anon/pen/avoKwQ
Done by pseudo elements and absolute positioning. Used bootstrap for faster demo. Actually, it is a specific problem and my solution may not fit your project. Especially not work when you require horizontal scrolling. But problem is solved.
&:after {
content: " ";
position: absolute;
top: 0; right: 6px;
width: 99999%;
height: 100%;
background: red;
z-index: -1;
}
Parent of this element ofcourse require position: relative. In example this is done by bootstrap.
Flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
This one has the added benefit of the columns always being equal height:
* { margin:0; padding:0; box-sizing: border-box; }
.grid {
display: flex;
}
.col-3 {
flex: 1 1 320px;
border-right: 20px solid #fff;
}
.col-2-3 {
flex: 1 1 640px;
}
.col-3 {
display: flex;
justify-content: flex-end;
}
.col-3>div {
padding: 20px;
flex: 0 1 300px;
}
.col-2-3>div {
display: flex;
justify-content: flex-start;
}
.col-2-3>div>div {
flex: 0 1 640px;
padding: 20px;
}
.red { background: #f00; color: #fff; }
.green { background: #0f0;}
.black { background: #000; color: #fff;}
<div class="grid">
<div class="col-3 red">
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris quis rhoncus erat. Morbi id pretium tortor. Sed tristique, leo non fringilla tempor, orci ligula lobortis velit, a efficitur tortor dui eget libero. Ut aliquam tortor sed diam placerat, ut lacinia ipsum lacinia. Cras a neque vehicula arcu rutrum luctus. Aliquam placerat ac ex in tincidunt. Quisque nulla diam, cursus nec orci sit amet, aliquet tempor massa.
</p>
</div>
</div><!-- col -->
<div class="col-2-3 green">
<div class="black">
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris quis rhoncus erat. Morbi id pretium tortor. Sed tristique, leo non fringilla tempor, orci ligula lobortis velit, a efficitur tortor dui eget libero. Ut aliquam tortor sed diam placerat, ut lacinia ipsum lacinia. Cras a neque vehicula arcu rutrum luctus. Aliquam placerat ac ex in tincidunt. Quisque nulla diam, cursus nec orci sit amet, aliquet tempor massa.
</p>
</div>
</div><!-- .black -->
<div class="green">
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris quis rhoncus erat. Morbi id pretium tortor. Sed tristique, leo non fringilla tempor, orci ligula lobortis velit, a efficitur tortor dui eget libero. Ut aliquam tortor sed diam placerat, ut lacinia ipsum lacinia. Cras a neque vehicula arcu rutrum luctus. Aliquam placerat ac ex in tincidunt. Quisque nulla diam, cursus nec orci sit amet, aliquet tempor massa.
</p>
</div>
</div><!-- .green -->
</div><!-- .col -->
</div><!-- .grid -->

Overflow - what am I doing wrong?

I have been working on trying to get a page to display a title at the top of the content pane, and then a scrollable list of products below that so that the title of the product range is displayed at all times. I am sure this is a very simple thing to do - but cannot figure it out.
Currently the actual page (not the test page for which the code is given below) works ok in the sense that I set the heading div to 5% of the height of .content-container and then set the scrollable div to 95% with top: 5%, both with position: absolute applied.
However I would like to place some links in the heading div to different pages (1, 2, 3 etc), which I would like to center vertically if they are shorter than the heading and expand the heading div to match the height of the heading or the links, whichever is smallest. Furthermore I would like the div below the heading to shrink so that it doesn't go below the bottom of the content div as the heading div gets taller. The point of this is because it is for a client who may, or may not, be happy with the heading sizes and so on - therefore the heading div height could easily change. Specifying heights so precisely means that changing the h1 height could mean 5 changes to the CSS file - something I want to avoid.
The content pane currently has its height fixed to 80% of the page, with the header and footer being 10% each on top of that, so there is no scroll bar at the side of the page and the header / footer are always showing. This is something I would like to keep.
In the code below, .content-container is the main content pane - this is contained in another div which is centered using the margin at 50% of the page width. .test-div is the div which contains the heading. .test-div-2 is an attempt to place a div below .test-div, in the hope that I can force .test-div-3 to extend to 100% of its' height but no further, and to display a scroll bar if the content exceeds the height.
So far I have the following, but it doesn't do exactly what I would like it to:
<div class="content-container">
<div class="test-div">
<h1 style="text-align: center;">Dogs</h1>
</div>
<div class="test-div-2">
<div class="test-div-3">
//Content here
</div>
</div>
</div>
and here is the css :
.content-container {
position: absolute;
width: 100%;
height: 100%;
left: 0;
right: 0;
bottom: 0;
overflow: auto;
}
.test-div {
position: relative;
padding: 0;
margin: 0;
}
.test-div-2 {
position: relative;
background-color: #CCCCCC;
}
.test-div-3 {
max-height: 100%;
background-color: #999999;
}
Any help with this would be greatly appreciated. I would like to achieve this without the use of JavaScript / jQuery if possible - pure HTML / CSS solutions only please!
try
fixing the height of the scrolling div or any block level element and add overflow:scroll property or overflow-y:scroll property
Sample would be
.test-div-2
{
position: relative;
background-color: #CCCCCC;
overflow: scroll;
height: 100px;
}
.test-div-3
{
max-height: 100%;
background-color: #999999;
}
</style>
<div class="content-container">
<div class="test-div">
<h1 style="text-align: center;">
Dogs</h1>
<div class="clear">
</div>
</div>
<div class="test-div-2">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue
massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero,
sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus
a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames
ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue
massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero,
sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus
a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames
ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue
massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero,
sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus
a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames
ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue
massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero,
sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus
a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames
ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci. </p>
</div>
</div>
try this
.content-container
{
position: absolute;
width: 100%;
height: 100%;
left: 0;
right: 0;
bottom: 0;
overflow: auto;
}
.test-div
{
position: relative;
padding: 0;
margin: 0;
}
.test-div-2
{
position: relative;
background-color: #009933;
max-height:100px;
}
.test-div-3
{
max-height: inherit;
background-color: #0064ea;
overflow: scroll;
}
working example
jsFiddle

Bottom positioned DIV

This is driving me crazy. I'm trying to get the footer div to be at the bottom of the page even if the main content doesn't fill the height of the browser. The below code works except for when I shrink the browser up and then the footer div overlaps the wrapper div, then the scroll bar appears. I want it to bump up against the wrapper div like most sites including this one. What am I doing wrong?
<html>
<head>
<style>
body { color:#000; margin: 0; height: 100%; }
#wrapper {min-height: 100%; height: auto !important; height: 100%; background:#ff0000;
margin: 0 auto -4em; text-align: left; width: 100%; }
#header { width: 100%; height: 80px; }
#content { width: 100%; background:#00ff00; }
#footer { background:#0000ff; height: 4em; }
</style>
</head>
<body>
<div id="wrapper">
<div id="header">
some menus;
</div>
<div id="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ac eros diam, nec ultrices nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed id ipsum libero. Sed ultricies orci ut magna vulputate eu congue justo condimentum. Phasellus a convallis ipsum. Nam nec sapien eget massa porta tristique. Proin metus diam, imperdiet nec eleifend a, faucibus eget quam. Nunc non lacus sit amet lorem vehicula viverra ut vitae sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id tellus id ligula dictum consequat non ut ligula. Morbi interdum felis sed turpis sagittis vulputate.
</div>
</div>
<div id="footer">
© 2009 Somebody
</div>
</body>
</html>
http://ryanfait.com/resources/footer-stick-to-bottom-of-page/
Check out this live example of how it works:
http://www.toonklaas24.ee/