I have an image background for a div that I want to show on different devices, The problem is that I have to give height of the image in order to fit it correctly. Now on different phones, I have to adjust the height with different px. forexample on iphones 65px works for portrait mode but not for landscape and etc. Is there a way that the div just gets resized in height to cover 100% of the background image?
here is my code
<style>
div.testing {
height: 95px;
background-image: url(/myimageurl);
background-repeat: no-repeat;
background-size: 100%;
}
#media screen and (max-width: 320px) {
/* iphone portrait */
div.testing {
height: 65px;
}
}
#media screen and (min-width: 321px) and (max-width: 480px) {
/* iphone portrait */
div.testing {
height: 80px;
}
}
</style>
<div class="testing"> </div>
You could use background-size: cover;
.thing {
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;
}
but why are you using a background-image? if you can use a regular image, you could do it like this:
.thing {
width: 100%;
max-width: [your biggest width];
}
.thing img {
display: inline-block;
width: 100%;
height: auto;
}
ALSO
I would recommend flipping your mindset on the max-width and start small screen first, using min-width and getting bigger.
And you don't really need div.testing - it can just be .testing
And if you are using a background image for a good reason... you should investigate making the div -
.thing {
height: 0;
padding-bottom: 30%; /* play with this */
}
This will keep the proportions... but it's only useful in specific cases.
A complete jsfiddle with an actual image would be useful.
Good luck!
Nest your div inside the background div and set the height to 100%
Related
So I have a background image that you can see on this image it is centered
But when I try to make it responsive this happens to the image
And I want it to not move and I've tried a lot of things like position: fixed or changing the margin but nothing happens and the image always does this.
Here's the CSS code:
body{
padding-left:14.12%;
padding-right:14.12%;
background-color:#000;
margin: 0;
background-image: url(images/principal.jpg);
background-position: top center;
background-repeat: no-repeat;
}
And here's the CSS code to make it responsive:
#media only screen and (max-width: 1024px) and (min-width: 321px){
body{
background-position: top center;
background-position-x: -42%;
}
}
You should put the image background in a DIV not in the body element, maybe in a header tag and set the height for the header.
HTML:
<body>
<header></header>
</body>
CSS:
header {
height: 100vh;
width: 100%;
background-image: url(images/principal.jpg);
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
100vh means that the header will be always on screen size height resolution (mobile, desktop).
Also you can use #media and change the image on different resolutions.
I'd like to have a background image on my homepage that is full width on desktop, but as the screen size shrinks, less and less of the right hand side of the image should disappear (but always shows the left hand edge and the objects in the left hand of the image).
The CSS code I have for this is as follows;
.sm-img-bg-fullscr {
background-position: 0% 0px;
background-size:auto;
width: 100%;
display: block;
position: relative;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
overflow: hidden;
background-repeat: no-repeat;
}
and the html where this class is called is as follows;
<div class="sm-img-bg-fullscr parallax-section" style="background-image: url(assets/system/backgroundimage.jpg)" data-stellar-background-ratio="0.5">
I have tried adjusting the background-size variables, and the background position variable, but with no luck. The image being used is 1920 x 1080.
I would like to know what combination of CSS parameters will allow the full image to be shown when the screen width is large enough, and have it remove more and more of the right hand side of the image as the screen size is narrowed, making only the left hand side of the image visible.
Why don't you use media queries to adjust the image background on certain screen sizes?
All we are doing is adjusting the background-position.
body {
margin: 0; padding: 0}
*{
box-sizing:border-box}
.image {
width:100vw;
height:100vw;
background-image:url("https://upload.wikimedia.org/wikipedia/commons/9/94/Cirrus_clouds_mar08.jpg");
background-repeat:no-repeat;
background-position:center center;
background-size: cover
}
#media only screen and (min-width:1px) and (max-width:700px) {
.image {
background-position:left center;
}
}
<div class="image">
</div>
Di you tried this?
background-position: center;
Try this in your CSS
{background-size: auto;}
or try inline style using max-width: ??.px
The website has a section, which has a background image. In the desktop website to make it look good, background image was made fixed so that it can have parallax effect. But in the mobile website, parallax does not make sense. So I wanted to remove the fixed in the mobile view. Here is the code I have tried.
<section class="girl-cover-photo">
</section>
.girl-cover-photo {
height: 730px;
background: url("../img/girl-cover.jpg") fixed;
background-repeat: no-repeat;
background-size: cover;
}
#media screen and (max-width: 768px) {
.girl-cover-photo .girl-cover-photo {
background: url("../img/girl-cover.jpg") relative;
}
}
But somehow the parallax shows up in the mobile view. Any pointers on what to do to fix this?
instead background: url('../img/girl-cover.jpg') relative;
use background: url('../img/girl-cover.jpg') scroll;
https://developer.mozilla.org/en-US/docs/Web/CSS/background-attachment
.girl-cover-photo{
height: 730px;
background: url('http://media02.hongkiat.com/baby_photography/baby_photography.jpg') fixed;
background-repeat: no-repeat;
background-size: cover;
}
#media screen and (max-width: 768px){
.girl-cover-photo{
background: url('http://media02.hongkiat.com/baby_photography/baby_photography.jpg') relative;
}
}
<div class="girl-cover-photo"></div>
Try this
/* Responsive Full Background Image Using CSS
* Tutorial URL: http://sixrevisions.com/css/responsive-background-image/
*/
body {
/* Location of the image */
background-image: url(images/background-photo.jpg);
/* Image is centered vertically and horizontally at all times */
background-position: center center;
/* Image doesn't repeat */
background-repeat: no-repeat;
/* Makes the image fixed in the viewport so that it doesn't move when
the content height is greater than the image height */
background-attachment: fixed;
/* This is what makes the background image rescale based on its container's size */
background-size: cover;
/* Pick a solid background color that will be displayed while the background image is loading */
background-color: #464646;
/* SHORTHAND CSS NOTATION
* background: url(background-photo.jpg) center center cover no-repeat fixed;
*/
min-width: 100%;
max-width: 100%;
min-height: 100%;
max-height: 100%;
}
/* For mobile devices */
#media only all and (max-width: 768px) {
.logo {
/* The file size of this background image is 93% smaller
* to improve page load speed on mobile internet connections */
background-image: url(images/background-photo-mobile-devices.jpg);
height: 300px;
width: 100%;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
}
The background image shows up fine on a desktop browser but when I view it on my iphone 6 plus or any other mobile phone the image doesn't scale correctly and is partially cut off. Can anyone help? Thanks.
There are two ways to work on the background and make it work well:
You should delete the onset:
min-width: 100%;
max-width: 100%;
min-height: 100%;
max-height: 100%;
And
height: 300px;
width: 100%;
The first way to make the length or width is supported on the other by adding the following code:
background-size: 100% auto;
The second way to make the rear rubber with any size by adding the following code:
background-size: 100% 100%;
Thank you
Also you can use or add for more optimization:
html{
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
it will cover the all html or body base on the tag you want the image to cover.
I've a big image to fit in the middle of a login screen. It shows ok with IE10 and Chrome. But it doesn't show anything on Firefox (36.0.1).
#media screen and (min-width: 401px) {
body {
background: url("../../Content/images/Eplod Login DrillBit.png") center no-repeat #2b984b;
background-size: auto 90%;
-moz-background-size: auto 90%;
-webkit-background-size: auto 90%;
background-origin: content-box;
}
}
#media screen and (max-width: 400px) {
body {
background: url("../../Content/images/Eplod Login DrillBit.png") center no-repeat #2b984b;
background-size: 600px;
-moz-background-size: 600px;
-webkit-background-size: 600px;
background-origin: content-box;
}
}
I don't know where I'm doing wrong. In the Mozilla Developer Network it shows the way of using background-size; I've put every elements individually rather than short hand. But nothing working and I'm now puzzled. For screen size bigger than 401px doesn't show any image. But for screen 400px or smaller it shows the image but wrongly.
It seems to me that you want this.
html {height: 100%;}
body {
min-height: 100%;
background: url("http://globe-views.com/dcim/dreams/camel/camel-06.jpg") center no-repeat #2b984b;
background-origin: content-box;
}
#media screen and (max-width: 400px) {
body {
/* -moz-background-size: 90% auto;
-webkit-background-size: 90% auto; */
background-size: 90% auto;
}
}
#media screen and (min-width: 401px) {
body {
/* -moz-background-size: 600px;
-webkit-background-size: 600px; */
background-size: 600px;
}
}
What I did:
Turned the values around: set a responsive, percentual width on small window sizes (max-width: 400px) and a fixed size on larger screens. Though I would recommend setting the break-point to the size of your background-size. (Give or take a few pixels)
Moved shared properties into body outside a media query
Changed the values of background-size in the first media query: first comes the width: 90% and then comes to height: auto. The background image will thus always be 90% of its parent with an automatic height.
Put the prefixed lines (browser specific) before the un-prefixed lines (W3C standard) as you should. Browsers should always use the latest implementation of technology, which in cases like these are the un-prefixed ones. Background-size is widely supported, though, so I don't think you need any prefixes at all.
Thank you #Bram Vanroy. Your solution actually worked after changing couple of things. Here are the codes worked for me.
html {
height: 100%;
}
body {
max-height: 90%;
background: url("../../Content/images/Eplod Login DrillBit.png") center no-repeat #2b984b;
background-origin: content-box;
}
##media screen and (min-width: 401px) {
body {
background-size: auto 90%;
-moz-background-size: auto 90%;
-webkit-background-size: auto 90%;
background-origin: content-box;
}
}
##media screen and (max-width: 400px) {
body {
background-size: 600px;
-moz-background-size: 600px;
-webkit-background-size: 600px;
background-origin: content-box;
}
}