[css] Background images: how to fill whole div if image is small and vice versa

I have three problems:

  1. When I tried to use a background image in a smaller size div, the div shows only part of image. How can I show the full or a specific part of image?

  2. I have a smaller image and I want to use in a bigger div. But don't want to use repeat function.

  3. Is there any way in CSS to manipulate the opacity of an image?

This question is related to css background-image opacity

The answer is


  1. I agree with yossi's example, stretch the image to fit the div but in a slightly different way (without background-image as this is a little inflexible in css 2.1). Show full image:

    <div id="yourdiv">
        <img id="theimage" src="image.jpg" alt="" />
    </div>
    
    #yourdiv img {
        width:100%;
      /*height will be automatic to remain aspect ratio*/
    }
    
  2. Show part of the image using background-position:

    #yourdiv 
    {
        background-image: url(image.jpg);
        background-repeat: no-repeat;
        background-position: 10px 25px;
    }
    
  3. Same as the first part of (1) the image will scale to the div so bigger or smaller will both work

  4. Same as yossi's.


Rather than giving background-size:100%;
We can give background-size:contain;
Check out this for different options avaliable: http://www.css3.info/preview/background-size/


This will work like a charm.

background-image:url("http://assets.toptal.io/uploads/blog/category/logo/4/php.png");
background-repeat: no-repeat;
background-size: contain;

Try below code segment, I've tried it myself before :

#your-div {
    background: url("your-image-link") no-repeat;
    background-size: cover;
    background-clip: border-box;
}

This worked perfectly for me

background-repeat: no-repeat;
background-size: 100% 100%;

To automatically enlarge the image and cover the entire div section without leaving any part of it unfilled, use:

background-size: cover;

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to background-image

How to add a color overlay to a background image? CSS: stretching background image to 100% width and height of screen? CSS blur on background image but not on content Adding background image to div using CSS How to apply a CSS filter to a background image How to use responsive background image in css3 in bootstrap Responsive background image in div full width Overlay a background-image with an rgba background-color Is there a way to set background-image as a base64 encoded image? Using HTML data-attribute to set CSS background-image url

Examples related to opacity

How to darken a background using CSS? css transition opacity fade background How to make in CSS an overlay over an image? How to change the background colour's opacity in CSS jQuery CSS Opacity How to make blinking/flashing text with CSS 3 Making text background transparent but not text itself Change background image opacity Using CSS for a fade-in effect on page load Transparent CSS background color