skip to Main Content

I am not a developer but need to troubleshoot a lot of CSS at work. One of the most common problems many users face is with a background image they have set to cover and a background-position: center (with some text, button, etc on top ) and then important “content” in the background image getting cropped out for different devices. I read up on how background-size: cover works and understand that it is due to the image’s aspect ratio not being the same as the container’s aspect ratio for different devices and that if you are using cover, there is no way to avoid that (CSS: Full Size background image).
Here is an example of what I am talking about (https://machineheadonion.000webhostapp.com/background-image.html) . I have a container with a background image first and then an img with max-width:100% ( the latter is what I want as close to as possible ). The image I am using as the background has an aspect ratio of 15:11 . On a large screen when the container is 1903×881.34 see how the lady’s head gets cut off

enter image description here

I can remove background-position:center to make it better

enter image description here

On the other hand on mobile (375×667) it looks even worse

enter image description here

Somebody told me to resize the image in photoshop (eg 768 px wide for mobile) and then use media query to show that image for mobile devices. I tried it here : https://machineheadonion.000webhostapp.com/background-image-mobile-cropped.html but that doesnt seem to be doing anything other than making the background image a little blurry . I think this makes sense as the aspect ratio is still the same after I resize the image.

I tried making adjustments using CSS then, that is reduce the height of the container and change the background position to right only for mobile in this page : https://machineheadonion.000webhostapp.com/background-image-fixed.html

@media only screen and (max-width: 600px) {
         .container {
          background-image: url("/img/it.jpg");
          background-repeat: no-repeat;
          background-size: cover;
          background-position: right;
          }
          .container p {
            margin:0px;
          }
          .caption-box {
            margin:0px
          }

        }

This seems to make things better still not what I want it to be

enter image description here

I would like to know generally: a) why the cropping of the image is so drastic in this case? Is it because the image is squarer with the main subject to the right? Should square images be avoided? b) What can be the best done in this case? Am I following the right process of using media query and then adjusting the height (to make it closer to the aspect ratio of the image) along with changing the background position using CSS for large screens, mobile, tablet, and so on c) Is there any sense in asking clients to resize the images in photoshop since the aspect ratio is still the same after resizing.

I think I sort of intuitively know how the scaling works when the background image is set to cover for containers with different aspect ratios but I am not sure how to deal with it for different types of devices.

3

