简体   繁体   English

像图像一样缩放div

[英]Scale div like an image

I am trying to scale content within a div based on it's width. 我正在尝试根据其宽度在div中缩放内容。

As an example, I have a div: 例如,我有一个div:

<div id="container"></div>

I have styling such as: 我的样式如下:

#container { margin: 0px auto; width: 810px; height: 800px; border: 1px solid #000; }

This presents me with a div 810px wide and 800px tall, nicely centered on screen with an outline. 这为我提供了810px宽和800px高的div,很好地在屏幕上居中显示了轮廓。

Let's say I have a banner graphic at the top which should scale with the div, so I have it's width at 100%. 假设我在顶部有一个横幅图形,该图形应随div缩放,因此宽度为100%。 Works great. 效果很好。

I have a background graphic for the container div itself set to scale with the width as well, working great. 我有一个容器div本身的背景图形,它也设置为与宽度成比例,效果很好。

What I need help with, is let's say I had a heading underneath the banner, but this font size needed to scale with everything else, based on the width of the container. 我需要帮助的是,假设我在横幅下方有一个标题,但是该字体大小需要根据容器的宽度与其他所有内容进行缩放。 How would I accomplish this? 我将如何完成?

I am also looking to add other elements such as buttons, which would need to scale. 我还希望添加其他元素,例如按钮,它们需要缩放。

At the end of the day, imagine and image with a width of 100%, and how it scales proportionately, perfectly. 一天结束时,请想象并想象宽度为100%的图像,以及它如何按比例完美地缩放。 This is how I need the container div and all its children to act, like an image. 这就是我需要容器div及其所有子元素充当图像的方式。 I hope this makes sense. 我希望这是有道理的。

I have looked at scaling text like in this link: http://jsfiddle.net/Aye4h/ 我已经看过缩放此链接中的文本: http : //jsfiddle.net/Aye4h/

This is the perfect behavior, but I need more than just text to scale. 这是完美的行为,但是我不仅需要缩放文本。

Scaling is a complicated matter as some content is vector based or otherwise rendered on-demand, and some content is raster based (eg, images). 缩放是一件复杂的事情,因为某些内容是基于矢量的或按需呈现的,而某些内容是基于栅格的(例如,图像)。 If you want to scale an entire element as if it was just an image, then have a look at transform: scale : 如果要缩放整个元素就好像它只是一幅图像,那么请看一下transform: scale

 #scaled { border: #f00 solid 5px; background: #0ff; height: 500px; margin: -125px; transform: scale(0.5); width: 500px; } 
 <h1>This is outside the scaled element</h1> <div id="scaled"> <h2>Inside the scaled element</h2> <p>An image:</p> <p><img src="http://i.imgur.com/3A1Loxw.jpg"></p> </div> 

Keep in mind that the transform is applied after the image has been laid out on the page so all content around it will think it's still at its original size. 请记住, transform是在图像放置在页面上之后应用的,因此周围的所有内容都将认为它仍然是原始大小。 You can work around this in many other ways, such as by using negative margin values (as I did in the example). 您可以通过许多其他方法来解决此问题,例如使用负margin值(如我在示例中所做的那样)。

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

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