html - CSS preserve aspect ratio but fill parent div -
Actually, I have an image that I want to mask with a circle
< Code> & lt; Div class = "thumbnail-mask" & gt; & Lt; Img class = "thumbnail-picture" src = "/ image.jpeg" & gt; & Lt; / Div & gt;CSS (I'm using less) is very easy:
. Thumbnail-mask {width: @circleSize; Height: @ circle size; Boundary-radius: @ circle size / 2; -Vibit-boundary-radius: @ circle size / 2; -image-radius: @ circle size / 2; Hidden flurry; }
I have come to know how both parent parent images stand vertically and horizontally
. Thumbnail-pic {text-align: center; Status: Relative; Top: 50%; Webkit-Conversions: Translation Y (-50%); Conversions: Translator Y (-50%); // width: 100%; // height: auto; Height: auto; Width: 100%; }
But now the problem is height and width.
If I try
height: 100%; Width: 100%; Aspect ratio changes.
If height & gt; Width, then I want
width: 100%; Height: auto; If width> Height, I want
height: 100%; Width: Auto . In this way, the circle is completely filled. But it does not seem possible I have tried to set the
minimum-width: 100%; Minimum height: 100% but then without setting height or width, the image is too large.
$ (document) .ready (function ())
{$ ("Img"). Calculate each function () {// aspect ratio and place it in HTML data-attribute var aspectwrite = $ (this) .wide () / $ (this) .it (); $ (this) .data ( "Aspect ratio", aspect ratio); // conditional statement if (aspect ratio> gt; 1) {// image landscape is $ (this) .css ({height: "100%"})}} If (aspect Ratio & lt; 1) {// Image image is $ (this). CSS ({width: "100%"});}});});
Obviously this is not very elegant for pure CSS methods, but it will probably be more reliable.
Comments
Post a Comment