Perfect CSS Background Image - css

I've been experimenting with a lot of ways to create a background image on a website that works well, however I am yet to find a version that does not causes some sort of an issue.
Here is what I am looking for:
When in 1920 x 1080 then the image is perfect, fits the screen as intended.
When the user zooms out the browser, the image stays intact.
When the user zooms in, the image zooms in as it should (scales with the website)
When the users makes the window smaller, the image stays centered and reduces left and right to keep the center of the image focused.
When the user zooms out and resizes browser, image stays centered, shrinks left and right and the image doesn't shrink.
Here is what I tried, and problems I have found:
Method 1
html {
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Demo here: http://css-tricks.com/examples/FullPageBackgroundImage/progressive.php
Problem: Scroll down to the bottom of the page, refresh a few times. You will notice that the image doesn't load every time until you start scrolling. This issue only happens on chrome browser (Seeing how this is the most used browser by our visitors, its an issue) Also I have found that the image doesn't load as fast as with other methods listed below.
No fallback for older browsers (or at least I don't know how to set it)
Method 2:
img.bg {
/* Set rules to fill background */
min-height: 100%;
min-width: 1024px;
/* Set up proportionate scaling */
width: 100%;
height: auto;
/* Set up positioning */
position: fixed;
top: 0;
left: 0;
}
#media screen and (max-width: 1024px) { /* Specific to this particular image */
img.bg {
left: 50%;
margin-left: -512px; /* 50% */
}
}
Demo: http://css-tricks.com/examples/FullPageBackgroundImage/css-1.php
Problem: When browser is resized, image gets stretched.
Method 3:
<div id="bg">
<img src="images/bg.jpg" alt="">
</div>
#bg {
position: fixed;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
#bg img {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 50%;
min-height: 50%;
}
Demo: http://css-tricks.com/examples/FullPageBackgroundImage/css-2.php
Problem: Image shrinks and becomes bigger and browser resize (this doesn't happen in other examples) When you zoom out and resize, image stretches, doesn't stay centred.
I understand that what I'm asking for might not be possible with CSS, however I think if we can come up with a single solution that can work cross browsers for an image that stays centred and is not shrunk or stretched, that would benefit a lot of people.

Usually i use first method.
Else am usin an jquery plugin called Backstretch. Link here
Let me know how it goes after testing it.

Related

Best method for making Hero Image fill entire screen on mobile without ruining quality?

I'm trying to replicate the way these images change on this website when you shrink the screen https://sweetbasilvail.com/
I went ahead and added the css I saw from their site, but it still is just shrinking my image on mobile instead of doing of this site is doing
.hero {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-color: #211f1f;
}
img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-size: cover;
background-position: center;
}
So I have this image that I want to be the background for my entire home screen, but if I use this code below and shrink it to mobile, it basically squishes the image.
img {
width: 100vw;
height: 100vw;
}
And on mobile it ends up looking like this
If I use the regular responsive css, it shrinks the image and ends up leaving a huge white space below
img {
width: 100%;
height: auto;
}
So I'm not sure how to display an image as the entire background without sacrificing the quality of the image or distorting the way it looks. Is it normal to just have the image shrink on mobile? or is there a proper way when using a full image background?
Note the 2nd image with the giant white space is what the original image is supposed to look like at 100% width;
Use background-size: cover
#bgimg{
height: 100vh;
width: 100vw;
background-image: url(https://placekitten.com/1000/500);
background-size:cover;
background-position: center;
}
<div id='bgimg'></div>
You want to keep the proportions, so you use width: 100% on a div and change its background image. This is much easier to manipulate than an HTML img tag.
Quick code snippet on CodePen (https://codepen.io/ma-henderson/pen/eYzGwJe?editors=1100)
<div class="container">
<header class="header">
<nav class="header__nav-item">Our Menu</nav>
<nav class="header__nav-item">Our Concept</nav>
<nav class="header__nav-item">Locations</nav>
</header>
<div class="hero"></div>
<footer class="footer">
<nav class="footer__nav-item">Gallery</nav>
<nav class="footer__nav-item">Special Events</nav>
<nav class="footer__nav-item">Catering</nav>
</footer>
</div>
.container {
posiiton: relative;
}
.header{
position: absolute;
top: 0;
left: 0;
width: 100vw;
padding: 1rem 0;
z-index: 3;
color: white;
background: rgba(0,0,0,.3);
display: flex;
flex-direction: row;
justify-content: space-evenly;
}
.hero{
background-image: url("https://images.unsplash.com/photo-1572116469696-31de0f17cc34?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1934&q=80");
/* I've added the below line to keep it always centered regardless of viewport width, change it to your liking, you can google "background-position w3 schools" for more info */
background-position: 50% 50%;
height: 100vh;
width: 100vw;
position: fixed;
top: 0;
left: 0;
z-index: 1;
}
.footer{
position: absolute;
bottom: 0;
left: 0;
width: 100%;
padding: 1rem 0;
z-index: 3;
color: white;
background: rgba(0,0,0,.3);
display: flex;
flex-direction: row;
justify-content: space-evenly;
}
You have two options, depending on exactly what your use case is.
If you'd like the img to fill the entire screen but without distortion you can use object-fit: cover. Of course, with this if the image is very wide compared to height you will get some bits cropped from the left and right in portrait mode, and conversely top and bottom in landscape mode - but in either case the screen will be covered.
If it's important that the user sees the whole of your image regardless of its aspect ratio in relation to the viewport aspect ratio then use object-fit: contain. This will result in some space either to the sides or the top and bottom of the image depending on how its aspect ratio compares to the viewport's.
object-fit is quite widely available on browsers, but not on any version of IE.
Note: for background-image as opposed to img use background-size: cover or contain.
Doing that is not really ruining the quality, it's stretching it because that is what you are telling it to. The second option is normal, you just want to probably fill it up with content below so that its not white space. But that doesn't fill up the entire screen.
You probably want to use a media query so that the browser detects when the user is on mobile, then you can crop the sides of your image to make it fit without stretching it, either manually using something like Photoshop, or with CSS, which is answered here on how to do it: How to automatically crop and center an image

Trying to get a background Image to remain 'static' across all monitor and device sizes

I found the below code on stack overflow and was hoping for some clarity. I am updating my portfolio page and wanted to put a landscape image background but how it looks on my laptop (the way I want it to look) does not translate over to my second monitor which is much larger. If I were to implement something similar to the below code would this help alleviate the issue so I can ensure that my page is identical across all sceens?
.background-wrapper {
left: 0px;
top: 0px;
width: 100%;
height: 250px;
display: block;
overflow: hidden;
background-color:#ccc;
background-image: url(https://psuk.s3.amazonaws.com/asset/p4/image/c3c59e5f8b3e9753913f4d435b53c308/Shaun/3040ce690fce2054c2011cf6d2f8b537.jpg);
background-position: 50% 50%;
/* By defining this, your image _will_ be cut off when the screen is
* wider than 1920px, but since you don't want to stretch this would
* happen anyway. */
background-size: 1920px auto;
}
You can try this for a simple example of a fullscreen background image. You can edit the min-height value to get the effect you want.
Example: https://codepen.io/brooksrelyt/pen/REzMZW
HTML:
<div class="full-home" style="background-image:url('https://via.placeholder.com/1200x800');" ></div>
CSS:
.full-home {
width: 100%;
background-repeat:no-repeat;
background-position:50% 50%;
background-position:50% 50%\9 !important;
background-size: cover;
min-height: 100vh;
}

Blank space underneath background between footer (mobile only)

As the title says, the issue arises only when viewed on mobile.
On my pc it looks exactly as desired but when I open it up on a mobile device there is a blank space between the background image and the footer?
Site is live #
https://claritysalonspa.com
Any help would be appreciated!
I am not sure how you are adding the background image in the backend, but try adding the following style. In here, .page-id-29 is the class added to the current page, and you added a background image to it.
.page-id-29 {
background-image: url(YOUR-IMAGE);
background-size: cover;
background-repeat: no-repeat;
height: 100vh; // add the height
background-position: 50% 50%; // center the image
}
so the solution is to add height: 100vh and also change the image position so it is centered.
Add this in your style.css
#media screen and (max-width:600px){
.page-id-29{height: 95vh;}
}
It's because your bodydoesn't have enough content. If you add more content then there is no trick needed.
You can overcome this by adding min-height to your body tag.
.page-id-29 {
background-image: url(https://claritysalonspa.com/wp-content/uploads/2018/03/IMG_5215.jpg);
background-size: cover;
background-repeat: no-repeat;
min-height: 92vh; /* this for height */
background-position: center center; /* to center the image */
}
If you want to make your footer always bottom of the viewport please add thi also.
footer {
position: fixed;
bottom: 0;
width: 100%;
height: 60px;
padding: 0 !important;
}

How to: background-size: 100% auto, background-attachment: scroll

Firefox has a curious bug relating to background-attachment and background-size. I call it a bug, I suspect it isn't, but this renders the way I think it should in the other major browsers (emphasis on I think). The desired effect is to size a vertically repeating background to 100% width while maintaining image proportion.
Here's my original CSS, in this version, the background won't render at all for firefox (even with firefox specific background tags).
body {
background: #000 url('../background.jpg') left top repeat-y;
background-attachment: scroll;
background-size: 100% auto;
}
Changing background-attachment: scroll; to background-attachment: fixed; causes the background to render, but the "floating background" behavior is not desired.
[UPDATE]
Removing background position causes the image to render correctly again.
e.g.
body {
background: #000 url('../background.jpg') repeat-y;
background-size: 100% auto;
}
This is actually a problematic fix because my position values were actually 'left 113px'. I can fix this by applying the background to a wrapping div below the site header instead of the body element, but the fact remains that Firefox has trouble rendering background images with background-size: 100% auto; when position values are used and attachment is scroll.
[UPDATE 2]
I realize the bug only shows up when an additional piece of CSS is present. That's not to say the additional piece of CSS was at fault (it isn't), it's to say that this Firefox specific rendering bug is somehow caused by it.
The additional CSS (used for creating layouts with flexible 100% height)
html { width: 100%; height: 100%; position: absolute; }
And the complete body CSS to go with to replicate the bug
body {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
margin: 0;
background: #000 url('image.jpg') top left repeat-y;
background-size: 100% auto;
}
[SOLUTION]
I solved my problem by creating an additional wrapped div immediately within the body and attaching the background there without specifying position properties.
#wrapper {
position: absolute;
min-height: 100%;
width: 100%;
background: #000 url('image.jpg') repeat-y;
background-size: 100% auto;
}
But because I now have an additional wrapper anyway, I can go ahead and remove the position, height, and width attributes from html, rendering the bug moot and allowing me to set the position of the background as well.
I solved my problem by creating an additional wrapped div immediately within the body and attaching the background there without specifying position properties.
#wrapper {
position: absolute;
min-height: 100%;
width: 100%;
background: #000 url('image.jpg') repeat-y;
background-size: 100% auto;
}
But because I now have an additional wrapper anyway, I can go ahead and remove the position, height, and width attributes from html, rendering the bug moot and allowing me to set the position of the background as well. (see UPDATE 2 in my original question)

