简体   繁体   English

div中的中心图像,具有相同的比例和隐藏的溢出,并始终填充

[英]Center image in div, with same proportions and overflow hidden, and always fill

In short, I want to center image in div, Keep proportions, have overflow hidden, and have the image always fill the div. 简而言之,我想在div中居中放置图像,保持比例,隐藏溢出,使图像始终充满div。

Here is an example of what i want: 这是我想要的示例:

~

我想要的是

~

is there any way to do this? 有什么办法吗?

Sounds like you may want to put the image as a background inside of a div as a container and then put the text or whatever inside another div?... maybe like this? 听起来您可能想将图像作为背景放在div作为容器的内部,然后将文本或其他内容放入另一个div?...也许是这样?

    .container{
        background:url('image.png') no-repeat;
        width:500px;
        height:500px;
        overflow:hidden;
    }
    .content{
        width:20%;
        height:10%;
        margin: 0 auto;
    }

    <div class="container">
        <div class="content">
             text here text here text here text here 
        </div>
    </div>

I believe you may possibly be looking for background-size: cover; 我相信您可能正在寻找background-size: cover; in combination with background-position: center . 结合background-position: center

This means your div would have CSS defined like the following; 这意味着您的div将具有如下定义的CSS;

div {
  background: url('image.png');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  width:100px;
  height:100px;
  overflow:hidden;
}

Hope that helps you out! 希望对您有所帮助!

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

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