HTML: Images floating left? - html

I wanna make a page with lots of images being shown.
<style>
img{margin-bottom: 3px; float: left}
</style>
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
<img src="myimg.png">
It looks good if they all have the same width and height.
But what happens when one of them has a larger size? It messes all the layout,
showing a white space that makes it look ugly.
How can this be fixed?

why don't you put width: 150px or whatever the size you want inside that css?
<style>
img{margin-bottom: 3px; width: 150px; float: left}
</style>

With a bit javascript you could bring them all to the same size. But it won't look much better.
Maybe the same width and a simple repeating background which won't be annoying would be a compromise.

Limit sizes
img {
max-width: 100px;
max-height: 100px;
border: 1px solid grey; /* show visible element size */
}
Sample:
http://jsfiddle.net/UqGW7/3/
Fixes sizes (strech)
img {
width: 100px;
height: 100px;
border: 1px solid grey; /* show visible element size */
}
Sample:
http://jsfiddle.net/UqGW7/4/
Crop images (using a wrapper)
<span class="wrapper">
<img src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/592211_21253884267_736366040_q.jpg" />
</span>
.wrapper {
width: 100px;
height: 100px;
display: inline-block;
overflow: hidden; /* crop larger images */
border: 1px solid grey; /* show visible element size */
text-align:center; /* center horizontally */
}
Sample:
http://jsfiddle.net/UqGW7/5/

If it is not very necessary to show the entire image you can consider placing your images in div tags, either as background images, centering them and then setting a fixed width and height for the div elements.
<style>
.imgpanel{
margin-bottom: 3px;
float: left;
background-repeat:no-repeat;
background-position:center;
width:200px;
height:200px;
}
</style>
<div class="imgpanel" style="background-image:url('/images/sample.png');"></div>
<div class="imgpanel" style="background-image:url('/images/sample.png');"></div>
<div class="imgpanel" style="background-image:url('/images/sample.png');"></div>
<div class="imgpanel" style="background-image:url('/images/sample.png');"></div>
Or you can place the image within the div tags and set the overflow to hidden:
<style>
.imgpanel{
margin-bottom: 3px;
float: left;
width:200px;
height:200px;
overflow:hidden;
}
</style>
<div class="imgpanel"><img src="/images/sample.png" /></div>
<div class="imgpanel"><img src="/images/sample.png" /></div>
<div class="imgpanel"><img src="/images/sample.png" /></div>
Optionally, you can use a thumbnail generation script to create images that are always the specific size you want, if re-sizing the image is okay. Check http://sourceforge.net for code samples of a thumbnail generator.

Related

How to fit image so that there will be no white spaces beside my image?

I am limited to use only html and css for this project. I try extending the width but that leaves me having horizontal scroll and I don't want to do that. I also try using px as a measurement but that doesn't work either.
<img src="img/gallery.jpg" style="width:100%;height:90%;white-space:nowrap">
https://i.stack.imgur.com/wsxrk.png
use width:100% as shown below:
img{
width:100%
}
You need to use object-fit property on your image. Here is a demo:
.container {
width: 500px;
height: 500px;
border: 1px solid #000;
}
.container img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="container">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
There are a few known way of removing white spacing. Here are a the best two, IMO:
Set the font size of the parent to be 0px:
div {
background-color: orange;
margin-bottom: 20px;
}
.nows {
font-size: 0px;
}
<div>
<img src="https://via.placeholder.com/150x150">
<img src="https://via.placeholder.com/150x150">
</div>
<div class="nows">
<img src="https://via.placeholder.com/150x150">
<img src="https://via.placeholder.com/150x150">
</div>
Use comments to mitigate the white space in the code
div {
background: orange;
margin-bottom: 20px;
}
<div>
<img src="https://via.placeholder.com/150x150">
<img src="https://via.placeholder.com/150x150">
</div>
<div><!--
--><img src="https://via.placeholder.com/150x150"><!--
--><img src="https://via.placeholder.com/150x150"><!--
--></div>
body {
margin:0px;
padding:0px;
}
I posted the same problem on other forum and this works. Thanks for the help.

Block that simultes images {width: 100%; height : auto} behaviour

