简体   繁体   English

如何使用CSS3 / JavaScript缩放DIV元素及其子元素以适合父元素

[英]How to scale a DIV element and its children to fit in parent element with CSS3/JavaScript

Overview 总览

I have a #container element which houses a centered element .center . 我有一个#container元素,其中包含居中元素.center

This web-app will be fullscreen on a number of monitors of various width and height; 该网络应用将在各种宽度和高度的监视器上全屏显示; some portrait and some landscape. 一些肖像和一些风景。

The .center element is significantly smaller than its parent #container , and on larger screens, it sits in the middle with lots of space between the wrapper and the edges, both horizontally and vertically. .center元素比其父#container小得多,并且在较大的屏幕上,它位于包装器和边缘之间的中间水平和垂直方向都具有很大空间的中间位置。

Issue 问题

I am trying to figure out a way to expand the .center so that it scales (all its contents scaling equally, including images and font size) until either: 我试图找出一种扩展.center的方法,以便它缩放(所有内容均等缩放,包括图像和字体大小),直到出现以下两种情况:

  • its width is equal to the width of #container , or 其宽度等于#container的宽度,或者
  • its height is equal to the height of #container 它的高度等于#container的高度

I am aware of the CSS3 transform: scale() functions, however as the height and width of the #container is unknown and variable, I don't know how to scale all elements dynamically. 我知道CSS3 transform: scale()函数,但是由于#container的高度和宽度未知且可变,因此我不知道如何动态缩放所有元素。

How can this be achieved? 如何做到这一点?

Code

 #container { position: absolute; width: 100%; height: 100%; margin: 0; padding: 0; background: rgb(37, 66, 141); background: -moz-radial-gradient(center, ellipse cover, rgba(37, 66, 141, 1) 0%, rgba(0, 0, 0, 1) 54%); background: -webkit-radial-gradient(center, ellipse cover, rgba(37, 66, 141, 1) 0%, rgba(0, 0, 0, 1) 54%); background: radial-gradient(ellipse at center, rgba(37, 66, 141, 1) 0%, rgba(0, 0, 0, 1) 54%); filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#25428d', endColorstr='#000000', GradientType=1); } #wrapper { padding: 20px; box-sizing: border-box; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; position: relative; height: 100%; width: 100%; text-align: center; z-index: 2; } #wrapper .center { top: 50%; left: 50%; transform: translate(-50%, -50%); position: absolute; } * { color: white; font-family: "Lato", Helvetica, Arial, sans-serif; font-weight: 400; line-height: 1.62em; } #logo { margin: 40px 0; } #pulsor { -webkit-animation: pulsate 60s linear 0s infinite; -moz-animation: pulsate 60s linear 0s infinite; -animation: pulsate 60s linear 0s infinite; text-shadow: 0 0 8px #ccc; } @-webkit-keyframes pulsate { from { -webkit-transform: rotate(0deg); } to { -webkit-transform: rotate(360deg); } } @-moz-keyframes pulsate { from { -moz-transform: rotate(0deg); } to { -moz-transform: rotate(360deg); } } @keyframes pulsate { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } body, html { margin: 0; padding: 0; } 
 <div id="container"> <div id="overlay"></div> <div id="wrapper"> <div class="center"> <h1>Tonight at</h1> <div id="logo"><img src="http://design.ubuntu.com/wp-content/uploads/logo-ubuntu_cof-orange-hex.png" id="pulsor" width="280"></div> <h2>Theatre</h2> <p>19:30 - Les Miserables</p> <p>20:30 - A cheeky Indian take-away</p> <p></p> <h2>Devant Room</h2> <p>17:30 - My Favourite Books with JK Rowling</p> <p>18:00 - Look! I did a sleight of hand! Just kidding with Lennart Green</p> <p>19:30 - Old people grumbling about the number of steps to the Theatre</p> </div> </div> </div> 

If you want to scale everything you can use em or pt to set sizes for everything including image widths and padding. 如果要缩放所有内容,则可以使用em或pt设置所有内容的大小,包括图像宽度和填充。 Then you can use vw or vmin on the font size to scale everything according to screen size. 然后,您可以在字体大小上使用vw或vmin来根据屏幕大小缩放所有内容。

padding:1em;
width:30em;
font-size:2vw;

Here is an example: https://jsfiddle.net/5ezu2gLu/ 这是一个示例: https : //jsfiddle.net/5ezu2gLu/

Also, you can control the minimum and maximum sizes, on whatever you wish. 另外,您可以根据需要控制最小和最大尺寸。 More info here: http://madebymike.com.au/writing/precise-control-responsive-typography/ 此处提供更多信息: http : //madebymike.com.au/writing/precise-control-sensitive-typography/

Try these changes: 尝试以下更改:

Remove the center DIV from your HTML. 从HTML中删除center DIV。

And change the following CSS: 并更改以下CSS:

#container {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background: rgb(37, 66, 141);
  background: -moz-radial-gradient(center, ellipse cover, rgba(37, 66, 141, 1) 0%, rgba(0, 0, 0, 1) 54%);
  background: -webkit-radial-gradient(center, ellipse cover, rgba(37, 66, 141, 1) 0%, rgba(0, 0, 0, 1) 54%);
  background: radial-gradient(ellipse at center, rgba(37, 66, 141, 1) 0%, rgba(0, 0, 0, 1) 54%);
  filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#25428d', endColorstr='#000000', GradientType=1);
}           

#wrapper {
  padding: 20px;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  position: relative;
  height: 100%;
  width: 100%;
  text-align: center;
}

#logo img {
   max-width: 100%;
}

Here's an updated EXAMPLE 这是更新的示例

This basically scales the elements as the viewport width is reduced, however, I would advise using @media() queries to be more precise when scaling fonts etc. 这基本上会随着视口宽度的减小而缩放元素,但是,我建议在缩放字体等时使用@media()查询更为精确。

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

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