Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale a div without scaling content

Tags:

html

jquery

css

I have this div (id="myDiv"), when the div is under css - scale all it's children are under scale as well. I need the div to enlarge but not the children, in a "creative way" i tried 2 things which didn't work.. how to scale a div without scaling it's content ?

HTML

<div id="myDiv">
<h3>Song</h3>
<p>
Strawberry fields forever
</p>
<div>

jQuery

$("#myDiv").css("transform", "scale(2,3)");   
//I tried to...
$("#myDiv").children().css("transform", "scale(-2,-3)");
$("#myDiv").children().css("transform", "scale(0,0)");
like image 611
Damkulul Avatar asked Sep 16 '14 14:09

Damkulul


People also ask

Why won't my Div scale to the center?

In that case, the problem is that the transform origin is set to 50% 50% by default. This is good for rotations (you typically want to rotate something around its center) but not really for scaling; your div gets shrunk down to the center of where it would originally be. (with the proper prefixes) to the style for #toBeScaled.

Is there a way to scale things with CSS?

But things aren’t so happy if there are a bunch of HTML element children just doing what regular ol HTML elements do. Let’s say this is the kind of thing we’re after: CSS alone can’t really do this. But CSS is still the answer! transform: scale (); is what we need. It scales things perfectly proportionally.

What happens if I scale my content to 0?

If you scale to 0, your content will hide and if you scale to negatie value, it will flip. Child elements will always inherit scale from its parent so setting scale for children to 1 wont help either.

What does it mean to scale the content of an element?

The HTML element creates an inline frame for embedding a third-party content. Scaling the content of an element may be needed if you want to display an object in the area not matching its original size.


1 Answers

You need to calculate backwards for the child elements.

For instance, you are scaling the div by 200% in the x axis...to reduce the children back to 100% you need to scale down to 50% (0.5)

Ditto for the y-axis 300%...therefore 33% (0.3)

CSS

.scaled { /* the parent */
    transform: scale(2,3);
    transform-origin:top left;
 }

.scaled * { /* the children */
   transform: scale(.5, .33); 
}

Jsfiddle Demo

NOTE: As you can see there are transform-origin issues as well and other spacing issues youl will need to address.

like image 199
Paulie_D Avatar answered Oct 30 '22 08:10

Paulie_D