简体   繁体   English

像设置背景位置一样在div内居中放置图像

[英]Center image inside a div like setting background position

Are there any ways that we can set a tag inside a div perfectly center no matter what is the width and height of that div? 无论该div的宽度和高度如何,是否有任何方法可以在div的完美中心内设置标签? In other way, I want to set an image position inside a div tag like a center background. 换句话说,我想在div标签内设置图像位置,例如中心背景。 For example: 例如:

.image-wrap {
  width: 50vw;
  height: 720px;
  background: url('some-images.jpg') no-repeat center center / auto 100%;
}

I want to set an image inside a div like a background above with auto width and 100% height so that all the important content in an image will be in the center of the div. 我想在div内设置一个图像,例如上面的背景,并设置自动宽度和100%的高度,以便图像中的所有重要内容都将位于div的中心。

<div class="image-wrap">
  <img src="some-images.jpg" alt="some image here"/>
</div>

Thank you very much! 非常感谢你!

You can center it easily using flex property. 您可以使用flex属性轻松地将其居中。 Demo here 在这里演示

 .image-wrap { height: 400px; display: flex; align-items: center; justify-content: center; border: dotted 1px #CCC; } 
 <div class="image-wrap"> <img src="http://lorempixel.com/400/200" alt="some image here"/> </div> 

You could do it like so: 您可以这样做:

 .image-wrap { width: 50vw; height: 720px; background: url('some-images.jpg') no-repeat center center / auto 100%; position: relative; } img { position: absolute; top: 50%; left: 50%; margin-left: -200px; /* (EXAMPLE) - value should be half of the image width */ margin-top: -100px; /* (EXAMPLE) - value should be half of the image height */ } 
 <div class="image-wrap"> <img src="some-images.jpg" alt="some image here"/> </div> 

You could use transform: translate 您可以使用transform: translate

 .image-wrap { position: relative; height: 400px; text-align: center; border: 1px dashed gray; } .image-wrap img { position: relative; top: 50%; transform: translateY(-50%); } 
 <div class="image-wrap"> <img src="http://placehold.it/200" alt="some image here"/> </div> 

Now, if you want it to behave as background-size: auto 100% does, do like this 现在,如果您希望它的行为与background-size: auto 100%可以,请执行以下操作

 .image-wrap { position: relative; height: 200px; border: 1px dashed gray; overflow: hidden; } .image-wrap img { position: relative; height: 100%; left: 50%; transform: translateX(-50%); } 
 <div class="image-wrap"> <img src="http://placehold.it/600x100" alt="some image here"/> </div> <div class="image-wrap"> <img src="http://placehold.it/200x400" alt="some image here"/> </div> 

And here is a version behaving as background-size: cover does 这是一个表现为background-size: cover的版本background-size: cover

 .image-wrap { position: relative; height: 200px; overflow: hidden; border: 1px dashed gray; margin-bottom: 10px; } .image-wrap img { position: relative; min-height: 100%; min-width: 100%; top: 50%; left: 50%; transform: translate(-50%,-50%); } 
 <div class="image-wrap"> <img src="http://placehold.it/600x100" alt="some image here"/> </div> <div class="image-wrap"> <img src="http://placehold.it/200x400" alt="some image here"/> </div> 

And this version behaving as background-size: contain does 这个版本的background-size: contain

 .image-wrap { position: relative; height: 200px; overflow: hidden; border: 1px dashed gray; margin-bottom: 10px; } .image-wrap img { position: relative; max-height: 100%; max-width: 100%; top: 50%; left: 50%; transform: translate(-50%,-50%); } 
 <div class="image-wrap"> <img src="http://placehold.it/600x100" alt="some image here"/> </div> <div class="image-wrap"> <img src="http://placehold.it/200x400" alt="some image here"/> </div> 

 .parent-div { width: 50vw; height: 720px; position: relative; z-index: 1; } .image-wrap { background-position: 50% 50%; background-size: cover; position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: -1; } 
 you can use like this <div class="parent-div"> <div class="image-wrap" style="background-image: url('http://weknowyourdreams.com/images/nature/nature-02.jpg')"></div> </div> 

This is where FlexBox properties become very useful. 这是FlexBox属性变得非常有用的地方。 You can set align-items: center; 您可以设置align-items: center; on a container to (by default) vertically center any child elements. (默认情况下)将容器上的所有子元素垂直居中。 Here's an example: https://jsfiddle.net/ks62qtns/ 这是一个示例: https : //jsfiddle.net/ks62qtns/

The advantage of this is that you don't need to know the dimensions of any of the elements involved in the layout - which is very useful for responsive designs and/or dynamic content. 这样做的好处是您不需要知道布局中涉及的任何元素的尺寸-这对于响应式设计和/或动态内容非常有用。

Flex properties are reasonably well supported in modern browsers. Flex属性在现代浏览器中得到很好的支持 You might need fallbacks to support older versions of IE (if you need to) 您可能需要后备支持旧版本的IE(如果需要)

HTML: HTML:

<div class="container">
  <div class="content">
    <h1>
    Content. Any Content.
    </h1>
    <p>
    I might have anything in me!
    </p>
  </div>
</div>

CSS: CSS:

.container {
  display: flex;
  align-items: center;
  justify-content: center;

  background: #EEE;

  /* This is just to make a big container. You can set the dimensions however you like.*/
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.content {
  background: #89D485;
  padding: 2rem;
  text-align: center;
}

This is the most over thought out problem that most developers run into. 这是大多数开发人员遇到的最深思熟虑的问题。 If the object is inside another object you are able to just use margin: 0 auto; 如果对象在另一个对象内,则可以使用margin:0 auto; inside the CSS. 在CSS中。 This will make the left and right ways to line up correct. 这将使左右对齐方式正确。 This also then works for all media queries from small screen to large screens. 然后,这也适用于从小屏幕到大屏幕的所有媒体查询。

You can use jquery to calculate the width of the div and image. 您可以使用jquery计算div和图像的宽度。

$(img).css({
    position: "absolute",
    left: ($(img).parent().width() - $(img).width()) / 2
});

This would mean: ((width of div)-(width of image))/2 这将意味着:(((div的宽度)-(图像的宽度))/ 2

This would center image perfectly. 这将使图像完美居中。

just do it like this.set the container's property "text-align:center",make it is the inline-block element and 100% height,then can get what you want. 只需这样做。将容器的属性设置为“ text-align:center”,使其成为inline-block元素,高度为100%,即可获得所需的内容。

.image-wrap{
    width: 50vm;
    height: 720px;
    text-align: center;
}
.image-wrap img{
    display: inline-block;
    height: 100vm;
}

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

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