473,756 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

limit caption width to image width

Could someone show me how to limit caption width to image width dynamically
(image width is not known in advance) if that's possible? I was thinking of
using Javascript to get the image width which is then used for caption
width. Is there a better way? Thank you.
May 5 '07 #1
7 5817
Peter Parker wrote:
Could someone show me how to limit caption width to image width dynamically
(image width is not known in advance) if that's possible? I was thinking of
using Javascript to get the image width which is then used for caption
width. Is there a better way? Thank you.

I suppose this is too elementary. Contain the image and the caption in
a div with the same width as the image.

If the image with caption is to be floated, the div will take the width
of the image without it being actually declared on the div.

Other than that the width of the containing div must be specifically
declared.

If the image width is not known, presumably the image file is being
sourced from a remote location using some sort of server side scripting.
If so each of those scripting programs have a native function to get
the image width (and other size parameters) For example the php function
is getimagesize()

There is nothing wrong getting the image width client side with
javascript except where the client disables javascripting.

Louise
May 6 '07 #2
On 2007-05-06, boclair <bo*****@bigpon d.net.auwrote:
Peter Parker wrote:
>Could someone show me how to limit caption width to image width dynamically
(image width is not known in advance) if that's possible? I was thinking of
using Javascript to get the image width which is then used for caption
width. Is there a better way? Thank you.

I suppose this is too elementary. Contain the image and the caption in
a div with the same width as the image.

If the image with caption is to be floated, the div will take the width
of the image without it being actually declared on the div.
Yes, although in that case the width it takes on will be the maximum of
the image's width and the width of the caption (generally with no line
breaks if enough space is available). This is probably a good thing
though.
May 6 '07 #3
Ben C wrote:
On 2007-05-06, boclair <bo*****@bigpon d.net.auwrote:
>Peter Parker wrote:
>>Could someone show me how to limit caption width to image width dynamically
(image width is not known in advance) if that's possible? I was thinking of
using Javascript to get the image width which is then used for caption
width. Is there a better way? Thank you.

I suppose this is too elementary. Contain the image and the caption in
a div with the same width as the image.

If the image with caption is to be floated, the div will take the width
of the image without it being actually declared on the div.

Yes, although in that case the width it takes on will be the maximum of
the image's width and the width of the caption (generally with no line
breaks if enough space is available). This is probably a good thing
though.
Yes. However more often than not, the image will need to be bordered in
which case the image needs to display block declared. This is what I had
in mind.

Louise
May 6 '07 #4
Scripsit boclair:
>>I suppose this is too elementary. Contain the image and the
caption in a div with the same width as the image.

If the image with caption is to be floated, the div will take the
width of the image without it being actually declared on the div.

Yes, although in that case the width it takes on will be the maximum
of the image's width and the width of the caption (generally with no
line breaks if enough space is available). This is probably a good
thing though.

Yes. However more often than not, the image will need to be bordered
in which case the image needs to display block declared. This is what
I had in mind.
I don't quite follow. How would bordering affect this? Usually if you want a
border around an image, it's best to edit the image in an image processing
program, adding a border. But in any case, you can set a border for an image
in CSS (or HTML) without setting display: block.

And I think you would still have the problem that for

<div style="float: right">
<img ...><br>
caption text
</div>

the width would be determined by the caption text requirements, not the
image width, when the caption is longer than the image width.

So I think the only reasonable solution is to determine the width of the
image (via server scripting, if the page is more or less dynamic) and set
that width for the block.

I think I found a tricky way though...

<table width="1">
<tr><td><img ...></td></tr>
<tr><td>capti on text</td></tr>
</table>

It's too tricky though, and there's no guarantee of success.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

May 6 '07 #5
On 2007-05-06, boclair <bo*****@bigpon d.net.auwrote:
Ben C wrote:
>On 2007-05-06, boclair <bo*****@bigpon d.net.auwrote:
>>Peter Parker wrote:
Could someone show me how to limit caption width to image width dynamically
(image width is not known in advance) if that's possible? I was thinking of
using Javascript to get the image width which is then used for caption
width. Is there a better way? Thank you.
I suppose this is too elementary. Contain the image and the caption in
a div with the same width as the image.

If the image with caption is to be floated, the div will take the width
of the image without it being actually declared on the div.

Yes, although in that case the width it takes on will be the maximum of
the image's width and the width of the caption (generally with no line
breaks if enough space is available). This is probably a good thing
though.

Yes. However more often than not, the image will need to be bordered in
which case the image needs to display block declared. This is what I had
in mind.
You get the same effect though.

This:

<float>
<img display: block>
<caption>
</float>

(in pseudo-CSSTML) will come out looking much the same as this:

<float>
<img display: inline>
<br>
<caption>
</float>

with the float taking on the width of the img or the caption whichever
is wider, assuming the available space is wider than either.
May 6 '07 #6
On 2007-05-06, Jukka K. Korpela <jk******@cs.tu t.fiwrote:
Scripsit boclair:
>>>I suppose this is too elementary. Contain the image and the
caption in a div with the same width as the image.

If the image with caption is to be floated, the div will take the
width of the image without it being actually declared on the div.

Yes, although in that case the width it takes on will be the maximum
of the image's width and the width of the caption (generally with no
line breaks if enough space is available). This is probably a good
thing though.

Yes. However more often than not, the image will need to be bordered
in which case the image needs to display block declared. This is what
I had in mind.

