html - 防止背景重复

标签 html css

如何防止背景重复?目前它重复了两次,但我需要背景填充整个页面而不重复(因为否则它看起来很奇怪!)。下面是我的代码,但如果需要,我可以提供站点链接。

HTML

<div class="container">
<img src="box.png" style="float: left;" />
<div class="navrow">
<ul>
<li class="navmenu" style="background: #6dc066;"><a href="index.php">Home</a></li>
<li class="navmenu" style="background: #a155e7;"><a href="about.php">About</a></li>
<li class="navmenu" onclick="shakeregister();" style="background: #ffa500;">Register</li>
<li class="navmenu" style="background: #3399ff;"><a href="play.php">Login</a></li>
<li class="navmenu" style="background: #ff6666; margin-right: 0px;"><a href="contact.php">Contact</a></li>
<div style="clear: left;"></div>
</ul>
</div>
<div style='clear:both'></div>
</div>

CSS

body {
 font-family: Titillium Web, sans-serif;
 background-image: url("images/polarvillebg.png");
}

h1, h2, h3 {
 font-weight: normal;
}

.container {
 width: 70%;
 margin: 0 auto;
 margin-top: 2%;
}

.navrow {
 float: right;
 text-align: center;
}

.navrow li {
 display: inline-block;
}

.navrow li a {
 color: #fff;
 text-decoration: none;
 width: 100%; 
 height: 100%;
 display: block;
}

.navmenu {
 height: 100px;
 width: 78px;
 color: #fff;
 margin-right: 4px;
 line-height: 100px;
 transition:height 0.5s; 
 -webkit-transition:height 0.5s; 
 box-shadow: rgb(204, 204, 204) 0px 0px 10px 0px;
 cursor: pointer;
}

.navmenu:hover {
 height: 107px;
}

最佳答案

CSS 规则:

background-size: cover;

将使背景图像足够大,以便它覆盖作为背景的元素。这也会产生不重复背景图像的副作用。


如果您之后仍然遇到背景重复问题,您可以使用:

background-repeat: no-repeat;

明确强制背景不重复。


因此,在您提供的代码中,完整的解决方案是将您的 body 规则更改为:

body {
 font-family: Titillium Web, sans-serif;
 background-image: url("images/polarvillebg.png");
 background-size: cover;
 background-repeat: no-repeat;
}

关于html - 防止背景重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34273778/

相关文章:

javascript - 使用 jQuery ScrollTop animate 在 Chrome 中出现奇怪的第一次滚动延迟

html - 为什么 `position:relative` 在 safari 和 opera 的开发工具中被计算为 `position: static`?

javascript - 使用数据库中数据的动态 HTML 元素

html - 侧边栏菜单不是全高

javascript - 如何使用 php 将任何文件(如 .docx、xslx、img、txt)转换为 PDF 预览?

css - 如何在带有 css 模块的 React 组件中使用 @apply

html - 如果行未满,css 网格对齐内容中心

javascript - 单击 <tr> 标记复选框时如何做到这一点

javascript - 在 JavaScript 中创建选项卡 Controller ?

javascript - 溢流体不起作用?