I have the following reference to my css stylesheet:
<link rel="stylesheet" type="text/css" href='#routes.Assets.versioned("stylesheets/main.css")' media="screen" />
The stylesheet is located at /public/stylesheets/main.css.
And has this content:
body {
background-image: #routes.Assets.versioned("images/deer.jpg");
}
I'm not getting any errors, but no image appears in the background.
Am I doing something wrong?
Public resources (assets) are not processing by the template engine, so
body {
background-image: #routes.Assets.versioned("images/deer.jpg");
}
never processed.
Here is the similar question:
Can you reference images from css without using relative paths?
Solutions
Use the static path
body {
background: url("/assets/images/deer.jpg");
}
The /assets part depends on your routes setting, /assets is a default.
Write CSS in the template file, so it would be one that returns but your controller (not one in the public folder)
Related
I have an index.html and style.css. I currently use the inline css but I want to use a stylesheet. I have this code in my index.html just before my </head> tag.
<link rel="stylesheet" href="/style.css">
and then i have this in my style.css
body {
background-color: yellow;
}
note both of those files are located in public_html. But whenever I open the index.html the background stays white and does not change to yellow.
how can i link the style.css to index.html ?
You need to give your CSS at least a height and width.
Try adding:
height: 100%;
width: 100%;
If you're working locally, the /style.css reference will try to load the file from the root of your filesystem. Same thing applies if you're loading from a subfolder in a domain, it will try to look for the file at the root of the domain folder.
If you change it to <link rel="stylesheet" href="style.css"> (notice the missing slash) it will then try to find the style.css file in the folder relative to the index.html file, which is the same.
The background will then be yellow.
The main thing is you need to have relative file path to your stylesheet.In your case imagining you have style.css inside css folder you need to do
<link rel="stylesheet" href="style.css">
Remember / path is for absolute path of file. This means that it
represents your file path from the root directory. And without / the file path becomes relative
Since you said you were beginner this may be another issue. Maybe be you haven't specified where to apply your style
body{
background-color:yellow;
}
Try adding
height:1000px;
width:100%;
You cannot give percentage to body height because body has no parent.
And add
<link rel="stylesheet" href="style.css">
to <head> of html file.
If your html page has nothing in it (text or images) then it is difficult to see any bg color since it has no height/width, as mentioned above.
Insert some dummy text to see if this is the case.
If you only have one css file, it is not unusual to have it in the same folder as the index.html file, and in this case change the code to:
This may be the case since you indicate that both files are in the same root folder, so delete the "/" since that would mean the css is not being applied.
It is worth naming the stylesheet to something relevent to the site you are working on, such as catstyle.css or lollipopstyle.css so that, if you have many css files open in your text editor, you can clearly see which one is for which site.
I am assuming also that you have the skeletal structure of the html "bare bones" complete as shown here.
Make sure to put the link to the stylesheet inside the section, say, after the meta-charset line.
I have created a jsp file called top.jsp and a css file called top.css. My problem is that my top.jsp doesn't show the background color specified in top.css.
Code in top.css
#CHARSET "ISO-8859-1";
body {
background-color: blue;
}
Code in top.jsp
<link rel="stylesheet" type="text/css" href="css/top.css"/>
What do I need to do in order to make the background color blue?
Here is a picture of my file structure:
Your <link rel="stylesheet" type="text/css" href="css/top.css"/> statement is looking for a css folder inside the same folder as the top.jsp file. This means it's looking for your css in the wrong place: currently it's looking for -> /jsp/css/top.css
You actually want it to go up one level in the folder structure to be in the webapp folder and then into the css folder so you need to change your href to href="../css/top.css" (the .. takes you up to the webapp folder and out of the jsp folder).
I want to apply single font for each html pages through out my website.
So far I tried this:
* { font-family:Nyala; }
but this works for only one page.
You should include this CSS style to each html page you wish it to take affect in.
Each HTML page should include this:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
And the mystyle.css is:
* { font-family:Nyala; }
Note: see #Raptor comments in order to improve your code.
Move * { font-family:Nyala; } in to a external css file like style.css and call it to the head section of the all html file you want like below.
<link type="text/css" rel="stylesheet" href="style.css" />
Don't forget to give exact file path where you keep your .css file.
Add Below css
body{font-family:Nyala;}
To make a css global you can place it in one common css file and make it's reference on every page.
Another point is if you are using master page in your website then you can place the reference of that css file once in the master page and it will automatically inherited on every content page which have master page applied.
Maybe a very newbie question.
I have a working site (apache, php, mysql etc) and links everywhere:
<img src="/images/x.gif" />
<link rel="stylesheet" href="/css.css" />
<link rel="stylesheet" href="/js.css" />
in a CSS:
.x { background-image: url('/images/y.gif');
and it all works. But as soon as I put the generate content to a standalone HTML file, to another directory (so it can be viewed, without apache), links gets broken. I know I should loose the / character - but what about CSS? I must change them .x { background-image: url('..images/y.gif'); ? I dont want to this relative tricking, I want absolute
i use this
.x { background-image : url('../images/y.gif');
Change '/ to './
.x { background-image: url('./images/y.gif');
I have a stylesheet defined in the index.html as
<link rel="stylesheet" type="text/css" href="css/template.css">
Inside the template.css, I have declared
body{
background-image: url(images/background.png);
background-repeat: repeat;
}
Why is the image not getting displayed? The path is correct
The path in the CSS should be relative to where the CSS file is located, not from where the page where the CSS file is being loaded from.
Try:
background-image: url(../images/background.png);
When you are starting out, this can seem counter intuitive, but it's actually a good design as your CSS file can then be called from any page in any folder and the references to the images will not be broken.