I'm making a CSS layout that has a 960px wide div, and I'd like to have it reach from the top of the page to the bottom. The obvious solution is min-height: 100%;, but it doesn't work. Here's my CSS:
* {
margin: 0px;
padding: 0px;
}
body {
background: #FF0000;
height: 100%;
}
html {
height: 100%;
}
#sheet {
width: 960px;
min-height: 100%;
background: #000000;
margin: 0px auto 0px auto;
}
#sheet1 {
width: 760px;
min-height: 100%;
top: 0px;
bottom: 0px;
background: #FFFFFF;
}
And from my HTML:
<div id="sheet">
<div id="sheet1">
</div>
</div>
It display fine when I change #sheet's min-height to height, but then it would get cut off if the content took up more than a page. Is there a solution for this?
Try changing #sheet to height:100%, not min-height 100%, and add some content inside of #sheet-1.
Using 100% only takes effect only if there is HTML Element (text, image, or other elements) exist in it. Its height will be higher or shorter according to the length of the element. Thus min-height is used to specify only the exact number of length or height of the element. Try using pixels instead of percentage.
because you don't add any float or clear property ...
CSS will ignoring min-height and max-height, if the height property not defined,
Except if you add display:inline-block, but it can break your margin:0px auto; and i decide you to do like this:
* {
margin: 0px;
padding: 0px;
}
body {
background: #FF0000;
height: 100%;
text-align:center;
}
body * {
text-align:left;
}
html {
height: 100%;
}
#sheet {
width: 960px;
display:inline-block; // It can make height auto
min-height: 100%;
background: #000000;
margin: 0px auto 0px auto;
text-align:center;
}
#sheet1 {
width: 760px;
display:inline-block;
min-height: 100%;
background: #FFFFFF;
text-align:left;
}
Replace your css, with my code and i think it should work...
Related
I have been puzzling over this bit of HTML/CSS for hours and I can't figure out where I'm going wrong. It is part of a more complicated layout, but I think I have isolated the problem.
This is my HTML:
<table>
<tr>
<td>
<div class = "outer">
<div class = "middle">
<div class = "inner">
<svg viewbox = "0, 0, 250, 250">
</svg>
</div>
</div>
</div>
</td>
</tr>
</table>
...and this is my CSS:
* {
margin: 0px;
padding: 0px;
}
body,html {
height: 100%;
font-family: sans-serif;
font-size: 12px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-text-size-adjust: none;
}
table {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
min-width: 600px;
/*table-layout: fixed;*/
border-collapse: collapse;
border-spacing: 0;
}
tr {
background-color: #ffffff;
height: 100%;
height: calc(100% - 190px);
}
td {
height: 100%;
background-color: red;
margin: 0;
}
.outer {
width: 94%;
width: calc(100% - 20px);
height: 94%;
height: calc(100% - 20px);
background-color: blue;
display: block;
margin: 3%;
margin: calc(10px + 0%);
overflow-y: scroll;
}
.middle {
display: white;
background-color: lightblue;
padding: 0px;
margin: 0px;
width: 100%;
height: 100%;
height: calc(100% - 10px - 2.5rem);
overflow-y: scroll;
}
.inner {
background-color: pink;
display: block;
width: 40%;
height: 100%;
overflow: hidden;
}
svg {
height: auto;
max-height: 100%;
width: auto;
max-width: 100%;
background-color: salmon;
}
If you remove the SVG, the divs and table behave as I would like - on window resize, the table doesn't exceed the size of the page, and the divs all scale accordingly.
When you include the svg, what I would expect (and what I would want) is for the svg to scale to fit the .inner div, but not to exceed it or alter the flow of the layout in any way.
This is not what happens.
I've checked this in Safari and in Firefox, and both give me unexpected but different results. In firefox, if the window shrinks beyond a certain point, the svg height expands and the whole window ends up scrolling.
In Safari, the svg height scales as the width changes, but the width doesn't scale as the height changes.
This seems like a pretty simple thing and I don't really know where I'm going wrong. I am sure it is an obvious rookie error of some sort. I have tried every combination of max/min height/width on the svg.
Thanks for your help!
EDIT: If I get rid of the table (remove the table, td, tr) everything works as expected. Equally, if I get rid of the svg and keep the table, everything works as expected.
As stated here, you can't set a width/height value of auto for an svg element. Use 100% for the height and width instead like this:
svg {
height: 100%;
max-height: 100%;
width: 100%;
max-width: 100%;
background-color: salmon;
}
If the problem still persist, try adding the height and width property as an inline style declaration to the svg and see if that fixes the problem:
<svg style="width:100% max-width:100%; height: 100%; max-height: 100%;" viewbox = "0, 0, 250, 250"></svg>
you missed to give height on tbody
tbody {
height: 100%;
}
Actually browser automatically add tbody in your table if you not add. you can see this in inspect element
I'm having problem with making a div stretch and shrink depending on the size of the browser.
Here is the html
<div class="content_container">
<div class="content_menu"></div>
<div class="content_left"></div>
<div class="content"></div>
</div>
.content_container{
margin: 0 auto;
height:100vh;
display:block;
}
.content_left{
background: #eee none repeat scroll 0 0;
display: inline-block;
float: right;
padding-top: 50px;
position: relative;
text-align: center;
width: 25%;
height:calc(100vh - 50px);
}
.content_menu{
background: #eee none repeat scroll 0 0;
float: left;
height: 100px;
width: 25%;
height:100vh;
}
.content{
background: #fff none repeat scroll 0 0;
display: inline-block;
margin-bottom: 0 !important;
margin-right: auto !important;
vertical-align: top;
width: 50%;
}
I've already tried giving height:auto, 100% and 100vh but none seems to work.
The .content_left and .content_menu fall short of the height of the .content so there are blank white spaces.
Is there anyway those layers can resize themselves to fit to the height as well as the .content div.
Can anyone help me out?
Use viewport width/height to set an elements dimensions relative to the window
body {
padding: 0; margin: 0;
width: 100%;
width: 100vw;
}
div {
background: lightblue;
height: 45px;
margin-top: 20px;
}
#two {
width: 100%;
width: 100vw;
}
#one {
width: 60%;
width: 60vw;
}
<div id="one">div one</div>
<div id="two">div two</div>
I'm guessing the blank white spaces you are talking about are those surrounding the gray elements on the left and right side. Those are caused by the default margin on the body. Just set the body margin to zero.
body { margin: 0; }
Using your markup in your question, it appears to work as I think you want it to.
Let's consider one element #top. I want it's height to be 100% of the page, but diminished by 10em from the bottom. I'm unable to figure out how to do it. Please help.
HTML
<div id="top"></div>
<div id="bottom"></div>
CSS
html, body {
height: 100%;
padding: 0;
margin: 0;
font-size: 100%;
}
#top {
width: 100%;
height: 70%;
background-color: red;
}
#bottom {
position: absolute;
bottom: 0;
margin-left: 10%;
width: 100%;
height: 10em;
background-color: #000000;
}
See http://codepen.io/anon/pen/jEqYMK (for illustration, i have shown one more element, #bottom).
Your demo is lil weird but anyways, if you want to deduct 10em from 100% then use calc() property like
#top {
width: 100%; /* You won't need this */
height: calc(100% - 10em);
background-color: red;
}
Demo
Note : I've removed position: absolute;, margin-left like properties from your demo because I have no idea why you were using them at first place but if you want you can still use them.
I'm trying to get a div to fill the remaining height of a div. Here's my HTML and CSS:
CSS:
* {
padding: 0px;
margin: 0px;
}
#container {
margin: 85px auto 0px auto;
background: #444444;
min-height: 500px;
width: 900px;
}
#topbar {
width: 900px;
height: 85px;
background: #555555;
}
#leftbar {
width: 250px;
height: 100%;
background: #666666;
}
HTML:
<div id="container">
<div id="topbar">
</div>
<div id="leftbar">
</div>
</div>
I expected leftbar to fill the height between the bottom of topbar and the bottom of container, but it's scretching container so that leftbar is 100% of the page height.
You can stretch the leftbar with absolute positioning and setting the top/bottom values:
* {
padding: 0px;
margin: 0px;
}
#container {
position: relative;
margin: 85px auto 0px auto;
background: #444444;
min-height: 500px;
width: 900px;
}
#topbar {
width: 900px;
height: 85px;
background: #555555;
}
#leftbar {
position: absolute;
top: 85px;
bottom: 0;
width: 250px;
background: red;
}
Fiddle:
http://jsfiddle.net/robertp/CQ7pf/
Try adding this to container:
position: relative;
and then add this to leftbar:
position: absolute;
top: 0;
bottom: 0;
Set your left bar to position: relative;
So leftbar should be container's height minus topbar's height. Since container and topbar have hard-coded height values, it follows that leftbar will have to be hard-coded also. It's not the prettiest thing in the world but it's simpler than the alternative, JavaScript.
If container is 500px in height, subtract the height of topbar (85) and container's margin (85) to arrive at a height of 330px. Since container uses min-height, use min-height for leftbar also to allow it to stretch the container if need be. You should also change leftbar's position to relative to render the height of container correctly.
Bottom line:
#leftbar {
position: relative;
min-height: 330px;
}
I have been reading all the 100% div height discussions and I can't find a simple solution. How can I make my div take up all of the vertical height of the screen?
Here is my code:
css
#mother {
width: 100%;
margin: 0 auto;
z-index: 1;
}
#stripe_wrap {
width: 1053px;
min-height: 500px;
margin: 0 auto;
background: lime;
}
#stripe1 {
width: 39px;
min-height: 500px;
margin: 0px 0px 0px 0px;
background: #000;
float: left;
}
#stripe2 {
width: 39px;
min-height: 500px;
margin: 0px 0px 0px 0px;
background:#000;
float: right;
}
html
<div id="mother" style="overflow-x: hidden;">
<div id="stripe_wrap">
<div id="stripe1"></div>
<div id="stripe2"></div>
</div>
</div>
You have to make the <body> tag of the height 100% as well, otherwise it is vertically truncated to fit the content.
Also make sure to put the margin of <body> to 0px, because otherwise it will become 100%_of_visible_area + margin, resulting in a vertical scroll bar.
html, body {padding: 0px; margin: 0px; height: 100%;}
http://jsfiddle.net/kEv8F/ - my version.
http://jsfiddle.net/kEv8F/ - your version.
Is that what you meant?
Try to set height:100% for your <body> and <html>, too.
If there is nothing except this div on your page, 100% height will be 0px without these settings.