简体   繁体   English

如何缩小html元素以适合固定大小的div?

[英]How to shrink html elements to fit in fixed-size div?

I have a div tag <div id="preview"></div> with a fixed width and height, and in the preview I'd like to display some html: h1, h2, p, block quotes, lists, all shrunk down so the entire chunk of html fits inside the preview, perfectly scaled and proportioned. 我有一个具有固定宽度和高度的div标签<div id="preview"></div> ,并且在预览中,我想显示一些html:h1,h2,p,块引号,列表,全部缩小向下,因此整个html块都可以放入预览中,可以完美缩放和缩放。 How would I go about it? 我该怎么办呢? Thanks! 谢谢!

Use CSS's zoom property: 使用CSS的zoom属性:

#preview { 
    zoom: 0.5; 
    /* For Firefox */
    -moz-transform: scale(0.5); 
    -moz-transform-origin: 0 0;
}

After more research it seems that the iframe is more appropriate for displaying html: instead of supplying an external link to a web page, you can load the iframe with raw html directly. 经过更多研究后,iframe似乎更适合显示html:您可以直接用原始html加载iframe,而不是提供网页的外部链接。 Also you can embed a stylesheet in the iframe and the stylesheet won't affect the rest of your site. 您也可以在iframe中嵌入样式表,该样式表不会影响网站的其余部分。 Perfect for themes. 完美的主题。 You can see the jsfiddle here. 您可以在这里看到j​​sfiddle

HTML HTML

<textarea id="editor"><p>This is plain html to keep it simple. In my 
application, this would be Markdown text and converted to html by a 
Markdown converter in javascript.</p></textarea>

<iframe id="preview"></iframe>​​​

JavaScript JavaScript的

var html = document.getElementById('editor').value;
head = '<style type="text/css">#inner {color: blue; margin: auto; zoom: 0.6; -moz-transform: scale(0.6); -moz-transform-origin: 0 0;}</style>';     
var body = '<div id="inner">' + html + '</div>';
var preview = document.getElementById('preview').contentWindow.document;
preview.head.innerHTML = head;
preview.body.innerHTML = body;​

CSS CSS

#editor, #preview {
    height: 300px;
    width: 200px;
}

textarea {
    resize: none;
}​

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

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