I want to create the following layout :
Is a stripe of a variable number of images that have various widths and heights, that are:
proportional
scaled at the same height;
and the sum of their widths are equal to the parent width.
***It's kind of complicated to express myself;
I was wondering if it's possible for a block to simulate the img neat proportion behavior when you set a width to a percentage and it calculates the height of it automagically.
I've made up a diagram that maybe explain better what I want to achieve :
I want for the image to have collectively 100% width of the parent element, scaled with at the same height without loosing their proportion.
I've tried various implementations trying to figure out a way in which I can translate compute a percentage height in css that fills all the width for a block, just how the image behaves when there are {width: 100%; height : auto} properties.
So here is what I've got so far :
Strike #1, tried a simple solution
Problem: container height must be predefined.
.container {
width : 100%;
border: 1px solid black;
height: 50px; /* I would like to say here auto */
}
.image-wrapper {
white-space: nowrap;
height: 100%;
max-width: 100%;
border: 1px dashed gray;
}
.image {
height: 100%;
width: auto;
}
<div class="container">
<div class="image-wrapper">
<img class="image" src="http://placehold.it/100x200" />
<img class="image" src="http://placehold.it/300x200" />
<img class="image" src="http://placehold.it/800x400" />
<img class="image" src="http://placehold.it/10x80" />
<img class="image" src="http://placehold.it/800x400" />
</div>
</div>
Strike #2, display: table anyone ?
Problem: Don't even need to mention it, images are cropped the container size doesn't follow its parent size .
.container-wrapper {
width: 40px;
height: 50px;
}
.container {
width : 100%;
border: 1px solid black;
display: table;
height: 100%;
}
.image-wrapper {
display: table-row;
height: 100%;
border: 1px dashed gray;
}
.item {
display: table-cell;
border: 1px solid blue;
width: 100%;
height: auto;
}
.image {
height: 100%;
width: auto;
}
<div class="container-wrapper">
<div class="container">
<div class="image-wrapper">
<div class="item">
<img class="image" src="http://placehold.it/100x200" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/300x200" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/800x400" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/10x80" />
</div>
<div class="item">
<img class="image" src="http://placehold.it/800x400" />
</div>
</div>
</div>
</div>
***I must say that I am looking for a HTML/CSS solution without the involvement of JavaScript code.
Do you have a clue on how can I approach this ?
So a trick I just came up with is to use the automagic scaling of an image to scale the containing filmstrip div, but hide it with opacity (in a real example, I'd use a transparent .png as well). This sets the height of the filmstrip relative to its width. If you want your filmstrip to be 5:4 or 16:9 or whatever, just change the proportions of the .magic image.
The container inside is then set to be absolutely positioned so it inherits the size of the .magic image.
The images themselves are set to take up the full height of the filmstrip, and are given different widths. The actual image is set with background-image which uses background-size: cover and background-position: center to fill the div.
.filmstrip {
width: 100%;
position: relative;
/* just to make it easier to see what's going on */
border: 1px solid red;
}
.magic {
width: 100%;
height: auto;
display: block;
/* we don't actually want to see this, we're just using it for it's ratio */
opacity: 0;
}
.contents {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
}
.contents .image {
height: 100%;
background-size: cover;
background-position: center;
float: left;
margin-right: 2%;
/* just to make it easier to see what's going on */
border: 1px solid blue;
box-sizing: border-box;
}
.contents .wide {
width: 30%;
}
.contents .narrow {
width: 10%
}
<div class="filmstrip">
<img class="magic" src="http://placehold.it/400x100" />
<div class="contents">
<div class="wide image" style="background-image: url('http://placehold.it/300x100');"></div>
<div class="narrow image" style="background-image: url('http://placehold.it/300x100');"></div>
<div class="wide image" style="background-image: url('http://placehold.it/300x100');"></div>
</div>
</div>
Browser support should be: Chrome 3+, Firefox 3.6+, IE 9+, Opera 10+, Safari 4.1+ which is basically because of the use of background-cover.
Have a look at my stackoverflow 33117027 answer in which I made suggestions about creating a filmstrip. It has a reference to an eleborate Codepen example. You can easily strip/add what you need...

how to make a div responsive with respect to the image

Hi I am trying to develop a web page. In that I am supposed to place an image at the center of the page. Below that image I placed an div so that by clicking on that div certain items will be displayed.My problem is that when the size of the window reduced the image will become responsive,but not that div. I want to make both of them responsive.Here is my code
index.html
<div class="container">
<div class="row">
<div class="col-lg-12" style="top:-125px;">
<img id="i1" class="img-responsive" src="img/2.png" height="600" width="600" alt="">
<div style="padding-left:45%;top:-250px;">
<div id="triangle-up"></div>
</div>
</div>
</div>
</div>
this is my css
#triangle-up {
position: absolute;
bottom:0;
left: 46%;
padding: 5px;
width: auto;
height: auto;
border-left: 60px solid transparent;
border-right: 60px solid transparent;
border-bottom: 67px solid grey;
}
.container
{
position: relative;
}
Please help me to solve this issue..
Use CSS to set up width of your div to 100% and max-width as pixels amount that you don't want your div to grow any larger than.

Preventing a height change when adding a border with box-sizing

