I'm creating a new HTML5 page that has multiple image layers. The first layer is a background layer that is a full screen image (proportional to the screen size).
The second layer is another image object, which I want to be in a fixed position proportional to the first layer; so if I changed the browser size, both of the 2 images would change size so they are proportional one to each other.
Does anyone have any idea how can I do so?
So, let's suppose we have a "sky.jpg" background image and a "sun.png" image on top of it.
Here's the HTML(5) code:
<div id="sky">
<img src="sun.png" id="sun">
</div>
And here is the responsive CSS:
#sky {
width:100%; /* width in percentage, so it will adapt to user's screen */
height:600px; /* here i've set a fixed height, but maybe it's not what you want */
background:url(sky.jpg) top center no-repeat;
text-align:center; /* #sun will be centered, not sure it's what you need */
background-size:cover;
-moz-background-size:cover;
}
#sun {
width:15%; /* adjust it to the needed ratio */
max-width:300px; /* max-width will prevent it from being bigger than the original image */
min-width:20px; /* min-width is optional */
}
Live demo (resize the window)
Notice the background-size property set to cover (more info here).
It means that the aspect ratio will be preserved to the smallest size such that both its width and its height can completely cover the background positioning area.
That's why you need an image bigger than most of resolutions (if it's a body-background image).
Other possible values for this property are "contain" and "auto" (have a look at the specs).
background-size:cover; is not supported by IE8 (that's why we still add top center for background positioning) and for some FF versions you'll need a vendor prefix (-moz-)
Now you can set a width in percentage for #sun, so that it'll keep a ratio to its containing div. To prevent it from becoming bigger than the original size, you can set a max-width (and eventually a min-width).
The height of the image will auto-adjust in modern browsers.
If you don't want a fixed height on #sky, be sure that its container also has a height different than auto (the default value).
For example, if #sky is your page background image, you've to set:
html, body { height:100%; }
#sky { height:100%; }
Related
I have a background image that exceeds the height of the browser. How might I incorporate the image so that I can scroll down to see the remaining portion while maintaining the scale across different browser sizes? Code that I have written or used only clips the image to fit the size of the browser. Overflow seems to have no affect.
Depending on the end result you're looking for, here are some approaches and tools you could consider.
because of the different aspect ratios of desktop and mobile screens, it's sometimes difficult to have a single background image that will work well for everyone. But if you prepare a desktop background image of say 1920px wide by 1080px high, and a mobile background image of say 640px wide by 960px high, you can use media queries to specify different backgrounds for different viewports.
more info: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Backgrounds_and_Borders/Resizing_background_images
#media(max-width:480px){
body{
background-image:URL(mobile-background.jpg);
}
}
#media(min-width:481px){
body{
background-image:URL(desktop-background.jpg);
}
}
background image size of cover or contain are also really useful. Specifying contain means that the background image will be scaled to fit the size of the element it's applied to.
And specifying cover means that it will fill the background of the element it's applied to, and you should expect some croping of the background image. You can also position the image.
more info: https://developer.mozilla.org/en-US/docs/Web/CSS/background-position
div{
background-size: contain;
/* or */
background-size: cover;
background-position: center;
}
and sometimes you might want to stop the background from scrolling:
more info: https://developer.mozilla.org/en-US/docs/Web/CSS/background-attachment
div{
background-attachment: fixed;
}
Is there a way to make a website 100% fluid?
As in, fill the background edge-to-edge with an image, no matter the size of the browser window. Also, have it resize larger or smaller as the browser window changes without neccesarily retaining aspect ratio. Images inside divs and font sizes should obviously resize accordingly and maintain the same amount of white space so the page shows exactly the same content in screens from 800x600 to 4K Ultra HD, being the idea to above any kind of vertical scrollbar. Let's forget about mobile and tablets for a moment.
What I have tried for background:
body {
background-image: url(./Si0rPf7.png);
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
}
Which will fit background as long as the aspect ratio of the image is kept, the moment it changes you will see white spaces on both sides. If cover instead of contain, image will get cropped, which is undesirable if we want to show our whole background to everyone, even if we have to stretch it.
Now, for img src I've had half success with two methods:
.image_1 {width: 100%; height: auto;}
and
<img src="img/image_1.jpg" width="100%"/>
None of them seems able to react to both width and height. Looks like you have to choose between one or other.
For font-size I would just use vw and hope for the best.
You want
background-size:100% 100%;
You should look into the flex model: https://philipwalton.github.io/solved-by-flexbox/demos/grids/
Hi I am trying some stuff out with html and css to see what works and what doesn't, and I was wondering if it is possible to make an image stay in the center of your screen and always adapt to the size of the screen. For example an image that when I look at it on a screen that's 1920*1080 is 192*108 large and when I look at it on a 1600*900 screen it becomes 160*90 whilst the center of the image stays in the middle.
PS: It doesn't matter that the resolution of the image changes even though it makes the image sometimes look terrible.
You can't do this with just an image tag. You could put the image you're using as background-image and then set the sizing to cover. Basically this:
body {
background-image: url('url/to/your/image.jpeg');
background-size: cover;
background-position: center center;
}
To adapt to the size of the screen,place image in container and then do :
img{
width:100%;
height:auto; /*maintains aspect ratio */
}
now image will have the dimension of container!!
To align image in center of your div...do:
div{
width:80%;
margin:0 auto; /*align div in center first*/
text-align:center /*center content*/
}
taken from
Use CSS to make an image scale up and down
img {
width:100%;
}
However, that can easily make the image look like total crap. A safer way might be:
img {
max-width:100%;
}
Either way will get the image changing sizes with browser resizing. However, the second won't stretch the image past its natural size, so it doesn't look deformed.
I have to cut PSD file into CSS and HTML. The problem is container with background image has 1160px of width. This container have only background image on right side so I could be hidden on smaller resolution. Main content container have 996px so it's good.
I'm trying to do it this way:
if resultion is more than 1160xXXX show whole image on right side,
if resolution is less than 1160xXXX hide a smart part of image on the right side.
Image on the right side have to be always on the same place - its relative to the .container .inner which together looks nice.
My code:
.container {
max-width:1920px;
margin:0 auto;
position:relative;
}
.container .background {
background:black url("../img/woman.png") no-repeat scroll right top;
max-width:1160px;
overflow:hidden;
}
.container .inner {
width:996px;
}
The goal is to put background image always in the same place and cut this image if resolution is less than 1160px. Any advices?
EDIT:
For no .container .background changing it's position depending on resolution... I don't want it - image have to be always in the same place.
Use media queries like
#media only screen and (max-width:1160px){
#something{
some css
}
}
You can read more about media queries Here
One new approach often used for mobile and tablet browsers, is to use pairs of images of different sizes. In responsive design, for example, you can use the CSS #media to check the device window width, and return a larger or smaller image depending on the circumstance.
How can I stretch background image of a table cell with CSS? I have Googled it with no results.
When you set background image of a cell and the background size is smaller than the cell, then it will be repeated.
How can I force this background to stretch the entire table cell instead?
It is possible to stretch a background image using the background-size CSS property.
Examples:
body { background-size: 100% auto } /* Stretches image to full horiz. width */
body { background-size: 50% 50% } /* Stretches image to half of available
width and height - WARNING: aspect ratio not maintained */
body { background-size: cover } /* Ensures that image covers full area */
body { background-size: contain } /* Ensures that image is completely visible */
All major current browsers support the feature:
IE since 9.0
Chrome since 3.0
Firefox since 4.0
Opera since 10.0
Safari since 4.1
You can't stretch a background image.
If you want to stretch an image, you have to put it in an img tag. You can use absolute positioning to put content on top of the image.
It is possible.
NOTE: This is using CSS3 and will not be supported by inferior browsers.
If you set your cell with this styling it should solve your problem. It will also allow for collapsible tables too because you're using %.
background-image: url('XXX');
background-repeat: no-repeat;
background-size: 100% 100%;