CSS body background-image issue - css

OK, this one works, the ocean picture shows up in the background but repeated 4 times to fill entire screen.
<style type="text/css">
body {background-image:url('ocean.png');}
</style>
<body>
</body>
Then change to
body {background-image:url('ocean.png') no-repeat center center;}
Now nothing shows up background picturewise.

Here's how you can do this and stretch it to the full height/width of the screen (note: if it is not the right proportion, some distortion will occur):
body {
background-image: url(ocean.png);
background-repeat: no-repeat;
background-size: 100%;
}
IIRC, background-size and being able to write url(...) without quotes are part of the CSS3 standard.

Because background-image can't take more paramters - change it to background: url..., which is the correct property to set all of those in one line.
body {background: url('ocean.png') no-repeat center center;}

Try it this way:
body {
background: url('ocean.png');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}

You can size the background image in the background property as follows:
background: url('ocean.png') no-repeat top center 100% auto;
or
background-size:100% auto;
The first parameter is for the width, the second for the height. The first example will put the image top center and stretch it to fill the browser window in width, and set the height proportionately. You can also use "cover" in place of "100% auto" and the browser will fill the image in whatever way it needs to to fill your background.
Note: this is CSS3, and as such the background-size propery will only work in newer browsers, IE9, Firefox 4, ect.

Related

Set background image in the center for all window size

