Background is too zoomed in - html

I have a background image for my hero section. On some screen, the background image fits perfectly. However, on some other screens such as my iPhone, it's really way too zoomed in.
I tried solutions on Stack Overflow, such as background-size:contain or background-size:100%, but I end up with like a background that it is repeated 3-4 times. Here is my code for the background:
.bg-hero {
background-image: url('/img/background.jpg');
background-size: cover;
background-position: center;
background-attachment: fixed;
}

UPDATE the initial answer raised the question of background-attachment: fixed which can cause problems in some combinations of settings. However, in this case testing with different aspect ratios images and leaving the fixed in I was unable to recreate the problem, the background-size: cover worked in both orientations. I leave the info below here for now in case it helps point to something that is causing the problem.
Original:
The problem may be in these settings:
.bg-hero {
background-image: url('/img/background.jpg');
background-size: cover;
background-position: center;
background-attachment: fixed;
}
background-attachment: fixed is not supported in IOS. Depending on what version it can lead to e.g. overstretched image (though this question talks of zooming in rather than stretching). See https://caniuse.com/?search=background-attachment but the background-size: cover should have got round that for you.
Partial support refers to supporting local but not fixed

The dimensions of the image you are using for the background are not appropriate for mobile screens. The image you are using has landscape orientation. There are two things you can do :
Use different image having portrait orientation. And add media query in your CSS code.
This approach might not be suitable but you can give it a try. Instead of using image as background, use a relevant color for background (like some shade of brown in this case), and add image as a separate element in your HTML. You can again manage the size of image using media queries. And make sure you position image element as fixed.

Try min-height and check one
.bg-hero {
background-image: url('/img/background.jpg');
background-size: cover;
background-position: center;
background-attachment: fixed;
min-height:100vh;
}

Related

background-size: 100% 100% doesn't work after uploading

I have a Django site, and when I run it on my local machine everything is fine, but when I make it to pythonanywhere.com one little problem appears: somewhy and somehow my background image doesn't want to fill the whole page even though it does on my local host (using the same browser, so the problem is not here). Basically I don't mind changing the aspect ratio, I just want each pixel of the background to be fully on the screen, would it be either 600x400 or 200x2000.
body {
background: url("images/classes.jpg") no-repeat center center fixed;
background-size: 100% 100%;
}
It seems to me like {background-size: 100% 100%} just... doesn't work?
I tried to switch percents to 100vw 100vh, but the output was literally the same.
Page itself: http://seltsamermann.pythonanywhere.com/classes/
Image itself: https://i.ibb.co/0G86wL2/classes.jpg
I wonder what might be the problem.
(in case that might help somehow)
https://github.com/Seltsamermann/Specs-SL-PvP/blob/master/main/templates/main/classes.html
https://github.com/Seltsamermann/Specs-SL-PvP/blob/master/main/static/main/style_classes.css
The object-fit CSS property sets how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container. Use this propriety for the image in your css, or the container that stores that image.
object-fit: cover;
For more : https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
Try this:
body {
background: url("") no-repeat center center fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
Well, basically I just outplayed myself; the real problem was the version control, I just missed several things. Code in the main post works totally fine.

Div Background Image repeating on laptop display but not on external monitor

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.

Background cover attribute not working as expected

I have made a background image, 1366px wide and 768px high, which I want to use as background for the main page of my website.
I have each page of my website divided in sections, using the FullPage plugin.
This is the main page so I'm using just the first section.
What I've tried so far is adding this CSS code to the #first section of my main page:
#first{
width: 500px;
height: 500px;
background-image: url(images/ClanshnowXmasEventSmall.jpg) no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Unfortunately the image gets displayed just partially. In fact it's a little shorter than it actually is.
I read the documentation for the background-size attribute, and at the cover attribute it says:
Scale the background image to be as large as possible so that the background area is completely covered by the background image. Some parts of the background image may not be in view within the background positioning area
So basically how can I make my background-image fit the screen size? Considering the mobile side I think it would be better to fit just the width of it. I'm open for suggestions and help!
Take a look over here: https://www.google.be/amp/s/css-tricks.com/perfect-full-page-background-image/amp/?client=safari
Good luck!

Background image distorted on mobile

So I've seen other people posting about similar problems and I've tried their solutions but none of them have worked.
On desktop, the header background image works perfectly. Even when I resize the window to that of a mobile size, it works. But when I load it on an actual mobile device, the background image doesn't show up. I've discovered that if I remove the background-position: center center;, the background image does show up on mobile, but it's distorted.
So, my question is: how can I make the background image show up on mobile without it looking distorted.
My CSS for the header container in question is:
header {
width: 100%;
height: 100%;
background-size: cover;
background-position: center center;
background-attachment: fixed;
background-image: url(../img/header-background.jpg);
position: relative;
}
You can see it in action at: http://coledavidson103.github.io/davidsoncreative/.
Again, the problem only presents itself on a mobile device. It doesn't even happen when I use Inspector to emulate a mobile device.
Thanks in advance!
EDIT:
Here is a screenshot of the problem when I include background-position: center center. You'll notice that the image doesn't show up.
Here is a screenshot of the problem with background-position is not included. You'll notice the image is distorted.
change background-size: cover to background-size:100% 100%.

Full size background without resizing

I'm facing this trouble for quite some time now, have tried n number of options but still no luck.
I have this background image :
Which I want to make as the background of my website. I added it as a full sized background, but I don't know why it always gets resized and becomes something like this.
Here's my css code for the background image.
body
{
background: url('../images/home.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I'm really unsure how to fix this! Please guide me in the correct way.
The background-size: cover property in CSS3 stretches your images to fit the background of the element you have specified, which in this case your body.
Edit: As others have mentioned, using background-size: contain would give you the most desirable result as it is defined by the following:
contain
This keyword specifies that the background image should be scaled to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area.
The link below also has other properties that may give you your desired results:
Source:https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
Try to use background-size:contain instead of background-size:cover. Maybe that is more appropriate.
If you want to keep your image in proportion you would want to use
background-size: contain;
However the issue with this is it will keep the background in proportion, so if your window is not the same proportions you will have white space around. Building off #Sebastien 's fiddle I can show you what I mean -
http://jsfiddle.net/j0n19rw5/5/
You could try re-sizing the image itself to work better with contain.
But if you want it to cover the background you would probably be best using cover. One trick is to crop the image to the space (if you know a general sizing).
You can get away without stretching it too much with a method like this one :
https://jsfiddle.net/r3qpe4dm/1/
But it will still cut off your image at smaller sizes.
To be sure, split the background property for each sub-attribute :
body
{
background-image: url('http://i.stack.imgur.com/ZQinh.jpg');
background-position: center center;
background-attachment: fixed;
background-repeat: no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
http://jsfiddle.net/Paf_Sebastien/j0n19rw5/1/
Edit:
Add this to your CSS since your HTML/body seem to have a low height:
html, body {height: 100%;}