display image full size css

29,129

Add these properties to your .header class. But I am not sure if this'll work in old browsers.

background-repeat: no-repeat;
background-position: center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;

Update

If your height is less then the image height, you will loose dimension ratios. In this case your image will be stretched. You can define width and height like this background-size: 300px 150px; as described here.

Moreover you can use cover and contain options with background-size property. As your container height is less then the image you have to rely on this otherwise set width height screw background image.

For more reference please see this link.

Share:
29,129
sukhvir
Author by

sukhvir

Updated on March 09, 2020

Comments

  • sukhvir
    sukhvir about 4 years

    I am trying to display this image in my blog full size : http://i.imgur.com/BruV8Hk.jpg

    However it gets cropped ... i don't know why. I have tried rechecking the .header boundaries and I can't see where I would be limiting the size available for the image.

    Here is the codepen for the site : http://codepen.io/anon/pen/grLED

    Please help

    EDIT:

    Just to clarify .. I would like my .header container to be big enough to house that image in full

    EDIT 2:

    so i just did this : http://codepen.io/anon/pen/vJyFn to get the desired result ... can it be done without all those <br> ?

    • Ruddy
      Ruddy over 9 years
      Add background-size:cover; to .header
    • Benjamin
      Benjamin over 9 years
      Posible duplicate
    • Fergoso
      Fergoso over 9 years
      use background-repeat:no-repeat; and background-position:center center;
  • sukhvir
    sukhvir over 9 years
    that one didn't do anything
  • sukhvir
    sukhvir over 9 years
    it still doesn't show full picture .. here is the edited codepen with your suggestion : codepen.io/anon/pen/Loect
  • sukhvir
    sukhvir over 9 years
    link you posted .. is the same one i posted in my original post .. if you have made any changes to file .. press 'fork' on top and post the new url
  • Siyam Kumar
    Siyam Kumar over 9 years
    try using 'background-size:auto 100%;'
  • sukhvir
    sukhvir over 9 years
    do you mind making changes to my code pen please to see if it works
  • sukhvir
    sukhvir over 9 years
    which height are you referring to? My height for the .header section is set to 100% ...
  • Bilal
    Bilal over 9 years
    Suppose height of header in the page is 200px and you image height is 1000px. This is what I am saying that .header height is less than image height.
  • Bilal
    Bilal over 9 years
    As you updated your question, how can you make header of page to full height of screen? If this is the case it should not be your header. Attach background to body or container element.
  • sukhvir
    sukhvir over 9 years
    ok you missed the point ... i don't want that image to be the background of the entire site ... i just want it to be the background of the .header container .. see edit to the original question
  • ThatMSG
    ThatMSG over 9 years
    alight, sry for that. All you have to do is add: background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; to your .header class
  • sukhvir
    sukhvir over 9 years
    i don't want the header to be full height of the screen .. i just want it to be big enough to house that image in full ... isn't .header like any other container whose dimensions can be varied with contents in it
  • sukhvir
    sukhvir over 9 years
    i am trying to make it look like this: bitofpixels.com/blog/…
  • Bilal
    Bilal over 9 years
    Then you should user <img /> tag instead of background.
  • sukhvir
    sukhvir over 9 years
    if I do that .. the text will be displayed below the image .. i want it to be overlayed over the image
  • Bilal
    Bilal over 9 years
    Set position of text using absolute.