简体   繁体   English

具有最大宽度和最大高度的Div并保留比例(仅CSS)

[英]Div with max-width and max-height and retain proportions (CSS only)

OK here's a challenging one! 好,这是一个挑战! I really want to see if this can be accomplished with CSS only if possible. 我真的很想看看是否只有在可能的情况下才能使用CSS来实现。

I have a unique layout that requires images to be sized and positioned based on their parent container. 我有一个独特的布局,要求根据父容器调整图像的大小和位置。 If the image exceeds it's parent in height or width I need it to reduce size to fit. 如果图像在高度或宽度上超过其父级,则需要缩小尺寸以适合。 At the moment I'm using max-width and max-height together and it's working fine. 目前,我正在同时使用max-width和max-height,并且工作正常。 The image resizes to fit and of course keeps it's aspect ratio. 图像会调整大小以适合,并且当然会保持其宽高比。

Now here's the tricky part. 现在是棘手的部分。 I need to add a special shadow to this image that cannot be accomplished with CSS box shadows. 我需要为此图像添加一个特殊的阴影,而CSS框阴影无法实现。 The shadow uses PNG image. 阴影使用PNG图片。 The shadow needs to be sized and positioned in relation to the image - meaning it falls at the bottom of the image and it equals the width of the image. 阴影的大小和相对于图像的位置需要确定-这意味着阴影落在图像的底部并且等于图像的宽度。

Normally I would achieve the shadow with ::after and size and position it relative to it's parent element, which works perfectly Except it's parent is the image and images do not allow ::before or ::after. 通常,我将使用:: after来实现阴影,并相对于其父元素将其大小和位置定位,这可以完美地工作, 除非它的父元素是图片,并且图片不允许:: before或:: after。

So as far as I can tell, the only way to achieve this is to wrap the image in another container so I can use that container as the parent elements and positioning reference for the shadow layer. 据我所知,实现此目的的唯一方法是将图像包装在另一个容器中,以便可以将该容器用作父元素和阴影层的定位参考。 But I cannot find a way to make that container div behave in the same way as the image in terms of the max-width and max-height sizing while still maintaining it's aspect ratio. 但是我找不到一种方法来使容器div在最大宽度和最大高度大小方面保持与图像相同,同时仍保持其宽高比。

The best methods for maintaining aspect ratios use padding top, which works brilliantly when the width is the only important factor. 保持长宽比的最佳方法是使用padding top,当宽度是唯一重要因素时,该方法效果很好。 But the padding-top technique doesn't allow for the container to have a max-height. 但是padding-top技术不允许容器具有最大高度。

So I'm looking for a CSS technique that will allow a block element to maintain its aspect ratio, and have max-width and max-height at the same time. 因此,我正在寻找一种CSS技术,该技术将允许一个块元素保持其长宽比,并同时具有max-width和max-height。 Similar to how an image would behave in this situation. 类似于图像在这种情况下的行为。

I've scoured the internets for a solution and haven't seen anyone describe this exact situation. 我已经在互联网上搜寻了解决方案,还没有看到有人描述这种确切的情况。 Would be extremely grateful to anyone who can assist. 非常感谢任何可以提供帮助的人。

Added 1 Sept 2017: 新增2017年9月1日:

I should mention that it's more than just the shadow I need to position relative to the image. 我应该提到的是,这不仅仅是我需要相对于图像定位的阴影。 There are some other elements as well that need to be positioned in this way, and those other elements are not simple background images. 还有一些其他元素也需要以这种方式定位,而这些其他元素并不是简单的背景图像。 So while Lightbender's solution is great for the shadow, it doesn't solve the bigger issue at hand. 因此,尽管Lightbender的解决方案非常适合阴影,但它并不能解决当前更大的问题。 I need a container around the image that I can use as reference to position other child elements. 我需要在图像周围放置一个容器,以用作放置其他子元素的参考。

While before and after won't work (easily) but you can still use padding and a background image and it will work exactly the way your current setup works. 虽然之前和之后都无法(轻松),但是您仍然可以使用填充和背景图像,并且它将完全按照您当前设置的工作方式工作。

img.fancyshadow {
    height: auto;
    width: auto;
    max-width: 100%;
    max-height: 100%;
    padding: 0 10px 10px 0; /* adjust as needed */
    background: url('path/to/your/shadow');
    box-sizing: border-box;
}

I don't have a Mac handy, so I've only tested this in Firefox, Chrome, and IE, can anyone confirm Safari as well? 我没有Mac,因此我只在Firefox,Chrome和IE中进行了测试,有人还能确认Safari吗?

So I would like to see a simple example of what you are attempting as a starting point but you mentioned that the images need to be sized/positioned based upon the size of their container. 因此,我想以一个简单的示例作为起点,但您提到需要根据容器的大小调整图像的大小/位置。

Here is an starting example of something like that. 这是一个类似的例子。 Not sure if it can be modified to suit your issue. 不知道是否可以对其进行修改以适合您的问题。 Let me know and I can tweak. 让我知道,我可以进行调整。

When needing to have responsive images, I never use IMG tags. 当需要响应式图像时,我从不使用IMG标签。 Setting the background image in CSS provides much more control on responsive sites/apps. 在CSS中设置背景图片可对自适应网站/应用提供更多控制。

Documentation on background-size : 有关背景尺寸的文档:

cover Scale the background image to be as large as possible so that the background area is completely covered by the background image. cover将背景图像放大到尽可能大的程度,以使背景区域完全被背景图像覆盖。 Some parts of the background image may not be in view within the background positioning area 在背景定位区域内可能看不到背景图像的某些部分

contain Scale the image to the largest size such that both its width and its height can fit inside the content area 包含将图像缩放到最大尺寸,以使其宽度和高度都可以适合内容区域

 $(function() { $('.banner').resizable(); }); 
 .banner { background-image: url('https://s-media-cache-ak0.pinimg.com/originals/15/ae/a6/15aea601612443d5bddd0df945af6ffd.jpg'); background-size: cover; background-position: center; height: 175px; width: 100%; } p { color: #666; } .ui-resizable-se { box-shadow: -1px -3px 10px 3px white; } 
 <link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script> <h1>Resize the image using the black triangle at bottom right of image</h1> <p>Note how the image fills the container and the position is always centered (you can control where the position is, doesn't have to be in the center)</p> <div class="banner"> </div> 

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

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