i have a problem with an image in my website it does not appear properly in browsers the picture cuts certain areas in mobile browser
this are the images
desktop pc screen image
image on a mobile browser
this is the code for the image
section#landing {
width: 100%;
height: 100vh;
background: url('../../img/bg.jpg');
background-size: cover;
background-position: center;
background-attachment: scroll;
}
#media only screen and (max-device-width: 320px) {
}
Step 1: Look up the code you are using to see what it is supposed to do:
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.
Well, it is clipping the image, and you don't want it to clip the image, so clearly cover is wrong.
Step 2: Look at the other options:
contain
A keyword that scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). Image is letterboxed within the container. When the image and container have different dimensions, the empty areas (either top/bottom of left/right) are filled with the background-color. The image is automatically centered unless over-ridden by another property such as background-position.
That might do the job.
<percentage>
A value that scales the background image in the corresponding dimension to the specified percentage of the background positioning area, which is determined by the value of background-origin. The background positioning area is, by default, the area containing the content of the box and its padding; the area may also be changed to just the content or to the area containing borders, padding, and content. If the background's attachment is fixed, the background positioning area is instead the entire area of the browser window, not including the area covered by scrollbars if they are present. Negative percentages are not allowed.
… or that (with 100% 100%), depending on what you actually want:
see here jsfiddle
do not use cover because that makes the image to be cropped
instead use contain also add background-position:top center because with contain the img resizes and the empty spaces ( top and bottom ) are filled with the background-color which in your case is transparent . so it's better to align the bck img to top and fill the bottom area with whatever you want
you can with media query set the background-size:cover on pc and contain on mobile
code :
section#landing {
width: 100%;
height: 100vh;
background: url('http://i.stack.imgur.com/fXmkE.jpg');
background-size: contain;
background-repeat:no-repeat;
background-position: top center;
background-attachment: scroll;
}
Related
My website is (https://www.yahoonewsz.com) and I've uploaded an image on the public_html folder in the server.
I have used the following code for background
#main {
background-image: url("image908.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
in advanced CSS section in WordPress.
Why is the image not responsive in mobile?
Thanks.
You're using background-size: cover which according to the background-size docs "Scales the image as large as possible without stretching the image. If the proportions of the image differ from the element, it is cropped either vertically or horizontally so that no empty space remains."
What this means is that it's scaling to the smallest "non-squished" size it can be that prevents any empty space in the container. Since your site (namely the #main) is roughly 3000px tall on mobile, the background image is being scaled to ~3000px tall.
If you want to constrain it to max width, use background-size: contain which "Scales the image as large as possible without cropping or stretching the image." meaning that it will always fill the container without being cut off. Note this will give you white space under (or above) the image based on your background-position settings.
I'm trying to find out ways to keep images, type and other graphic elements that fill a browser window to maintain their proportion and relationships to one another while continuing to fill the window while that window is resized.
To see what I mean, please take a look at the following examples:
Example 1:
https://www.nytimes.com/2017/09/26/magazine/how-fake-news-turned-a-small-town-upside-down.html?rref=collection%2Fsectioncollection%2Fmagazine&action=click&contentCollection=magazine®ion=stream&module=stream_unit&version=latest&contentPlacement=1&pgtype=sectionfront
Example 2:
https://www.nytimes.com/2017/09/28/magazine/here-comes-the-closer-in-the-seventh-inning.html?rref=collection%2Fsectioncollection%2Fmagazine&action=click&contentCollection=magazine®ion=rank&module=package&version=highlights&contentPlacement=6&pgtype=sectionfront
Note how the photo in each, especially in Example 1, is not stretched or squeezed out of its natural shape. Also note how either the full width or the full height of the image is always shown no matter what size the browser window is. Further, the type (headline and intro copy) remains anchored to the bottom left and remains the same size.
How can I achieve this effect?
Additionally, I would like to know how to set the page up so that large image and the graphics that accompany it, change every few seconds.
I would prefer to do this is CSS, but also welcome HTML and other solutions.
Thanks.
I think you are looking for background-size: contain;
In contrast to background-size: cover; it does not ensure the background image covers the whole container, instead the background image gets resized so that the height AND the width are the same or smaller than the size of the container.
Example for contain:
textarea {
width: 100px;
height: 100px;
background-image: url('http://via.placeholder.com/100x100');
background-size: contain;
background-repeat: no-repeat;
}
<textarea></textarea>
Example for cover:
textarea {
width: 100px;
height: 100px;
background-image: url('http://via.placeholder.com/100x100');
background-size: cover;
background-repeat: no-repeat;
}
<textarea></textarea>
Sidenote: I intentionally used textareas for the examples, because they can easily be resized in the bottom right corner for testing
You will have to give width in % for this kind of effect. Don't specify the height and width for the image in pixels. You will have to use '%' for varying the image width ( with proportionate height ) with the screen size or browser size.
If you wish to provide height and width in pixels then you will have to use media queries in CSS to specify height and width for varying screen sizes.
I am having an issue getting my background image in my header to look right.
Right now, it is set to:
.hero {
background: url(http://wordstream-prod.s3.amazonaws.com/landing_pages/assets/img/e682443e-b4c0-483f-823e-8170fd4b71b2) no-repeat center center;
background-size: cover;
background-position: center;
}
Ive tried many variations of css to get it to work but cant figure it out. I would like the section to show the full image and keep showing it (not cut it off) as the browser shrinks. As of now, it is cutting on the top and bottom of the image until I shrink down and then it shows the whole thing. When I shrink further, it cuts off the sides.
When I switched the bg size to contain, I was left with a bunch of space around the image on small devices. Any help is appreciated.
Link: http://solatube.solabrite.com/premier-dealer
To do that, the aspect ratio of .hero needs to match that of the image. You can do this by applying a padding to the element with the percentage amount that represents the image aspect ratio. You can get that percentage by dividing the image height by it's width (500/1280 = 39.0625%).
Add this CSS
.hero {
height: 0;
padding-top: 39.0625%;
}
If you usebackground-size: cover, then the image will be scaled until it covers the whole available space.
Maybe try it with background-size: contain, then the image will be scaled until it covers either the x or y dimension of the available space.
BUT: If your image has the same aspect ratio as the area it is trying to cover, neither of this should be a problem though.
I want to place an image as a background, I've also apply the
background-size:cover
for no-scroll. The problem is when i view the page at different resolutions the whole picture (full width) showed up instead of the center portion (blue bordered area), is there any possible way that I can set the image as background with no scrolling and image will remain center aligned.
this image may describe more specifically what I'm trying to ask. I just want to fix this image at any resolution but the blue bordered area must be remain center aligned,
You can combine background-position: center center with background-size: cover.
use:
overflow: hidden;
max-width: 1200px; // change the width
and add to the background :
no-repeat 50% 0;
I have a container div of unknown size*. I want to fill it with an image of unknown size such that the image completely fills the div, maintaining the aspect ratio.
Then I want to center the image so that the cropped parts are at the edges.
I also need a way to ensure that the container's padding doesn't show any of the image.
*If it helps, the container's size isn't really unknown. It's a Bootstrap column so I can compute the width if I need to.
Solution
These styles did the trick:
background: url(...) no-repeat;
background-size: cover;
background-position: center center;
width: 100%;
height: 100%;
This can be easily achieved using a background image. After you've specified it as you'd like, just add the following CSS to your container to make the background certainly cover its container, while keeping the aspect ratio:
CSS code
.container {
background-size: cover;
}
This will not take maximum size into consideration, and will stretch the image beyond its native resolution, resulting in potential blurring.