简体   繁体   English

使用CSS创建流畅的背景图片

[英]Create Fluid Background Image with CSS

I am trying to build a simple fluid image that can resize based on screen size. 我正在尝试构建一个可以根据屏幕尺寸调整大小的简单流体图像。

But I am having trouble to get the image smaller when the width of the browser gets smaller. 但是当浏览器的宽度变小时,我很难使图像变小。

HTML: HTML:

<main>
    <section class="slider-ctn">
        <figure class="logo"></figure>
    </section>
</main>

CSS: CSS:

.slider-ctn figure.logo {
    margin: auto;
    background-image: url('../Images/logo.200x100.png');
    width: 200px;
    height: 100px;
}

If you set the background-size to 100% 100% or cover the image will stretch to fit the container. 如果你设置background-size ,以100% 100%cover图像将延伸到适合的容器。 Remove the width (or restrict the width by setting the parent's width) and set height: 0; padding-top: 50% 删除宽度(或通过设置父级的宽度限制宽度)并设置height: 0; padding-top: 50% height: 0; padding-top: 50% on the figure to make the height of the figure half of the width, so the aspect ratio will match the image and be fluid. height: 0; padding-top: 50%在图形上height: 0; padding-top: 50% ,以使图形的高度为宽度的一半,因此长宽比将与图像匹配并且流畅。

 * {margin:0;padding:0;} .slider-ctn { margin: auto; width: 200px; } .slider-ctn figure.logo { background: url('https://dummyimage.com/200x100/000/fff') top left no-repeat; height: 0; background-size: cover; padding-top: 50%; } 
 <main> <section class="slider-ctn"> <figure class="logo"></figure> </section> </main> 

只需使用以下代码将以下内容添加到您的head标签中:

<meta name="viewport" content="width=device-width, initial-scale=1">

You should add contain as background-size to your CSS: 您应该将contain作为背景大小添加到CSS:

.slider-ctn figure.logo {
  margin: auto;
  background-image: url('../Images/logo.200x100.png');
  background-position:center center;
  background-repeat:no-repeat;
  background-size:contain;
  width: 200px;
  max-width:100%;
  height: 100px;
  display:inline-block;
}

Don't forgot to add position and repeat to background properties everytime when you use background images. 使用背景图像时,请不要忘记每次在背景属性中添加positionrepeat

Also I added max-width to fit within the parent container if it is smaller than the logo container. 另外,如果它小于徽标容器,我还添加了max-width以适合父容器。

Example here 这里的例子

NOTE: Don't use cover in background-size because that will cut your logo to fit only within width limits, contain will fit width and height . 注意:请勿使用背景尺寸的cover ,因为这样会切掉徽标,使其仅适合width限制, contain将适合widthheight

Another option using vw units and contain. 使用大众单位和包含的另一种选择。

If you know the aspect ratio of your image you can use vw (view width) as your units for width and height. 如果知道图像的纵横比,则可以使用vw(视图宽度)作为宽度和高度的单位。

I am using an image that has a 4x3 aspect ratio. 我正在使用长宽比为4x3的图像。 If I want the width to always be 80% of the screen I will use 80vw. 如果我希望宽度始终是屏幕的80%,我将使用80vw。 Then, my height will be 3/4 of my width, or 60vw. 然后,我的身高将是宽度的3/4,即60vw。

Then be sure to use contain for your background sizing. 然后,请确保使用contain作为背景大小。

Try this css and see if it satisfies your needs: 试试这个CSS,看看它是否满足您的需求:

.slider-ctn figure.logo { margin: auto; background-image: url('https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg'); background-size: contain; background-repeat: no-repeat; width: 80vw; height: 60vw; border: 5px solid red; }

I think setting up the max-width of the image to 100% should do the trick. 我认为将图像的最大宽度设置为100%应该可以解决问题。

.slider-ctn figure.logo {
  background-image: url('../Images/logo.200x100.png');
  background-repeat:no-repeat;
  background-size:contain;
  background-position:center;
  width: 200px;
  height: 100px;
}

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

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