When you set the width/height to 100% for divs, they should take the dimension of their parent element.
I see this is the case for the width but not the height. The elements in question are as such where fm is the parent and fm_xxx are the children.
#fm{
position: relative;
text-align: left;
width: 370px;
height: 100%;
}
#fm_tags{
position: relative;
width: 100%;
height: 100%;
}
#fm_arcmarks{
margin-top:10px;
position: relative;
width: 100%;
height: 100%;
clear: both;
}
#fm_space{
height: 10px;
width: 100%;
height: 100%;
clear: both;
}
for this html
<div id = 'fm'>
<div id = 'fm_tags'></div>
<div id = 'fm_space'></div>
<div id = 'fm_arcmarks'></div>
</div>
To see the code live go here:
https://frozen-dusk-2587.herokuapp.com/
and click on FAVS
There are two ways to make height work in CSS.
For elements that are in the normal "flow" of the document, height needs to be set all the way up to and including the HTML element.
In your code, you have set the height of the main #fm div to 100%, but 100% of what? Percentages are always based on the parent element and your code doesn't show the height set on the parent element of #fm
Add:
html, body {height:100%;}
to your code and it will work. But note in the snippet below, that I've changed the child div elements to have heights of 33%, rather than the 100% you initially had. This way each will take up 1/3 of the parent div which is taking up 100% of its parent (body) height, which is taking up 100% of its parent (html) height:
html, body {height:100%;}
div {border:1px solid black; }
#fm{
position: relative;
text-align: left;
width: 370px;
height: 100%;
}
#fm_tags{
position: relative;
width: 100%;
height: 33%;
}
#fm_arcmarks{
margin-top:10px;
position: relative;
width: 100%;
height: 33%;
clear: both;
}
#fm_space{
height: 10px;
width: 100%;
height: 33%;
clear: both;
}
<div id = 'fm'>div
<div id = 'fm_tags'>div</div>
<div id = 'fm_space'>div</div>
<div id = 'fm_arcmarks'>div</div>
</div>
Take the element out of the normal "flow". This can be done with float:value or by setting position:absolute or position:fixed. It also applies to <table> and <tr> elements (as special cases).
When elements are taken out of the normal flow of the document, they are no longer contained by their parent, so parent height no longer matters and the elements are now free to use the height you've specified.
It looks like you need to set
body_main to height: 100%
as I have checked the debugger on your dev site and html and body are already set to 100%.
The entire chain must be set to 100%. So when you omit body_main in the height chain. It looses it's height and so does its child elements.
Related
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 there any solution without JS?
html
<div class="wrapper">
<div class="fix"></div>
</div>
css
.wrapper {
max-width: 500px;
border: 1px solid red;
height: 5500px;
position: relative;
}
.fix {
width: inherit;
height: 20px;
position:fixed;
background: black;
}
I cant add any other styles for .wrapper except width: 100%;.
I try with width: inherit but it doesn't work for me because of I have parent div with only max-width. source
Here is JsFiddle Demo
A position:fixed element is not relative to its parent anymore. It respects only the viewport's boudaries.
MDN Definition:
fixed
Do not leave space for the element. Instead, position it at a specified position relative to the screen's viewport and don't move it when scrolled.
So any width, max-width, or whatever property will not be respected by the fixed element.
EDIT
In fact, it won't inherit the width because there's no width property defined on the wrapper.. So, try setting the child as width: 100% and inherit the max-width:
http://jsfiddle.net/mx6anLuu/2/
.wrapper {
max-width: 500px;
border: 1px solid red;
height: 5500px;
position: relative;
}
.fix {
max-width: inherit;
width: 100%;
height: 20px;
position:fixed;
background: black;
}
there is already a width on the column, just set the width of the fixed element to inherit. no reason to complicate things.
CSS:
.col-sm-3 { width: 25%; }
.fixed-in-col { width: inherit; ... }
HTML:
<div class="col-sm-3">
<div class="fixed-in-div">
...
</div>
</div>
It seems there is no solution without JS.
This blog post by Felipe Tadeo explains why:
https://dev.to/phillt/inherit-the-width-of-the-parent-element-when-position-fixed-is-applied
It explains the confusion around width: inherit
"Fixed positions itself relative to the viewport... whenever you inherit width (with position fixed) it will be with respect to the viewport"
I am trying to position an div element at the bottom right of an image, that is inside a container element. I set position relative to the container, and position absolute to the inner div, but it does not work. Here is the (http://jsfiddle.net/ZC84G/). Please, help.
<div class="container">
<div class="icon"></div>
<img src="/images/someImage.png" />
</div>
CSS:
body {
background-color: black;
}
.container {
position: relative;
}
.container img {
max-width: 75%;
max-height: 80%;
}
.icon{
background-image: url('http://icons.iconarchive.com/icons/iconfactory/star-wars-lego/32/Biggs-No-Helmet-icon.png');
width: 31px;
height: 31px;
position: absolute;
bottom: 5px;
right: 5px;
}
This is because by default div has block display mode, and it's width is 100% of the parent container. Try to add display: inline to .container
.container {
position: relative;
display: inline;
}
Here's the corrected jsfiddle: http://jsfiddle.net/ZC84G/4/
Your container div has no width and height set. And since a <div> is a block-level element by default, it will be set to 100% width ie expand to however much horizontal space is left.
Plus, you're also constraining your image size:
max-width: 75%;
max-height: 80%;
If you replace the img CSS with:
max-width: 75%;
max-height: 80%;
It works fine, and as expected: http://jsfiddle.net/ZC84G/3/
I've modified your CSS on the image a bit.
Basically, I set it to scale properly to the size of its container, and now it sits where I think you wanted it. The way you could find this yourself in the future would be to inspect the element by using right click from your browser, and looking at the size of the different elements to see what was expanding larger/smaller than it should.
.container img {
max-width: 100%;
height: auto;
}
I am trying to understand a problem I am facing when moving my application from one area to another. I was previously testing my HTML in an isolated test application and have got it to a stage where I am happy with it. So I began integrating it into the correct place. When doing this I found I am having a curious CSS problem as the div elements no longer appear to be inheriting the dimensions of the child divide.
I created a JSFiddle in order to demonstrate the problem, code also provided below.
Working backwards, the outermost div with the style attributes hard-coded for height and width (100px) appears to have a computed style that I would expect.
Happy so far. We can see 100px for height and width.
The div with the class child also appears to inherit the dimensions from the child content, as I would expect. We see 100px for height and width.
So far, correct behaviour.
However, this is where my knowledge of CSS falls down. The div with class parent appears to lose all width and height information from it's content and so the user sees nothing in the browser as the size for these div's essentially becomes 0px and the content is hidden.
The question I have, is why does the width and height not get inherited from the children of the div element with the parent style class.
HTML
<div class="grandparent">
<div class="parent">
<div class="child">
<div style="height: 100px; width: 100px; background-color: black;">hello</div>
</div>
</div>
</div>
CSS
.grandparent {
height: auto;
position: absolute;
margin: 0;
overflow: auto;
display: block;
background-color: red;
}
.parent {
position: relative;
height: 100%;
width: 100%;
padding: 0;
margin: 0;
overflow: hidden;
display: block;
background-color: green;
}
.child {
height: auto;
position: absolute;
margin: 0;
overflow: auto;
display: block;
background-color: blue;
}
pros and cons for it but position:absolute; in this case is the problem:
http://jsfiddle.net/CyubA/1/
.child {
height: auto;
position: relative;
margin: 0;
overflow: auto;
display: block;
background-color: blue;
}
How can I have a div with 100% height that has a particular aspect ratio, e.g. 2:3?
For example, if the outer element has a height of 900px, the width of the inner element should be 600px, but this should be responsive.
I don't want to use any JavaScript for this.
Using the CSS3 flexible box model would be fine.
If you are targeting modern browsers that support CSS3, you can try the following.
Consider the following HTML snippet:
<div class="wrapper">
<div class="inner">Inner content...</div>
</div>
and apply the following CSS rules:
html, body {
height: 100%;
}
body {
margin: 0;
}
.wrapper {
background-color: lightblue;
height: 100%;
}
.wrapper .inner {
margin: 0 auto;
background-color: beige;
height: 100%;
width: 66.6666666666vh;
}
The .wrapper element takes up 100% of the view port height because I have set
height: 100% on the body and html elements.
The inner wrapper .inner has a height: 100% and fills up the parent block.
To set the .inner width, use the viewport-percentage length vh that scales with the height of the parent block.
In this example, 66.66vh means 66.66% of the vertical height, which corresponds to a 2:3 aspect ratio (width:height).
See demo at jsFiddle
Reference: http://www.w3.org/TR/css3-values/#viewport-relative-lengths
Browser Compatibility
The vh unit and other vertical percentage lengths have pretty good support with the latest browsers, see the reference below for more details.
See reference:
https://developer.mozilla.org/en-US/docs/Web/CSS/length#Browser_compatibility
Alternative Approach Using a Spacer Image
Consider the following HTML:
<div class="ratio-wrapper">
<img class="spacer" src="http://placehold.it/20x30">
<div class="content">Some content...</div>
</div>
and apply the following CSS:
.ratio-wrapper {
position: relative;
display: inline-block;
border: 1px solid gray;
height: 500px; /* set the height or inherit from the parent container */
}
.ratio-wrapper .spacer {
height: 100%; /* set height: 100% for portrait style content */
visibility: hidden;
vertical-align: top;
}
.ratio-wrapper .content {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
padding: 20px;
}
The .ratio-wrapper container has two child elements, an img.spacer and div.content.
The image as a portrait aspect ratio, for example, 20x30 (wxh) and is set to expand to fill the height of the parent container using height: 100%. The image is hidden from view but retains its space in the parent block.
The .content element is positioned absolutely to fill the parent container and can contain any content. Because .content is constrained in height and width, the content could overflow in some cases, so setting overflow: auto may be appropriate.
See demo at: http://jsfiddle.net/audetwebdesign/BVkuW/
Related question and answer:
In Fluid Container, Can I Make Elements as Tall as they Are Wide?
You can do this by sticking a 2px by 3px image and an inner div as siblings into an outer div which has display: inline-block; applied. Now when you set the image to have a height of 100%, and you absolutely position the inner div to be as high and wide as its ancestor, you can set the height of the outer div, and the width of all elements involved will be exactly equal and based on the aspect ratio of the image.
Here's a jsFiddle demonstrating this approach.
HTML
<div>
<div>2 by 3</div>
<img src=".../twobythree.png" />
</div>
CSS
body > div {
height: 300px;
position: relative;
display: inline-block;
}
img {
display: block;
height: 100%;
}
div > div {
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
}