CSS Footer Fixed Width, Bottom of screen, and centred

I'm an iPhone Developer mainly, I'm a bit rubbish at CSS and I'm trying to make a webpage for my app.
I want to make my footer have the following properties:
Fixed width of 640px
Centered
Attached to bottom of screen, not page. So when the user resizes the window, the footer is always at the bottom
All the other styling I can do myself, it's just positional styling that I find really difficult.
Can someone please explain to me how to do this in just CSS.
footer {
width: 640px;
margin: 0% -320px;
position: fixed;
left: 50%;
bottom: 0%;
}
Example: http://jsbin.com/imisig/3
Example with heaps of text: http://jsbin.com/imisig/4
Put the footer HTML into a <div id="footer">. And the CSS would be something like this:
#footer {
width: 640px;
position: fixed;
bottom: 0px;
left: 50%;
margin-left: -320px;
}
Explanation
The width property sets the width to 640px
position: fixed will make it so it scrolls with the page
bottom: 0px makes it fixed on the bottom of the page (distance to bottom = 0px)
left: 50% puts the left side of the div to the center of the page
margin-left: -320px - now we have to move it 320px from the left to make it centered
.footer{
width:100%;
position: fixed;
bottom: 0%;
}
position: fixed will make it so it scrolls with the page
bottom: 0px makes it fixed on the bottom of the page (distance to bottom = 0px)
The width property sets the width to 100%

Resources