简体   繁体   English

CSS background-size:1000x100图形的封面和背景位置

[英]CSS background-size: cover and background positioning for a 1000x100 graphic

I have a layout issue I can't seem to figure out. 我有一个布局问题,我似乎无法弄清楚。 It involves a background image in a div that is 100% width of a viewport. 它涉及div中的背景图像,该背景图像是视口宽度的100%。 The background-image is 1000px wide, but I want it to always stretch to fill the width of the div mentioned above, whether the viewport is 200px wide or 600px wide or 1000px wide or 1600px wide. 背景图像的宽度为1000像素,但是无论视口是200像素还是600像素,还是1000像素还是1600像素,我都希望它始终拉伸以填充上述div的宽度。 Another requirement is that the graphic should keep its aspect ratio unless it's less than 50px tall, at which time the graphic should remain at a minimum height of 50px and kept centered horizontally within the div. 另一个要求是,除非高度小于50像素,否则图形应保持其宽高比,此时图形应保持最小高度为50像素,并在div内水平居中。 Which means that the narrower the viewport gets, the less and less you can see of the graphic itself -- until you can see only the center of it. 这意味着视口越窄,您看到的图形就越少-直到您只能看到图形的中心。

I'm using background-size: cover, which works beautifully for when the viewport is >= 500px (ie, the background-graphic is at the minimum height of 50px). 我使用的是background-size:cover,当视口> = 500px(即背景图形的最小高度为50px)时,它的效果很好。 However, I don't know how to keep the graphic centered as a background-image at widths below 500px. 但是,我不知道如何在宽度小于500像素时将图形作为背景图像居中放置。

Is there a CSS rule that can handle this? 是否有可以处理此问题的CSS规则? Or do I need to use a media query and/or javascript to make this happen? 还是我需要使用媒体查询和/或JavaScript来实现这一目标?

Rather than background-size: cover; 而不是background-size: cover; try background-size: 100% auto; 尝试background-size: 100% auto; to base it off the width of it's container and not the height. 使其基于容器的宽度而不是高度。 It still follow it's aspect ration but doesn't worry about the height anymore. 它仍然遵循其长宽比,但不再担心高度。

Hello your question is not very clear. 您好,您的问题不是很清楚。 But you could use this to have a Background stretched and covered.You can set a fixed and centered background on it, then adjust it's size using background-size. 但是您可以使用它来拉伸和覆盖背景。您可以在背景上设置固定和居中的背景,然后使用background-size调整其大小。

html { 
  background: url (images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

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

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