I'm working on a small project where I need to shrink a whole div, and it's children to the same ratio as when their big. I'm currently using the CSS3 functions and they are not working correctly.
So to sum, I just need to be able to shrink a whole div with the same ratio as before.
CSS: Shrink-to-fit a DIV to equal the width of its content. Shrinking a DIV-Container to fit the width of its content can be very useful sometimes, especially if you tried margin: 0 auto to center and found out it doesn’t work. Why not? Because DIVs are block elements and take all the space they can get.
Using inline-block property: Use display: inline-block property to set a div size according to its content. making web pages presentable. CSS allows you to apply styles to web pages. More each web page. Using fit-content property in width and height: In this method, we set the width and height property to fit-content value.
In the end, we want to present a way of centering the content within a <div> both horizontally and vertically. Here, besides the justify-content and display (with "flex") properties, you need the align-items property which will set the vertical alignment to the center.
Definition and Usage. The flex-shrink property specifies how the item will shrink relative to the rest of the flexible items inside the same container. Note: If the element is not a flexible item, the flex-shrink property has no effect.
You can make use of css3
transfrom's scale
property which scales the element in modern browsers.
Assume you have the following HTML
<div id='shrinkMe'>
<div id='shrink1' class='content'></div>
<div id='shrink2' class='content'></div>
</div>
and CSS
.shrink{
-webkit-transform:scale(0.5);
-moz-transform:scale(0.5);
-ms-transform:scale(0.5);
transform:scale(0.5);
}
script for calling a function
$('#shrinkMe').click(function(){ // or any other event
$(this).addClass('shrink');
});
$('#shrinkMe').click(function() { // or any other event
$(this).addClass('shrink');
});
#shrinkMe {
position: relative;
height: 200px;
width: 200px;
text-align: center;
background: cyan;
}
.content {
position: absolute;
height: 50px;
width: 50px;
}
#shrink1 {
top: 0;
left: 0;
background: blue;
}
#shrink2 {
right: 0;
bottom: 0;
background: yellow;
}
.shrink {
-webkit-transform: scale(0.5);
-moz-transform: scale(0.5);
-ms-transform: scale(0.5);
transform: scale(0.5);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='shrinkMe'>
<div id='shrink1' class='content'></div>
<div id='shrink2' class='content'></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With