demo [Firefox & IE]
HTML:
<div class="content">
<div class="img">
<div class="divs">
<img src="path.jpg" alt="" />
</div>
<div class="divs">
<img src="path.jpg" alt="" />
</div>
<div class="divs">
<img src="path.jpg" alt="" />
</div>
<div class="divs">
<img src="path.jpg" alt="" />
</div>
</div>
</div>
CSS:
html, body {
height: 100%;
}
.content {
/* height: 100%; */
position: relative;
width: 100%;
}
.img {
height: 100%;
margin-left: 0.5%;
max-width: 100%;
width: 100%;
}
.content .divs {
border: 1px solid #b64024;
float: left;
height: 25.5%;
margin: 1% 0.5% 0;
overflow: hidden;
width: 20%;
}
.content .divs img {
height: 100%;
width: 100%;
}
If you uncomment the height: 100%; from .content then you'll see the equal height divs there with images.
So, my question is:
How the .divss' height is calculated without having parent div height .img -> .content in percentage calculation for inherited div ?
Here's your problem in a nutshell:
Demo: http://jsfiddle.net/a75ekc0e/9/
.content {
height:400px;
position:relative;
}
.content div {
float:left;
background:yellow; border:1px solid blue;
width:20%; height:25%;
margin:1em;
}
That simplified demo shows the content 'working'. The key parts are that:
Your divs have a percentage height.
The 'containing block' of the divs has an explicit height.
Super-important: Note that the position:relative causes .content to be the 'containing' for its children; without this declaration it will be the body that is used for positioning.
…and thus, the divs can calculate their height based on their positioning.
If you remove the height from the .content then this element has no explicit height, and its height is taken from its content. When you do that with the above demo and Run it, you see the divs collapse to have no height at all, because they cannot calculate the percentage of something that will be determined later on.
In your demo they get their height from their varying-height image content, which causes them to vary. The height:25.5% is completely ignored because the parent has no explicit height.
Per the specification of the CSS height property:
The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'.
For more details, read the section "Calculating Heights and Margins" in the CSS specification.
This might not be an answer.
Though, struggling a lot, I found the key reason behind this in short is due to different height of images in original size.
Let see the demo with equal height original image size:
demo
But still amazed of behavior of height of images just due to original image sizes and it also varies in different browsers.
Related
I hope you are having a good day! I have created a div in where I put 3 info boxes. The boxes are just not appearing when I set the width and height with percentages. But if I set them with pixels, they appear. I want them to be in percentages, because I want the website to be fully responsive on every device it's accessed. Any tips?
HTML&CSS:
#info {
height: 50%;
background-color: red;
}
.infoBox {
width: 20%;
height: 35%;
background-color: blue;
float: left;
margin: 0 1%;
}
<div id="info">
<div class="infoBox" id="infoBox1">
</div>
<div class="infoBox" id="infoBox1">
</div>
<div class="infoBox" id="infoBox1">
</div>
</div>
Your boxe has an height calculated to 'auto' because #info's parent has no height explicitly specified.
https://www.w3.org/TR/CSS2/visudet.html#the-height-property
<percentage>
Specifies a percentage height. The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'. A percentage height on the root element is relative to the initial containing block. Note: For absolutely positioned elements whose containing block is based on a block-level element, the percentage is calculated with respect to the height of the padding box of that element. This is a change from CSS1, where the percentage was always calculated with respect to the content box of the parent element.
Usually, you manage to inherit viewport's height from html through body all the way to your container
html,
body {
height: 100%;
}
#info {
height: 50%;
background-color: red;
}
.infoBox {
width: 20%;
height: 35%;
background-color: blue;
float: left;
margin: 0 1%;
}
<div id="info">
<div class="infoBox" id="infoBox1">
</div>
<div class="infoBox" id="infoBox2">
</div>
<div class="infoBox" id="infoBox3">
</div>
</div>
NOTE
https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#the-id-attribute
The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.
check now it is blank div so you have to give height in pixel or add content
.infoBox {
width: 20%;
min-height:200px;
background-color: blue;
float: left;
margin: 0 1%;
background-color:red;
}
#info {
height: 50%;
background-color: red;
}
.infoBox {
width: 20%;
min-height:200px;
background-color: blue;
float: left;
margin: 0 1%;
background-color:red;
}
<div id="info">
<div class="infoBox" id="infoBox1">
</div>
<div class="infoBox" id="infoBox2">
</div>
<div class="infoBox" id="infoBox3">
</div>
</div>
Hi,
See the screenshot, I'd like to know how I can fit my simple countdcown to always take 100% of the screen? I've made it to fit my phone, but Id like it to be 100% on the desktop aswell.
What I've tried:
html{
width:100%;
height:100%;
}
body{
width:100%;
height:100%;
}
But, this will only make the body 100%..
Where do I start? Does anybody have a tutorial or anything?
A simple example of using vw or vh (viewport), try it and you will see the difference.
Also with to center your element. you could use:
display: flex;
align-items: center;
justify-content: center;
Vertical Centering
REF: https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/
Viewport
REF: https://developer.mozilla.org/en/docs/Mozilla/Mobile/Viewport_meta_tag
body {
margin: 0;
}
.test1 {
background: red;
width: 100%;
height: 300px;
display: flex;
align-items: center;
justify-content: center;
}
.test2 {
background: green;
width: 100vw;
height: 300px;
display: flex;
align-items: center;
justify-content: center;
}
.innerwraper {
height: 100px;
width: 200px;
background: aqua;
}
<div class="test1">
<div class="innerwraper">This is 100% width</div>
</div>
<div class="test2">
<div class="innerwraper">This is 100vw</div>
</div>
Automatically the height and the width of the body is 100% and it can not be changed to an other value so your code is unuseful.
To make the content take bigger height and width you should modify the css height and weight properties of the content (buttons, text inputs, divs, etc).
You will have to define all sizes, lengths and widths in 'vw' and 'vh'. It stands for viewport width and viewport height. This will tell the browser to render every objects size relative to the width of the screen (or height, depending on what you choose).
In your example every object should be about 20vh heigh, with a margin of 5vh. Four objects make then a perfect 100vh (100% viewport height).
You could start with this css:
input, div {height: 20vh; margin: 5vh 1vh;}
You can wrap the entire combination of buttons and views in a div, for example:
<div id = "wrapper"> </div>
Then inside of the div modify each element's height and width based on percentages. For example, you have 4 elements vertically, and three buttons on the bottom. So your three buttons on the bottom could be further wrapped in another div making them act as one element. Then you can split the 4 elements to height: 25%; and make width inherited.
So it would look something like this:
<body>
<div id="wrapper">
<div class="element"> insert element here such as input </div>
<div class="element"> element here such as input button </div>
<div class="element"> element here such as counter </div>
<div class="element">
<div> </div>
<div> </div>
<div> </div>
</div>
</div>
CSS:
.element{
height:25%;
width:inherit;
}
#wrapper{
height:100%;
width: 100%;
}
Above all the properties inside the block of codes if there is no * {
box-sizing: border-box;
}
The Content will not fit in...
I'm wanting to make my images smaller as the window size gets smaller.
However, I have to define the size of these two images by width, yet because 'max-width' overrides 'width' then it makes the images really small? I need to use 'max-width' to resize my images. However, I have two images on the left hand side that I have used both width and max-width and its width is defined and it resizes? What am I doing wrong with the other two?
img {
width: 100%;
height: auto;
width: auto\9;
}
/* css for the two larger images on the left-hand side*/
#imageleft {
display: inline-block;
height: 30px;
}
/* css for the two smaller images on the right-hand side*/
#imageright {
display: inline-block;
width: 50px;
height: 100px;
}
<!-- large images to left -->
<div id="imageleft">
<a href="index.html">
<img src="images/photo-1464375117522-1311d6a5b81f.jpeg" alt="Guitar image" style="max-width:100%; width:600px;height:400px">
</a>
<a href="index.html">
<img src="images/photo-1470020618177-f49a96241ae7.jpeg" alt="Fire breather" style="max-width:100%; width: 300px;height: 400px">
</a>
</div>
<!-- small images to the right -->
<div id="imageright">
<a href="index.html">
<img src="images/photo-1472653431158-6364773b2a56.jpeg" alt="festival" style=" max-width: 100%; height: 200px">
</a>
<a href="index.html">
<img src="images/photo-1473396413399-6717ef7c4093.jpeg" alt="stage view" style="width:291px; max-width: 100%;height: 196px">
</a>
</div>
#helpme123 I really didn't get what kind of layout you desire to achieve. Could you change your post and provide an example of it, please?
When you use width and max-width together, it's usually because you are giving the element a width relative to its parent (or the viewport or the current font-size or the base font-size), but you also want to explicitly state an absolute width, beyond which the element should not widen.
Working Example:
div {
width: 90%;
padding: 12px;
}
.parent {
max-width: 1000px;
background-color: rgb(255,0,0);
}
.child {
max-width: 600px;
background-color: rgb(255,255,0);
}
.grandchild {
max-width: 400px;
height: 100px;
background-color: rgb(0,0,255);
}
<div class="parent">
<div class="child">
<div class="grandchild">
</div>
</div>
</div>
If you run the example in a full window and shrink the window size and then gradually grow it, you'll see that the divs each widen relative to their parent... until they reach their max-width, after which they stop widening.
I want my content area to stretch to the height of the parent, and I have a fixed height for the title area. I cannot hard-code the height of the content area because in the case I'm working on, the height of the parent area may change.
HTML:
<div class="parent">
<div class="title">Title</div>
<div class="content">
<p>My Content</p>
</div>
</div>
CSS:
.parent{
width : 500px;
height: 300px;
background-color : gray;
position: absolute;
}
.title{
height:50px;
background-color: #94A6E0;
margin:5px;
}
.content{
background-color: #8CBF99;
margin:5px;
}
JSFiddle: http://jsfiddle.net/PGJJv/
There is a way to do it without using fixed heights:
You can set the parent to display: table; and the children to display: table-row. Then the lowest div will take the rest of the height. The only thing is that you need an extra element in between to fake the space between the two elements as border-top or border-bottom don't work on <tr>s. Also you must add padding to the parent in place of margin on the children.
(This is not a real <tr>, it is a sematic div but it is just emulating the behavior of a <tr>.)
HTML:
<div class="parent">
<div class="title">Title</div>
<span class="greyLine"></span>
<div class="content">
<p>My Content</p>
</div>
</div>
CSS:
.parent{
width : 500px;
height: 300px;
background-color : gray;
position: absolute;
display: table;
padding: 5px;
}
.title{
height:50px;
background-color: #94A6E0;
display: table-row;
}
span.greyLine
{
display: table-row;
background-color: gray;
height: 5px;
}
.content{
background-color: #8CBF99;
display: table-row;
}
http://jsfiddle.net/Kyle_/PGJJv/6/
EDIT:
As Dipaks rightly points out, IE7 doesn't support the display: table-row; property.
Maybe you can use the property of a table. Set your parent as a table
You can have a fixed height for your title, that you display as a table-row.
And your content is the second and last table-row; so it always fit the height of the table.
Here is a fidde example : http://jsfiddle.net/PGJJv/5/
You just have to play with margin and border to recreate exactly your template.
I have a webpage containing a centered container with content and I want to display a logo next to it.
The layout is as following: div - container. Where the container is centered and the div lef of the container needs to fill out the width left on the screen.
html, body {
height: 100%;
width: 100%;
}
#container {
width: 800px;
margin-left: auto;
margin-right: auto;
min-height: 100%;
}
<div id="container">
</div>
<div id="lef">
</div>
A jsfiddle with this code is available on http://jsfiddle.net/7QJQn/
This is the option that comes closed
http://jsfiddle.net/7QJQn/4/
I think that the best solution for doing something like this is just using javascript / jQuery.
Depending on which browsers you wish to support, you could use calc().
Basically, you want 50% of the viewport width (50vw) minus half of width of #container (so you're measuring from the center of your #container and you use half of all the values) - I'm assuming that you're OK with absolute positioning #lef to the viewport to keep it to the right?
CSS (fiddle here):
#lef {
background-color:yellow;
width:calc(50vw - 100px);
height:20px;
position:absolute;
right:0;
top:0;
}
Add this to your css:
#lef{
float:left
}
And change the order of the divs in the html, like this:
<div id="lef"></div>
<div id="container"></div>
First of all, you should wrap your markup in a wrapper div so elements stay tight.
I made some changes, take a look:
<div id="wrapper">
<div id="lef">
</div>
<div id="container">
</div>
</div>
And the css:
html, body {
height: 100%;
width: 100%;
}
#wrapper{
width: 360px;
}
#container {
width: 200px;
margin-left: auto;
margin-right: auto;
height: 100px;
background-color:red;
}
#lef {
background-color:yellow;
width: 160px;;
height:100px;
float: left;
}
Example
If using flexbox is an option, you can do this with the flex-grow property:
With the following markup
<div class="main-row">
<div class="filler"></div>
<div class="row-content">Fixed width centered div</div>
<div class="filler"></div>
</div>
you need to set flex-grow: 1 on the filler divs. See this fiddle.