Have an alternate background if the background image doesnt load - html

I currently have an image that covers some screen resolutions, but if the screen resolution is bigger than the image, it will be white for each pixel its bigger, so instead of that ugly white, I want a color behind that background image so it wont look ugly. My current code is just an img tag for the background and the css scales it up to 100% but if the screen res is larger than the image res, it will create problems.

Is there a specific reason why you're using an img tag as your background image, instead of setting a div to have it's background as that image? Because you can do this with css fairly easily.
.background-div {
width: 100%;
height: 100%;
background: url(your_image_url) no-repeat;
background-size: cover;
background-color: #e3e3e3;
}
Where the .background-div would be in place of your img... This could also be the body of the document, or whatever. It's just a container div that covers the whole background.
The background then has a fallback colour of #e3e3e3, and background-size: cover means that it will always scale the image to fill the screen, so you won't see that colour.
You could also use contain instead, which would make the image scale how you're expecting, and show the background colour where the image doesn't reach.

Find the parent div of your image and add a stylesheet attribute.
It will look something like this
<div class="parent" style="background-color: #000000;height: 100%;width: 100%;">
<img src="your image url" class="image" alt="alt" title="your title"/>
</div>
the attr style="background-color: #000000;" will give the div a black background color.
You can also set the background color by its classname insde you style.css file. Or what ever you named the file.
.parent {
background: #000;
height: 100%;
width: 100%;
}
.parent img {
max-width: 100%;
}
I hope this is the awnser your searching for. If not, provide some more details. Maybe show the structure of your current code.

Related

How to make background-image all full-screen with html+css

I am working on a file html+css and I want to create background-image full-screen in HTML, and CSS I have set all properties for background image.
I want that background-image to fill all full-screen on devices and without scrolling on background-image so I try to do this:
* {
margin: 0;
padding: 0;
}
.background-image {
position: relative;
}
img {
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
position: fixed;
height: 100%;
width: 100%;
}
</style>
<div class="background-image">
</div>
I set the background-image position relative and img position fixed to make fixed without scrolling but it brings the background-image is streched also text? Any idea to fixed it ?
If you want a background image use create a container and with css add the background-image property to the container: url('the path of my img')
Some advice I can give you is:
img
Use IMG if you want people to print the page and you want the image to be included by default.
Use IMG if you want the browser to display an image in proportion to the size of the text.
Using IMG instead of background-image can drastically improve the performance of animations over a background.
css background-image
Use CSS background-image if the image is not part of the content.
Use CSS background-image when the image replaces the text
Use CSS background-image combined with CSS background-size:cover in order to stretch a background image to cover the entire container
Use CSS background-image if you want people to print the page and you do NOT want the image to be included by default.
use the object-fit property and object-position property.
Here you are not actually working on the background. The image is a element not a background. To make it a background you have to use URL of the image.

make an image in background page html in a responsive way

I want to put an image in background of the page and in of course it must be in a responsive way
this is the html code :
<div > title </div>
<img src="/assets/img/image1.jpeg" />
and the css :
img {
background-size: cover;
background-repeat: no-repeat;
}
Marshall's solution is better if you want the image to cover the whole viewing area.
Although it would not be in the background, using your <img/> tag it would be like this:
img {
width: 100%;
height: 100%;
}
<div> title </div>
<img src="https://picsum.photos/200/300" />
You want to set the background-image propierty of the body
body {
background-image: url("/assets/img/image1.jpeg");
background-size: cover;
background-repeat: no-repeat;
}
A background image for the page would be best placed on the body element — here's a simple example which uses the background shorthand:
body {
background: url('../relative/path/to/img/goes/here') no-repeat #paleblue;
}
Here, we've provided the image, an instruction that the image should not repeat (it will by default), and a 'fall-back' background colour — this is useful if, for example, the image fails to load.
There's more that can be done, here. See MDN for more details. Pay special attention to background-size, as you'll likely want to use either cover or contain depending on the image, screen size, etc. The spec for both is straightforward, but you can very easily swap one for another and back again in your code and see the (often obvious) effect each has.
you can add background property to which container you want the background img is, if its body or any other container, also add to your img width and height property otherwise it will go beyond the container, you should add 100% if you want to cover the whole container.
background-position:center center

