I am trying to create a vertical stack of image thumbnails. One thumbnail per line.
I am trying to upgrade my CSS skills. I have this working with a table using
<table>
<tr>
<td><img /></td>
</tr>
<tr>
<td><img /></td>
</tr>
<tr>
<td><img /></td>
</tr>
</table>
I'd like to not use a table and accomplish this using divs and CSS. This was I have better control over the spacing and layout in the future.
Any thoughts?
Very simple:
<div><img /></div>
<div><img /></div>
<div><img /></div>
A div is a block level element. Unless you specified other wise it acts similarly to a <tr> tag
Use a simple div and put some id's for every img. In CSS for the same you can modify the properties of the different id.
I would recommend you start by learning about DIVs and their properties. W3 Schools is a good start.
To get started with yours simply do:
<div>
<img src="" />
</div>
<div>
<img src="" />
</div>
<div>
<img src="" />
</div>
DIVs by default are treated as block elements meaning they start on a new line automatically. So if you want them to appear inline then you will need to specify that by either using display: inline-block; or float:left;
Use divs/sections, define how many columns you want, lets say 4 columns...
http://jsfiddle.net/tBDjV/
<div class="col-1">
<img />
<img />
<img />
</div>
<div class="col-2">
<img />
<img />
<img />
</div>
<div class="col-3">
<img />
<img />
<img />
</div>
<div class="col-4 last">
<img />
<img />
<img />
</div>
.col-1, .col-2, .col-3, .col-4 {
float: left;
width: 22%;
margin-right: 4%;
box-sizing: border-box;
}
.last {
margin-right: 0;
}
Related
I dont know if this is the right place to ask this question but I am really stuck on this part. Basically, I am trying to align my github stats side by side in the readme file.
I added this code in my readme file:
![User GitHub Stats](https://github-readme-stats.vercel.app/api?username=user&show_icons=true&theme=radical)
![Top Languages Card](https://github-readme-stats.vercel.app/api/top-langs/?username=user&theme=radical&layout=compact)
How can I make it so that the two cards align side by side? Something like this:
Any suggestions?
UPDATE:
I tried adding this but it still did not work:
<img align="center" src="https://github-readme-stats.vercel.app/api?username=hussaino03&show_icons=true&theme=radical" />
<img align="center" src="https://github-readme-stats.vercel.app/api/top-langs/?username=hussaino03&theme=radical&layout=compact" />
<div style="display: flex; flex-direction: row;">
<img class="img" src="https://github-readme-stats.vercel.app/api?username=hussaino03&show_icons=true&theme=radical" />
<img class="img" src="https://github-readme-stats.vercel.app/api/top-langs/?username=hussaino03&theme=radical&layout=compact" />
</div>
Please see the CSS I added and changes to your HTML. Simply doing <img align="center" is not enough.
<div style="display: flex; flex-direction: row;">
<img class="img" src="https://github-readme-stats.vercel.app/api?username=hussaino03&show_icons=true&theme=radical" />
<img class="img" src="https://github-readme-stats.vercel.app/api/top-langs/?username=hussaino03&theme=radical&layout=compact" />
</div>
Inspected front-end and changed div styles to those suggested and it worked as expected. img
The difference in both box sizes annoyed also me. I tried the other proposal here but for some reason it didn't work for me(both in the same size on the same row). So, I did the following that has made them look much better, although they are not on the same line
### GitHub Stats
<div><img style="height: auto; width: 40%;" class="img" src="https://github-readme-stats.vercel.app/api?username=Tsvetoslav88&theme=radical&show_icons=true&include_all_commits=true&hide_border=true" /></div>
### GitHub Languages
<div><img style="height: auto; width: 40%;" class="img" src="https://github-readme-stats.vercel.app/api/top-langs/?username=Tsvetoslav88&theme=radical&langs_count=8&layout=compact&hide_border=true" /></div>
This is the solution that worked for me. Both images are about the same size and on the same line.
<div class='container'>
<img style="height: auto; width: 55%;" class="img" src="https://github-readme-stats.vercel.app/api?username=user&show_icons=true&theme=blue-green" />
<img style="height: auto; width: 40%;" class="img" src="https://github-readme-stats.vercel.app/api/top-langs/?username=user&theme=blue-green&langs_count=8&layout=compact" /></div>
</div>
img
Try this below:
Using the dir="auto" (which is used for text alignment) along with max-width 100% you should be able to center whatever you want.
<div align="center" dir="auto" <img style="max-width: 100%;" src="https://github-readme-stats.vercel.app/api?username=hussaino03&show_icons=true&theme=radical" />
<img style="max-width: 100%;" src="https://github-readme-stats.vercel.app/api/top-langs/?username=hussaino03&theme=radical&layout=compact" />
</div>
This is my code, but the image is above the table, and I want them side-by-side:
<div style="float:left;">
<img src="http://localhost/test1/wp-content/uploads/2017/04/GautengNew-1.gif"; alt="" width="500" height="538" />
</div>
<div style="float:left;">
<table style="margin-left: 10%; float: top;" border="1" width="440">
<tbody>
<!-- Table Rows Here -->
</tbody>
</table>
</div>
See current and desired output:
I think the issue is that you are using <div> rather than <span> tags. If you must use <div> you could either set the second one to float:right or add to their style - display: inline-block;.
Something like this:
<span style="float:left;">
<img src="http://localhost/test1/wp-content/uploads/2017/04/GautengNew-1.gif"; alt="" width="500" height="538" />
</span>
<span style="float:left;">
<table style="margin-left: 10%; float: left;" border="1" width="440">
<tbody>
<!-- Table Rows Here -->
</tbody>
</table>
</span>
or this:
<div style="float:left;display:inline-block;">
<img src="http://localhost/test1/wp-content/uploads/2017/04/GautengNew-1.gif"; alt="" width="500" height="538" />
</div>
<div style="float:left;display:inline-block;">
<table style="margin-left: 10%; float: left;" border="1" width="440">
<tbody>
<!-- Table Rows Here -->
</tbody>
</table>
</div>
i got 3 images next to each other, wrapped in a div which can be resized using min-width and max-width.
the left and right image have a fixed width/height.
the center image should fill the available space depending on how wide the outer div is.
what i got so far:
<div id="orangeheader" style="min-width:750px;max-width:1140px;overflow:hidden;white-space:nowrap;">
<img src="images/header_left.gif" width="220" height="150" border="0" alt="" style="float:left"/>
<img src="images/header_middle.gif" height="150" alt="" style="width:100%" />
<img src="images/header_right.gif" width="275" height="150" alt="" style="float:right" />
</div>
maybe this should be solved with a table?
i say this is impossible.
but blow code Partly does:
<div style="min-width: 750px; max-width: 1140px; overflow: auto; width: 100%;" id="orangeheader">
<img width="220" height="150" border="0" style="float: left;" alt="" src="images/header_left.gif">
<img height="150" style="float: left; max-width: 645px; min-width: 255px; width: 30%;" alt="" src="images/header_middle.gif">
<img width="275" height="150" style="float:right" alt="" src="images/header_right.gif">
</div>
you can give percent % width to right and left so.
also you can use pattern for middle image. http://pattern8.com/
hope helps.
fwiw, i went with a table solution:
<table style="border-padding:0;border-spacing:0;border-collapse:collapse;padding:0;spacing:0;border:none;width:100%;overflow:hidden;white-space:nowrap;">
<tr>
<td style="border-spacing:0;border-collapse:collapse;padding:0;spacing:0;border:0;width:220px;"><img src="images/header_left.gif" style="width:220px;height:150px;border:0" alt=""></td>
<td style="border-spacing:0;border-collapse:collapse;padding:0;spacing:0;border:0;"><img src="images/header_middle.gif" style="width:100%;height:150px" alt=""></td>
<td style="border-spacing:0;border-collapse:collapse;padding:0;spacing:0;border:0;width:275px;"><img src="images/header_right.gif" style="width:275px;height:150px" alt=""></td>
</tr>
</table>
try this
<table style="border-padding:0;border-spacing:0;border-collapse:collapse;padding:0;spacing:0;border:none;width:100%;overflow:hidden;white-space:nowrap;">
<tr>
<td style="border-spacing:0;border-collapse:collapse;padding:0;spacing:0;border:0;width:220px;">
<a href="index.php">
<img src="images/header_left.gif" style="width:220px;height:150px;border:0" alt="">
</a>
</td>
<td style="border-spacing:0;border-collapse:collapse;padding:0;spacing:0;border:0;">
<img src="images/header_middle.gif" style="width:100%;height:150px" alt="">
</td>
<td style="border-spacing:0;border-collapse:collapse;padding:0;spacing:0;border:0;width:275px;">
<img src="images/header_right.gif" style="width:275px;height:150px" alt="">
</td>
</tr>
</table>
I am usually using table to do layouting. But I would like to try out the layouting in Div. Here is the problem, When I "float: left" a div, it makes the succeeding div to float with it. I tried to put "float: none" to the next div to make it break a line but it does not work.
Here is what I want to do using table:
<hr style="width: 100%;" />
<table>
<tr>
<td>
<div id="divLeftPane">
...
</div>
</td>
<td>
<div id="divCenterPane">
...
</div>
</td>
<td>
<div id="divRightPane">
...
</div>
</td>
</tr>
</table>
<hr style="width: 100%;" />
Here is what I have so far tried with div using float:
<hr style="width: 100%;" />
<div id="divContainer">
<div id="divLeftPane" style="float: left;">
...
</div>
<div id="divCenterPane" style="float: left;">
...
</div>
<div id="divRightPane">
...
</div>
</div>
<hr style="width: 100%;" />
The problem with the use of div is the bottom is also being floated to the left. I tried putting float: none to the last div, the HR tag and even the divContainer. Why does the last hr float?
This will give you the desired effect:
<hr style="width: 100%;" />
<div id="divContainer">
<div id="divLeftPane" style="float: left;">
...
</div>
<div id="divCenterPane" style="float: left;">
...
</div>
<div id="divRightPane" style="float:left; ">
...
</div>
<div style="clear: left;"></div>
</div>
<hr style="width: 100%;" />
Floating the 3 divs left will make them appear side by side, but you'll notice that divContainer does not take the height of the div tags inside it. Adding the div with clear:left basically undoes this.
edit: Avoid inline styles when you do this for real.
Use the attribute
clear:left;
on the bottom element.
adding
#divContainer {
overflow: hidden;
}
will accomplish this without the extra div at the end
I have a list of images (not in a list, but could be if that might solve the problem) that I want to fill the whole width of a div. I've tried the code at the bottom, and while it does justify any text in the p tag it doesn't do the same for images. How can I get it to evenly space the images across the full width of the div?
<div>
<p>
<img src="image1.png" alt="foo" />
<img src="image2.png" alt="foo" />
<img src="image3.png" alt="foo" />
<img src="image4.png" alt="foo" />
<img src="image5.png" alt="foo" />
</p>
</div>
My CSS:
div { width: 30em; }
p { text-align: justify; }
The simplest way to make it is :
.justify-image{ text-align: justify;}
.justify-image img{display:inline-block;}
.justify-image:after{content:""; display: inline-block; width: 100%; height: 0;}
<p class="justify-image">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
<img src="https://via.placeholder.com/150x40" alt="my img">
</p>
1. You need a container with text-align:justify; (a <p> paragraph in this case.)
2. Your images need to be display:inline-block; else they will not interact as "text" (in justify text last line is not affected you need to add one to make it works.)
3. You need a line of "text" under your images for justifying to apply, with pseudo-element you can achieve it easily by creating a *:after.
*:after :
Don't forget content:"";. Without it, it will not appear. In my example I've used an inline-block with width:100%. It will always break the line after the content in my paragraph.
Happy coding ! :)
Because no one mention in the previous answers. Nowadays (2017) and for awhile ago you can use CSS3 flexbox, by applying the property justify-content:space-between
div {
display: flex;
justify-content: space-between
}
<div>
<img src="https://picsum.photos/50" alt="foo" />
<img src="https://picsum.photos/50" alt="foo" />
<img src="https://picsum.photos/50" alt="foo" />
<img src="https://picsum.photos/50" alt="foo" />
<img src="https://picsum.photos/50" alt="foo" />
</div>
Images are displayed inline so you might as well use the text-align:justify css property to align your images with css. As others suggest, if you want to justify the final row you will have to insert some sort or blank image that takes up the full width at the end of your image list.
See http://fiddle.jshell.net/ewv6K/show/
<!DOCTYPE html>
<title>
Justified Images example
</title>
<style>
body {
text-align:center;
}
#container {
width:800px;
text-align:justify;
margin-left:auto;
margin-right:auto;
}
#force {
width:100%;
height:0px;
}
</style>
<div id="container">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/AfricanWildCat.jpg/220px-AfricanWildCat.jpg" alt="cat 1">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/WhiteCat.jpg/220px-WhiteCat.jpg" alt="cat 2">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Ojo_de_gata_trim.jpg/220px-Ojo_de_gata_trim.jpg" alt="cat 3">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Scheme_cat_anatomy-en.svg/220px-Scheme_cat_anatomy-en.svg.png" alt="cat 4">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Olhos_de_um_gato-3.jpg/220px-Olhos_de_um_gato-3.jpg" alt="cat 5">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Listen%2C_do_you_want_to_know_a_secret.jpg/220px-Listen%2C_do_you_want_to_know_a_secret.jpg" alt="cat 6">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/AfricanWildCat.jpg/220px-AfricanWildCat.jpg" alt="cat 1">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/WhiteCat.jpg/220px-WhiteCat.jpg" alt="cat 2">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Ojo_de_gata_trim.jpg/220px-Ojo_de_gata_trim.jpg" alt="cat 3">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Scheme_cat_anatomy-en.svg/220px-Scheme_cat_anatomy-en.svg.png" alt="cat 4">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Olhos_de_um_gato-3.jpg/220px-Olhos_de_um_gato-3.jpg" alt="cat 5">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Listen%2C_do_you_want_to_know_a_secret.jpg/220px-Listen%2C_do_you_want_to_know_a_secret.jpg" alt="cat 6">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/AfricanWildCat.jpg/220px-AfricanWildCat.jpg" alt="cat 1">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/WhiteCat.jpg/220px-WhiteCat.jpg" alt="cat 2">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Ojo_de_gata_trim.jpg/220px-Ojo_de_gata_trim.jpg" alt="cat 3">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Scheme_cat_anatomy-en.svg/220px-Scheme_cat_anatomy-en.svg.png" alt="cat 4">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Olhos_de_um_gato-3.jpg/220px-Olhos_de_um_gato-3.jpg" alt="cat 5">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Listen%2C_do_you_want_to_know_a_secret.jpg/220px-Listen%2C_do_you_want_to_know_a_secret.jpg" alt="cat 6">
<!-- Force the last row to be justifed - use a blank image here, not a real one like I have -->
<img id="force" src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Olhos_de_um_gato-3.jpg/220px-Olhos_de_um_gato-3.jpg" alt="end">
</div>
In justified text, the last line of text in a paragraph is not expanded to fill the whole width. So to make the inline images spread out you need enough content to pull the paragraph out to two or more lines, eg.:
<div>
<p>
<img src="image1.png" alt="foo" />
<img src="image2.png" alt="foo" />
<img src="image3.png" alt="foo" />
<img src="image4.png" alt="foo" />
<img src="image5.png" alt="foo" />
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
</p>
</div>
(or some other equally-ugly but less-visible version involving .) This is a bit of a nasty hack.
I realise this is way out of date but just thought I'd add a simple solution.
text-align: justify will only work if there is more than one line (if you use this on a paragraph of text, you will see that the last line does not justify). This little jQuery extention will add an invisible element to the element you are trying to justify and thus, your images will be evenly spread:
$.fn.justify = function() {
$(this).css("text-align", "justify");
$(this).append("<span style='display: inline-block;visibility: hidden;width:100%'></span>");
}
Then all you need to do is call:
$("#your-element").justify();
This javascript will spread the images over the div from the middleand to the sides:
var container = document.getElementById("imageContainer");
var imageList = container.getElementsByTagName("img");
var totalWidth = container.offsetWidth;
var sliceWidth = totalWidth/imageList.length;
for( i = 0; i < imageList.length; i++)
{
var totalMargin = sliceWidth - imageList[i].offsetWidth;
var margin = totalMargin / 2;
imageList[i].style.margin = "0px " + margin + "px 0px " + margin + "px"
}
The script asume a couple of things;
There must be an ID of the div (imageContainer)
All the images must have the same width
For my convinience I only tested in IE7, there are things that I'm not sure about such as "offsetWidth" that may be different in Firefox etc. but this is mostly for you to get the concept of the script.
You could of course add support for variable image widths and so forth, but that wasn't really in the scope here.
This script will distribute the images evenly if you add or remove images, ofcourse to a limit. If the total width of the images is greater than the width of the div, there will be wrapping.
Hope it helps!
<!-- not elegant -->
<div style="color:backgroundcolor">
<p>
<img src="image1.png" alt="foo"
/><em>.</em>
<img src="image2.png" alt="foo"
/><em>.</em>
<img src="image3.png" alt="foo"
/><em>.</em>
<img src="image4.png" alt="foo"
/><em>.</em>
<img src="image5.png" alt="foo" />
</p>
</div>
My CSS:
div { width: 30em; }
p { text-align: justify; }