How to stop a background image centring at a specific width - css

Is there a way to stop a background image centering at a specific width?
To illustrate the problem, reduce the width of the following site untill scroll bars appear. Then observe how the background image remains centred, however, all other elements hold their place.
See it here

You could add min-width to your #header css
#header { background: #f8f8f8 url("background.jpg") no-repeat center bottom; background-position-y: 160px; min-width:960px;}

Use media queries to set some styles at a specific width and use background-position to stop centering the image. An example: http://3.jsbin.com/ojemur/4/watch (resize your browser window and if the background gets red the image doesn't center vertically)
More information about media queries:
css3 media queries on webdesignerwall
Responsive design with CSS3 media queries on webdesignerwall
A crash course in CSS media queries on nettuts+

Related

Background image differing in inspect vs actual mobile

I'm trying to get a background image to position correctly on a mobile view. When I look at the website with inspect (or re-sizing the monitor to be a mobile view), the image shows fine. However, when I view it through a phone, the image, for some reason, seems to be re-sizing to fit the phone width instead.
#hero{
height: 100vh;
max-width: 100%;
background-image: url('http://i.imgur.com/molLHMj.jpg');
background-size: cover;
background-repeat: no-repeat;
}
<body>
<div id='hero'>
</div>
</body>
If I remember correctly, setting background size as cover would make it so that if the image is bigger than the screen width, it'll simply be hidden, but this seems to be resizing for some reason.
Edit:
Setting background-size to auto displays the image like how it looks like in chrome's inspector. This shouldn't be the case, as the wider version displays the full width/height (given that it's auto). I'm really lost on what is going on.
Edit2
Looks like only the chrome mobile version displays it that way. The default browser renders the background just like how it looks like in the desktop inspector. Really weird.
The image is resizing because you are telling it to with height: 100vh;. Additionally, the image is getting skewed because you are placing a restriction on the width with: max-width: 100%;.
Setting just max-width: 100%; (and removing the height:100vh) or removing both lines and just sticking with background-size:cover could clear this up.
But, if you want the background to cover the entire viewing area, don't place the background on the div. In fact, get rid of the div and just apply the background to the body.
Also, background-size:cover does not cause the image to hide, it causes the image to resize to cover the background available. This results in cropping when the background size is smaller than the image.
From MDN:
cover
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.
You can achieve your goal like this:
.hero {
height:100vh;
width:100%;
}
.hero:nth-child(1) {
background-image:url('http://placehold.it/1920x1080');
background-size:cover;
}
.hero:nth-child(2) {
background-image:url('http://i.imgur.com/molLHMj.jpg');
background-size:cover;
}
<div class="hero"></div>
<div class="hero"></div>
The problem seemed to be the size of the image. It was around 3000 in width. When I resized it to around ~1900, the behavior returned to normal.

Setting background size in css to 100% results in poor print quality (pixelation)

My report screen consists of DIVs with background images that are adjusted using:
background-size: 100% auto;
When printing the page (via Chrome 53) the images are pixelated. If I use an img element all is good. Any idea how to set it right? Do I have to use an img element to have a proper printing quality?
JSBin with background-image div:
https://jsbin.com/royufibeko/edit?output
JSBin with img element:
https://jsbin.com/zulocideri/1/edit?html,output

Background not filling the whole mobile view

I want to get the background to be dynamic, proportionally re-size, and fill in the whole background instead of simply covering half of it like it is now on any mobile platform.
I'm an absolute novice at web design and am going through a merchant website. I thought I had figured out my website, but I completely forgot about mobile views so it is a mess in that area. I have tried media queries, viewport things, and more to no avail, so I feel like there was something default preventing them from working.
Website: http://www.reliefinsleep.com/
Thanks so much!
background-size: cover; will achive what you are looking for. This stretches the bg image to the size of the viewport cropping the edges rather that squashing it.
#media all and (max-width: 699px) { /* max-width would be the breakpoint for when this style kicks in */
body {
background-size: cover; /* stretches to size of viewport */
background-position: center; /* center the bg image */
}
}
You can also use percentage, pixel and em values for the background-size property. See MDN

100% Height Div on iPad

I am working on a responsive website using the Skeleton Grid and have a left floated navigation bar div that is 100% height of the browser window. It works in all browsers I've tested except for mobile safari on the iPad. In mobile safari the navigation div is 100% of the viewport, but when you scroll down on the page the div does not stretch to the bottom of the content. [See image below].
How do I make it so that the div is 100% height on the iPad, not just 100% of the viewport?
Dev Site: http://www.id8agency.com/jeremiah
Screenshot of the problem: http://www.id8agency.com/jeremiah/jeremiah.png (as you can see the white bar on the left does not stretch to the bottom of the page once you scroll)
How it should look: id8agency.com/jeremiah/jeremiah1.png (sorry, I don't have enough reputation to post more than 2 links)
Things I've tried:
Set height and min-height to 110% (works, but creates a scroll bar on
the desktop site)
Set height to height of content (works, but not all pages will be the
same length, so creates a scrollbar on some pages where content is
shorter)
Set position:absolute and and position bottom:0px (doesn't work,
positions the div at the bottom of the viewport, not the bottom of the page)
Set meta viewport tag to height=device-height and height=1000 (breaks
the responsive functionality of the website)
Thank you for any help you can provide!
try having the .navigation element be position: fixed; and then place the the content in the right in its own wrapper (something like .content-right).
Then set these properties on the .content-right element include a media query "reset" for when everything goes to one column (XXX is the breakpoint):
.content-right {
margin-left: 240px;
}
#media screen (max-width: XXX){
.content-right {
margin-left: auto;
}
}
Why don't you put on outer div and apply the background of the sidebar (sliced so it can be repeated on the y axis) to it and repeat-y?
It will expand up to the content end and when you don't need it anymore, you can remove the background from the media queries.
The problem also appears when I set my desktop browser viewport to a small height. I think the problem is, that all the 100%-heights here are relative to the viewport, and I think you want the navigation bar to be the height of the content, not the viewport.
But possible solutions would be to set a faux column on the #container-element (see http://alistapart.com/article/fauxcolumns).
Another possibility would be to measure the height of the #container-element with jquery and assign it to the navigation-bar. Something like:
var containerHeight = $('#container').height();
$('#navigation-container').css('height',containerHeight);
(I haven't checked if it works, but I think it should.)

Fixed background with CSS?

I would like to fix my background to 100% height of body and leave it there even when rest of the page scrolls.
How do I achieve this?
Right now all I have is background:url(bg.png);. The height of the image is 1200px and width 20px, if that matters.
in css, use this:
background-attachment: fixed;
Depending on what you want the background image to do there are a couple of options. There is a great article on ALA about full screen BG images that accounts for scaling:
http://www.alistapart.com/articles/supersize-that-background-please/
If you are just looking to position the image in the browser you would do:
background:url(bg.png) no-repeat top left;
background-attachment:fixed;
Or however you want to position it respectively (top right, etc.)
Some browsers still have trouble supporting the stretching of background images so here's a workaround.
CSS3 Example and Support
Since it's already 1200px, you can use background-attachment:fixed; on the background to make it follow when they scroll. Example at w3schools. You can make the image look like it is meant to flow into a solid color at the bottom with a nice gradient, etc.

Resources