使用CSS3

时间:2017-02-21 21:28:20

标签: html css

我意识到有类似的问题,但我找不到合适的解释。我正在尝试了解具有多个重复背景的最佳CSS实践。基本上我希望能够重复背景图像以适应屏幕的整个高度和宽度,并在每个重复的背景图像内部使另一个div居中,但是这个以内部为中心的div应该每个背景图像只重复一次。

所以下面我设置了一个背景图片来填充整个视口并重复。但是,我试图做的所有事情都没有中心图像重复。我最终在整个视口中重复内部图像div,并且反对每个背景图像仅重复一次。

我知道我可以在其中设置一个带有img标签的div,然后根据我的需要设置样式,然后只需复制+粘贴HTML中的div,但这不是DRY,我想避免这种情况。我也知道你可以用CSS3声明多个背景,但到目前为止还没有任何效果。我不确定这是否是解决这个问题的正确方法,但我觉得CSS对于这种性质的东西真的很困惑。我不确定是否有完全不同的更好的方法来做到这一点,所以我感谢任何帮助,谢谢

<div id='bground'>
 <div id='center-image'></div>
</div>

<style>
#bground {
  height:100vh;
  width:100vw;
  background-image: url(./img);
  background-repeat: repeat;
}

#center-image {
???
}
<style>

1 个答案:

答案 0 :(得分:0)

图片

如果您打算重复两个图像,一个图像位于另一个图像的上方,则可以使用SVG作为居中图像。不幸的是,您需要为居中的图像使用数据URI。

基本上,您将使用SVG图像作为居中图像的容器。您需要使SVG的宽度和高度与基础图像的尺寸相匹配。然后,需要定位SVG内的较小图像,使其相对于下面的图像居中。注意SVG中图像元素的x,y,width和height属性。

以下示例使用多个背景图像。

这个问题的答案更详细地讨论了SVG方法:CSS, background-repeat distance

&#13;
&#13;
body {
  margin: 0;
}
#bground {
  height:100vh;
  width:100vw;
  background-image: url('data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20width%3D%22200%22%20height%3D%22300%22%3E%3Cimage%20x%3D%2280%22%20y%3D%22130%22%20width%3D%2240%22%20height%3D%2240%22%20xlink%3Ahref%3D%22data%3Aimage%2Fjpeg%3Bb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f%2FaAAgBAgEBPxCgDk3lNiiJX%2F%2FEACEQAQACAgIBBQEAAAAAAAAAAAEAESExQVFxEGGBobHR%2F9oACAEBAAE%2FELW0gqmyDDiYrR5lZuXo9KVbULukWV5B0pzUsrFUqlmyGZMLEtl8wBS6zwWtmuZz55VQ3LMSNlQbLcZilBff%2B0RlvhzNRXR3Q52y0I1bVzNQpLb5p6gGyHhB%2FZtwQwhRpbIkobZnuBFUklgL3Eiln3P%2F2Q%3D%3D%22%20%2F%3E%3C%2Fsvg%3E'), url('https://unsplash.it/200/300/?random');

