Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrink everything in a div

Tags:

html

css

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.

like image 589
Jordan Schnur Avatar asked Apr 12 '14 05:04

Jordan Schnur


People also ask

Can CSS shrink-to-fit a DIV-container to equal the width of its content?

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.

How to set a Div size according to its content using CSS?

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.

How to center the content within a <Div> Div?

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.

What is the use of flex shrink?

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.


1 Answers

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>
like image 185
T J Avatar answered Oct 02 '22 18:10

T J