简体   繁体   English

包含图像从容器div溢出的块锚

[英]Block Anchor containing Image overflows from container div

I have the following element markup: 我有以下元素标记:

<div class="container">
    <a href="#">
        <img src="http://placehold.it/250x500" alt="img" />
    </a>
</div>​

with this CSS: 使用此CSS:

.container { max-width: 150px; max-height: 150px; }
.container a { display: block; width: 100%; height: 100%; max-width: 100%; max-height: 100%; }

​ see this fiddle (I added colors and opacity in there to better show my problem) 看到这个小提琴 (我在那里添加了颜色和不透明度以更好地显示我的问题)

The problem is that while the anchor is fitting to his parent's width (max-width: 100%;) it's not fitting to his parent's height, it fits his child img's height instead, and I really don't know why, since I set max-height: 100%. 问题在于,虽然锚点适合其父辈的宽度(最大宽度:100%;),但不适合其父辈的身高,却适合其子代img的身高,而我真的不知道为什么,因为我设置了最大高度:100%。 If I change the container's max-height to just height it works, but I need it to be max-height . 如果我将容器的max-height更改为仅height,则可以使用,但是我需要将其设置为max-height

PS: I don't want to hide the container's overflow (overflow: hidden), I want the anchor to be the container's height! PS:我不想隐藏容器的溢出(溢出:隐藏),我希望锚点是容器的高度!

PPS: The problem is not in the anchor, every item I put in the container as the img's parent is gonna fit the img's height unless I give a fixed height to the container or to the img's father (in my case, the anchor), but I don't want to do that, 'cause I need it to be fluid. PPS:问题不在于锚点,作为img的父级,我放入容器中的每个项目都将适合img的高度,除非我为容器或img的父级(在我的情况下为锚点)设置了固定的高度 ,但我不想这样做,因为我需要它保持流畅。

PPPS: I partially answered the question: I can set the anchor's max-height to inherit, like in this example , but that's not gonna work for percentage max-height of the container, which is, unfortunately, what I need. PPPS:我部分回答了这个问题:可以像下面的示例一样设置锚点的max-height来继承,但是对于容器的max-height百分比来说,这是行不通的,不幸的是,我需要的是。

In your anchor you specified both max-height and height property. 在锚中,您同时指定了max-height和height属性。 Only either one is needed. 只需要一个。

I think you are trying to hide extra visible image inside the container... So, 我认为您正在尝试将多余的可见图像隐藏在容器内...因此,

Try Overflow : hidden in container div. 尝试溢出:隐藏在容器div中。

http://jsfiddle.net/E6aqZ/2/ http://jsfiddle.net/E6aqZ/2/

.container { max-width: 150px; max-height: 150px; background: red; overflow: hidden }

Okay, so here's the answer (a colleague of mine helped me with this one): 好的,这就是答案(我的一位同事帮助了我):

The .container should have a fixed width OR if it's a % height it should be inside a supercontainer with a fixed height, and the anchor 's height can be successfully set to 100%. .container应该具有固定的宽度,或者如果它是%高度,则它应该位于具有固定高度的 容器内部,并且锚点的高度可以成功设置为100%。

That's it! 而已! Thanks for everyone who contributed. 感谢大家的贡献。 Here's a jsFiddle of the result. 这是结果的jsFiddle

Here's the HTML: 这是HTML:

<div class="superContainer">
    <div class="container">
        <a href="#">
            <img src="http://placehold.it/250x500" alt="img" />
        </a>
    </div>
</div>

and here's the CSS: 这是CSS:

.superContainer { height: 200px; }
.container { width: 30%; height: 50%; background: red; font-size: 0; }
.container a { display: block; width: 100%; height: 100%; background: orange; opacity: 0.7; }
.container a img { opacity: 0.7; }​

Sorry if the answer doesn't suit your needs but I needed for a very specific case. 抱歉,如果答案不符合您的需求,但我需要一个非常具体的案例。 I'm eventually gonna upload a jQuery plugin of mine using this method! 我最终将使用此方法上传我的jQuery插件!

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

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