according to the window size, the picture should both cover the window and even if the window height is greater than the height of the picture, let the picture be centered and fully covered.
But I don't want like this (Because the image is not centered, it just starts from the corner.):
background-size: center cover;
Your attempt looks like you try to do it with a OneLiner.
body {
background: url(https://via.placeholder.com/500/green) transparent no-repeat center center / cover;
height: 100vh;
}
<body>
<div>
hello World
</div>
</body>
background-size sets the size of background images for the element. The size of the image can be constrained, completely or partially in order to maintain its aspect ratio.
then remove the margin and padding from the element's parent
try to separate each term, like this
background-size: cover;
background-position: center;
try using margins
I have defined a css for you
.image{
width: 40%;
height: 40vh;
margin-top:30vh;
margin-left: 30%;
}
u can change the width and height of image but remember change margin top and margin left by half.
I used this for a div with an image inside of it. should work just fine. it will get smaller/larger depending on the window size and it will be in the exact center of the page.
background-image: url(path/to/image);
background-size: cover;
background-position: center center;

My background image is cut off at one side, how do I display the full image correctly? [duplicate]

I have a background image in the following div, but the image gets cut off:
<div style='text-align:center;background-image: url(/media/img_1_bg.jpg);background-repeat:no-repeat;width:450px;height:900px;' id="mainpage" align="center">
Is there a way to show the background image without cutting it off?
You can achieve this with the background-size property, which is now supported by most browsers.
To scale the background image to fit inside the div:
background-size: contain;
To scale the background image to cover the whole div:
background-size: cover;
JSFiddle example or runnable snippet:
#imagecontainer {
background: url("http://3.bp.blogspot.com/_EqZzf-l7OCg/TNmdtcyGBZI/AAAAAAAAAD8/KD5Y23c24go/s1600/homer-simpson-1280x1024.jpg") no-repeat;
width: 100px;
height: 200px;
border: 1px solid;
background-size: contain;
}
<div id="imagecontainer"></div>
There also exists a filter for IE 5.5+ support, as well as vendor prefixes for some older browsers.
If what you need is the image to have the same dimensions of the div, I think this is the most elegant solution:
background-size: 100% 100%;
If not, the answer by #grc is the most appropriated one.
Source:
http://www.w3schools.com/cssref/css3_pr_background-size.asp
You can use this attributes:
background-size: contain;
background-repeat: no-repeat;
and you code is then like this:
<div style="text-align:center;background-image: url(/media/img_1_bg.jpg); background-size: contain;
background-repeat: no-repeat;" id="mainpage">
background-position-x: center;
background-position-y: center;
you also use this:
background-size:contain;
height: 0;
width: 100%;
padding-top: 66,64%;
I don't know your div-values, but let's assume you've got those.
height: auto;
max-width: 600px;
Again, those are just random numbers.
It could quite hard to make the background-image (if you would want to) with a fixed width for the div, so better use max-width. And actually it isn't complicated to fill a div with an background-image, just make sure you style the parent element the right way, so the image has a place it can go into.
Chris
try any of the following,
background-size: contain;
background-size: cover;
background-size: 100%;
.container{
background-size: 100%;
}
The background-size property specifies the size of the background images.
There are different syntaxes you can use with this property: the keyword syntax ("auto", "cover" and "contain"), the one-value syntax (sets the width of the image (height becomes "auto"), the two-value syntax (first value: width of the image, second value: height).
percentage - Sets the width and height of the background image in percent of the parent element.
cover - Resize the background image to cover the entire container, even if it has to stretch the image or cut a little bit off one of the edges
contain - Resize the background image to make sure the image is fully visible
For more: https://www.w3schools.com/cssref/css3_pr_background-size.asp
Alternative:
background-size: auto 100%;
you can also try this, set background size as cover and to get it look nicer also set background position center like so :
background-size: cover;
background-position: center ;

Style not applying correctly to background image

I am a student and I'm having an issue with one of the video tutorials regarding using a background image.  I followed the code exactly as is in the video but it's not producing the same results.  It just keeps showing the image tiled throughout the whole web-page.  Any help would be appreciated.
<style type="text/css">
body {
background-image:url(goku.jpg);
background-repeat:no-repeat;
background-attachment: 50% 60px;
}
</style>
Your background-attachment value is invalid, and you should add a background-sizesetting. In the snippet below I used cover to cover the whole screen, but you can also use other sizes. But then you should also add background-position
EDIT after comment: Well then just change to background-position: 50% 50%;. The image - if you don't use background-size will be displayed at its original size, and with that setting, be centered horizontally and vertically. If you don't like the vertical centering, change the second value to whatever you like, also in pixels, if you want.
html,
body {
margin: 0;
height: 100%;
}
body {
background-image: url(https://placehold.it./240x180/fb4);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
<div>Test</div>

My background image is not scaling correctly. I've changed the height to 100%, etc

My background image doesn't want to scale with the rest of the page. And when I've gotten it to do so, it created a huge white-space gap underneath it when I'm scaling down the page.
.vintage {
width: 100%;
height: 100vh;
background-image: url(vintagemcdonalds.jpg);
background-repeat: no-repeat;
}
use background-size:cover for the background-image to cover the whole div.
see here more about this property : CSS3 background-size Property
.vintage { width: 100%;
height: 100vh;
background-image: url(http://placehold.it/350x150);
background-repeat: no-repeat;
background-size:cover;
}
<div class="vintage">
</div>
Try adding the property value cover to your css file.
Like this:
div {
background-image:url('vintagemcdonalds.jpg');
background-size:cover;
}
This enables you to scale the background image to be as large as possible so that the background area is completely covered by the background image.
If some parts of the background image are not visible within the background positioning area, try giving some extra information to your css such as:
width: 100vw;
height: 100vh;
(Note that CSS3 gives us viewport-relative units. 100vw means 100% of the viewport width. 100vh; 100% of the height.)
If you don't want the background image to repeat simply add:
background-repeat:no-repeat;
For more info, check " https://css-tricks.com/perfect-full-page-background-image/ " it will give you a good idea of different approaches to be considered when trying to work with a full screen background.
Hope this helps and good luck! :)

how to position two image as a background image on div by css

here is my css by which i position one image on at center.
.BusyStyles
{
background-image: url('../images/busy.gif');
background-repeat: no-repeat;
background-position: center center;
height: 350px;
width: 300px;
}
can i enhance the above css as a result i can place another image at center on the div just below the busy.gif......is it possible? if yes then please give me the css by which i can position two image as background for div at center one after one. thanks
Check sample for two background image in a single div tag.
CSS:
.container{
width:300px;
height:150px;
background:url(http://img.b8cdn.com/images/icons/loading_large_icon.gif) no-repeat 50% 28%, url(http://twitter-badges.s3.amazonaws.com/t_logo-a.png) no-repeat 50% 60%;
border:1px solid #CCCCCC;
}
You can only do this in CSS 3 (http://stackoverflow.com/questions/423172/can-i-have-multiple-background-images-using-css)
body {
background-image: url(images/bgtop.png), url(images/bg.png);
background-repeat: repeat-x, repeat;
}
I agree with LiamB's solution to this if you have the ability to only support browsers that are compatible with CSS 3.
However, if you need to support more browsers than that I recommend you solve this problem by having 2 divs. Both divs will be positioned on top of each other. The div positioned below contains only a background image. The div positioned on top contains another background image (positioned to look as if it is below the background image from the other div) and any content you want to have.

Resources