6

If you reduce your window browser width while you're on http://twitter.github.com/bootstrap/examples/carousel.html you can see that the background image aspect ratio changes. I would like to maintain it (1:1), cropping my image on the left, or on the right, or aligning it horizontally.

What do you suggest?

2
  • isnt this img {max-width:100%;} or am i mis-understanding the question?
    – Richlewis
    Commented Jan 24, 2013 at 12:23
  • @Richlewis it's different: this problem is related with the bootstrap responsive stylesheet. Try it ;) Commented Jan 27, 2013 at 12:43

3 Answers 3

5

You've got 2 options, and both maintain the image ratio:

Option 1

background: url(yourimage.jpg) no-repeat center center / cover;

The last part is the background-size: cover property. It will make the background image zoom in depending on the browser width, to make sure it fills the whole div.

Example: https://i.sstatic.net/xnhev.png

Option 2

background: url(yourimage.jpg) no-repeat center center;

It's the same effect but without the zoom in part. The background will keep its aspect and remain centered, but if your browser width is bigger than your image width (in this case, 1500px), you'll see the background-color (in this case white).

Example: https://i.sstatic.net/NpXBK.png

I recommend option 1. You only see the zooming effect if you resize your browser but almost nobody does, and the visitor will only see a filled background.

2
  • The first option is what I needed, but the second one is also interesting. Thank you :) Commented Jan 31, 2013 at 21:00
  • Much better than the change in aspect ratio, thanks! Though, when the size of the view/screen grows too small (e.g., on an iPhone), the image is cropped... Commented Feb 22, 2014 at 6:13
5

As a follow-up to @jgthms' reply, I used option 1. However, on iOS Safari the background image wasn't showing up at all. It appears that the one-line CSS syntax wasn't playing nice. By changing it to the following I was able to get it working:

background: url(yourimage.jpg) no-repeat center center;
background-size: cover;
0

For those who, like me, didn't see where the CSS comes into play here's an in-context example (with the iOS style). I should note that this is using Bootstrap 3.1.1.

    <div class="item active" style="background: url(slide1.jpg) no-repeat center center; background-size: cover;">
      <div class="container">
        <div class="carousel-caption">
          <p>Example text
            <a class="btn btn-lg btn-primary" href="YourURLHERE" role="button">YourLink</a>
          </p>
        </div>
      </div>
    </div>

Not the answer you're looking for? Browse other questions tagged or ask your own question.