I'm unable to find something that describes this issue, but if I'm missing it, just let me know.
Below is a demo (Tested in IE11 and Chrome) which shows the problem perfectly
Essentially, I'm trying to add a border to an element, and keep the size the same. It's working perfectly for the width, but the height, it only accommodates half of it, so I have an extra 3px in height.
Is there a way to prevent this / get around it without using a script? I can make changes specifically to the element(s) which have the border, but I don't know the actual height (200px is used here just for demo purposes, so simply reducing the height isn't an option.
Thanks.
EDIT (Since apparently it's unclear what I'm asking)
Is there a way to prevent the height changing without using a script?
DEMO:
div {
width: 200px;
float: left;
margin-right: 3px;
}
div img {
max-width: 100%;
max-height: 100%;
}
.div {
border: 3px dotted blue;
box-sizing: border-box;
}
<div id="div1">
<img src="http://placehold.it/200x200" alt="" />
</div>
<div class="div">
<img src="http://placehold.it/200x200" alt="" />
</div>
<div class="div" style="clear:left">
<img src="http://placehold.it/200x200" alt="" />
</div>
You can set add line-height:0px if there is no text for .div as image is inline-block element it add whitespace
div {
width: 200px;
float: left;
margin-right: 3px;
}
div img {
max-width: 100%;
max-height: 100%;
}
.div {
border: 3px dotted blue;
box-sizing: border-box;
line-height:0px;
}
<div id="div1">
<img src="http://placehold.it/200x200" alt="" />
</div>
<div class="div">
<img src="http://placehold.it/200x200" alt="" />
</div>
<div class="div" style="clear:left">
<img src="http://placehold.it/200x200" alt="" />
</div>
Either add the line-height attribute to the div as #Vitorino suggested or add vertical-align: middle to the img. Both will fix the whitespace issue for an inline-block
div img {
max-width: 100%;
max-height: 100%;
vertical-align: middle;
}
.div {
border: 3px dotted blue;
box-sizing: border-box;
line-height:0px;
}
Here is a variant of solution:
div {
width:200px;
float:left;
margin-right:3px;
box-sizing:border-box;
}
.div img {
max-width:100%;
max-height:100%;
box-sizing: border-box;
border:3px dotted blue;
}

Grid with different height images

I'm trying to create a grid layout for my portfolio site. I'm having trouble with getting it to work online. When I try a preview in dreamweaver it look just fine, but when uploaded it's all messed up.. Please help
http://www.kaspervanvliet.nl/index.html
HTML:
<div id="images-containter">
<div class="col">
<img src="images/k_Web-03.jpg">
<img src="images/curriculum_new.jpg">
<img src="images/Finnley's_2.jpg">
<img src="images/Justme_1.jpg">
</div>
CSS:
.images-containter {
position: relative;
width: auto;
height: auto;
}
.images-containter img{
width: 350px;
height: auto;
background: #fffff;
padding: 0px;
margin: 15px;
border: none;
}
.col {
width: 350px;
display: block;
float: left;
margin: 15px;
vertical-align: top;
align: center;
}
Set a width and height on your image tags. I assume you want it to all look the same cross browser. That would be the best bet.
Your code: my changes
<div class="col">
<img src="images/k_Web-03.jpg" **height="100" width="75"**>
<img src="images/curriculum_new.jpg" **height="100" width="75"**>
<img src="images/Finnley's_2.jpg" **height="100" width="75"**>
<img src="images/Justme_1.jpg" **height="100" width="75"**>
</div>
etc...
This takes some planning on your part, but you could simply allow them to click the image and see a full view. Otherwise, you're going to have different images with different aspect ratios and it will never look as good as you want it to.
Also, please don't structure your site with tables unless you're going to be using tabular data. It's a pain to edit in the long run.
EDIT**
The below html/css will give you the desired result. There are 4 divs with the class of "cols" those 4 divs are set up to be 4 columns. The images inside the div will stack, evenly spaced above and below. Also, the height will automatically adjust based on the width being constrained to 230px.
Let me know if you need anything else.
<!doctype html>
<html>
<head>
<style>
.wrap{
width:960px;
margin:0 auto;
}
.cols{
width:230px;
padding:5px;
float:left;
}
.cols img{
width:230px;
display:block;
margin:5px 0;
}
</style>
</head>
<body>
<div class="wrap">
<div class="cols">
<img src="images/k_Web-01.jpg">
<img src="images/k_Web-02.jpg">
<img src="images/k_Web-03.jpg">
</div>
<div class="cols">
<img src="images/k_Web-04.jpg">
<img src="images/k_Web-05.jpg">
<img src="images/k_Web-06.jpg">
</div>
<div class="cols">
<img src="images/k_Web-07.jpg">
<img src="images/k_Web-08.jpg">
<img src="images/k_Web-09.jpg">
</div>
<div class="cols">
<img src="images/k_Web-10.jpg">
<img src="images/k_Web-11.jpg">
<img src="images/k_Web-12.jpg">
</div>
</div>
</body>
</html>
You can also use css to set the heights and widths one time. Try this:
HTML:
<div class="img_container">
<div class="port"><img src="images/..."></img></div>
<div class="land"><img src="images/..."></img></div>
</div>
CSS:
.img_container {
width: 250px;
height: 250px;
background-color:grey;
}
.port {
width: 100px;
height: 200px;
}
.land {
width: 200px;
height: 100px;
}
That's better, because you cut the style from code.