/*
SVG image before URL encoding (required for IE/Edge)
data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="300"><image x="80" y="130" width="40" height="40" xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wgARCAAoACgDASIAAhEBAxEB/8QAGwAAAwEAAwEAAAAAAAAAAAAAAAQGAwECBQf/xAAXAQADAQAAAAAAAAAAAAAAAAABAgMA/9oADAMBAAIQAxAAAAG86acOV1s9MqB6A8n5WklJX+e1spUAXYuMNlgBjUw09AMf/8QAIRAAAgICAQQDAAAAAAAAAAAAAgMAAQQRBQYQEiETFBX/2gAIAQEAAQUClwpcZ77/AGkk+tHPCbqc3ksWJUW+n8mwO2rnqc0L4T23fAibGWIQj1LbUyFoXkDmfFP0jn//xAAZEQACAwEAAAAAAAAAAAAAAAAAAQMQESD/2gAIAQMBAT8BGOPecr//xAAYEQEBAQEBAAAAAAAAAAAAAAAAARAREv/aAAgBAgEBPwFHpUzqXP/EACMQAAEDAwMFAQAAAAAAAAAAAAEAAiEDEBESMTIEE0FxkSD/2gAIAQEABj8C/ZpNqDWPCg59XDKOZE4WxTqL4aZGVzb9sH9PqAAnSubvqfUe7LBGCthaV3GjfwiGDHq3/8QAHhAAAgICAwEBAAAAAAAAAAAAAAERITFBEFFhkXH/2gAIAQEAAT8haky4Yk6rhk9ldiBK0xsVPRDcN4kl5b+G6dVLEs/IN9V8Hk4zXQWCcOpK2NluSVmRnNR6PKc26JjoircsTUz/2gAMAwEAAgADAAAAEDOwo1gwYfv/xAAYEQEBAQEBAAAAAAAAAAAAAAABABEQYf/aAAgBAwEBPxC8yLXuss6dbL//xAAYEQEAAwEAAAAAAAAAAAAAAAABABARMf/aAAgBAgEBPxCgDk3lNiiJX//EACEQAQACAgIBBQEAAAAAAAAAAAEAESExQVFxEGGBobHR/9oACAEBAAE/ELW0gqmyDDiYrR5lZuXo9KVbULukWV5B0pzUsrFUqlmyGZMLEtl8wBS6zwWtmuZz55VQ3LMSNlQbLcZilBff+0RlvhzNRXR3Q52y0I1bVzNQpLb5p6gGyHhB/ZtwQwhRpbIkobZnuBFUklgL3Eiln3P/2Q==" /></svg>
*/
}
&#13;
<div id="bground">
</div>
&#13;
&#13;
&#13;

任意内容

如果您的目的是让您的内部div包含任意内容,那么您必须使用服务器端解决方案或客户端脚本。

这是一个使用html2canvas将div转换为图像的解决方案。然后将图像插入SVG,如上例所示。

&#13;
&#13;
var repeats = document.getElementsByClassName('background-repeat-centered');

for (var i = 0, max = repeats.length; i < max; i++) {
  var repeat = repeats[i],
      measureImage = new Image(),
      parentImage = repeat.parentElement,
      parentImageStyle = parentImage.currentStyle || window.getComputedStyle(parentImage, false),
      parentImageSrc = parentImageStyle.backgroundImage;

  measureImage.src = parentImageSrc.replace(/url\((['"])?(.*?)\1\)/gi, '$2').split(',')[0];
  
  measureImage.addEventListener('load', function () {
    var parentWidth = measureImage.width,
        parentHeight = measureImage.height;

    html2canvas(repeat, {
      onrendered: function(canvas) {
        var repeatWidth = canvas.width,
            repeatHeight = canvas.height,
            x = (parentWidth - repeatWidth) / 2,
            y = (parentHeight - repeatHeight) / 2;

        repeat.parentElement.style.backgroundImage = 'url(\'data:image/svg+xml,' + encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' + parentWidth + '" height="' + parentHeight + '"><image x="' + x + '" y="' + y + '" width="' + repeatWidth + '" height="' + repeatHeight + '" xlink:href="' + canvas.toDataURL() + '" /></svg>') + '\'), ' + parentImageSrc;
      }
    });

  }, false)
}
&#13;
.background-repeat-centered {
  visibility: hidden;
}

body {
  margin: 0;
}
#bground {
  height: 100vh;
  width: 100vw;
  background-image: url('http://lorempixel.com/200/300/');
}
#sample {
  background: Purple;
  color: White;
  display: inline-block;
  font-family: sans-serif;
  font-weight: bold;
  padding: 10px;
  text-align: center;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>

<div id="bground">
  <div class="background-repeat-centered" id="sample">
     Repeat after me!
  </div>
</div>
&#13;
&#13;
&#13;