4

I have this simple markup for a div:

<div class="gallery__card">
    <img class="gallery__card__img" src="1.jpg">
    <div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
</div>

But I want gallery__card to be as wide as gallery__card__img, so the text below the image doesn't take the full page width.

enter image description here

I want to avoid setting fixed widths, since this is supposed to be responsive.

Is there a way to accomplish this without JS nor fixed widths?

jsfiddle: http://jsfiddle.net/6h19oa1q/

Ajean
  • 5,528
  • 14
  • 46
  • 69
Boel
  • 917
  • 2
  • 11
  • 23

7 Answers7

3

The reason .gallery__card is wider than gallery__card__imgis because of the text you have in .gallery__card. If you remove the text, it will stay as wide as .gallery__card__img. See here.

You can set you imgs to have width: 100% and remove your static height property like here, but then you have to make sure to maintain aspect ratios of all the images you are using; or you will have differing heights (if that's an issue).

Otherwise, you have to change the way you are constructing your elements.

justinw
  • 3,856
  • 5
  • 26
  • 41
  • Or set a width to the `.gallery__car` and then `.gallery__card__img` width:100% and height: auto; – Mattia Nocerino Nov 10 '15 at 16:11
  • 1
    @MattiaNocerino `height: auto` is implied, also the OP wanted to avoid fixed widths, but certainly that is an option as well. – justinw Nov 10 '15 at 16:12
3

The non-fixed width thing seems to be out of the question here, when you make a div element display: inline-block it will resize to it's "biggest" child which in this case is text that doesn't wrap without a width constraint either.

As @Quoid mentioned in his answer you can also make the images scale to width: 100% or height: 100% depending on what you need[1,2].

Another solution is to possibly not use basic image tags but instead use another div element with an inline background-image: url(...) property. How awful this sounds it's a good alternative if you want to have an image tag on steroids and aren't too OCD-ish about some inline CSS.

Refs for aspect-ratio

  1. http://daverupert.com/2012/04/uncle-daves-ol-padded-box/
  2. Maintain the aspect ratio of a div with CSS
SidOfc
  • 4,552
  • 3
  • 27
  • 50
  • 1
    `background-image` is a great method to use; wasn't sure if OP wanted to alter his structure though – justinw Nov 10 '15 at 16:25
  • Hence I put it in there as "another solution" It's always nice to give an alternative next to what the OP actually needs so that he has a lead if it's an absolute dead-end which in this case it is with this no-width-if-possible constraint :) – SidOfc Nov 10 '15 at 16:27
  • 1
    Oh, I completely agree w/you! My comment was meant to be a compliment! – justinw Nov 10 '15 at 16:29
  • @Quoid I know, don't worry - there was a context smiley attached: ":)" – SidOfc Nov 11 '15 at 09:12
2

you can use JS (my solution is with using jQuery), in each iteration you are looking on child element with selector img and then setting up parent element width

$('.gallery__card').each(function() {
    var newWidth = $(this).children('img').width();
    $(this).width(newWidth);
});

example: http://jsfiddle.net/cwp83vkq/

areim
  • 3,371
  • 2
  • 23
  • 29
  • 1
    Why use js when you can easliy do it with css? – Mattia Nocerino Nov 10 '15 at 16:23
  • 1
    It is one possible solution, maybe is not the best one :) But it can help somebody. Is simple and clear on first look what I'm doing. – areim Nov 10 '15 at 16:26
  • 1
    It is a nice trick to solve certain problems, but definitely the wrong way to solve this question. The OP mentioned clearly that he's pursuing responsive design and your JS solution makes it much harder to do so. This JS will get in the way when trying to add `@media` and other standard CSS techniques. – Racil Hilan Nov 10 '15 at 16:36
  • Sure, I understand and thanks for comments, but I think there is no super danger risk if i keep my answer here, someone can use it for his purpose. – areim Nov 11 '15 at 09:34
1

Try this http://jsfiddle.net/6h19oa1q/5/

CSS

.gallery__card {
    margin-top: 10px;
    display: table;
    margin-right: 5px;
    border: 1px solid;
    width: 1%
}

