html - 如何从浏览器中心添加纹理背景?

标签 html css

我在 html 主体部分添加了一个纹理背景图像,它重复了整个主体部分,但我希望这个纹理在浏览器中重复一半

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Background</title>
  <style>
    body{
      background:url('bg.png');
    }
  </style>
</head>
<body>
</body>
</html>

reference image - what I want

最佳答案

只需在绝对定位的 body 上使用伪元素即可。

宽度为 50%,高度为 100%,超过 50%。

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
body {
  min-height: 100%;
  position: relative;
}
body:before {
  content: '';
  position: absolute;
  left: 50%;
  height: 100%;
  width: 50%;
  background-image: url(http://lorempixel.com/image_output/abstract-q-c-25-25-1.jpg);
  z-index: -1;
}
h1 {
  text-align: center;
  color: red;
  margin: 0;
}
<h1>My Heading</h1>

关于html - 如何从浏览器中心添加纹理背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34289012/

相关文章:

html - self 关闭的 HTML/XML 标签的正确术语是什么?

javascript - 工具提示嵌套在<label>中,防止 'mousedown'触发输入:active

html - <fieldset> 样式不起作用

css - 为 div 重置继承的 margin-left

css - :hover pseudoclass doesn't work with universal selector *

html - 没有索引的 Pandas DataFrame 样式器 HTML 显示?

javascript - 使用二进制响应填充输入类型标签

javascript - MathJax 不使用 Angular 渲染

javascript - 我有一个粘性标题,当我们滚动页面时,标题内有 jquery 自动建议,然后自动建议也在滚动

javascript - 如何在我的两个 Electron 应用程序窗口上放置一个主题(深色/浅色模式)?