Pictures aligned horizontally adjust in size with window size - html

I made a quick example of images aligned horizontally:
All of the images adjust to the height of the "wrap" div.
http://i.imgur.com/VOA1pBG.png
Yet, when I make the window smaller, images start to come out of the div and go below as such:
http://i.imgur.com/VKUA4Ju.png
I want to make it so as I make the window smaller, the images get smaller. To fit horizontally in the size of the browser.
Here is the code used to make the existing page:
html:
<h1> Thriller </h1>
<div id="week-wrap">
<div id="sunday" class="day"><img src="http://www.allipadwallpaper.com/wp-content/uploads/tropical-island-ipad-wallpaper-500x500.jpg"></div>
<div id="monday" class="day"><img src="http://mountains.insidrinfo.com/mountains-asia/Media/mountains-asia.jpg"></div>
<div id="tuesday" class="day"><img src="http://s4.favim.com/orig/50/beautiful-city-light-night-street-Favim.com-460323.jpg"></div>
</div>
CSS:
body{margin: 0 auto;}
#week-wrap {border: 1px solid #000; height: 300px;}
.day {float: left;}
img {height: 100%;}

Instead of float: left use display: inline-block for the divs. Then, add white-space: nowrap to the container.
http://jsfiddle.net/ExplosionPIlls/uYTxW/

try resizing image div with javascript on resize window:
$(window).bind("resize", function(){ //Adjusts image when browser resized
// do your image size logic here
// $('#week-wrap').width(); // width of div container
});

I would advocate removing the divs around each image. You can style the images directly. Also, instead of setting an explicit height on your wrapper, set overflow: hidden to establish a block formatting context so that it automatically expands to contain your floats.
You wind up with markup like:
<h1> Thriller </h1>
<div id="week-wrap">
<img src="http://www.allipadwallpaper.com/wp-content/uploads/tropical-island-ipad-wallpaper-500x500.jpg" id="sunday" />
<img src="http://mountains.insidrinfo.com/mountains-asia/Media/mountains-asia.jpg" id="monday" />
<img src="http://s4.favim.com/orig/50/beautiful-city-light-night-street-Favim.com-460323.jpg" id="tuesday" />
</div>
and CSS like:
body{margin: 0 auto;}
#week-wrap {border: 1px solid #000; overflow: hidden; }
#week-wrap > img { width: 33.333%; height: auto; float: left;}
with this result: http://jsfiddle.net/4Aytd/
var imageSizer = function () {
var images = document.querySelectorAll('#week-wrap > img'),
numImages = images.length,
imageWidth = (100/numImages) + '%',
i;
for(i = numImages-1; i >= 0; i--) {
images[i].style.width = imageWidth;
}
};
http://jsfiddle.net/4Aytd/4/

Related

css: div height flexible to reach the bottom of the screen with some margin

I am trying to build a two-column page using twitter bootstrap responsive classes:
<div class="container">
<div class="row">
<div id="left" class="col-xs-7">
some article here
</div>
<div id="right" class="col-xs-5">
google map here
</div>
</div>
</div>
The left part will contain an article that may be long enough to exceed the height of the screen, and it will have its own scrollbar if that happens.
In the right part, I want to put google map with height that reaches certain margin above the bottom of the screen.
I've tried viewport height but when it has a navbar above the section I want the height to be viewport minus the height of the navbar (ex: 40px plus certain margin, say 10px below):
#left {
height: 100vh - (40px + 10px);
overflow-y: auto;
}
#right {
height: 100vh - (40px + 10px);
}
Thus i want only #left will have a scrollbar. Is there a simple way to do this?
Demo: http://jsfiddle.net/kw4qLrgr/1/
You can do it through JS by sizing your div.container and a little update to your css.
<script>
$(document).ready(sizeContent);
$(window).resize(sizeContent);
function sizeContent() {
var height = window.innerHeight - 90; //90 = The size of your header minus margins
$('.container').height(height);
};
</script>
#left {
overflow-y: auto;
height: 100%;
}
#right {
background-color: #ddd;
height: 100%;
}
Regards.

How to set the container height automatically maintaining the ratio

I have an img object within a div object. I want to set the width of img (or div) and have its height set automatically to whatever is necessary to retain the original ratio of the image.
Suppose I set the width to 200px, which is shorter than the image's natural width. When I do this:
HTML
<div id="container">
<img id="foo" src="foo.png">
</div>
CSS
#foo{
width: 200px;
}
or
#container{
width: 200px;
}
the img has the height set to whatever is necessary to retain the original ratio, but the outer div has the height set to the inner image's natural height, and top and bottom margins are inserted between the inner img and the outer div.
I want the outer div's height to be whatever is necessary to retain the original ratio of img so that the top and bottom margin between div and img would become zero. How can I do that?
Try this code
DEMO
<div class="container">
<img src="demo.jpg" alt="" />
</div>
body{
margin: 0;
}
.container{
width: 200px;
border: 1px solid red; //tmp added to check the height of container
}
.container img{
display: block;
width: 100%;
}
Use this function:
<script type="text/javascript">
function resizeHeight(){
var resizedImage = document.getElementById('foo');
resizedImage.style.height = (resizedImage.style.height * 200)/resizedImage.style.width;
resizedImage.style.width = '200px';
}
</script>
And edit your image line to this:
<img id="foo" src="foo.png" onload="resizeHeight()">
It should set the picture's width to 200 automatically, and resize the height proportionally.

Horizontal align images in a container

