[英]How to crop not stretch a background-image
I want to use a background image to my section
element with width:100%
and height:40%
. 我想对我的
section
元素使用width:100%
和height:40%
的背景图像。
So i used CSS3 and used this solution: 所以我用CSS3并使用了这个解决方案:
background-image: url(My_Local_Image);
background-repeat: no-repeat;
background-size: 100% 40%;
background-position: center top;
It worked nice! 效果很好!
My problem now is that i want the background-image to be cropped to fit the size i specify. 我现在的问题是我希望对背景图像进行裁剪以适合我指定的尺寸。 Now image is streched to fit.
现在图像被拉伸以适合。
Is there ant way that i can achieve this? 有没有可以实现这一目标的方法?
Unfortunately you cannot do something like 不幸的是你不能做这样的事情
background-size: cover 40%;
cause you'll loose the 100%
因为你会失去
100%
the solution would be so make a separate image container, and after it an element for your (I suppose) text, setting simply background-size: cover;
解决的办法是,制作一个单独的图像容器,然后在其后放置您(我想)文本的元素,只需设置
background-size: cover;
for the image container, setting also width: 100%;
对于图像容器,还设置
width: 100%;
and height : 40%;
height : 40%;
for the same. 对于相同的。
But what you can do is 但是你能做的是
<section>
<div class="sectionImage" id="first"></div>
<div class="sectionContent">1</div>
</section>
<section>
<div class="sectionImage" id="second"></div>
<div class="sectionContent">2</div>
</section>
<section>
<div class="sectionImage" id="third"></div>
<div class="sectionContent">3</div>
</section>
section{
background:#444;
position:relative;
margin:10px auto;
height:300px;
width:800px;
color:#fff;
}
.sectionImage{
width: 100%;
height:30%;
background: transparent none no-repeat center center;
background-size: cover;
}
.sectionContent{}
#first{
background-image: url('1.jpg');
}
#second{
background-image: url(2.jpg);
}
#third{
background-image: url(3.jpg);
}
如果我了解您要执行的操作,只需从背景尺寸中删除40%,图片就会以800x300px的尺寸填充div。
You must place the background container inside your main container. 您必须将背景容器放入主容器中。 After that you must provide width and height of main containter and make overflow:hidden.
之后,您必须提供主容器的宽度和高度并进行溢出:隐藏。
You can then play with main container's width and height to change crop size. 然后,您可以使用主容器的宽度和高度来更改作物大小。 (You can use width:40%; and height:100% too)
(您也可以使用width:40%; height:100%)
HTML: HTML:
<section id="first">
<div id="bg"></div>
</section>
CSS: CSS:
#first{
height:300px;
width:200px;
overflow:hidden;
}
#bg{
background-image: url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQOhLJod_xPxdgo339zfIJipPzOUZg9BunbT-ftIgDMiu2HLi0o');
background-repeat: no-repeat;
background-size: 100% 100%;
background-position: center top;
width:800px;
height:300px;
}
Use an inner div to get the crop effect: 使用内部div获得裁剪效果:
CSS CSS
#first{
height:300px;
width:800px;
}
#first div{
width:100%;
height:40%;
background-image: url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQOhLJod_xPxdgo339zfIJipPzOUZg9BunbT-ftIgDMiu2HLi0o');
background-repeat: no-repeat;
background-size:100%;
background-position: 50% 50%;
}
Use the :before
pseudo class 使用
:before
伪类
#first:before {
content: '';
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 40%;
background-image: url(image.jpg);
background-repeat: no-repeat;
background-position: center center;
}
This will give you many CSS options to deal with both bigger/smaller images, stretching/cropping, etc., without messing with the html 这将为您提供许多CSS选项来处理较大/较小的图像,拉伸/裁剪等,而不会弄乱html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.