Answers


  1. Whatever you do, there is always work to do. Maybe in x years there’ll be AI to take care of all of this. Whatever the case, just be ok with never achieving perfection. You will always need a human eye to test everything.

    Some food for thought…

    Provide ‘bleed’ guidelines

    On the supply side (where images come from), along with guidelines for resolution and aspect ratio, you may want to provide recommendations for "bleed" such that any important subjects in a photo are framed with enough space away from the edge of a photo. So as a general rule, what, 20% padding? Hopefully the original photographer has this in mind already.

    Classify photos based on composition

    For more technical systems, it may be useful to classify photos based on composition. So if a photo has subject matter dominating the left side, maybe it gets a tag "composition_left", or whatever. This may be useful in scripts dealing with cropping, components, layouts etc.

    Smart Anchors

    Getting a bit more technical, an image could also have "anchor points" defined, where the points would be considered the most important parts of the photo (e.g. the nose of the main subject matter). Smart cropping technologies can take this data into account to make smarter crops. I have used WordPress plugins that do this; it’s real.

    At the same time, perhaps "zones" could be created to tell scripts that "Hey, this area will have a block of text". Again, cropping scripts need to be sophisticated to take it all in. TBH there really is no technological barrier here. It’s more ux/ui design issues and market issues (how do you make this a product and who will pay for it).

    Login or Signup to reply.
  2. I think its better to answer your questions in a different order:

    a) why the cropping of the image is so drastic in this case?

    You do no scaling. You just crop the image.

    To get better results, you should trust the displaying device that the full width (landscape) respectively the full height (portrait) is suitable to get a good impression of the image. Here the resolution of the screen is not relevant, as the dpi (dots per inch) are different at almost every device.
    So scaling the image to the longer size of the screen bywidth: 100vw; is a first step, but cannot be the last.

    c) Is there any sense in asking clients to resize the images in Photoshop since the aspect ratio is still the same after resizing.

    The sense of reducing the physical size of photos is to save bandwidth and reduce load time. To avoid the tradeoff of a blurred picture on high-dpi devices (as many mobiles are), you should use your @media decisions to provide images that roughly fit displayed size, preferably not being smaller.

    Then the image should be fitted to the viewport by the browser via CSS.

    But for production use there are tools out there that resize these pictures by command-line. For a smooth workflow, a customized script should provide the fitting images derived from the master image (that obviously should have the highest resolution). So Photoshop is only the tool to develop the master image. The down-scaling can be done at the server automatically by script.

    b) What can be the best done in this case?

    Your approach to make decisions via @media is the right direction to walk.

    But just taking the first nice picture for the universal background usage is not the right way. You have to face the fact, that you’ve to deal with two extrema of screens:

    A long slim smartphone in vertical vs. the smartphone in horizontal display mode.

    You also have to realize, these extrema appear not occasionally but frequently as smartphones almost always display in fullscreen mode and do not have the choice of smooth sizing windows like computers. Also the computer user is a quite frequent visitor, too.

    As a result, you have to optimize the background for these 3 occasions:

    • extreme portrait
    • extreme landscape
    • almost square

    Your possibilities are

    • scaling
    • cropping
    • using different pictures
    • using multiple pictures
    • Most important: choosing the right picture!

    A melange of these possibilities will probably bring the smoothest results.

    But there cannot be the one and only solution for all cases. So the decision has to be done fitting to each individual use-case.

    An iconic picture that tells you something will preferably be less cropped but more scaled (at least when the bleed space has been cropped).

    An background picture that shall give a distinctive ambient but should not tell you something. So a generic skyline might be preferable than a individually recognized one (possibly achieved by hiding and cropping). Here the right size of the generic structure is of interest, not the whole picture. So here the cropping would have more weight as it helps to keep the structure in the right display size.

    The example picture:

    Pictures, that have two focuses as your sample up there (the persons + computer) are difficult as they require the decision, which focus shall win when it comes to cropping.

    Might be the solution is to split the picture in advance to get a floating collage over a nice canvas. Finally, the text will always be the 3rd and most important focus and often splits the impression of the background picture. So splitting the image is not necessarily a tradeoff.

    But by splitting them you can let the parts drift together having the anchor of each part of the picture at its related edge. — This declares the center of the background image as the bleed, a space that can be omitted and in this case will be covered by the text.

    A word about the "bleed":

    In print media some extra millimeters are given to the background canvas color/image or an image at the paper’s cutting edges. This prevents a an ugly flashing of a white needle, where the tolerances of the cut have left too much paper.

    For the web layout this is the part of the picture, that can vanish without disturbing the intended impression of the picture. Typically these are the edges of the picture, but you also have an overlay of content hiding parts of the picture. So here we have the need of bleed space also in the middle of a background picture, at least if the picture will never be shown in the whole.

    Having enough bleed is essential, especially when you try to use one picture for portrait and landscape usage.

    Choosing the right picture:

    If you have the possibility to choose the picture, always respect the text content as a further focus to the whole scenery.

    Categorize the picture (and the customers intent):
    • Shall the background picture be a kind of photographic tapestry, giving the total impression a desired ambient, but does not really tell something? Here the bleed is over the whole picture. Essential is only that the displayed parts are large enough to let the basic structures be recognized and the brain can complete it in the mind.
    • Shall the background picture be an expression by itself? — So here you have an interaction between the picture and the text. This must fit. You can use the bleed space of the picture to show both in a appropriate manner at the same time. Or you can show the picture first as a whole (perhaps with a headline) to let the brain remember. Then scrolling in the text and hiding possibly essential parts is no issue anymore, as the brain remembers the image by the not covered rudiments and completes it in the mind.
    Find the optimum for each relevant screen:

    Which screens do you want to respect (make individual @media definitions)?

    For each screen get the right crop situation (remember the space for your text content).

    Find the right transition path. Where to will the eyes of the person move, if you change the window size and aspect ratio? Ideally the head of the person will have resized and moved almost to the same size and position as the next @media will define, when it comes to the change.

    So having here a mathematical dependency between scaling, cropping and positioning is very helpful for a good result. Such dependencies can be expressed within CSS by using variables and calculating their values.

    :root {
        /* ... */
        --bg-width: calc((--bg-height * --aspect-ratio-screen) + --bg-crop-width)
        /* ... */
    }
    
    style1 {
        background-size: var(--bg-width) var(--bg-height);
    /* ... */
    }
    
    Login or Signup to reply.
  3. What you are trying to do is a responsive design. Either you make use of media query in your css or set a rule that is automatically responsive regardless of your viewport. CSS uses viewports rather than resolutions. So when you’re using media queries, find out what viewport to target. You can convert resolutions to viewports.

    However, try this without media queries:

    background-image: url('your-image-url.jpg');
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    
    • background-position: This sets the position of the background image. Using center center will center the image both horizontally and vertically.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search