1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

DIV width to match image width automatically?

Discussion in 'CSS' started by Mr.Dog, Dec 28, 2013.

  1. #1
    Hi,

    Is there any way to create a wrapper DIV that automatically matches the image's width?

    I would use this wrapper for all my images (which have various widths). The DIV will be placed within text/context and will appear in the middle, so no text around anywhere...

    My CSS for it so far is:

    .wrp01{margin: auto; padding: 20px 0px 20px 0px; border: none;}
    Code (markup):
    Too bad there is no "width: auto;" option.
     
    Last edited: Dec 28, 2013
    Mr.Dog, Dec 28, 2013 IP
  2. wiicker95

    wiicker95 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #2
    If the container only has that image in, there's zero reason to even be putting a container in the first place.
    And by the way, padding: 20px 0px 20px 0px is functionally identical to padding:20px 0.
     
    wiicker95, Dec 28, 2013 IP
    deathshadow likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    As images are pixel width, fixing the width to that of an image probably means broken layout methodology...

    ... as Wiicker95 said, if all that's in the DIV is an image, why put a DIV around it. Likewise the redundant padding is a bit wonky...

    IF there is a legitimate reason to have a wrapper (like other content in there with the IMG) a float or inline-block will shrink to fit -- the float may or may not be an option depending on your positioning, and remember that legacy IE can only display:inline-block; an inline-level element (like A or SPAN) -- which a DIV is decidedly not!

    Of course, the vague/meaningless class name isn't helping either! :D
     
    deathshadow, Dec 29, 2013 IP
  4. Lucky Pangestu

    Lucky Pangestu Member

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #4
    I think if you want to make the outer automatically re size based on image's width then it is necessary for you to use bootstrap css :D, because it would automatically fit with your image's size
     
    Lucky Pangestu, Jan 7, 2014 IP
  5. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I kind-of found another solution. Problem solved :)
     
    Mr.Dog, Jan 10, 2014 IP