Setting background image without stretch the image using CSS - html

I'm trying to set the background image using CSS but the original image is stretched.
How do I keep the original image size and set the background for the entire page at the same time?
body {
margin: 0;
background-image: url(background.png);
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
The original image is: Original Image
And the result is: Resultent Image
The difference is the background in my web is larger than the original.
Any help would be appreciated !!!

All you have to do is to remove background-size: cover,
then all should be fine.

You have a lot of options regarding the background image and size.
For your needs you can check this W3c and try the different options you can apply.
Remember that since you apply CSS to your Body, all your pages will "follow" those rules. But some of your pages might have different height from the others.
The result also, depends on the screen resolution of the client.
You have to deside what is your desired result in all screen resolutions.

Try to remove background-size:cover; & set width of image to your requirement.
background-size:cover;stretches the image to full background.
or
Please change your image extension from .png to .jpeg or .jpg because .png always stretches your image and after that the css property u have defined earlier will work properly.

What you can do is just remove, background-size:cover;
Now let's see what does that mean,
background-size
Because background-size CSS property specifies the size of the
background images. The size of the image can be fully constrained or
only partially in order to preserve its intrinsic ratio.
cover
A keyword that is the inverse of contain(contain value specifies that regardless of the size of the containing box, the background image should be scaled so that each side is as large as possible while not exceeding the length of the corresponding side of the container). cover scales the image as large
as possible and maintains image aspect ratio (image doesn't get
squished). The image "covers" the entire width or height of the
container. When the image and container have different dimensions,
the image is clipped either left/right or top/bottom.
To get much identification just try JSFiddle
For the more reference on background-size:

remove background-size: cover;

Thanks.
I found the problem.
the source image is open with a zoom of 35% therefore when I set the backgroud it stretch to 100%.
body {
margin: 0;
background-image: url(background.png);
background-size: 60% 60%;
background-position: center center;
}

Try like this:
.your-class {
background-image: url(background.png);
/* Required Height */
height: 800px;
/* Center and scale the image nicely */
background-position: center !important;
background-repeat: no-repeat !important;
background-size: cover !important;
}

Related

how to make background image to be one full page without cropping the image

I am trying to make the webpage, and I have trouble setting the background image of the webpage. I want my background image to be one full page without cropping or zooming the image or requiring scrolling.
I have my background image with the size of 1920x1080. I thought this size should make the webpage to be one full page, but it did not. What should I do for the image to fit on one page?
background-image: url('image/background1-1.png');
width: 100%;
height: 100vh;
This code is what I have done for the background image, but it crops the image.
Also, I am following the size or location of the images from the design that I did in XD. However, it does not match if I apply it as a code. For example, according to XD, the location of the text is top: 328px; left: 786px, but if I apply this information to my code, the text does not appear where the text is supposed to appear.
I am struggling with these questions.
I would appreciate your help as I do not have much knowledge regarding this topic.
You're looking for the background-size property. If you truly don't want to crop the image at all, use contain, but this will likely not feel like a true background image. cover will enlarge the image just enough to fit within the bounds of the browser window's dimensions, and will crop it slightly when the browser's w/h ratio isn't identical to your images w/h ratio.
https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
background-image: url('image/background1-1.png');
background-color: #000;
background-position:center center;
background-repeat; no-repeat;
background-size: cover; /* contain */
width: 100%;
height: 100vh;
you can add this
background-image: "url(/meals/3.jpg)",
background-position: "center",
background-repeat: "no-repeat",
background-size: "cover",

I want to make a HD picture as my responsive background

so I want to make an image(2988 x 2988) as my background I tried this code as my css
body {
background-image: url(images/nicholas-kwok-225380-unsplash.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
background-color:#464646;
}
and please look at the picture to understand what I am talking about guys
Thank you !
body {
background-image: url('images/nicholas-kwok-225380-unsplash.jpg');
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
background-color:#464646;
margin:0; /* 1 */
width:100%; /* 2 */
min-height:100vh; /* 3 */
height:auto; /* 4 */
}
1: As described in comments the Body HTML element often has a default margin value (0.5rem) which means it can't naturally cover the screen size entirely.
2: Set the image to scale to the size of the body.
3: Set the body to be a minimum height of the entire screen viewport height. So if the body contents is less than the screen size you still see the full body background image.
4: A fix for some browsers (IE / Edge) that will not accept min-height values being set without a height value also being set.
As a significant but side issue: your image size (2988px x 2988px) is HUGE and deeply impractical for 95% of your website viewers. You should either consider reducing your image size to something more usual (~1800px) and/or using some sort of image optimisation tool to radically reduce the filesize.
Image files will need to be downloaded by the client so a ~400Kb image (for example) loading on a ~12Kb webpage will be a big burden for your server to serve and will be slow for the client browser to get and load into memory.
I would recommend reducing the image pixel dimensions and running it through an optimiser tool .
OR
You should use media queries in the CSS so that you only need to load the largest image for the current client screen width. A user on a 480px screen has no need for a 2988px width image.
IF you load a very large image into a small screen space, the browser will firstly download the whole image (wasting above 90% of the data) and will then badly/quickly rescale the image to fit the screen -- making it look worse than it actually is by using a fast but imprecise scaling algorithm.
Update:
To show the full picture in the background regardless of screen size, you want to change cover to contain thus:
background-size: contain;
Do you want to keep the aspect ratio or you just want it to fill the whole space?
#shomz , I want it to fill the whole space (screen) !
Based on the comment above, a simple:
background-size: 100% 100%;
should do. This will always stretch the image to fill your screen.
See here:
body {
background-image: url(https://www.picpng.com/images/large/hat-male-man-person-silhouette-transparent-95008);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
background-color:#464646;
}

Fitting background image properly to a widescreen

Kind of weird title, but couldn't think of better way to word it.
Basically I'm working on a website, and I want to use an asset I made in photoshop for a navbar that looks like a leather suitcase/belt background. Issue is, if I stretch it too far, it won't fit the screen. Basically if I use the CSS background-size: cover; background-repeat: no-repeat; property, it looks something like this
It fits nicely on the 100% width of the element, but as you can see, the image is clipped because it's not 100px as I want it to be.
If I used background-size: contain; background-repeat: repeat-x; properties my image would obviously fit nicely when it comes to height, but since it's not a seamless texture it doesn't clip properly horizontally. As seen on the image below:
Which looks pretty weird as you can see. Last but not least I tried using the 50% 50% trick - background-position: 50% 50%; background-size: cover; background-repeat: no-repeat;, which kind of worked but i still have a problem with it not fitting vertically (the edges are being cut off), as you can see here:
So I'm asking if there's a way to fit the image properly with CSS that I'm missing. Alternatively the second image i posted with repeat-x, however if there would be a way to check (probably with javascript/jquery) that once the image doesn't fit (ala second image), it needs to be flipped horizontally with scaleX so the edges fit, or should I simply downscale the image in photoshop? Thanks for your advice.
Have you tried :
background-size: (100% 100%);
Try using viewport to give the element width in accordance with the screen width (100vw = 100% browser width).
.nav-belt {
width: 100vw;
height: 100%;
}

Setting background image without zooming in using CSS

I am attempting to set the background image for an application, however, the image is zoomed in rather then neatly covering the page. How do I get the image to properly fit to the size of the website.
The code looks as follows:
body {
background: url(http://www.1zoom.net/big2/155/323865-alexfas01.jpg) no-repeat top right;
}
background-size: cover;
From https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
"A keyword that is the inverse of contain. Scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). The image "covers" the entire width or height of the container. When the image and container have different dimensions, the image is clipped either left/right or top/bottom."
body {
background: url(http://www.1zoom.net/big2/155/323865-alexfas01.jpg) no-repeat top right;
background-size: cover;
}

Ensure image is zoomed out

I'm displaying some images as backgrounds on a webpage but the image isn't displaying entirely 'zoomed out'. Instead, it's taking just the left side for example.
How can I make the image display completely? Is it to do with the resolution?
#kitchenimage{
width:100%;
background: url("siteimages/kitchenimage.jpg") no-repeat center fixed;
padding:200px 0;
}
The image is 3249 x 1679.
Thanks.
Depending on which option prefer, define one of the following:
background-size: cover;
background-size: 100% 100%;
background-size: contain;
The first will scale the background image to be as large as possible so that the background area is completely covered by the background image, leaving some parts of the background cropped while keeping aspect ratio.
The second won't keep the aspect ratio and will cover the background without any cropping.
The third will scale the image to the largest size such that both its width and its height can fit inside the background area.