I don't quite follow. How would bordering affect this? Usually if you want a
border around an image, it's best to edit the image in an image processing
program, adding a border. But in any case, you can set a border for an image
in CSS (or HTML) without setting display: block.

And I think you would still have the problem that for

<div style="float: right">
<img ...><br>
caption text
</div>

the width would be determined by the caption text requirements, not the
image width, when the caption is longer than the image width.
I suspect that's desirable though.
So I think the only reasonable solution is to determine the width of the
image (via server scripting, if the page is more or less dynamic) and set
that width for the block.

I think I found a tricky way though...

<table width="1">
<tr><td><img ...></td></tr>
<tr><td>captio n text</td></tr>
</table>

It's too tricky though, and there's no guarantee of success.
Yes that will generally work, although if the longest word in the
caption is wider than the image, the table will get that width. So not
strictly what the OP seemed to be asking for, which is that width should
only be determined by the width of the image in all circumstance.

Another approach which does satisfy the literal requirement would be to
make the caption absolutely positioned (perhaps leaving top as auto to
rely on the browser's guess at its static position). That way it will be
taken out of the flow and play no part in the calculation of the float's
width. I didn't suggest this at first though because I think it is not
really a good thing to do in practice. The table cell (image gets min of
image width and width of longest word in caption) is always going to
look better.
May 6 '07 #7
Jukka K. Korpela wrote:
Scripsit boclair:
>>
Yes. However more often than not, the image will need to be bordered
in which case the image needs to display block declared. This is what
I had in mind.

I don't quite follow. How would bordering affect this? Usually if you
want a border around an image, it's best to edit the image in an image
processing program, adding a border. But in any case, you can set a
border for an image in CSS (or HTML) without setting display: block.
Again depending. I was thinking along these lines

If the image might be used on a number of pages, declaring a thin css
border provides differentiation between the image and the page
background (e.g. an image with sections shaded close to that of the page
background).

If a css border is used a "display block" declaration to prevent the
descender space being rendered between the image and the bottom border.

The con is css being disabled.

I think that that the adding of image borders server side is certainly
do-able, but manipulation of images on the server on call is expensive
compared to the use of css. But there is the con

Louise

May 7 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3948
by: Wally | last post by:
I would like to have an image with a caption displayed below it. The size of the image will vary. The caption should not extend beyond the width of the image. How can I cause the text of the caption to wrap so that it will stay within the (varying) width of the image? Any ideas?
6
2035
by: Michael Rozdoba | last post by:
I've tried to apply something along the lines of http://www.spartanicus.utvinternet.ie/test/caption_sized_to_image.htm to a floated span containing an image & caption, but I can't find anything that'll work in IE - floating seems to break it. Can it be done without having to specify each image's width explicitly? The best related info I could find was posted here a few days ago, but doesn't seem to work with floats in IE, unless I'm...
2
5268
by: aqualizard | last post by:
I have made and image with a caption using CSS, but I am hoping someone can show me how to do it better. By better I mean less HTML, and hopefully have it work with any sized image where I do not have to hard code the width of the container that holds the image. Here is a description: I want to have an image with a caption underneath, aligned on the left. There should be a black keyline around the image, and the caption underneath, and...
1
2200
by: Colin Ward | last post by:
Hi. I have a popup modal form which gets part of its caption from code. The part of the caption which is determined from code is the name of the event. For example the caption would be "Add Year for ", where event name is the name of the event for which the user wants to add a year. The event names are all different lengths and sometimes when the form pops up the caption is truncated with the ellpsis(...) Is there a way to determine how...
11
4209
by: Tomek Toczyski | last post by:
What is the best way to attach a caption to an image in xhtml? I can attach a caption to a table by a "<caption>" tag but I would like to do sth similar to an image. How to do it in a natural way? -tt.
5
3964
by: VK | last post by:
On the demo at <http://www.geocities.com/schools_ring/tmp/demo01/index.xml> the table caption has 1px(?) indentation from the left in Firefox 1.5 and I'm running out of ideas how to kill it - if it's possible at all. (Opera 9 shows a bigger indentation (2px?) but from the right side, but this fun is for later :-) IE displays the expected (at least by me) layout. The relevant HTML block in the transformer...
3
13142
by: Jefferis NoSpamme | last post by:
Hello all, I'm trying to limit the file size to 1 meg on upload of image files and I am trying a script from javascript internet, but it is giving me errors on IE ² is null or not an object ³ and isn¹t checking the file size or preventing the upload. ERROR IS <<<<Œthis.form.uploadfile.value² is null or not an object on this line:
3
1482
by: Roy Reed | last post by:
I found a script that converts image alt text to a caption which I've tried to modify for my needs: function addCaption(imgElem) { var captionElem = document.createElement("div"); captionElem.className = "caption"; var captionText = document.createTextNode(imgElem.alt); captionElem.appendChild(captionText); if(imgElem.nextSibling) imgElem.parentNode.insertBefore(captionElem,imgElem.nextSibling);
11
4998
by: Chris Beall | last post by:
See http://pages.prodigy.net/chris_beall/Demo/photo%20block%20experiments.html I've ended up with what seems like a rather complex structure for what I thought would be a somewhat simple problem. Even that complex solution works well only in FireFox (haven't tested Safari...). Can anyone come up with a better solution, where 'better' means: - Works as well as the example above, but is simpler. OR
0
9325
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9716
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4996
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.