How to write css in MVC (asp.net) for the application who goes in directory of domain and root?
i want to set a background image in a application and image goes in my appli开发者_StackOverflow中文版cation is
application /images/ --> images is here
but when application hosted the background image not worked because path i given based on current settings who broken when application hosted in subdomain.
how i can set it dynamically
You use relative paths in a .css file, they are relative to the location of the stylesheet, not the location of the current page:-
a.external
{
background-image:url('external-link.gif');
background-repeat:no-repeat;
}
will look for an image called 'external-link.gif' in the same directory as the .css file.
So if your directory structure looks like:-
- {Domain Root}
- Content
- Content\Style.css
- Images
- Images\MyImage.png
you could reference MyImage.png from Style.css with:-
url('../Images/MyImage.png')
Then if you moved your application into a subdirectory:-
- {Domain Root}
- Site
- Site\Content
- Site\Content\Style.css
- Site\Images
- Site\Images\MyImage.png
The same url ('../Images/MyImage.png') still finds the image because the relative path between the two resources has not changed.
See also:
- Using relative URL in CSS file, what location is it relative to?
精彩评论