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;
}
Related
I have been working on positioning a background image, but the image is only positioned correctly while the webpage window is adjusted to its minimum width. As I resize the browser window the image is clipped on all of its' side lengths. The photo has a height much greater than its width(1391 x 2471). I thought I might have to incorporate a vertical scroll? The website is being designed for mobile platforms but I will be viewing and designing it primarily on a computer monitor. How might I maintain the images' integrity from Min. Width of browser to Max. Width of browser?
* { margin: 0;
padding: 0;
}
html {
background: url("image.jpg") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
My CSS code for the positioning of the image was taught to me on CSS-Tricks though it has provided the best results so far. I have added a link to the image encase you would like to view. It is a photo I took myself so I hope the link provided is functional.
You need to get the html element (or whatever element you want the img to show in) to have at least the full height of the img when the img has full width (100vw) of the viewport.
You can do that if you know the aspect ratio of the image. In this case you know the natural width and height of the original so the aspect ratio can be calculated by CSS if you give it those dimensions as variables.
Here's an example using your CSS settings (except see caveat below):
* {
margin: 0;
padding: 0;
}
html {
--imgw: 1391;
--imgh: 2471;
width: 100vw;
min-height: calc(100vw * var(--imgh) / var(--imgw));
/* make sure the whole height of the image is always shown */
background-image: url(https://picsum.photos/id/1015/1391/2471);
background-size: cover;
}
HELLO
Caveat: you have background fixed in your CSS. Two problems with that: it renders the element unscrollable and in any case it is not properly supported in Safari and makes the background look 'fuzzy' on IOS. So this snippet has removed it.
body{
background: url("");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
background-attachment: fixed;
}
If I have understood this properly, you want the image to be displayed properly on all the sizes you want?
Well in that case you can use #media query.
#media(max-width: your max width in px) {
/*And here change the height and width so that it doesn't look weird*/
}
So I want to do something like this, having an image as the fullbackground of a website.
However, I keep trying and trying with ridiculously huge images (The one on my JSFiddle is 6000 x 4000 I think) and they're all still small and get on low resolution when I scale them to fit the website.
Here is my JSFiddle showing my CSS code.
body {
background-image: url(https://snappygoat.com/b/d6249bb487c44ca8e93f4bc0faa46c8f1df7c690);
background-color: #464646;
background-repeat: no-repeat;
background-size: cover;
}
Any help appreciated.
The background cover approach is correct and the resolution of the image shouldn't matter if not for the quality of the image itself (you can have a 400px image fill a 3000px element).
The image will always adapt to fill its container and what I noticed in your fiddle is that the container of the image seems to be the problem, not the image itself.
here's your fiddle, edited with the background element height and width set.
{
height: 100vh;
width: 100vh;
/* Bonus: I think you want your background centered, as in the example that you provided */
background-position: center center;
}
https://jsfiddle.net/edLm73r2/
The image you are used on the example fiddle is very small(768 x 512)
https://snappygoat.com/b/d6249bb487c44ca8e93f4bc0faa46c8f1df7c690
Go for some large image if you want to make it as full screen background. May be use this one from unsplash https://unsplash.com/photos/It0DCaCBr40
body {
background: url(https://source.unsplash.com/user/nolanissac/It0DCaCBr40) #464646 no-repeat;
background-size: cover;
}
It looks like wherever the image is being hosted is not serving the full resolution of the image or quality.
I found the image you are using from a stock image site here and downloaded a re-uploaded so it stays it's full size.
body {
background-image: url(https://i.lensdump.com/i/itbHyH.jpg);
background-color: #464646;
background-repeat: no-repeat;
background-size: cover;
background-position-x: center;
background-position-y: center;
background-attachment: fixed;
}
I've got a div with a background image I'm using for a hero image on my website. On my external monitor it fits perfectly and displays correctly.
When I drag the window over to my laptop display the background image is shifted over to the right. I've tried setting background size to cover but this just makes the image repeat.
The div is set to the width and height of the image (1920 x 600) and both screens are set to 1920x1080 resolution.
Is it because my laptop screen has a higher dpi? What should I change to get the image to display correctly on both screens? Would I need to have more than one image?
Edit:
Here's how it looks on my external monitor (correct)
And here's how it looks on the laptop (when i did the screen grab it was actually bigger than the original image size?)
Here's my CSS for the div
.hero-image {
background-color: #679da7;
background-image: url("../images/hero-image.png");
height: 600px;
}
Using background-size: contain / cover doesn't help, it just makes the image repeat on my laptop screen.
Edit 2:
Joe's solution worked. After a bit more googling it seems that despite my laptop display saying it's 1920 x 1080, it's actually only 1535px wide, which is why the image wasn't displaying correctly (it's wider than the screen) - facepalm
Thanks for the help guys.
You should use
background-size: cover;
background-repeat: no-repeat;
on your background-image.
You need to set the position and repeat rules:
background-position: right;
background-repeat: no-repeat;
What is the resolution of your monitor and laptop ?
you can use
background-size : 100% 100%;
background-repeat: no-repeat;
on your div background
Use CSS3's background-size so that the image always fits in any container without being stretched.
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-image: no-repeat;
In using background size property, make sure that you set your background image property like this:
background-image: url('path/to/img');
instead of
background: url('path/to/img');
Depending on the wanted result (not clear in question) you can do two ways.
1) This will keep the image within the div, if the height is heigher than the div, It will leave a empty space there, use a background-color to see it more clear
background-size: contain;
background-repeat: no-repeat;
2) this will always cover the full width and height of the div
background-size: cover;
background-repeat: no-repeat;
Joe's comment on my question worked. I also found out my laptop screen is only 1535 wide despite being at a resolution of 1920.
Thanks all.
I have the following website:
www.thewhozoo.com
When viewed from a screen with a width of more than 1240px, it displays the images side by side. With a screen below 1240px (e.g. mobile phone), it displays the images beneath each other. This is achieved using:
#media only screen and (max-width: 1240px) {
This all works fine.
My problem however is with the background image:
.top-container {
float: left;
width: 100%;
background: linear-gradient( rgba(0,0,0,0.1), rgba(0, 0, 0, 0.1) ),url('../images/background1.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
When the browser is wide enough (e.g. 930px here), it displays:
But with a narrower browser (e.g. 480px here) or a mobile device, it displays:
As you can see, in the image, the background work "WORK" gets chopped off in the second screen.
Question
Is it possible in css, to set a minimum width, so that the word "WORK" will always be visible, no matter the size of the browser?
This will give smaller browsers the effect as if they are viewing the page from further away (zoomed out).
Thank you.
The background-size: cover setting makes sure the whole element is always filled by the background images. In your case, the mobile version displays the complete height of the image and centers it horizontally, this way cutting something off at the left and right. If you would display it smaller (which would be necessary to see the whole word "work"), the height would shrink too, and the image wouldn't fill the window anymore.
You can try background-size: contain instead, which will always display the whole image, but will leave empty space on either top and bottom or left and right, depending on the orientation. But combined with a background color, this might be something you can live with.
Try background-size: 100% 100%, or background-size: 100%
Hope this helps!
What you can do is make two copies of the background image, when the browser is resized to the minimum width you can use javascript(jquery) to swap the background image from large to small version.
Or you can style the background like:
background-image:url('../images/bg.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;
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;
}