Background image doesn't show when defined in stylesheet - css

I am very new to css so this maybe a simple answer. I have 2 scenarios and 1 works the other doesn't. I hope someone can help me out.
WORKS:
<head>
<style type="text/css">
body {
background-image:url('views/default/images/home.jpg');
;}
</style>
</head>
DOESN'T WORK:
<head>
<link rel="stylesheet" type="text/css" href="views/default/home_style.css" />
</head>
In home_style.css>
body{
background-image:url('views/default/images/home.jpg');
margin-top: 0px !important;
padding-top: 0px !important;
}

It looks like your CSS file is in the views/default/ folder, while the image is in the views/default/images/ folder.
Define image paths in your CSS relative to the CSS file, not the HTML file that displays everything:
background-image:url('images/home.jpg');

In my case:
I had a CSS folder. So, inside my CSS folder I had my style.css so I was typing inside my style.css the following:
background: url('img/mybgimg.jpg')
instead of
background: url('../img/mybgimg.jpg')
...I hope this can help to anyone who is having the same issue.

Related

Page Background-Color doesn't work (CSS)

I'm a real noob at CSS/HTML, so please forgive me.
I tried to change the background page color on the CSS file linked to my html file, and it doesn't work. Whereas when I just flat out change it between the style tags in my HTML file, it works. What gives?
Plain and simple:
Ex1.css
<head>
<style>
body {
background-color: lightblue;
}
</style>
</head>
SamplePage.html
<!doctype html>
<html>
<head>
<title> Sample Page </title>
<link rel = "stylesheet" href="Ex1.css">
</head>
<body>
Hello. This is a sample Page.
</body>
</html>
Your HTML is correct, and links to your CSS correctly (assuming Ex1.css is in the same folder as your HTML).
Your CSS is almost correct; the only problem is that you shouldn't include any HTML tags in your CSS document. Ex1.css should only contain the actual CSS declarations themselves (body { }).
body {
background-color: lightblue;
}
<body>
Hello. This is a sample Page.
</body>
If in doubt, you can validate your CSS with W3's CSS Validator.
Hope this helps! :)
Reduce the contents of your Ex1.css file to this:
body {
background-color: lightblue;
}
(no HTML code in CSS files!)
your css file will just have. Plain and simple
body {
background-color: lightblue;
}
change your css file to this:
body {
background-color: lightblue;
}

Binding stylesheet.css into html doesn´t work

I really hope you can help me because I spent 2 days now looking for my mistake. I want to link an external css stylesheet into my html website but the website opens without css affecting it. I already validated both codes and checked spelling 1000 times, still not working. I tried with FF Chrome and IE:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset = "UTF-8"/>
<link rel="icon" type="image/png" href="bilder/favicon.png" />
<title>Die Hauskatze</title>
<link rel="stylesheet" href="css/stylesheet.css" type="text/css" />
</head>
Stylesheet:
#charset "UTF-8";
body{
background: url(bilder/background.jpg);
}
img {
border: 4px black;
}
your css file is probably is in another folder than your bilder folder. Be sure to specify this in the path.
If the css file is in a css folder for example, you need to go one folder up then in you bilder folder.
body{
background: url("../bilder/background.jpg");
}
for the border you need to specify the border 'style' (solid, dotted, etc)
border: 4px solid black;
https://www.w3schools.com/cssref/pr_border-style.asp
For starter you can try to add quotes to your URL here
body{
background: url(bilder/background.jpg);
}
change it to:
body{
background: url("bilder/background.jpg");
}
If that doesn't help it is quite likely that your path is wrong.
You can see it inside your developer console once the page is being loaded.
In case of wrong url you will get an error that the img file was not found.

How to use external css for a background image with gedit

I am taking a couple of classes about HTML and CSS but I am really having a difficult time understanding how to make a background image with external css. I am successful at external css in other ways, just not a background image.
This is my code for the HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Forms</title>
<link rel="stylesheet" type="text/css" href="/CSS/forms.css">
</head>
<body>
<div id="Pictures"</div>
</body>
</html>
And this is my code for the CSS:
#Pictures {
background-image:url(../home/brian/Desktop/Becky.JPG);
}
I can get it to work using the <style> tag but not external CSS.
File structure is key. Where is your .html file located in relation to your .jpg file?
I suggest that you use another folder (call it 'assets' or 'images', something like that) to put your images into, instead of putting them on your Desktop.
So, in your case your file structure would look like this:
index.html
css/forms.css
assets/Becky.jpg
The path in your external css file would then be:
#Pictures {
background-image: url(../assets/Becky.jpg);
}
#pictures {
background: url("http://www.w3schools.com/cssref/img_flwr.gif");
background-size: 80px 60px;
background-repeat: no-repeat;
padding-top: 40px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Forms</title>
<link rel="stylesheet" type="text/css" href="/home/red/Desktop/Web Projects/Test/style.css">
</head>
<body>
<div id="pictures"></div>
</body>
</html>

External CSS background-image not showing

I have an issue with the background-image property in an external CSS file. I can't seem to get the image to show up. Its been while since I have coded so maybe it's just me forgetting everything I know but I'm pretty sure I have the link right. The website is set up like this
/Root/
index.html
/styles/
webstyles.css
/img/
header.jpg
The background works when I use it inline so it's starting to annoy me.
HTML
<html>
<head>
<meta charset="UTF-8">
<title>MYSITE</title>
<link rel="stylesheet" type="text/css" href="/styles/webstyles.css">
</head>
<body>
<div class="headerBar">
<h1 class="Hlogo">Title</h1>
</div>
</body>
</html>
CSS
body
{
background:#999999;
}
headerBar
{
background: url(/styles/img/header.jpg);
}
Thanks for any help.
If this style is in webstyles.css, then you need to use this way.
.headerBar{
background: url('img/header.jpg');
}
Based on the folder structure, "styles/img/header.jpg" is still a wrong path.
And also, I'm not sure why, but you are missing . for CSS class selector when selecting the headerBar. Fix that one too.
You can use:
background: url("styles/img/header.jpg");
Replace
background: url(/styles/img/header.jpg);
with
background: url('img/header.jpg');
You can use:
.headerBar
{
background: url("styles/img/header.jpg");
}
since styles folder is the same level as your HTML file.
However, if your img folder is not placed inside styles folder (which makes more sense), then you want this path instead:
.headerBar
{
background: url('img/header.jpg');
}
Also you're missing . to target class .headerBar

Cannot get page to read the CSS page for a simple div

everything in my style sheet will work apart from divs. Kinda strange. I created a test page to try and see why it won't work but no joy.
If I include the div in a tag at the top of the page it will work. Just not if I link the css file itself. I will put my code below.
HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="boxed">
This should be in a box
</div>
And a clean stylesheet. With just the information for the div class.
#charset "utf-8";
/* CSS Document */
.boxed {
border: 1px solid green;
}
Hopefully, someone can point me in the right direction.
Instead of this, try just typing the full URL , so instead of "style.css" ,
type "http://yourWebsite.com/style.css" instead of "style.css"
<link type="text/css" rel="stylesheet" href="style.css" />
edit: also add type="text/css"
2nd edit:
you also need to have a title in your head, that is required. maybe it's causing this issue, maybe not
<head>
<title>This is my Title! </title>
</head>
Try this in your Style.css file:
.boxed {
border: 1px solid #008000;
display: inline;
}
check to see if you haven't misplaced any '}' or semi columns and i don't think you need the
#charset "utf-8" in your stylesheet since you already specified it in your head

Resources