Back Home

Open Designs

Community. Driven.

    •  
      CommentAuthoracousticsam
    • CommentTimeMay 26th 2008 edited by acousticsam on the 27th May 2008 at 16:32:50 EDT
     
    Hi all,

    I'm running into trouble trying to style captions on images in a template I'm working on.

    As it stands, it looks fine as long as the caption text is shorter than the width of the image. As soon as the caption text gets too long, it stretches out the containing div.

    Is there a way to make this wrap instead, by using only CSS?

    Oh, and I don't want to specify a width, because everything is based off the image's width.

    Here's my HTML:
    <div class="img left">
    <img src="img/keyboard.jpg" />
    <p>When I have a long caption, it sticks out. How do I make it wrap?<p>
    </div>

    And my CSS:

    .left {
    float: left;
    }

    #main div.img {
    margin: 0.3em;
    background: url(img/img-bg.png) repeat-x;
    }

    #main div.img img {
    margin: 0.3em;
    }

    #main div.img p {
    background: url(img/caption-bg.png) repeat-x;
    font-size: 0.5em;
    color: #5B6873;
    padding: 0.2em;
    }


    Link to the template:
    http://samnabi.com/sandbox/my-designs/iTunes-theme/index.html
    (link disabled, see below for screenshot)

    Any thoughts?
  1.  
    first of all you addressing div.img in your css when the div is called "img left" so try this
    <div class="img-left">
    <img src="img/keyboard.jpg" />
    <p>When I have a long caption, it sticks out. How do I make it wrap?<p>
    </div>

    .left {
    float: left;
    }

    #main div.img-left {
    margin: 0.3em;
    background: url(img/img-bg.png) repeat-x;
    }

    #main div.img-left img {
    margin: 0.3em;
    }

    #main div.img-left p {
    background: url(img/caption-bg.png) repeat-x;
    font-size: 0.5em;
    color: #5B6873;
    padding: 0.2em;
    display: block;
    }


    i think the display "block" might do it.
  2.  
    div class="img left" is actually calling two separate classes, .left and .img. Because you may not always want your image to be floated, it's not part of the .img class. Then, if you want to float anything else to the left (tables, blockquotes, preformatted text, forms, etc) you can just append class="left" rather than messing about with inline styles.

    Got that trick from one of snop's templates. Pretty useful, I must admit.

    As for the display: block; ... it didn't make a difference. p is already a block-level element.
  3.  
    sorry i couldnt be of more help.
    i like that trick. thanks
    • CommentAuthorwebmatter
    • CommentTimeMay 27th 2008
     
    Hi, you need to define a width for your #main div.img-left otherwise the p tag won't know the width of the img where you want it to wrap.
    • CommentAuthorRob
    • CommentTimeMay 27th 2008
     
    I don't have an answer unfortunatly, but just to let you know when i've tried to look at your site i've been told by both Google Desktop and the latest build of Firefox 3 that your site is dangerous and to not enter it, it looks like it's coming from your wp stats, this is probably something you should look at dealing with.

    What is the current listing status for samnabi.com/?

    Site is listed as suspicious - visiting this web site may harm your computer.

    Part of this site was listed for suspicious activity 2 time(s) over the past 90 days.

    What happened when Google visited this site?

    Of the 4 pages we tested on the site over the past 90 days, 4 page(s) resulted in malicious software being downloaded and installed without user consent. The last time Google visited this site was on 05/18/2008, and the last time suspicious content was found on this site was on 05/18/2008.

    Malicious software includes 16 trojan(s). Successful infection resulted in an average of 2 new processes on the target machine.

    Malicious software is hosted on 1 domain(s), including wp-stats-php.info.

    Has this site acted as an intermediary resulting in further distribution of malware?

    Over the past 90 days, samnabi.com/ appeared to function as an intermediary for the infection of 2 site(s) including acousticsam.tk, acousticsam.com.

    Has this site hosted malware?

    No, this site has not hosted malicious software over the past 90 days.

    How did this happen?

    In some cases, third parties can add malicious code to legitimate sites, which would cause us to show the warning message.

    Next steps:

    * Return to the previous page.
    * If you are the owner of this web site, you can request a review of your site using Webmaster Tools. More information about the review process is available in Google's Webmaster Help Center


    Source
  4.  
    Gah. I thought I got rid of that problem.

    Thanks for bringing it to my attention, Rob. Must have been one of those stats programs I got rid of that's still lurking about.

    OK, well, since I don't want any of you guys catching my germs, here's a screenshot of the problem:



    Any ideas?
    •  
      CommentAuthorJJenZz
    • CommentTimeMay 28th 2008
     
    Without applying the width to the containing div (which you don't want to do) there isn't much you can do with CSS to fix this as there is no way of saying "Make this have the same width as the width of the image (whatever it might be)".

    There is one CSS property you could use though... if you add "white-space: pre;" to your p tag and then add a carriage return to your caption wherever you want it to break, then it will be fine.

    You will just have to make sure you remember to add carriage returns to your captions when needed.

    Otherwise, the only other alternative (that I can think of) is to use javascript to get the width of the image and apply it to the caption or surrounding div.
    •  
      CommentAuthoracousticsam
    • CommentTimeMay 28th 2008 edited by acousticsam on the 28th May 2008 at 20:40:09 EDT
     
    Hmm... well it was a long shot anyway.

    Thanks for the tip, Jen, I was thinking I'd just have to do manual < br / > tags.
    But your solution makes it a little more 'proper'. Yeah I suppose JavaScript is a little overkill for something like this.

    Thanks for the suggestions everyone.
    • CommentAuthorVertex
    • CommentTimeJun 5th 2008
     
    Setting:

    .img-left p{
    max-width:100%;
    }

    Works in Firefox...
    •  
      CommentAuthoracousticsam
    • CommentTimeJun 5th 2008 edited by acousticsam on the 06th June 2008 at 09:59:51 EDT
     
    hmm, that looks like it might work...
    i'm away from my home computer right now but when i get home i'll try it with other browsers and post the results.

    EDIT: No dice. It works perfectly in Firefox, but none of the other browsers seem to care...

    Thanks anyway!
    • CommentAuthorardarvin
    • CommentTimeAug 11th 2008
     
    Firefox 3 now ignores the "max-width: 100%;" trick.
    Dang.

    Anyone else come up with a CSS solution? I suppose there really isn't one.
    • CommentAuthorMike Weiss
    • CommentTimeAug 11th 2008
     
    What if you set a min-width: on img-left, and a max-width on the caption?

    The down side is that with wider images the caption won't be that wide, but it could form a nice looking short content block.