I'm trying to float text around an image, but for some reason, the text won't go in the right place.
Here is what I want it to look like. I was able to create this in an editor by itself, but when I add it to my page, it breaks it and doesn't work.
I'm having trouble getting it to look like my other example. They have the same code for that part. Need help figuring out how to get it close to the first example.
Mine Now:
section{
display:flex;
border-style:solid;
background-color:azure;
}
img {
padding-top: 5em;
padding-right:2em;
}
p{
float:right;
}
h1{
text-align:center;
}
<section>
<img src="images/GM05.png" alt="headshot">
<article>
<h1>Name Goes Here.</h1>
<p>Cras tristique gravida tellus, id fringilla lorem pellentesque iaculis. Donec vitae risus mauris. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc consectetur purus sed diam iaculis congue. Morbi vitae nisl est. Sed sed justo vitae risus porta commodo vestibulum eget est. Cras eu augue enim. Etiam at commodo tellus, at posuere ligula. Vivamus at dolor eget sem faucibus aliquet sed et diam. Mauris vel leo eget nulla pulvinar suscipit vitae eu sem. Quisque nisi nibh, aliquet sit amet urna non, commodo fringilla tellus. Nullam tincidunt est nec tellus laoreet, id mollis urna pulvinar. Donec ligula ipsum, ultrices in venenatis quis, ultricies ut enim. Vivamus porttitor lobortis dui, id aliquam ipsum imperdiet non.</p>
</article>
</section>
There are no floats involved. Put your image in another wrapper. Then apply flex property to that wrapper. Adjust the last value (flex-basis) to the width you want that "column" to be.
aside {
flex: 0 0 40%; /* flex-grow: 0; flex-shrink: 0; flex-basis: 40%; */
padding-top: 64px;
}
You can center the image in that space with margin.
aside img {
display: block;
max-width: 100%;
height: auto;
margin: 0 auto; /* auto on the sides centers the item */
}
section {
display: flex;
border-style: solid;
background-color: azure;
}
aside {
flex: 0 0 40%;
padding-top: 64px;
}
aside img {
display: block;
max-width: 100%;
height: auto;
margin: 0 auto;
}
h1 {
text-align: center;
}
<section>
<aside>
<img src="https://picsum.photos/200/300" alt="headshot">
</aside>
<article>
<h1>Name Goes Here.</h1>
<p>Cras tristique gravida tellus, id fringilla lorem pellentesque iaculis. Donec vitae risus mauris. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc consectetur purus sed diam iaculis congue. Morbi vitae nisl est. Sed sed justo vitae
risus porta commodo vestibulum eget est. Cras eu augue enim. Etiam at commodo tellus, at posuere ligula. Vivamus at dolor eget sem faucibus aliquet sed et diam. Mauris vel leo eget nulla pulvinar suscipit vitae eu sem. Quisque nisi nibh, aliquet
sit amet urna non, commodo fringilla tellus. Nullam tincidunt est nec tellus laoreet, id mollis urna pulvinar. Donec ligula ipsum, ultrices in venenatis quis, ultricies ut enim. Vivamus porttitor lobortis dui, id aliquam ipsum imperdiet non.</p>
</article>
</section>
If i understand correctly, you just wanna to display this image on the center.
So, change padding-top: 5em; to margin: auto;
Best regards,
Brhaka
Related
The issue I'm having is that I'm trying to change the layout between min-width:975px max-width: 1280px. I want to move the image and the text around.
In the picture, you can see the text is too much to the left. I want to move it closer to the image. This is inside of a flexbox. I tried to use padding, but nothing changed.
How would I move the text closer to the image?
/*Header for picture, and description*/
#display {
padding-top: 2em;
border: solid .125em black;
display: flex;
flex-flow: wrap;
background-color: white;
opacity: 0.9;
}
#display > * {
flex: 1 1 5%;
padding: 1em;
}
article h1{
font-size: 2em;
}
article p{
padding-top: 2em;
}
#media screen and (min-width:975px max-width: 1280px){
article{
}
}
<section id="display">
<figure id="headshot">
<img src="images/GM05.png" alt="headshot"/>
</figure>
<article>
<h1>Name Goes Here</h1>
<p>Cras tristique gravida tellus, id fringilla lorem pellentesque iaculis. Donec vitae risus mauris. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc consectetur purus sed diam iaculis congue. Morbi vitae nisl est. Sed sed justo vitae risus porta commodo vestibulum eget est. Cras eu augue enim. Etiam at commodo tellus, at posuere ligula. Vivamus at dolor eget sem faucibus aliquet sed et diam. Mauris vel leo eget nulla pulvinar suscipit vitae eu sem. Quisque nisi nibh, aliquet sit amet urna non, commodo fringilla tellus. Nullam tincidunt est nec tellus laoreet, id mollis urna pulvinar. Donec ligula ipsum, ultrices in venenatis quis, ultricies ut enim. Vivamus porttitor lobortis dui, id aliquam ipsum imperdiet non.</p>
</article>
</section>
Why do you set flex: 1 1 5%; to the flex-child ?
It is the shorthand for
flex-grow:1;
flex-shrink:1;
flex-basis:5%;
while flex-basis is
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
flex-basis
This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword. The auto keyword means "look at my width or height property" (which was temporarily done by the main-size keyword until deprecated). The content keyword means "size it based on the item's content" - this keyword isn't well supported yet, so it's hard to test and harder to know what its brethren max-content, min-content, and fit-content do.
If you set flex:1; to article, it should work fine, else, use the values you need (flex: X X X;), read the tutorials to go further.
/*Header for picture, and description*/
#display {
padding-top: 2em;
border: solid .125em black;
display: flex;
flex-flow: wrap;
background-color: white;
opacity: 0.9;
}
#display>* {
padding: 1em;
}
article {
flex: 1;
}
article h1 {
font-size: 2em;
}
article p {
padding-top: 2em;
}
#media screen and (min-width:975px max-width: 1280px) {
article {}
}
<section id="display">
<figure id="headshot">
<img src="images/GM05.png" alt="headshot" />
</figure>
<article>
<h1>Name Goes Here</h1>
<p>Cras tristique gravida tellus, id fringilla lorem pellentesque iaculis. Donec vitae risus mauris. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc consectetur purus sed diam iaculis congue. Morbi vitae nisl est. Sed sed justo vitae
risus porta commodo vestibulum eget est. Cras eu augue enim. Etiam at commodo tellus, at posuere ligula. Vivamus at dolor eget sem faucibus aliquet sed et diam. Mauris vel leo eget nulla pulvinar suscipit vitae eu sem. Quisque nisi nibh, aliquet
sit amet urna non, commodo fringilla tellus. Nullam tincidunt est nec tellus laoreet, id mollis urna pulvinar. Donec ligula ipsum, ultrices in venenatis quis, ultricies ut enim. Vivamus porttitor lobortis dui, id aliquam ipsum imperdiet non.</p>
</article>
</section>
I have some HTML that looks like this: https://jsfiddle.net/9uwmxLa8/
I can't get the footer to stay at the bottom and not overlap the text on the page. The footer should stay at the bottom of the page if there isn't enough content to force the footer below the window height. I have the first part right--but can't get it to not overlap; I know it's because of position: absolute--but everything I search online tells me this is necessary.
I want it to function like this: https://codepen.io/cbracco/pen/zekgx
Any ideas how to fix this?
<div class="container">
<div>
<h1>This is some long test content.</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis eget sapien risus. Cras eleifend, diam quis tempus mattis, nunc leo vulputate metus, et rhoncus elit libero ac nisl. In ligula lectus, ultricies in pretium eget, accumsan non turpis. Lorem
ipsum dolor sit amet, consectetur adipiscing elit. Praesent finibus molestie ultricies. Vivamus et libero et mauris rutrum hendrerit sed in leo. Fusce luctus lorem iaculis, mattis felis non, suscipit nisi. Integer tempus blandit est tempus ullamcorper.
Ut at risus eget arcu congue rhoncus sit amet pharetra turpis. Maecenas ultricies, ex eget egestas scelerisque, ante ipsum placerat mauris, vitae porta felis mauris dictum nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
<p>
Duis fringilla dolor a neque laoreet tristique. Donec non feugiat orci. Nulla nulla mauris, fermentum at arcu quis, venenatis consectetur odio. Vestibulum a risus non lorem ullamcorper sollicitudin. Praesent quis ante ipsum. Suspendisse odio ligula, tincidunt
vitae consectetur vel, posuere tincidunt odio. Donec at mollis neque. Sed ex felis, aliquam sed turpis eget, porta aliquet justo. Vivamus vestibulum libero id pulvinar egestas.
</p>
<p>
Quisque vestibulum urna eu blandit consectetur. Integer eget massa suscipit, scelerisque augue sed, mattis erat. Proin ut tincidunt nisi. Duis vestibulum congue accumsan. Vivamus a nisi tellus. Proin pretium neque eros, quis lacinia nisl pulvinar eget.
Quisque commodo urna eu nibh vulputate accumsan. Sed nec neque blandit, commodo metus sed, hendrerit orci. Nullam ullamcorper libero massa, vel pretium felis vulputate sed. Mauris in mauris pretium, accumsan erat quis, dapibus lacus.
</p>
<p>
Quisque arcu metus, ultricies sed urna a, gravida aliquam mauris. Nullam quis velit odio. Pellentesque dignissim odio tortor, at tempus nunc posuere et. In consequat eros a nunc varius, quis varius metus malesuada. Mauris viverra auctor elit quis hendrerit.
Donec risus ante, viverra et ante a, egestas faucibus purus. Integer semper id nulla eget vehicula. Aliquam a egestas leo. Suspendisse eu magna sit amet eros tempor ultrices. Quisque sed tempor purus, vitae egestas nunc. Cras congue nulla in sem
eleifend, sed pharetra ipsum volutpat. Etiam suscipit lacus ultricies dolor hendrerit fermentum. Pellentesque vestibulum in tellus ac blandit. In nec elit non eros tincidunt semper. Fusce sed leo eget ex laoreet fermentum sed vitae nibh. Aliquam
aliquam erat magna, sit amet gravida neque aliquam ac.
</p>
<p>
Phasellus vehicula, libero eget tempus tristique, lectus felis ornare erat, sit amet semper sapien sem ac nisl. Sed dignissim vulputate mauris, id imperdiet tellus tincidunt id. Proin eget risus a enim convallis pulvinar. In bibendum augue in tellus fermentum
sollicitudin. Proin finibus tellus lacinia hendrerit faucibus. Curabitur congue aliquam eros non condimentum. Nullam justo ex, pulvinar ac fringilla nec, ullamcorper in lorem ullamcorper in lorem ullamcorper in lorem. Quisque vestibulum urna eu
blandit consectetur.
</p>
</div>
<div class="footer">
<div>
This is a footer
</div>
</div>
</div>
SCSS:
p {
margin: 0 auto;
padding-top: 32px;
max-width: 75%;
font-size: 1.5em;
}
.container {
min-height: 100%;
position: relative;
}
.footer {
position: absolute;
right: 0;
bottom: 0;
left: 0;
padding: 1rem;
background-color: cyan;
text-align: center;
}
position: absolute takes the element out if the normal layout. You then use left, right, and bottom to stick the footer to the bottom, essentially putting it over the bottom of the page.
By taking out those lines, my fiddle seems to above what you're wanting. If you want the footer to not have a margin, turn the margin to 0. Absolute positioning is not how that's done.
https://jsfiddle.net/kLqp3my7/
Make the body 100% of your page, with a min-height of 100% too.
The footer is then given negative margin-top:
#footer {
clear: both;
position: relative;
z-index: 10;
height: 3em;
margin-top: -3em;
}
You can create a footer that will stick to the bottom of the page fairly easy using flexbox
set the .container to be a flex container with display: flex;, and stack the flex items in a column with flex-direction: column;
then set the main content to flex: 1 which is shorthand for
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;
this will make it so that it will grow to fill the space, which will push the footer to the bottom of the page.
When the main content hits the footer, it will push the footer off the page since its in a flex container.
$(".add-section").on("click", function(){
$("main").append("<div class='section s3'>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.</div>");
});
* {
box-sizing: border-box;
}
html, body {
font-weight: 300;
font-size: 16px;
padding: 0;
margin: 0;
height: 100%;
}
.wrap {
display: flex;
min-height: 100%;
flex-direction: column;
}
header {
background: grey;
}
header .logo {
padding: 1.4rem;
font-size: 2rem;
}
main {
flex: 1;
}
.section {
font-size: 1.1rem;
padding: 1rem 2rem;
margin: 0;
background: #ccc;
}
footer {
background: grey;
padding: 1.2rem 1.4rem;
font-size: 1.2rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<div class='wrap'>
<header>
<div class='logo'>
Flexbox Footer
</div>
</header>
<main>
<div class='section s1'>
<button class='add-section'>Add Section</button>
</div>
<div class='section s2'>
Pellentesque 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.
</div>
</main>
<footer>
Footer
</footer>
</div>
I want to create three divs inside of one 'wrapper'-div. I want the three divs, let's call them 'left', 'center' and 'right', to be visible like three columns next to each other. The columns are shown in the right way, and the height takes up 100% of the screen, but when I add more text in the center div, I want the divs to take all available height of the screen. Like you see in the snippet below, the heights of the divs won't vertically stretch like I'd want them too.
I tried changing the divs to table and table-cell display, but I couldn't get this to work. Besides that I tried messing around with position absolute and relative, but I couldn't get this to work either.
In the real situation the centered div takes up 1024px in width and the left and right divs have a width of 50%-512px.
I searching for a way to solve this but I still couldn't get it to work.
html, body{
margin: 0;
padding: 0;
height: 400px;
}
div#menu{
height: 30px;
background-color: green;
width: 600px;
}
div#wrapper{
height: 100%;
width: 100%;
}
div#left{
background-color: yellow;
float: left;
width: 100px;
height: 100%;
}
div#center{
width: 400px;
background-color: red;
height: 100%;
float: left;
}
div#right{
width: 100px;
float: left;
height: 100%;
background-color: blue;
}
<html>
<body>
<div id="menu">
menu-item 1, menu-item 2, menu-item 3
</div>
<div id="wrapper">
<div id="left">
</div>
<div id="center">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi eleifend tellus eget odio cursus, id dignissim dolor tincidunt. Maecenas libero quam, scelerisque tincidunt est sit amet, consequat faucibus massa. Fusce at varius nunc. Integer mattis velit purus, nec dignissim ipsum pulvinar eget. Nulla facilisi. Suspendisse tempor, turpis eu convallis commodo, erat elit cursus sem, vel dignissim augue urna sit amet purus. Fusce accumsan risus neque, a dapibus velit mollis in. Aliquam varius euismod lorem sit amet bibendum. Donec tempus neque ac interdum pulvinar.
Nulla id iaculis magna. Ut dui lorem, porttitor eget volutpat vel, interdum at nibh. Duis rhoncus, eros ut pharetra euismod, metus metus elementum enim, id egestas sem arcu sit amet nisl. Quisque sed aliquam est. Vivamus bibendum sapien sit amet nisi auctor, et congue elit cursus. Praesent feugiat ex ex, in elementum augue efficitur a. Sed a felis ut est pharetra venenatis eu ac metus. Donec sed nisl semper, dignissim est ac, faucibus dolor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Cras bibendum nulla dolor, et tincidunt ligula hendrerit ac.
Integer et tincidunt ante, tincidunt hendrerit lacus. Nullam viverra id enim et viverra. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean ullamcorper posuere pharetra. Nullam ac quam eu nisl vulputate condimentum quis eu ante. Mauris vel egestas tortor. Ut suscipit ex ac commodo pharetra. Sed pharetra convallis ornare. Curabitur posuere dictum sapien, et rutrum nulla tempor sit amet. Nulla sagittis massa quis vulputate vehicula. Proin dignissim lorem vel neque finibus, ut sodales dolor sollicitudin. Maecenas finibus leo non lorem porttitor condimentum. Aenean nec odio id nulla scelerisque bibendum.
Fusce feugiat sem turpis, id iaculis dui condimentum vel. Vivamus tempus semper ultricies. Vestibulum egestas viverra blandit. Nunc leo justo, semper ut elit in, luctus pharetra nibh. Cras ac lectus egestas quam interdum vehicula. Vivamus pulvinar sapien sed gravida pharetra. Mauris hendrerit congue augue, quis vulputate mauris vulputate at. Nulla sit amet ullamcorper ante.
In lobortis sem id arcu dignissim pretium. Sed vulputate eleifend leo. Donec eget risus sit amet ante molestie porta. Phasellus massa diam, lacinia ac imperdiet ut, molestie vitae nisl. Pellentesque tristique ligula lacus, eu tempus est feugiat vitae. Ut ac tincidunt nunc. Nulla et lectus quam. Aenean auctor tempus nibh sed efficitur. Nam blandit dictum ligula bibendum mollis. Proin mollis lorem at viverra porttitor. Duis placerat bibendum libero, id vulputate quam posuere id. Vestibulum vel ex mollis, tempor tortor et, suscipit risus. In augue dui, bibendum et tempus id, hendrerit vitae sapien. Aliquam erat volutpat.
</div>
<div id="right">
</div>
</div>
</body>
</html>
Have you tried using flexbox? You could make them all the same height using something like this:
#wrapper{
display: flex;
flex-direction: row;
}
#left, #center, #right {
display: flex;
}
https://jsfiddle.net/rxdd4mdd/
Update:
I had a look at the website you posted a link to and you need to replace the folowing rules in your css, with these:
#profile-wrapper {
display: flex;
flex-direction: row;
width: 100%;
}
#profile-wrapper .left,
#profile-wrapper .right {
background-color: #ebebeb;
display: flex;
flex: 1;
}
#profile-wrapper #profile-page {
width: 1024px;
display: flex;
flex-direction: column;
}
I've posted the full HTML of the edited page here: http://pastebin.com/9PvQvfiU
Update 2:
This should make sure that it is at least 100% of the height of the body:
body {
display: flex;
flex-direction: column;
}
#profile-page {
flex: 1;
}
Use Style Height :Auto; for wrapper div
#wrapper {
width: 600px;
height: auto;
}
https://jsfiddle.net/kn9spjhn/
This duplicate didn't solve because I don't want to use JS. CSS: Sidebar fixed width with background to edge of window
This is what I'm trying to achieve:
Basically all the content must be inside a div with a specific width and set to the center with margin: 0 auto. Inside this div there must be a main div with a white background and a sidebar with a blue background.
I don't want to use javascript. Isn't this possible with pure CSS?
CURRENT CODE (not working): https://jsfiddle.net/0p9jrnq1/1/
Try this..
.sidebar {
position: static;
}
.sidebar:after {
content: "";
position: absolute;
right: 0;
top: 0;
bottom: 0;
width: 22%; (adjust till get the right width)
height: 100%;
bacgkround: (the sidebar background);
z-index: (below sidebar);
}
Doing this with fixed with seems kind of tough for me. If you can do with percentages, then this works. All you have to add to this is use media queries in order to reduce the size or hide the left and right gutters when viewing this layout in smaller screens.
HTML:
<div class="container">
<div class="left-gutter"></div>
<div class='content'>
<div class="main"> </div>
<div class="sidebar"> </div>
</div>
<div class="right-gutter">
</div>
CSS:
.container {
width:100%;
}
.container > .left-gutter, .container > .right-gutter {
width:20%;
}
.container > .left-gutter {
height:100%;
float:left;
}
.container > .right-gutter {
height:100%;
background: #0000FF;
float:right;
}
.container > .content {
width: 60%;
height:100%;
float:left;
min-width: 200px;/*Your minimum fixed width here*/
}
.container > .content > .main {
width: 80%; /*Width for the content area in %*/
float: left;
height:100%;
background:#FFFFFF;
}
.container > .content > .sidebar {
width: 20%; /*Width for the sidebar area in %*/
float: right;
height:100%;
background:#0000FF;
}
Make sure width percent of main and sidebar add up to be 100%
Using padding and margins will require you to adjust the widths of the elements accordingly.
Take a look at this layout
body {
margin: 0;
padding: 0;
}
#header {
background-color: #02CC02;
width: 100%;
position: relative;
z-index: 2;
}
#header .clearfix {
padding: 40px;
}
#main-content {
background-color: white;
}
.page-content {
margin: 0 auto;
width: 55%;
}
#sidebar {
background-color: rgba(238, 130, 238, 0.92);
position: fixed;
right: 0;
top: 0;
width: 300px;
height: 100%;
z-index: 1;
}
#sidebar .clearfix {
padding: 60px;
}
<div id="header">
<div class="clearfix"></div>
</div>
<div id="main-content">
<div class="page-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor blandit mauris, vel ornare metus cursus eu. Maecenas faucibus nisl non mauris sagittis, at porttitor lorem vestibulum. Curabitur vulputate facilisis nunc nec imperdiet. Sed finibus risus eu quam bibendum, imperdiet commodo felis condimentum. Morbi dapibus, libero eu malesuada sagittis, justo urna ullamcorper odio, a venenatis orci turpis ac nisi. Ut porta commodo nibh, at auctor nisi dapibus sit amet. Nullam tincidunt urna at nisi finibus dictum.</p>
<p>Duis orci purus, varius vel dolor a, pharetra mattis ipsum. Duis aliquam velit sed ex consequat pretium. Donec eleifend mattis elit, sit amet accumsan diam sodales id. Nulla sed sem nisl. Sed mattis nunc massa, eget ultrices ex luctus sit amet. Curabitur porttitor turpis non tortor venenatis, at blandit dui elementum. Proin vehicula, augue ac tempor euismod, erat quam iaculis velit, a bibendum erat sapien sed dolor. Proin sed augue convallis, molestie sem id, finibus ante. Ut in tincidunt ligula, non rutrum tortor. Cras eu ex eleifend, volutpat nibh at, faucibus nunc. Nam eget augue porta, congue tellus id, viverra turpis. Curabitur quis felis ligula. Phasellus lacus erat, molestie eget sapien quis, luctus feugiat mi. Nam tristique, sem eget aliquam interdum, ligula neque malesuada diam, vitae rhoncus elit est eu arcu.</p>
<p>Etiam finibus purus mattis, elementum nibh sit amet, eleifend nulla. Duis tortor eros, bibendum eget mattis nec, feugiat quis sem. Curabitur consequat urna in turpis facilisis maximus. Nulla elementum molestie ligula. Vestibulum eleifend fermentum quam ut sagittis. Integer nunc tortor, condimentum et posuere vel, vestibulum quis leo. Ut feugiat vehicula arcu, laoreet vehicula mi rutrum vitae.</p>
<p>In venenatis, erat eu interdum ornare, leo magna eleifend elit, vitae fermentum metus dui vel quam. Vivamus auctor lacinia porta. Nullam vitae vestibulum libero. Quisque tincidunt pellentesque metus, sit amet pharetra est mattis quis. Sed mattis, nisl a interdum porttitor, velit ligula lacinia orci, sed hendrerit augue dolor vel nisi. Aliquam ut ex vitae nunc aliquam aliquam et in mi. Phasellus sit amet ante quis ipsum cursus volutpat eget eget lectus. Curabitur tempor sed odio id pulvinar. Suspendisse sed elit egestas, lobortis est id, aliquet urna. Mauris ac purus at justo condimentum rutrum non eget libero. Quisque scelerisque erat sed orci consequat suscipit. Quisque sit amet dui hendrerit, commodo arcu quis, tristique quam. Phasellus feugiat nulla velit, nec condimentum nisl varius eget. Mauris facilisis et arcu vitae ultrices. Vivamus viverra, lorem vitae eleifend vulputate, neque sem volutpat ante, eget rhoncus erat nisl ac turpis.</p>
</div>
</div>
<div id="sidebar">
<div class="clearfix"></div>
</div>
Use JS to show and hide the sidebar
So I have two divs, one is a .header and the other is the .content, together they would take up the whole page. The .header would be set in place and only the content would scroll.
I would like to make the content's height to be dynamic based on the height of the .header without javascript.
This could be achieved with a calc if the .header had fixed height, but it doesn't because the height can vary.
Code:
.container {
height: 100%;
}
.header {
height: 50px; // height is fixed, not great for dynamic content
}
.content {
height: calc(100% - 50px); // height is a subtraction
}
<div class='container'>
<div class='header'>Header</div>
<div class='content'>
Content
</div>
</div>
See switching of content here
Is there a CSS only solution in which would calculate the height of the .content based on the size of the .header?
you can use flexbox for that
html,
body {
margin: 0;
height: 100%;
}
.container {
display: flex;
flex-direction: column;
height:100%
}
.header {
display: flex;
border: 5px solid red
}
.content {
flex: 1;
border: 5px solid green
}
<div class='container'>
<div class='header'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas at libero ut arcu tincidunt porta. Vestibulum in ex nec neque rutrum vestibulum a nec eros. Morbi accumsan nisl in justo viverra, non euismod libero sagittis. Aenean vulputate tortor
molestie metus iaculis ornare. Vestibulum posuere, mauris eget tempor tincidunt, arcu risus eleifend felis, ac fringilla mauris lorem sit amet risus. Ut id ante eu nisl hendrerit tincidunt ac ut velit. Sed efficitur ante in neque mollis feugiat. Nulla
sed erat nec ipsum fermentum tristique. Sed auctor dolor quis fringilla ullamcorper. Duis luctus ligula nibh, ac facilisis eros dapibus at. Aliquam eget vestibulum sem, a euismod ante. Quisque at arcu arcu. Nunc massa tellus, imperdiet in facilisis
vitae, maximus at neque. Maecenas et dictum enim.</div>
<div class='content'>
Nam vehicula tempor est et ultrices. Cras elementum, mi ac pellentesque ultricies, dui urna rutrum risus, quis tristique ante eros in tellus. Quisque sit amet varius erat. Aliquam dapibus eros augue, et blandit nulla volutpat nec. Duis nibh lacus, scelerisque
in interdum in, hendrerit eget justo. Pellentesque finibus nisi sed fermentum aliquet. Mauris feugiat, magna in sagittis dapibus, neque justo convallis risus, eget rutrum metus tellus eget lorem. Suspendisse at turpis faucibus eros dapibus feugiat
vel at elit. Sed ac arcu tempor, molestie elit ut, eleifend dolor. Morbi fringilla enim ac lectus lobortis, ac convallis orci mollis. Ut tempus quam sodales, tincidunt dolor non, dapibus neque. Fusce elementum magna dolor, non suscipit est imperdiet
at. Vivamus mattis augue vestibulum, ultricies dui ac, finibus erat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla facilisi. Curabitur id cursus sapien. Phasellus placerat finibus vehicula. Donec
sit amet nisi eget risus venenatis euismod. Curabitur quis felis tempus, egestas ante eget, efficitur dolor. Duis ullamcorper commodo massa, a efficitur turpis finibus nec. Cras eget dui purus.
</div>
</div>
Or CSS tables for older browsers (as OP requested in comment)
html,
body {
margin: 0;
height: 100%;
}
.container {
display: table;
width: 100%;
height: 100%
}
.header {
display: table-row;
background: red
}
.content {
display: table-row;
background: green
}
<div class='container'>
<div class='header'>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas at libero ut arcu tincidunt porta. Vestibulum in ex nec neque rutrum vestibulum a nec eros. Morbi accumsan nisl in justo viverra, non euismod libero sagittis. Aenean vulputate tortor
molestie metus iaculis ornare. Vestibulum posuere, mauris eget tempor tincidunt, arcu risus eleifend felis, ac fringilla mauris lorem sit amet risus. Ut id ante eu nisl hendrerit tincidunt ac ut velit. Sed efficitur ante in neque mollis feugiat. Nulla
sed erat nec ipsum fermentum tristique. Sed auctor dolor quis fringilla ullamcorper. Duis luctus ligula nibh, ac facilisis eros dapibus at. Aliquam eget vestibulum sem, a euismod ante. Quisque at arcu arcu. Nunc massa tellus, imperdiet in facilisis
vitae, maximus at neque. Maecenas et dictum enim.</div>
<div class='content'>
Nam vehicula tempor est et ultrices. Cras elementum, mi ac pellentesque ultricies, dui urna rutrum risus, quis tristique ante eros in tellus. Quisque sit amet varius erat. Aliquam dapibus eros augue, et blandit nulla volutpat nec. Duis nibh lacus, scelerisque
in interdum in, hendrerit eget justo. Pellentesque finibus nisi sed fermentum aliquet. Mauris feugiat, magna in sagittis dapibus, neque justo convallis risus, eget rutrum metus tellus eget lorem. Suspendisse at turpis faucibus eros dapibus feugiat
vel at elit. Sed ac arcu tempor, molestie elit ut, eleifend dolor. Morbi fringilla enim ac lectus lobortis, ac convallis orci mollis. Ut tempus quam sodales, tincidunt dolor non, dapibus neque. Fusce elementum magna dolor, non suscipit est imperdiet
at. Vivamus mattis augue vestibulum, ultricies dui ac, finibus erat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla facilisi. Curabitur id cursus sapien. Phasellus placerat finibus vehicula. Donec
sit amet nisi eget risus venenatis euismod. Curabitur quis felis tempus, egestas ante eget, efficitur dolor. Duis ullamcorper commodo massa, a efficitur turpis finibus nec. Cras eget dui purus.
</div>
</div>
Use display: flex
html, body { margin: 0; height: 100%; }
.container {
display: flex;
flex-direction: column;
height: 100%;
}
.header {
background: blue;
}
.content {
flex: 1;
background: red;
}
<div class='container'>
<div class='header'> Header </div>
<div class='content'>
Content
</div>
</div>
Or display: table
html, body { margin: 0; height: 100%; }
.container {
display: table;
width: 100%;
height: 100%;
}
.header {
display: table-row;
height: 0; /* shrink to fit content */
background: blue;
}
.content {
display: table-row;
background: red;
}
<div class='container'>
<div class='header'> Header </div>
<div class='content'>
Content
</div>
</div>
And merged with fallback, where flex kicks in if supported.
Note, the header's height: 0 is needed if it should shrink to content
html, body { margin: 0; height: 100%; }
.container {
display: table;
width: 100%;
height: 100%;
}
.header {
display: table-row;
height: 0; /* shrink to fit content */
background: blue;
}
.content {
display: table-row;
background: red;
}
#supports (display: flex) {
.container {
display: flex;
flex-direction: column;
}
.header {
display: block;
height: auto;
}
.content {
flex: 1;
display: block;
}
}
<div class='container'>
<div class='header'> Header </div>
<div class='content'>
Content
</div>
</div>