Hide image content that is outside current view without squishing image - css

I have an img src that I use as background. Using this css: (image is 1920x1080)
.bg {
position: absolute;
z-index: -1;
width: 100%;
min-width: 1920px;
}
And this does what I want for desktop, upscales accordingly for higher res, and when I shrink the window, it only does some of what I want. The width is still kept (which is good), but I want it to not be scrollable to the sides, and I want the image to be cenered on the new resolution.
So here is an image of how it looks when I resize now, where red area is the current resolution, and the blue area is still scrollable.
http://i.imgur.com/7VJfKPW.jpg
And here is an image of how I want it to look, where the black area is simply empty, the user is not able to scroll to the sides, they can only scroll down if there is content there, and only scroll to the side if there is content there (except for the image itself) and of course the image is as well centered on the new resolution.
http://i.imgur.com/O3sLE31.png
Anyone have any idea of how I can accomplish this? Thanks.'
EDIT: more detailed mockups
So here it is in desktop resolution: http://i.imgur.com/Z8x4e3I.jpg
Here is mobile-ish resolution, where you can see you are able to scroll all the way to the end of the image size to the right: http://i.imgur.com/Y3bC2DI.png
And here is how I want it to be, where the image has been centered, and you are not able to scroll to either side, and all this has to still enable the image to grow in size when the window is bigger than 1920x1080 as well: http://i.imgur.com/7d2vpRK.png
EDIT: tl;dr I want background image to always fit the size of the browser window, even if the user has a 4k monitor, or a 1080p, or a mobile phone, but when we have to shrink the image, the page can't be scrollable to the sides. And the image will usually be 1080p.

the background-size element takes care of that
Just use
background-size: cover;

updated jsfiddle: http://jsfiddle.net/Uz5AY/4/.
* {
margin: 0;
padding: 0;
}
body {
background-image: url(http://placehold.it/1900x1080);
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
background-attachment: fixed;
}
#container > .content {
width: 70%;
margin: 20px auto;
background-color: rgba(255, 255, 255, 0.7);
}
#container > .content > p + p {
margin-top: 20px;
}

Related

Limit max-width of a background image in CSS

I'm making a website and using a background image in a div.
The background image itself is using "cover" as its size. The div itself has a max width, as does its parent div.
However, when I test on larger screens, it continues to stretch. The divs themselves do not, they stay bounded at their upper levels, but the image continues to grow as the window expands.
I've tried using a width of 100%, which has the same problem. I've tried limiting the img.bg size, I've tried changing the background-size to cover contain and 100% cover, but nothing seems to make a difference.
Here's where it's at right now. On 1920x1080 browsers, the image will stretch all the way to 1920, even though I can only see 1300px of it.
img.bg {
max-width: 1300px;
}
.parallax {
background-image: url("images/headerimage2.jpg");
height: 350px;
max-width: 1300px;
min-width: 650px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
The HTML where it's going I invoke with just this:
<div class="parallax"></div>

Prevent header background image going to narrow on larger viewports

I have a status menu as a header on certain pages on my site which works fine on narrower viewports but on larger ones the background image is stretched too wide and becomes too narrow so the status menu dissapears into the white body.
How can I stop the background image from going too narrow when the viewports are made wider?
Trying a min-height in css didn't seem to solve the issue for some reason.
with the html:
<header class="banner-header bg-light">
</header
and the css
.banner-header {
width: 100%;
background: url("../images/navbar-header.svg") no-repeat center bottom;
background-size: cover;
min-height: 180px; }
Working fine on smaller viewports
Goes to narrow on larger viewports
background image without status menu
Try to control background-size manually on larger screens with #media
More info on #media here
More info on background-size here
For example:
.banner-header {
width: 100%;
background: url("../images/navbar-header.svg") no-repeat center bottom;
background-size: cover;
min-height: 180px;
}
#media (min-width:1200px) {
.banner-header {
background-size: 1600px 300px;
}
}
1600px is the width and 300px is the height. Those are placeholder values, pick what best suits your needs.
I hope this will help you!

Set an origin point for resizing a background image in CSS

I want an image to be as responsive as can be, so they resize based on the device. But there is one image where I want to keep the center as the origin point of the resize.
For example, if the image is a small house with a nearby tree in the middle right of the image, I would like the image to resize around that house and nearby tree, rather than from the top left corner (which is the native resize point).
My image is currently set as the background-image of a div with background-size: cover.
Can this be done in CSS?
I've looked at background-origin, but I didn't see how it could answer my question.
Original image: https://i.ytimg.com/vi/pd9LAZk9V1c/maxresdefault.jpg
You can make use of the background-position:center property to align it to center.
You can also make use of background-size to adjust the image size with respect to the container.
.bg {
background: url("https://i.ytimg.com/vi/pd9LAZk9V1c/maxresdefault.jpg");
background-repeat: no-repeat;
width: 500px;
height: 500px;
background-position: center;
}
.bg2 {
background: url("https://i.ytimg.com/vi/pd9LAZk9V1c/maxresdefault.jpg");
background-repeat: no-repeat;
width: 500px;
height: 500px;
background-position: -20px 60px; /* Xvalue Yvalue */
}
<div class="bg"></div>
<br>
<br>
<div class="bg2"></div>

Small CSS image not displayed correctly

I'm having a problem displaying a small image using CSS. I'm trying to show an icon sized picture (the picture has a few pixel border so it isn't edge to edge) but the image itself isn't centered when it's displayed and part of it is being hidden by the right and bottom shadows of the surrounding box. I like the look of the shadows but I think the image is so small, the shadows of the box can't be ignored in the sizing. Here's my CSS. Any ideas?
.delete_button {
background: url('trash_can.png');
background-repeat: no-repeat;
width: 20px;
height: 24px;
display: inline;
}
Try this, adjusting the background-position values until your image is positioned correctly:
.delete_button {
background: url('trash_can.png');
background-repeat: no-repeat;
width: 20px;
height: 24px;
display: inline;
background-position : -3px -4px;
}
Expanding on this a little further, you might want to try to add all your small images into one icon image in a matrix style. Then you can select just the image you want using the width, height and background-position. This will allow all your icons to be loaded at once as a single file, reducing internet traffic. When a "new" icon is needed, it will already be cached and immediately be available.
background-image: url("icons.png");
background-position: 30px 40px; /* Use these values to select your small image contained in your large image */
background-repeat : repeat;
width : 20px; /* or however large your icon is */
height : 24px; /* or however large your icon is */

What is the best way to crop an image with CSS?

I want to show a photo in my page, the DIV layer is 500 * 500px. I will replace the picture very often, the picture size is not sure, may be horizontal version may be vertical version, maybe 800*600px maybe 576*720px.
I don't want to get the photo deformation. How to set CSS or JS, make the photo show only the center 500 * 500 px, hide the around part.
Use a background image on a DIV with pre-defined dimensions, and set the image position to 50%, which essentially centers it. Whatever overflows the 500x500 will be cropped...
#yourImageDiv {
background: url(../img/image.jpg) no-repeat 50%;
height: 500px;
width: 500px;
}
One nice trick is to use a transparent PNG instead of a div and apply a background-image style to it. That way you get to use the image as you normally would (inline, etc.) but can crop at will.
#cropper {
width: 500px;
height: 500px;
background-image: url(myBackgroundImage.png);
background-repeat: no-repeat;
background-position: center center;
}
...
<img id="cropper" src="clear.png">

Resources