简体   繁体   English

为什么我的背景图片拒绝显示?

[英]Why is my background image refusing to show up?

I can't get a background image to show up in the body of my file. 我无法在文件正文中显示背景图片。 Here is the html- 这是html-

<body>
<div class="test">
<form action="search.php" method="GET">
    <input type="text" name="query" />
    <input type="submit" value="Search" />
</form>
</div>
</body>
</html>

and the css 和CSS

#test{
background-image:     url("https://www.planwallpaper.com/static/images/background-wallpapers-32_NRz0mTd.jpg");
}

I initially tried a local image and that didn't work so I tried a different image from the web. 最初,我尝试使用本地图像,但该方法不起作用,因此我尝试了与网络不同的图像。 It is a php document not sure if that matters or not. 这是一个php文档,不确定是否重要。

A class attribute is not interchange-able with id attribute. 类属性不能与id属性互换。

Your CSS currently says #test , meaning look for an id named "test". 您的CSS当前显示#test ,这意味着查找一个名为“ test”的ID。 # symbol refers to id. #符号代表ID。

But you currently have no <div id="test" , what you are using is <div class="test" . 但是您当前没有<div id="test" ,您正在使用的是<div class="test"

Recommendation 建议

In your HTML, change it to <div id="test" 在您的HTML中,将其更改为<div id="test"

Your style sheet is referencing an ID #test . 您的样式表引用了ID #test You have declared your div to be of the class test. 您已将div声明为类测试。 So like @Taig says you need to change one or the other to match. 因此,就像@Taig一样,您需要更改一个或另一个以匹配。

.test{
background-image: url("https://www.planwallpaper.com/static/images/background-wallpapers-32_NRz0mTd.jpg");
}

in css #value references an ID, .value references a class. 在css中,# #value引用一个ID, .value引用一个类。

When setting a div background with CSS, you need to specify a width and a height. 使用CSS设置div背景时,您需要指定宽度和高度。 For example, an image with 380x380px: 例如,一张380x380px的图片:

#test{
    background-image:     url("https://www.planwallpaper.com/static/images/background-wallpapers-32_NRz0mTd.jpg");
    width:380px;
    height:380px;
}

Your div id / class must be the same as on your stylesheet. 您的div ID /类必须与样式表上的相同。

So if your div is an id it must be #test {} on the stylesheet, and if it is class it must be .test {}. 因此,如果您的div是id,则它必须是样式表上的#test {} ,如果是class,则必须是.test {}.

But you can not just pick and make it into class or id. 但是,您不能只是选择并将其放入类或ID。 A class must be used more then once and can be bind to other tags. 一个类必须使用一次以上,然后才能绑定到其他标签。

An id is bind once to a tag. ID一次绑定到标签。

http://www.html5-tutorials.org/html-basics/id-and-class/ http://www.html5-tutorials.org/html-basics/id-and-class/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM