简体   繁体   English

确保调整任何内容的大小以适应固定的div

[英]Ensure any content is resized to fit within a fixed div

Ok, so here's the problem: I have a div which is set to almost the entire screen size (on a mobile device), content will be placed within it, however the content itself is not under my direct control. 好的,所以这就是问题所在:我有一个设置为几乎整个屏幕尺寸的div(在移动设备上),内容将放在其中,但内容本身不在我的直接控制之下。 There will be no inline styles on images or links inside. 内部图像或链接上没有内联样式。 I need to ensure that: 我需要确保:

1 - Everything is visible within the div, and if there are long lines they are forced to wrap, or are simply cut-off. 1 - div中的所有内容都是可见的,如果有长行则会被迫包裹,或者只是被切断。

2 - Images should maintain their aspect ratio but be resized to stay within the confines of the div. 2 - 图像应保持其纵横比,但应调整大小以保持在div的范围内。

3 - The content should "fill" the space, becoming larger or smaller as needed. 3 - 内容应“填充”空间,根据需要变大或变小。

One last bonus: There will be a maximum of ONE image in the content, but there could be multiple links. 最后一个奖励:内容中最多只有一个图像,但可能有多个链接。

I've found some other answers on here, but nothing that meets this particular challenge. 我在这里找到了一些其他答案,但没有遇到这个特殊的挑战。

jQuery is an option, but I've found it sluggish on mobile devices, jQuery Mobile is an option, but same basic problem. jQuery是一个选项,但我发现它在移动设备上很慢,jQuery Mobile是一个选项,但同样的基本问题。

What's the best combination of CSS and Javascript to perform this task in the most efficient way possible. 什么是CSS和Javascript的最佳组合,以尽可能最有效的方式执行此任务。

#wrapper {
 width: 960px;
 margin: 0 auto;
 overflow: hidden;
}

.img_class {
 height:auto;
 width:auto;
 max-width:960px;
}

/*If you have a div inside your wrapper then you would need this*/

#wrapper .contained_div {
 width: auto;
 display: inline-block:
 margin: 0;
}

/*If you want to stack stuffs one after another in a column then use this div */
.column {
 margin: 0 10px;
 overflow: hidden;
 float: left;
 display: inline;
}

I don't think you'll need any form of JavaScript. 我认为你不需要任何形式的JavaScript。 But without a sample of what you're working with, this is my best guess: 但是如果没有您正在使用的样本,这是我最好的猜测:

#divId { height:auto; width:900px; white-space:normal; overflow:hidden; }
#divId img { height:auto!IMPORTANT; width:auto!IMPORTANT; max-width:900px; }

(Replace my width values with yours) (用你的宽度值替换我的宽度值)

This will format the text so it displays correctly within the div, but it may come out as one blob of text, but that might be what you want. 这将格式化文本,使其在div中正确显示,但它可能作为一个文本blob出现,但这可能是你想要的。 If you want formatting, you'll have to use jQuery to search the text within the div for line breaks and replace them with </p><br /><p> ... Just be sure to add opening and closing Paragraph tags. 如果你想要格式化,你将不得不使用jQuery搜索div中的文本换行符并用</p><br /><p>替换它们......确保添加开始和结束段落标记。

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

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