I'm using position: fixed; to make a div adjusting to different screen sizes. The height is set to 100% in this simplified example to make the div "example" always take up the whole height of the screen. What I want to do is making space both over and under this div and I'm doing so by using position: fixed; and top: 100px; bottom: 100px;
The problem is that the code only runs top: 100px; not both. Is there any way around this problem?
Fiddle
HTML
<div id="example"></div>
Css
#example {
background-color: #333;
width: 500px;
height: 100%;
position: fixed;
bottom: 100px; /* This is clearly not working, how do I do this with absolute/fixed position? */
top: 100px;
}
EDIT
And if I set the height using this function istead of setting height in css to 100%. How do I do then?
$(document).ready(function() {
function setHeight() {
windowHeight = $(window).innerHeight();
$('#example').css('min-height', windowHeight);
};
setHeight();
$(window).resize(function() {
setHeight();
});
});
You have to remove the height: 100%. The browser will calculate the distance between the top and bottom value and create the height you need.
new code:
#example {
background-color: #333;
width: 500px;
position: fixed;
bottom: 100px; /* This is clearly not working, how do I do this with absolute/fixed position? */
top: 100px;
}
JSFiddle
You seem to misunderstand the box model. When you say "height: 100%" it means that the height of the element will be the same number of pixels as the containing element with layout. Setting top:100px will result in moving the box down 100px, but it will not affect the height of the box. Thus, 100px of the box will overflow the viewport. This is the same if you specify bottom:100px, except that top 100px of the box will underflow the viewport.
Remove height 100% and the top and bottom instructions will calculate the element height.
#example {
background-color: #333;
width: 500px;
position: fixed;
bottom: 100px; /* This is clearly not working, how do I do this with absolute/fixed position? */
top: 100px;
}
Just try this instead of height: 100%, it would be enough; http://jsfiddle.net/xd2j2shm/1/
height: calc(100% - 200px);
EDIT: I like RMo's answer more. It is simpler. That being said, they both work.
To clarify, it sounds like you want the div to cover 100% of the height, minus for 100px at the top and 100px at the bottom.
The way to do this is relatively new. You need to use the calc value for your height property:
#example {
background-color: #333;
width: 500px;
height: calc(100% - 200px);
position: fixed;
top: 100px;
}
It has wide support in modern browsers: http://caniuse.com/#feat=calc
Related
I'm trying to make a fixed position div stuck to the bottom of the page, that has the starting height, 70% of the screen ( like vh ).
I will make it resizable with jQuery resizable.
The problem is that if I apply height: 70vh or height: 70%, the div resizes when the user resizes the browser height, and I want to keep it the same.
Any idea what to do?
div {
position: fixed;
display: block;
bottom: 0;
width: 500px;
height: 70vh;
background-color: red;
}
<div>
</div>
View the snippet in full page.
vh or % will be relative to the height of the viewport or screen height. So we need to set the initial height of the div with JavaScript on DOM load.
Next (The resizing part) can be done with CSS resize property.
**PS: In the div bottom right corner you can see the resize icon and do the resizing.
document.getElementById("demo").style.height = window.innerHeight*.7+"px";
div {
position: fixed;
bottom: 0px;
width: 500px;
background-color: red;
resize:vertical;
overflow:auto;
}
<div id="demo"></div>
You can add min-height to div so that it will not resize itself beyond a specific height.
Like this
div {
position: fixed;
display: block;
bottom: 0;
width: 500px;
height: 70vh;
min-height: 500px;
background-color: red;
}
<div>
</div>
I am building an app in Ionic where I am trying to locate four row elements and a button inside a div. The div has an height: 100% that covers the whole screen, and the content has to stretch to its full-height.
I have tried the following solution, but apparently it is not working:
ion-content {
position: relative;
height: auto;
width: 100%;
}
.boxes-container {
position: absolute;
height: 100%;
width: 100%;
}
This is the complete code. Do you have an idea of what is a possible way to solve it?
Thanks in advance for your replies!
You can use a combination of the calc function and viewport units to achieve this layout.
height: 100vh will give an element a height equal to that of the viewport.
You have a header element that is 44px in height. Each row has a vertical margin. You can deduct these from 100vh using the calc function:
.boxes-container {
height: calc(100vh - 44px - 50px);
}
This will give your element a height equal to that of the viewport minus the height of the header element.
You then need to give your four rows and the button a height of 20% so that they occupy all of the available vertical space in the container.
.row {
...
height: 20%;
}
.button {
height: 20%;
}
Updated live demo
calc() is available in all major browsers and IE>8 (caniuse.com)
An alternative would be to give both .scroll and .boxes-container a height of 100% and .row a height of 20%:
.scroll {
height: 100%;
}
.boxes-container {
height: 100%;
}
.row {
...
height: 20%;
}
Updated live demo
I am attempting to create a game where the first thing that the user sees is a start-menu modal on top of a game background.
Basic HTML:
<div class="game-board">
<div class="menu"> </div>
</div>
CSS:
html, body{
min-height:100%;
}
.game-board{
background-image: url(../images/sand.png);
width: 1260px;
height: 100%;
}
.menu{
position: absolute;
width: 400px;
right: 0;
top: 30%;
left: 31%;
background: whitesmoke;
border-radius: 4px;
}
I expected the above code to show the background-image in the background, and then somewhere near the middle of the image, the "modal" is above the background. However, for some reason that I'd love to know, the parent div .game-board is collapsed with no height and thus no background image, but the modal appears fine. Why is this?
Rule - For height in percentage to work in CSS, the parent element should have a height that can be calculated.
For example, when you say .game-board should have a height of 100% - then the question that arises is 100% of what? Because the parent element body in this case, does not have height specified explicitly. Min-height does not work because that does not fix the height of the element to a particular value on a particular view port. For example, if the viewport has height 100px then min-height: 100% could mean anything from 100px to infinity. Thus the height rule on .game-board doesn't work.
To fix this, change min-height to height
html, body {
height: 100%;
}
Also, the absolutely positioned menu, needs to have a height if there is no content as of yet inside it, else it would not appear.
Here is a working fiddle. http://jsfiddle.net/8dhfac8w/
.game-board needs a fixed height. .menu can do with a variable height so long as it's contained by a fixed height parent. This works (Fiddle).
html, body{
min-height:100%;
}
.game-board{
background-image: url("http://trikkiworld.com/images/bg/bg_sand/25012011/sand006.jpeg");
width: 200px;
height: 200px;
}
.menu{
position: relative;
width: 50%;
height: 50%;
top: 25%;
margin-left: auto;
margin-right: auto;
display: block;
background: whitesmoke;
border-radius: 4px;
}
Is it possible to dynamically set a divs width, then set its height to a certain percentage of that width. Something like this:
#myDiv{
width:%100;
height: {35% of width};
}
I want to retain the ratio of the width to height of a div regardless of what the width may be.
You can do this with CSS by setting the height to zero and then adding a percentage to padding-bottom. You might have to tweak it a little to get the desired outcome but here's a fiddle with an example
http://jsfiddle.net/wbq8o3s7/
#myDiv {
width: 100%;
height: 0;
padding-bottom: 35%;
background-color: #333;
}
<div id="myDiv"></div>
HTML:
<div class='box'>
<div class='content'>All your content are belong to us</div>
</div>
We use two block elements to achieve the desired behaviour, box for width, content for height.
CSS:
.box{
position: relative;
width: 50%; /* desired width */
}
.box:before{
content: "";
display: block;
padding-top: 100%; /*What you want the height to be in relation to the width*/
}
The Content:
Then set the content to cover the entire box absolutely so no matter the size, it fits!
.content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
Tada you are done...
SOURCE: Pure CSS
Try a simple JavaScript:
document.getElementById("elemID").style.height =
Math.round((document.getElementById("elemID").style.width * 100) / 35) + "px";
This will get a percentage (Rounded) of the with, and assign it to the height.
I have the following bootstrap example http://jsbin.com/EKEHeCIX/3/edit.
As you can see in the code above my form resides in the top of content. But I need to reside it in the middle.
Is it possible to make this without explicity setting of top in pixels? Because need to get it work for any screens (not tablets or phones)
You can do this without setting the top pixels.
Use this:
width: 50%;
height: 50%;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
With the position on absolute, you can make this responsive for any screensize.
You might want to take a look at this:
Absolute Centering
This can help you out with alot of centering/styling issues!
If you know the exact width and height of your element you could do something like:
.centered {
position: fixed;
top: 50%;
left: 50%;
margin-top: -150px;
margin-left: -220px;
}
Setting margin-top as 50% of element's height and margin-left as the 50% of element's width.
And obviously you must add class="centered" to your element.
Here is your example: http://jsbin.com/EKEHeCIX/6
If you don't know the size of the element or it has a mutable size, you could (using jQuery) implement the css code with javascript:
$(function() {
var $obj = $('.centered');
$obj.css({
marginTop: -($obj.outerHeight() / 2),
marginLeft: -($obj.outerWidth() / 2)
});
});
Demo with javascript: http://jsbin.com/EKEHeCIX/7
You can use this trick :
.form-signin {
width: 400px;
height: 230px;
position: absolute;
/* top:50px for header size */
/* bottom:60px for footer size */
top: 50px; bottom: 60px; left: 0; right: 0;
margin: auto;
}
JSBin updated