Background image displays wrong, don't know what to do

i'm pretty new to CSS but I made something in illustrator (web)and wanted to put this as background-image in css. I've put the image in a <div> and CSS in I just put background-image: url(berg.jpg), i'm sure the url is correct, but it doesn't display anything. I've put height: 100% in css after that and it showed up but it was all zoomed in and only displays a small corner of the picture because of the zoom. How can I display this correctly? The image size is width: 1366px; height:768px.
Sorry if this is already asked but I couldn't seem to find it. Thx on advance.
Your image is displaying as full size. If you add the background-size attribute to your css, you can define the size.
See this link for all background-size options:
https://www.w3schools.com/cssref/css3_pr_background-size.asp
You can also define the background-position to have image centered, etc.
https://www.w3schools.com/cssref/pr_background-position.asp
CSS example
#problem-image {
height: 200px;
width: 200px;
background-image: url('https://images.unsplash.com/photo-1463852247062-1bbca38f7805?auto=format&fit=crop&w=2255&q=60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D');
}
#correct-image {
height: 200px;
width: 200px;
background-image: url('https://images.unsplash.com/photo-1463852247062-1bbca38f7805?auto=format&fit=crop&w=2255&q=60&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D');
background-size: cover;
background-position: center center;
}
See jsfiddle for full working example:
https://jsfiddle.net/3o8b2e4s/
You can try to put bakcground-size: cover; in your CSS code and this will ocupe all background of your div independent of width or height.

imagebox in html auto orients the image

I have this imagebox that will display a user's uploaded image:
<img src = "./imgs/..imagenamehere..." style = "background-size:cover; width:357px; height:357px;"
I placed the background-size:cover there because I want the imagebox to autoadjust the image when it's not in a 1x1 size so it won't stretch the image and also not to increase the imagebox's size.
However when displaying a long portrait image, the imagebox auto-orients the image making the image looks like it standing instead of the original portrait position.
Is there any way to fix this?
What you want is object-fit CSS property, unfortunately not yet supported in IE and Edge:
img {
width: 100px;
height: 100px;
border: 1px solid #dedede;
}
.contain {
object-fit: contain;
}
.cover {
object-fit: cover;
}
<img src="http://lorempixel.com/100/300" class="contain">
<img src="http://lorempixel.com/100/300" class="cover">
background-size:cover;
is only for, as the name says, background-images. Example below
div {
height:200px;
width: 200px;
background-image: url("http://placehold.it/350x150");
background-repeat: no-repeat;
background-size:cover;
background-position: 50% 50%;
}
<div></div>
In this code you are adding and image but you are not applying the CSS on it. You are applying the CSS on background image.
use this code in your CSS instead
background-image : url (' < Path of image you want to show > ');
background-size : contain; <!-- You can use 'cover' also but see what you want it to look like -->
If you use background-size : cover; then if your image is not having suitable aspect ratio then it will be zoomed in to cover all the area where you are showing it, and if you use background-size : contain; then it will show your image in that area with it's maximum possible size by which both height and width of that image can fit inside.
If you have any doubt, then comment

PSD slicing background with image

I got a PSD design for my website, and I'm having trouble coding the main banner.
This is the page
The top banner (with the image of the little people) doesn't show properly on some screen resolutions. I sliced the entire banner together with the image and set is as a background image, and the text is live. The problem is, on some screens the image appears on top of the text.
How should I code this? I thought of slicing the image and adding it as a regular image element, but that's difficult because the image has a glow around it which blends into the background.
Is there any easier way to do this?
Thanks!
The reason why it does show up properly in certain screen resolution is because your image dimension is 1550x417, and you using CSS background position property to set it to 1300px 400px. That's not a very responsive way to setting background position, and it bound to vary from screen to screen.
There is an easier way to do this. Since the banner is mostly solid color why not slice only the graphic part of the image, and use background color? You can then position the image to the center of the element.
Slice: http://i.imgur.com/2s35JYs.png
Example:
background: url('../images/banner2.jpg') center center no-repeat #ECE8DF;
you should add "center 0" to background style.
#banner {
background: url("../images/banner2.jpg") no-repeat center 0;
background-color: #e8e4da;
height: 400px;
padding: 0;
background-size: 1300px 400px;
border-bottom: 2px solid #fff;
}