I am trying to have 3 images aligned in one block. They have to stay in the same sized container and fit horizontally.
Here's the code:
<div class="container">
<img src="http://images2.webydo.com/31/313624/3958/21b785db-14ea-42f7-af0d-7e7a8d8019d9.jpg" />
<img src="http://images2.webydo.com/31/313624/3958/9657ddfd-81e8-4154-bc61-bbe30e4a8740.jpg" />
<img src="http://images2.webydo.com/31/313624/3958/909af36d-b941-4a20-9441-20505c035da3.jpg"/>
</div>
.container {
width: 300px;
height: 200px;
position:relative;
float: left;
text-align: center;
}
.container img {
width: 100%;
height: auto;
margin: 5px;
}
In my CSS solution, I divided the "container" class width by 3 (300px /3) and then subtracted 10px (which i got from padding-left and padding-right of each image). So a single image should have a width of 90px. However, I also wanted to subtract 5px more for browser spacing so the total width of each image should be 85px. Here is the code:
<html>
<head>
<style>
.container {
width: 300px;
height: 200px;
position:relative;
float: left;
text-align: center;
}
.container img {
width: 85px;
height: auto;
margin: 5px;
}
</style>
</head>
<body>
<div class="container">
<img src="http://images2.webydo.com/31/313624/3958/21b785db-14ea-42f7-af0d-7e7a8d8019d9.jpg" />
<img src="http://images2.webydo.com/31/313624/3958/9657ddfd-81e8-4154-bc61-bbe30e4a8740.jpg" />
<img src="http://images2.webydo.com/31/313624/3958/909af36d-b941-4a20-9441-20505c035da3.jpg"/>
</div>
</body>
</html>
Hm...I don't think you can have all three images in a horizontal line if you give them all a width:100%. That property would cause each image to take the full width of the container, meaning each image would be pushed to the next line.
You'll have to give the images a smaller width to fit them all on one line. 100% / 3 = 33.3% (rounded), so use that instead. Here's some modified CSS for .container img that seems to work:
.container img {
width: 33.3%;
height: auto;
padding:5px;
box-sizing:border-box;
-moz-box-sizing:border-box;
}
Note that in addition to changing the images' widths, I also changed the margin to padding, and made use of the box-sizing attribute (read more about it here). This lets you keep that same spacing of 5px around images, without bumping any images onto a second line.
Also, the HTML needs to be altered slightly. In this case, we're taking advantage of the <img> element's default display:inline-block to have them all display on the same line. However, any whitespace in between this kind of element will result in a space between the images, so that needs to be eliminated:
<div class="container">
<img src="http://images2.webydo.com/31/313624/3958/21b785db-14ea-42f7-af0d-7e7a8d8019d9.jpg" /><img src="http://images2.webydo.com/31/313624/3958/9657ddfd-81e8-4154-bc61-bbe30e4a8740.jpg" /><img src="http://images2.webydo.com/31/313624/3958/909af36d-b941-4a20-9441-20505c035da3.jpg"/>
</div>
If you don't understand what I mean by that, try formatting each <img> element onto its own line in the HTML, and see how that affects their positioning.
Here's a JSFiddle so you can see what this achieves. Let me know if you have any questions, and I'll be happy to help further!
EDIT: Alternatively, if you really want to keep the whitespace between your <img> elements in your HTML, you could compensate for the unwanted extra space with a negative margin. Just add margin-right:-4px; to your styles for .container img. Updated JSFiddle to show what this results in.

How to set multiline text in floated div not expand his width

I want use floatet image with some text about this image in my content.
I'm using this HTML + CSS for this:
<p class="container">
<img src="http://www.google.com.ua/images/logos/ps_logo2.png" width="200"/>
<span class="text">Some text wider that image image image blablabla</span>
And CSS for it:
.container { float:right; border:2px solid #000; }
.container img { display:block; margin-bottom:10px; }
But, if text about image is wider, it is expand floated parent. I'm not want this behaviour. I want to limit max-width of parent p element to width of image.
Here example on jsfiddle: http://jsfiddle.net/wBVqt/1/
I can do what I want through position:absolute and padding-bottom, but I don't know value for padding-bottom. jsfiddle.net/wBVqt/3/
I don't see solution with only css if you want to have images of different sizes, so chek my solution with jQuery:
var imageWidth = 0;
$('.container img').each(function(index, el){
if(el.width > imageWidth) {
imageWidth = el.width;
}
});
imageWidth = imageWidth ? imageWidth : '100%';
$('.container').css('width', imageWidth);
It will work yet if you have a lot of images in your container. If you have no images, it will set originally 100% width to container.
Have you tried just putting a width on the container? (Which you should do anyway if you want your code to validate, as all floated elemets should have a width).
.container { float:right; border:2px solid #000; width: 200px; }

how to create proportional image height/width

So, I want to have an image resized to 30% of its original height/width. Pretend you don't know its height or width, how would you go about it using only CSS/HTML?
If you need a quick inline solution:
<img style="max-width: 100px; height: auto; " src="image.jpg" />
Update:
Using a display: inline-block; wrapper, it's possible to make this happen with CSS only.
HTML
<div class="holder">
<img src="your-image.jpg" />
</div>
CSS
.holder {
width: auto;
display: inline-block;
}
.holder img {
width: 30%; /* Will shrink image to 30% of its original width */
height: auto;
}​
The wrapper collapses to the original width of the image, and then the width: 30% CSS rule on the images causes the image to shrink down to 30% of its parent's width (which was its original width).
Here's a demo in action.
Sadly no pure HTML/CSS way to do it as neither is geared to perform calculations like that. However, it's pretty simple with a snippet of jQuery:
$('img.toResizeClass').each(function(){
var $img = $(this),
imgWidth = $img.width(),
imgHeight = $img.height();
if(imgWidth > imgHeight){
$img.width(imgWidth * 0.3);
} else {
$img.height(imgHeight * 0.3);
}
});
<img style="max-width: 100%; height: auto; " src="image.jpg" />
i am using percent to max-width and very nice