简体   繁体   English

CSS背景重复-x,strech-y

[英]css background repeat-x, strech-y

I have a striped background with a gradient which I want repeated in x and stretched in y. 我有一个带渐变的条纹背景,我想在x中重复并在y中拉伸。 I thought this would work: 我认为这可以工作:

background: url(bg.jpg) repeat-x;
background-size: auto 100%;

But it either stretches in y or repeats in x, never both at same time: 但是它要么延展y,要么延展x,永远不会同时出现:

在此处输入图片说明 http://codepen.io/anon/pen/JCjEb http://codepen.io/anon/pen/JCjEb

Edit: Note that I cannot simply repeat in y since the striped background also have a gradient (dark in bottom, lighter at top). 编辑:请注意,我不能简单地在y中重复,因为条纹背景也具有渐变(底部为深色,顶部为浅色)。

You can just use background: url(bg.jpg) repeat; 您可以只使用background: url(bg.jpg) repeat; without background-size. 没有背景大小。 Here is the example . 这是例子

The problem is that when you set the background-size to auto 100% , it's going to stretch the whole image proportionally, thus making the stripes too wide and distorted. 问题是,当您将background-size设置为auto 100% ,它将按比例拉伸整个图像,从而使条纹变得太宽和扭曲。 Set the x part of the background-size to the width of the original image, and it won't stretch anymore. background-sizex部分设置为原始图像的宽度,它将不再拉伸。

.b {
  background: url(http://img.photobucket.com/albums/v63/promisedyouheaven/stripe2.gif) repeat;
  background-size: 35px 100%;
}

http://jsfiddle.net/BsAcY/ http://jsfiddle.net/BsAcY/

Instead of giving it width auto , give it the width of the image ( 36px ). 而不是给它宽度auto ,而是给它图像的宽度( 36px )。 http://codepen.io/thgaskell/pen/Bjsix http://codepen.io/thgaskell/pen/Bjsix

CSS 的CSS

.c {
  background-size: 36px 100%;
}

Try background: url(bg.png) center repeat-x; 尝试background: url(bg.png) center repeat-x;

Not sure about IE8 and below though, if that's a problem. 如果这是一个问题,则不确定IE8及以下版本。

.a { background: url( http://img.photobucket.com/albums/v63/promisedyouheaven/stripe2.gif ) repeat; .a {背景:url( http://img.photobucket.com/albums/v63/promisedyouheaven/stripe2.gif )重复; display:block; 显示:块; width:500px; 宽度:500像素; } }

Is that what you need? 那是你需要的吗?

Try this 尝试这个

** HTML ** ** HTML **

<div class="b"></div>

** CSS for bg image & gradient ** ** CSS用于BG图像和渐变**

.b {     /* unprefixed gradient for example only*/
  background: 
  linear-gradient(to bottom, rgba(0,0,0,1) 0%,rgba(255,255,255,0) 100%),
  url(http://img.photobucket.com/albums/v63/promisedyouheaven/stripe2.gif);

  background-repeat:repeat;
}

div {
  height: 300px;
  width: 200px;
  margin-right: 50px;
  border:1px solid grey;
}

Codepen Example Codepen示例

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

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