简体   繁体   English

在将内容对齐div右下角时使用隐藏的溢出?

[英]Using overflow hidden while aligning the contents the bottom right of a div?

I have a div at 200px wide x 150px height. 我有一个div,宽度为200px x高度为150px。 The image in the div will be bigger. div中的图像将更大。 I want to align the image inside to the bottom right corner while using the overflow hidden setting on the rest of the image. 我想在其余图像上使用溢出隐藏设置时,将图像内部对齐到右下角。 So only the bottom right corner of the image should be visible. 因此,只有图像的右下角应该可见。

I can't figure out how to use relative positioning in this case because there will be different sized images in the div. 在这种情况下,我无法弄清楚如何使用相对定位,因为div中会有不同尺寸的图像。 I need whatever image is in the div to automatically lock alignment to the bottom right regardless of size. 我需要div中的任何图像以自动将对齐方式锁定到右下角,而不管大小如何。 Is this possible? 这可能吗?

Here's my code: 这是我的代码:

 <div align="right" valign="bottom" style="width:200px; height:105px; border: 1px dotted black; overflow: hidden;"> <div style="position:relative; bottom:0px; right:0px;"> <img src="https://www.catster.com/wp-content/uploads/2017/08/A-fluffy-cat-looking-funny-surprised-or-concerned.jpg" width="300" height="300" /> </div> </div> 

For the example I have the image set to 500x300px but in the finished product this will be pulling in different images. 对于示例,我将图像设置为500x300px,但在最终产品中,它将拉入不同的图像。

You can't do it using position relative to the image. 您无法使用相对于图片的位置来完成此操作。

But you can use the relative and absolute together. 但是您可以同时使用relativeabsolute

If the parent has the position: relative and the child has the position: absolute , the child will use the parent's area as limits to self positioning 如果父母的position: relative ,而孩子的position: absolute ,则孩子将使用父母的区域作为自我定位的限制

Try this: 尝试这个:

 <div align="right" valign="bottom" style="width:200px; height:105px; border: 1px dotted black; overflow: hidden; position: relative"> <div style="position:absolute; bottom:0px; right:0px;" > <img src="https://www.catster.com/wp-content/uploads/2017/08/A-fluffy-cat-looking-funny-surprised-or-concerned.jpg" width="300" height="300"/> </div> </div> 

You can use absolute position for image and set its right and bottom properties to 0. 您可以使用图像的绝对位置并将其right和bottom属性设置为0。

 .container { width: 200px; height: 150px; position: relative; overflow: hidden; } img { position: absolute; right: 0; bottom: 0; } 
 <div class="container"> <img src="https://www.catster.com/wp-content/uploads/2017/08/A-fluffy-cat-looking-funny-surprised-or-concerned.jpg" alt=""> </div> 

In order to position an element relatively to its parent, you need first to set position: relative on the parent container. 为了相对于父元素定位元素,首先需要在父容器上设置position: relative

HTML File : HTML档案:

<div class="outer">
    <div class="inner">
        <img src="https://www.catster.com/wp-content/uploads/2017/08/A-fluffy-cat-looking-funny-surprised-or-concerned.jpg" width="300"
        height="300" />
    </div>
</div>

CSS File : CSS文件:

.outer {
    width: 200px;
    height: 105px;
    border: 1px dotted black;
    overflow: hidden;
    position: relative;
}

.inner {
    position: absolute; 
    bottom: 0px; 
    right: 0px;
}

I've provided a JSFiddle to help you with that. 我提供了一个JSFiddle来帮助您。

Hope that helps! 希望有帮助!

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

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