The example below has an gray div (#outer) with a child orange div (#inner). #inner will fill the page proportionally on width only. Is it possible to have #inner scale proportionally based on width and height using only CSS? Please, no Javascript solutions as I am aware of how to accomplish it that route, but would prefer a CSS solution if possible.
http://jsfiddle.net/Gchr4/
CSS
body, html {
margin: 0;
padding: 0;
height: 100%;
}
#outer {
background-color: #EFEFEF;
width: 100%;
height: 100%;
}
#inner {
background-color: #ff9933;
width: 100%;
padding-top: 50%;
}
HTML
<div id="outer">
<div id="inner"></div>
</div>
Javascript example of what I am attempting to achieve: http://jsfiddle.net/Q4Qdy/
Use height:100% for the #inner.
However, due to your padding, the height will add 50% to it. This will be fixed by using box-sizing:border-box, but this still has some browser issues. So if you don't have to add padding, i would suggest to remove it.
working example
#inner {
background-color: #ff9933;
width: 100%; height: 100%;
padding-top: 50%;
box-sizing: border-box;
}
It is possible using media queries (and min-aspect-ratio, in particular) and viewport units: http://jsfiddle.net/79Fhb/.
HTML:
<div id="outer">
<div id="inner"></div>
</div>
CSS:
body, html {
margin: 0;
padding: 0;
height: 100%;
}
#outer {
background-color: #bbb;
height: 100%;
}
#inner {
background-color: #ff9933;
}
#inner:before {
content: "";
display: block;
padding-top: 50%;
}
#media screen and (min-aspect-ratio: 2/1) {
#inner {
height: 100%;
width: 200vmin;
}
}
Related
i need a responsive background image that will scale and keep its aspect ratio when resized within its container, anybody have any suggestions. This is what i been working on for 3 days, everything works great but the container takes up alot of space, and if i mess with the height it will mess up the aspect ratio.
HTML:
<div class="wrapper">
<h2 class="heading">Responsive Background CSS</h2>
<div class="container">
Background Image Applied Here
</div>
<section class="about"></section>
</div>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
}
.cf:before, .cf:after, {
display: table;
content:"";
}
.cf:after {
clear: both;
}
.heading {
color: #000;
font-size: 40px;
text-align: center;
}
.container {
position: relative;
min-height:1px;
padding: 0;
max-width: 80%;
background-color:orange;
margin: 30px auto;
}
.image {
background:url(img/image%201.jpg);
display: block;
max-width: 100%;
padding-bottom: 177.7777777777778%;
height: 0;
background-size: 100% !important;
background-repeat: no-repeat;
}
.about {
width:100%;
min-height: 500px;
background-color:tomato;
position: relative;
}
You can try this custom plugin made for responsive images(background)
http://webonise.github.io/jQuery-plugins/fullScreenImage/responsive-full-screen.html
You could always just change the image depending on the screen size. You'd want to use the same image, just different sizes. This way you can crop the images in ways that better display your intentions for the background image.
Here's a demo. Change the browser size to see the changes.
There are two floated divs of different height inside a wrapper div. I need both of them to be 100% of height of the body i.e. of the same height. Also used clearfix. But height:100% doesnt seem to work. How to do this?
Demo
Html:
<div class="wrapper">
<div class="primary">
<img src="http://demo1.opentaps.org/images/products/small/gis_computer_glen_rolla.png" />
</div>
<div class="secondary">
<img src="http://demo1.opentaps.org/images/products/small/gis_computer_glen_rolla.png" />
</div>
<div class="clearfix">
</div>
</div>
CSS:
body {
min-height: 100vh;
background-color: green;
margin: 0;
}
.wrapper{
background-color: blue;
height: 100%;
}
.primary{
float: left;
width: 80%;
height: 100%;
background-color: yellow;
}
.primary img{
height: 100px;
width: 100px;
}
.secondary{
float: right;
width: 20%;
background-color: red;
height: 100%;
}
.secondary img{
height: 500px;
width: 100px;
}
.clearfix{
clear: both;
}
All you need to do is add a height of 100% to the html and body tags like so:
html, body {
height: 100%;
}
Demo:
http://jsbin.com/EsOfABAL/1/
if you want to use vh units (seen in your code), it does makes it easier, no need to worry about 'heritage' and see your columns being stopped at 100% height of the window.
if you mix the method of faux-column and clear fix , you need to set only once min-height:100vh; on the floatting element.
Your yellow background has to be drawn in the wrapper and the red one in the non-floatting element wich is stretch with the clearfix method.
body {
margin: 0;
}
.wrapper{
background-color: yellow;
overflow:hidden;
}
.primary{
float: left;
width: 80%;
min-height:100vh;
}
.wrapper .primary img{
height: 100px;
/* width:1000px; */
width: 100px;
}
.secondary .overflow{
margin-left:80%;
background-color: red;
}
.overflow:after {
content:'';
height:0;
display:block;
clear:both;
}
.secondary img{
height: 500px;
/*height:100px;*/
width: 100px;
}
uncomment height value for image to check behavior and drawing of your page, scrolling or not .
http://codepen.io/anon/pen/chHtK
Hope this helps you to understand the use of vh (or vw) units , for the faux-column and clearfix methods, it's just a reminder of old methods :)
Enjoy
The html element also needs to be 100% - try this:
html { height: 100%; }
body {
height: 100%;
background-color: green;
margin: 0;
}
I need create element, that cover whole page except 20px margin on all sides. I try this and it works in webkit browsers and Firefox, but Internet Explorer (10) and Opera have problem with this :-( . Any idea how to solve this?
HTML
<div id="first">
<div id="second">
Hello world!
</div>
</div>
CSS
head, body
{
width: 100%;
height: 100%;
}
body
{
position: absolute;
margin: 0;
background-color: blue;
display: table;
}
#first
{
display: table-cell;
height: 100%;
width: 100%;
padding: 20px;
}
#second
{
height: 100%;
width: 100%;
background-color: white;
}
I'd suggest:
#first {
display: table-cell;
position: absolute;
top: 20px;
right: 20px;
bottom: 20px;
left: 20px;
}
Which will position the element 20px away from each of the sides. However I'd suggest not using display: table-cell; since that requires a parent element to have display: table-row which itself then requires a parent element with display: table.
Also, it looks like you're trying to emulate table-based layouts, if you could list the overall problem you're trying to solve you may get better/more useful answers.
Try a solution like this:
http://jsfiddle.net/cyHmD/
Never use position:absolute and display:table on body - leave those properties as they are since body is your base from where you build the rest of the site - at most use position:relative on body tag. box-sizing changes how the browser box model is calculated - for example instead of calculating 100% width + 20% padding + 20% border = 140% it calculates as 100% width + 20% padding + 20% border = 100%.
This solution will work from IE7 on including IE7.
head, body {
width: 100%;
height: 100%;
margin:0;
padding:0;
}
body {
background-color: blue;
}
#first
{
position:absolute;
width:100%;
height:100%;
padding:20px;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
}
#second
{
height: 100%;
width: 100%;
background-color: white;
}
How about this? Simply replace required margin with border:
#first
{
display: table-cell;
height: 100%;
width: 100%;
border: 20px solid blue;
background-color: white;
}
I have a two-column fluid layout, with the left-hand side set to width: 40% and the right-hide side set to width: 60%. I'd like to allow users to resize their browser as large or small as they'd like, but I must have the left-hand side display a minimum width of 300px.
The following is the code I am currently using for the fluid layout, which includes the min-width specification. But, CSS is ignoring it! It allows the left-hand column to shrink below 300px.
I've also attempted to set min-width to a percentage, such as 20%, but CSS ignores this specification as well.
div#left {
background: #ccc;
position: fixed;
top: 0;
bottom: 0;
left: 0;
width: 40%;
min-width:300px;
height: 100%;
}
div#right {
background: #aaa;
position: fixed;
top: 0;
width:60%;
right: 0;
bottom: 0;
}
jsFiddle Fullscreen Example: http://jsfiddle.net/umgvR/3/embedded/result/
jsFiddle Code: http://jsfiddle.net/umgvR/3/
What is causing this? How can the code be corrected?
If you're not too attached to the fixed positioning, this should do what you want.
View on JSFiddle
HTML
<div id="left">Left</div><div id="right">Right</div>
Note the lack of whitespace between the elements
CSS
html, body {
height: 100%;
}
body {
min-width: 800px;
}
div#left {
background: #ccc;
display: inline-block;
width: 40%;
min-width:300px;
height: 100%;
}
div#right {
background: #aaa;
display: inline-block;
width:60%;
height: 100%;
}
This should also work...
html
<div id="container">
<div id="left">Left</div>
<div id="right">Right</div>
</div>
css
body, div {width:100%;
height:100%;
margin: 0;
}
#container {
display:block;position: fixed;
}
#left, #right {
display: inline-block;
}
div#left {
background: #ccc;
min-width: 300px;
max-width: 40%;
}
div#right {position:fixed;
background: #aaa;
width: 60%;
}
I found out that the left hand div is keeping the minimum width, but its going underneath the right div. If you bring it forward, you can see it on top of the right div. I don't think that this is ideal though.
z-index: 1;
I used z-index: 1; on the left right and z-index: 0; on the right div.
It works but I think there are better solutions out there.
Why does height: 100% have no effect on #baz in the following code? How could you fix this when min-height on (some of) the ancestor element(s) is required?
HTML:
<div id="foo">
<div id="bar">
<div id="baz">
foo bar baz
</div>
</div>
</div>
CSS:
div { border: 3px solid red; padding: 5px; }
#foo { height: 300px; }
#bar { min-height: 100%; }
#baz { height: 100%; }
See example at http://jsfiddle.net/pmmyP/
Tested with Chrome 12 and Firefox 4.
Using the following kind of works:
#bar { min-height: 100%; position: relative; }
#baz { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
But is there another (or better) way?
Example at http://jsfiddle.net/pmmyP/1/
Don't use min-height when you want height
min-height means it can't go smaller. height: 100% means 100% of the parent element's height (which isn't specified and so it defaults to auto I think).
#bar, #baz { height: 100%; box-sizing: border-box; }
The box-sizing is so that they stay inside each other.
http://jsfiddle.net/Zweu7/1/
Explanation of min-height: http://www.dynamicsitesolutions.com/css/height-and-min-height/