简体   繁体   English

如何垂直放置重复Y图像背景?

[英]How to position vertically a repeat-y image background?

I have this code: 我有以下代码:

background:url("someimage.gif") repeat-y 50px right;

the problem here is that I need the background to start 50px from top so there is a 50px space between the image and the top but that wont happen. 这里的问题是我需要背景从顶部开始50px,以便图像和顶部之间有50px的空间,但是不会发生。 I know very well that the image would position right if not repeated but I need it to be repeated vertically. 我很清楚,如果不重复,图像将定位正确,但是我需要垂直重复图像。 Is there any way around? 有什么办法吗? How can I achieve that space between top and the start of the repeated image? 如何获得顶部和重复图像开始之间的距离?

There a way to fake it if there is nothing in that top space. 如果顶部空间中没有任何内容,则有一种伪造方法。

Apply 50px (or your chosen value) of padding-top to the div. 将50px(或您选择的值)的padding-top应用于div。

The apply the background as usual ( repeat-y ) but clip the background to the content-box of the div. 像往常一样应用背景( repeat-y ),但是将背景裁剪到div的content-box

Background-clip @ MDN 背景片段@ MDN

 div { background: url(http://placekitten.com/50/50) repeat-y; padding-top: 50px; height: 50vh; background-clip: content-box; width:50vw; margin:2em auto; border:1px solid red; } 
 <div></div> 

OR 要么

Use a position pseudo-element suitably sized/positioned 使用适当大小/位置的位置伪元素

 div { height: 50vh; width: 50vw; margin: 2em auto; border: 1px solid red; position: relative; } div:before { content: ''; position: absolute; width: 100%; height: calc(100% - 50px); background: url(http://placekitten.com/50/50) repeat-y; top: 50px; left: 0; } 
 <div></div> 

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

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