.text {
    overflow: hidden;
}

.gallery__card__img {
    height: 100px;
}
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

This should make exactly what you want. You can play around with the width of the parent element to make it fit a responsive layout.

HTML

    <a href='link/tofile.jpg'>
       <div class="gallery__card">
          <div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
       </div>
    </a>

CSS

      .gallery_card {
       width: 300px;
       border: 1px solid #000;
       margin: 0 auto;
       height: 300px;
       background: url('link/tofile.jpg');
       background-size: cover;
       background-repeat: no-repeat;
       position: relative;
    }

      .gallery_card .text {
       position: absolute;
       color: #000;
       text-decoration: none;
       text-align: center;
       background-color: #fff;
       bottom: 0;
       left: 0;
       padding-top: 5px;
       padding-bottom: 5px;
    }
Halycon
  • 78
  • 8
  • 2
    2 problems with this one, one is that it's what the OP is trying to **avoid** `width` regardless of being a valid solution and the other is that the text probably has to be responsive too which becomes alot more difficult to handle with `position: absolute` (not major but still, not everyone **knows** positioning in CSS) – SidOfc Nov 10 '15 at 16:21
  • 1
    +1 for the `width`, but i don't see a problem using `position: absolute`. In it's current state without a height or a width it merely fixes the text in the bottom and expands to fit the content with some padding. – Halycon Nov 10 '15 at 16:35
  • `position: absolute` also removes the element from document flow for as far as `relative` parents go anyways, I'm just re-reading this now and actually agree with `position: absolute` being ok-ish, even tho it's not a complete example it can work in a responsive design. – SidOfc Nov 11 '15 at 09:06
1

Try this: jsFiddle

Add this style to all images that you have

img {
    width: 100%;
    max-width: 100%;
    height: auto;
    display: block;
}

And remove all height restrictions from .gallery__card__img

AGE
  • 3,752
  • 3
  • 38
  • 60
paravibe
  • 121
  • 6
  • What's the point in specifying the maximum width to be 100%? It's redundant. More importantly, stretching the images beyond their actual size will make them look blurry. – Racil Hilan Nov 10 '15 at 20:35
  • @RacilHilan `max-width` and `width` can be redundant, but nothing about `width` stretches the images beyond their actual size unless the `width: 100%` reaches a pixel width greater than the dimensions of the image This is a precaution the user should take when using `width: 100%` - having this `width` value is perfectly appropriate, useful and acceptable – justinw Nov 11 '15 at 15:18
  • Yes, of course having the `width` value at 100% is perfectly OK if that's what the design requires. However, having the `max-width` also set to 100% is redundant and that's absolutely not OK. – Racil Hilan Nov 11 '15 at 17:44
1

You also define the width in responsive designs, but you just base it on percentage or other similar methods. In your case, you can set the container's width to some percentage like 50%, and set the maximum size to the actual size of the images (because they will look blurry if you stretch them beyond their actual size) which is 500px, then set the width of the images to 100% of the container:

.gallery {
    overflow: hidden;
    -webkit-user-select: none;
    border: 1px solid red; /* This is just for testing */
    box-sizing: border-box; /* This is just for testing */
}
.wrapper {
}
.gallery__card {
    width: 50%;
    max-width: 500px;
    margin-top: 10px;
    display: inline-block;
    margin-right: 5px;
    border: 1px solid;
}
.gallery__card__img {
  width: 100%;
}
<div class="gallery">
    <div class="wrapper">
        <div class="gallery__card">
            <img class="gallery__card__img width-1" src="http://www.cbre.us/o/losangelesmarket/AssetLibrary/GreaterLosAngelesMarket_mainimage.jpg">
            <div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
        </div>
         <div class="gallery__card">
            <img class="gallery__card__img width-1" src="http://students.marshall.usc.edu/undergrad/files/2012/04/Selling-Your-Los-Angeles-Home1.jpeg">
            <div class="text">Sed at elit nec ligula pellentesque congue eget a elit. Sed in purus nisi. Nulla dapibus non est ac lacinia. Suspendisse potenti.</div>
        </div>
    </div>
</div>
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55