background image is not getting displayed - html

Am new to html. I went through similar questions but none solved my problem. Can anyone help. Thanks in advance.
The File location is correct.
My code:
<div id="bg">
</div>
Css:
#bg{
background-image: url("bg.png");
}

Try this:
For demonstration i used 500px x 500px you can go with 100% x 100% too preferable with a container wrapped around the div. You don't need display: block; here in your css because a div already has display block as a standard for most browsers.
#bg{
width:500px;
height:500px;
background-image: url('bg.png');
}
Also be sure that the width and height matches the width and height of your image.
Your map with your files should be looking something like this:
index.html
bg.png
mycss.css
... many other .html / .php / .css
If you want to use a container, your css would look something like this. Try using container if you're going to work more with css, it's a really good thing too use for different browser sizes.
#bg{
width: 100%;
height: 100%;
}
.container{
width: 500px;
height: 500px;
}
And your HTML
<div class="container">
<div id="bg"></div>
</div>

The path is wrong, check my comment under the question. The Image should be in the same place as the css or the path should be url('../bg.png'); if it is up one level etc.... try this and if it comes p red the path is wrong
#bg {
background: #ff0000 url("bg.png") no-repeat right top;
}

As you are setting the background-image of a div that div must contain something. And the background-image will sized accordingly with the size of the element contained inside the div.
Try this,
#bg{
background: url("http://www.mascotdesign.com/_dev/images/famous-cartoon-character-mickey-mouse.png");
background-size : 100% 100%;
background-repeat : no-repeat;
}
jsFiddle

Related

Need help adjust header img to right size

So I want to decrease the size of the img on the header so it looks cleaner and a more sharp img , however i am unsure how to do it?
Here is the code
CSS:
.header {
background: #000000 url (C:/website/logo final.svg) no-repeat;
background-size: 80px 60px;
}
HTML:
<header>
<div id="header" align="center">
<img name="Antique Picture" src="C:\website\logo.jpg
" alt="logo" width="100%" height="100%">
</header>
all help would be rly appreciated thankyou
I'm going to tackle this one. As Michael Coker said, there are a number of flaws that need fixing in your HTML structure:
1) The align attribute is deprecated. We'll cover that with CSS.
2) Image widths must either be pixel-based, or covered in CSS.
3) The header div isn't closed.
4) You provided local images, so we can't access them, meaning we can't check if they're blurry.
Additionally, the img tag has no attribute name, and the background-image URL must have no space and quotes around the file path. I'll ignore the fact that it's bad practise to have a space in the filename, as it will work with the space.
Fixing up those problems, your new structure looks like this:
HTML
<header>
<div id="header">
<img src="C:\website\logo.jpg" alt="logo">
</div>
</header>
CSS
.header {
background: #000000 url('C:/website/logo final.svg') no-repeat;
background-size: 80px 60px;
}
To centralise the header, you need to add text-align: center to it in the CSS:
.header {
background: #000000 url('C:/website/logo final.svg') no-repeat;
background-size: 80px 60px;
text-align: center;
}
To adjust the image widths properly, you should use CSS for this as well:
.header img {
width: 100%;
height: 100%;
}
Keep in mind that percentage-based widths are relative to the parent element. The header parent element will need a height and width in order for the image child to adapt. If you'd prefer a fixed image width, these values should be specified in pixels:
.header img {
width: 100px;
height: 100px;
}
Then if you find it is stretched, you can adjust the pixel values above (or the image itself, if that is easier).
Hope this helps!

Setting background image for a Div

I am trying to set the background image for a div. But it aint seem to be loading..
html
<div class="cover"></div>
css
.cover
{
background-image: url(..images/Section.jpg);
background-size:cover;
height: 100%;
}
jsfiddle-http://jsfiddle.net/bfhzdpg5/
You're missing a forward slash before images by the looks of it.

full-screen fit images in two columns

