Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to make a group of images in a div tag. All that comes out is this stack of four images. What do I want? I want to put four images in a square area - two on the left, and two on the right. All I've been seeing on the internet is ways to use absolute positioning, and I don't want to go through that crap.

What I have tried:

<div class="img">
<a href="[SECRET]">
<img src="/Experimental/Profiles/[SECRET]1.jpg" width="75" height="75">
<img src="/Experimental/Profiles/[SECRET]2.jpg" width="75" height="75">
<img src="/Experimental/Profiles/[SECRET]3.jpg" width="75" height="75">
<img src="/Experimental/Profiles/[SECRET]4.jpg" width="75" height="75">
</a>
<div class="desc">[SECRET]5</div>
</div>


I need to know how to group this into two images on the left and two on the right, all within a 300px box with a description box underneath.
Posted
Updated 7-May-16 12:11pm
v2

1 solution

I agree with you: absolute position is not an adequate tool; even though it really is not absolute, but a kind of relative (the name "absolute" is wildly misleading) and should only be used when overlapping of HTML elements is required, which is not your case.

At the same time, your problem is easily solved via the CSS property display, which should have the value "inline-block". Such blocks themselves can flow as inline elements or be organized in a bigger structure in some other way.

Here is the simple prototype for you, as simple as I could invent:
<html>
   <head>
      <title>Display demo</title>
      <style>
         div.fourImages { display: inline-block; }
         img { width: 60; height: 60; border: black solid thin; }
      </style>
   </head>
<body>

<div class="fourImages">
   <img/><img/><br/>
   <img/><img/>
</div>

<div class="fourImages">
   <img/><img/><br/>
   <img/><img/>
</div>

</body>
</html>
Please ignore the "img" style; I added it as a tool to mimic images, which are not real images, because they lack URIs. Replace them with real img elements.

I should give you the idea. If the images come at different sizes and need more complicated layout rules, the inner layout also can be implemented based on the display properties. In complicated cases, table-like layout can be quite helpful, with values like "table", "table-cell", sometimes "table-row"…

See also: display — CSS | MDN[^].

[EDIT]

I almost forgot: I would recommend: never specify both width and height of img. Specify none of them or only one, and only if you need to re-sample the image on rendering; such re-sampling might be needed to bring several images of different sizes to the same width or height. Remaining component of size will be defined by the image itself; and not specifying the size is even better, unless required. Specification of both may distort the image, which will happen if its aspect ratio does not match your attribute. Another problem is: scaling an image up can greatly compromise its quality.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900