Setting background image in asp.net (Master page)

﹥>﹥吖頭↗ 提交于 2019-12-02 04:15:28

问题


I'm new in asp.net. And I am having trouble setting my background image. Here's the master page source:

<%@ 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:

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.


回答1:


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:

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:

background-image: url('../images/background.jpg')



回答2:


This worked for me :

background-image: url(<%=ResolveClientUrl("~/images/background.jpg")%>);



回答3:


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 ~/

background-image:url('~/images/background.jpg');



回答4:


Add master page, then link css script and paste the link of CSS using drag and drop method



来源:https://stackoverflow.com/questions/13666540/setting-background-image-in-asp-net-master-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!