• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

(CSS) Can I make background-size 'contain' or 'cover' do this?

Engaged ,
Sep 24, 2018 Sep 24, 2018

Copy link to clipboard

Copied

I've been toying with full-size background images, alternating between background-size: cover and contain. After some testing, I'm wondering if the behavior I'm looking for is even doable. Thought I'd ask here.

The image is of a standing silhouette.

The behavior I'm looking for is :

  • PROPORTIONALLY:
    Must retain original proportions at all times.
  • VERTICALLY:
    Must stretch to fill 100% viewport height at all times.
  • HORIZONTALLY:
    - If width is larger than image, left-justify.
    - If width is narrower, right-justify.
    (In other words, crop left side out of the viewport so the right side of the image remains in view.)

What is the simplest and most code-efficient way to obtain this result?

Thanks!

Views

245

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Sep 24, 2018 Sep 24, 2018

Found it! Or close enough behavior that my original plans for the layout don't need to be trashed.

Here it is, in case anyone wants to try anything similar :

div#bg {

    background-image: url(/images/bg-main.png);

    height: 100%;

    width: 50%;

    background-size: cover;

    background-position: right;

    background-repeat: no-repeat;

}

(with the appropriate adjustments to html+body to remove margins and stuff)

Sorry for wasting everyone's time with this one. For whatever reason, I kept trying to tr

...

Votes

Translate

Translate
Community Expert ,
Sep 24, 2018 Sep 24, 2018

Copy link to clipboard

Copied

Probably best done with two separate images and different media queries for when things begin to fall apart

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 24, 2018 Sep 24, 2018

Copy link to clipboard

Copied

Post the actual image and let's have a play with it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 24, 2018 Sep 24, 2018

Copy link to clipboard

Copied

LATEST

Found it! Or close enough behavior that my original plans for the layout don't need to be trashed.

Here it is, in case anyone wants to try anything similar :

div#bg {

    background-image: url(/images/bg-main.png);

    height: 100%;

    width: 50%;

    background-size: cover;

    background-position: right;

    background-repeat: no-repeat;

}

(with the appropriate adjustments to html+body to remove margins and stuff)

Sorry for wasting everyone's time with this one. For whatever reason, I kept trying to trick my way around left-justifying the image when it should've been right-justified the whole time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines