How can I align text directly beneath an image?
I used to know how to put an image on top and then justify the text below the image so that it stays within the borders of the width of the image. However, now I have no idea how to do this. How is this accomplished?
5 Answers 5
Some text
If you know the width of your image, your CSS:
.img-with-text < text-align: justify; width: [width of img]; >.img-with-text img
Otherwise your text below the image will free-flow. To prevent this, just set a width to your container.
If you would float the div itself to the left or right you do not need to set the width and the content is still centered. Html/css should center automatically according to the biggest component and not to the full-width.
and to get the image width, a couple of approaches: stackoverflow.com/a/5633302/227926 and stackoverflow.com/a/623174/227926 In turn these need the image source path, how to get that, here: stackoverflow.com/a/2765186/227926
how do you position the image using this format? when i set position:absolute so that I can put the image where I want to, the text just hides behind the image.
this is because you’re pulling the image out of the document flow by setting position:absolute , meaning the text has no concept of the image. you’re better off positioning the image using margins or padding or even position:relative
How to align text below an image in CSS?
If I add a paragraph text between img1 and img2 they get separated (img2 goes to a newline) What I’m attempting to do is this (with some space between the images):
[image1] [image2] [image3] [text] [text] [text]
I haven’t given the images their own individual class names because the images don’t align horizontally to one another.
7 Answers 7
Add a container div for the image and the caption:
Text below the image
Then, with a bit of CSS, you can make an automatically wrapping image gallery:
Text below the image Text below the image An even longer text below the image which should take up multiple lines. Text below the image Text below the image An even longer text below the image which should take up multiple lines.
Updated answer
Instead of using ‘anonymous’ div and spans, you can also use the HTML5 figure and figcaption elements. The advantage is that these tags add to the semantic structure of the document. Visually there is no difference, but it may (positively) affect the usability and indexability of your pages.
The tags are different, but the structure of the code is exactly the same, as you can see in this updated snippet and fiddle:
How to write a caption under an image?
@ceejayoz not to mention all the   as opposed to say setting a margin, or using other css layout tools.
While this has essentially been answered, the only way I’ve found to get a caption to fit an inline image’s width is to hard-code the width property on a wrapper or the caption itself.
@McGarnagle do not edit code meaning in other people’s posts. Formating is OK (well unless it’s python), but all code contents are important! Thanks to your edit, jxworkmans post looked meaningless. And really, fixing wrong code in question is complete nonsense. What would be the point of answers then?
9 Answers 9
#container < text-align: center; >a, figure < display: inline-block; >figcaption < margin: 10px 0 0 0; font-variant: small-caps; font-family: Arial; font-weight: bold; color: #bb3333; >figure < padding: 5px; >img:hover < transform: scale(1.1); -ms-transform: scale(1.1); -webkit-transform: scale(1.1); -moz-transform: scale(1.1); -o-transform: scale(1.1); >img
Those tags do not help in any way. They are just dummy markup, and you need to do all the work in CSS (and/or with other HTML tags). And you need an extra operation to make old versions of IE to recognize them even as dummy tags. So it is simpler to use div or span .
The problem is this: [figures] can be moved to another page or to an appendix without affecting the main flow .