CSS 在asp.net中设置背景图片(母版页)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13666540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting background image in asp.net (Master page)
提问by jomsk1e
I'm new in asp.net. And I am having trouble setting my background image. Here's the master page source:
我是asp.net的新手。我在设置背景图片时遇到了问题。这是母版页源:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Master.master.cs" Inherits="Master"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link rel="stylesheet" type="text/css" href="scripts/style.css"/>
<title>Tracker</title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="container">
<a href="#"><img src="images/cross-header.gif" alt="Insert Logo Here" width="100%" id="Insert_logo" style="background: #C6D580; display:block;" /></a>
<div class="sidebar1">
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">LINK</a></li>
<li><a href="#">LINK</a></li>
<li><a href="#"><span style="font-weight:italic">LINK</span></a></li>
<li><a href="#"><span style="font-weight:italic">LINK</span></a></li>
</ul>
</nav>
<p>SOME LABEL</p>
<p>SOME QUOTE HERE</p>
<p></p>
</div>
</div>
<footer>
<a href="#">LINK HERE</a> | <a href="#">LINK HERE</a> |
<a href="contact.php">CONTACT</a> | <a href="register.php">REGISTER</a> | <a href="login.php">LOGIN</a>
<address>
Copyright 2012 @JRC
</address>
</footer>
</div>
</form>
</body>
</html>
The image that I am trying to use is located at the folder image. I don't know whats wrong.
我尝试使用的图像位于文件夹图像中。我不知道怎么了。
And here's what the style.css source:
这是 style.css 源代码:
body
{
font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
background-image:url('images/background.jpg');
background-repeat:no-repeat;
background-attachment:scroll;
background-position:repeat-x;
}
I also tried this url('image/background.jpeg') etc. but still fails.
我也试过这个 url('image/background.jpeg') 等,但仍然失败。
回答by tjcertified
This largely depends on where your css file is located. If it is located in the same folder where the 'image' folder is located, then this looks correct. However, if you css file is in a different directory (say /css) then your link in your css file will not work. Instead, either change the css link to point to a complete link:
这在很大程度上取决于您的 css 文件所在的位置。如果它位于“图像”文件夹所在的同一文件夹中,则这看起来是正确的。但是,如果您的 css 文件位于不同的目录中(比如 /css),那么您的 css 文件中的链接将不起作用。相反,要么将 css 链接更改为指向一个完整的链接:
background-image: url('http://mysite.com/images/background.jpg)
I am unsure if the '~' link will work in a stylesheet. Since your css is in the scripts folder, you should be able to do this:
我不确定“~”链接是否可以在样式表中使用。由于您的 css 位于脚本文件夹中,您应该能够执行以下操作:
background-image: url('../images/background.jpg')
回答by Dany Gauthier
This worked for me :
这对我有用:
background-image: url(<%=ResolveClientUrl("~/images/background.jpg")%>);
回答by Tanmay Raman
Add master page, then link css script and paste the link of CSS using drag and drop method
添加母版页,然后链接css脚本并使用拖放方法粘贴CSS的链接
回答by Ravindra Bagale
it is getting confuse, because of your src
, it finding it in current_folder+src
, but your image folder is in root folder, for that to reach root_folder+src
you have to prefix your source with ~/
它变得混乱,因为你的src
,它在 中找到它current_folder+src
,但你的图像文件夹在根文件夹中,为了到达root_folder+src
你必须在你的源前加上前缀~/
background-image:url('~/images/background.jpg');