I'm a beginner and I tried to make one page by myself, however, the result is not good. I will try to explain what I need: Full-screen page with two images, one image will cover 50% of horizontal space of browser window, and second image will be on right side covering the rest of this page. I need both images to be responsive and to always keep 100% height. When the window is resized, left and right sides of both images will be cropped.
Very similar to this: http://www.gt3themes.com/website-templates/soho/striped_landing.html
Is this difficult to make? I tried to follow some guides on the web, but the result was that my images were stretched and not cropped. Maybe I am completely wrong and I need to create two columns and then put images inside?
I will appreciate any help.
My current code is:
.photo{
size: 100%;
position: relative;
overflow: hidden;
}
.photo img{
max-width: 50%;
height: 100%;
}
The site you linked more or less did something like this:
http://jsfiddle.net/xnLn6s5o/
HTML
<div id="container">
<div id="left" class="halfwidthcontainer">
<div id="left-image" class="image"></div>
</div>
<div id="right" class="halfwidthcontainer">
<div id="right-image" class="image"></div>
</div>
</div>
CSS
html, body, #container, #left, #right, #left-image, #right-image {
height:100%;
}
.halfwidthcontainer {
float: left;
width: 50%;
}
.image {
background-size: container;
background-repeat: no-repeat;
background-position: 50% 50%;
}
#left-image {
background-color: red;
background-image: url('');
}
#right-image {
background-color: blue;
background-image: url('');
}
The general idea is that two containers sit side by side, floated (see this answer as why to use floats to position containers side by side instead of inline-block).
The idea thereafter is to explot the CSS background property which will allow you to get the overflow/positioning effects you want. You can read more about that here.
You're going to want to fill in the background-image properties of the #left-image and #right-image IDs to add the images you want.

CSS/HTML Background, new picture when scrolling

I'm working on [this website][1]. I have a "mainbackground" which is shown on it.
Is it possible to make [this picture][2] continue when you scroll?
Here's my CSS:
background-image: url("bakgrund.jpg")
I'm having a little difficulty figuring out exactly how you envision this working.
If you want the background image to continue down the page as far as the content goes, use:
background-repeat: repeat-y;
If you want multiple images as the background, use:
background-image: url(image-url-1), url(image-url-2);
If you want the image to scroll with the text (i.e., not fixed) but then continue scrolling once you reach the end of the content, or if you want to dynamically load in multiple images as the need arises, you would need to do it programmatically using AJAX or something.
You can use:
background-attachment: fixed;
If you are trying for a fixed background try this http://jsfiddle.net/kkmLpqqd/
<div id="a">a</div>
#a{
width:300px;
height:2000px;
background-image:url(http://placekitten.com/300/301);
background-attachment:fixed;
background-repeat:no-repeat;
background-color:green;
}
.class-name {
background:url(your-image.jpg) no-repeat;
background-attachment: fixed;
}
http://davidwalsh.name/css-fixed-position-background-image
Try assigning the background image to the "body" element in the CSS, and then put all of the content you need to scroll in a "container" div, with a white background (or whatever you'd like).
css:
body {
width: 100%;
background-image: ...
background-attachment: fixed;
}
#container {
margin: 0 auto;
background: #FFF;
}
html:
<body>
<div id="container>
...
</div>
</body>

Image percentage not restricting

I have a div that I am giving a width of 60% to.
CSS
#image {
float:left;
width:60%;
background-image:url('cleaning.jpg');
}
HTML
<div id="image">
<img src="cleaning.jpg" />
</div>
My issue is that the image is being displayed at full size across the entire page!
How do I restrict it to the div it is being enclosed in?
Thanks!
As others have mentioned, you appear to be using two competing techniques - you've got a background-image on the container div, but you've also got the same image contained by the container div. Pick One.
For the background-image technique, you probably want to use something like:
#image {
width: 60%;
background:url('cleaning.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
The likelihood is that you need to manage the overflow of the element.
Try the following:
#image {
float:left;
width:60%;
background-image:url('cleaning.jpg') no-repeat;
overflow:hidden;
}
The issue you are having is that you set the parent div to have a width of 60%. You did not give the specific image element for that div a specific width. I hope this helped, I'm not 100% sure that I am correct but that is what this site is for, a question and answer format to help you work your bugs out. The issue how I see it is that you are setting the width of the specific div, and adding a background image to it which would not require you to set a <img></img> element. So in reality you have two copies of the image in the same spot, one which is 60% width, the second full size image covers the other. You must set the <img></img> element's width.
CSS:
#image {
float: left;
width: 60%;
/* No need for a background image to be set if you're using <img></img> */
}
#image img {
width: 100%;
}
Im not sure why you have a background image and a normal image. Background images will never scale with the container. I think your problem however is you need to set your image width as well. try
CSS
#image {
float:left;
width:60%;
background-image:url('cleaning.jpg');
}
#image img {
width:100%;
}
HTML
<div id="image">
<img src="cleaning.jpg" />
</div>
Example with http://jsfiddle.net/f5Ls3/
Example without http://jsfiddle.net/f5Ls3/1/