Reputation: 4992
I don't always do web development, but when I do CSS is the most frustrating thing I can imagine. I'm trying to create a simple class to hold an image together with description. I want to have two rows, each with three images. Very simple.
Here's my code for displaying images only, which works ok.
img {
position: relative;
margin: 0 auto;
max-width: 650px;
padding: 5px;
margin: 10px 0 5px 0;
border: 1px solid #ccc;
}
<h3>Screenshots</h3>
<p>
<a href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
<a href="images/img2.png"><img src="images/img2.png" width="200" height="140"></a>
<a href="images/img3.png"><img src="images/img3.png" width="200" height="140"></a>
<a href="images/img4.png"><img src="images/img4.png" width="200" height="140"></a>
<a href="images/img5.png"><img src="images/img5.png" width="200" height="140"></a>
<a href="images/img6.png"><img src="images/img6.png" width="200" height="140"></a>
</p>
<h3>Installation</h3>
This shows images exactly as I want, three in each row and then there's a padding from < p > and then Installation section. To get description under images in the same div I changed img in css to div.img and htm code to:
<h3>Screenshots</h3>
<p>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
<div class="img">
<a target="_blank" href="images/img1.png"><img src="images/img1.png" width="200" height="140"></a>
</div>
</p>
<h3>Installation</h3>
One would have thought that the resulting webpage is going to be exactly the same as img in the first case and div.img in the second have the same attributes. It is not the case though. With this code I get each div stretched to the full width of the column and images are one under the other at the left side of the div.
I have also tried this code: http://www.w3schools.com/CSS/css_image_gallery.asp, but in this case I get Installation and all the text that follows go onto the images, ignoring < p > tag which has bottom margin.
Upvotes: 1
Views: 13021
Reputation: 71908
To keep it simple: replace your divs with <span>
elements, or set display: inline
on your CSS for the divs. This will make them behave as you expected (as inline, not block elements).
Also:
margin: 0 auto
. It won't center inline elements as it does with block elementsposition: relative
, but don't seem to need it, either. Unless you intend to apply absolute
position to something inside the div at a later point.Here is a jsfiddle without these two properties, and adding display: inline
to the divs: http://jsfiddle.net/3BwKY/.
Upvotes: 2