当浏览器大小改变/在不同大小的设备上,我需要一组html元素,这些元素在语义上都相关,保持在一起并在块中移动.也就是说,如果其中一个元素移动到下一个“行”,因为它们的宽度不足以包含整个分组,所有元素都应该向下移动.
IOW,这有点像文字处理文档中的一些项目分组所具有的“保持在一起”属性.
更具体一点,说我有以下元素的集合:
- 1) an anchor tag,filling out a first "column"
- 2) a collection of tags,to the right of the anchor tag,consisting of:
- (a) a div,followed by a <br/>
- (b) a cite,followed by a <br/>
- (c) another div,followed by a <br/>
- (d) two or three anchor tags that are aligned side-by-side at the bottom of the second "column"
总而言之,如果没有足够的空间容纳“行”中的第二个“列”,而不是保留在第一个“列”中并将第二列中的元素向下移动到下一个“行”,在第一列中应该遵守其兄弟姐妹,并始终与它们保持同一“行”(我将“行”和“列”放在引号中,因为我没有使用html表,而这些只存在于虚拟感).
如果你发现这有点难以想象(我不怪你),请查看小提琴:http://jsfiddle.net/W7CYC/8/
注意:将分组包装到html5中并没有帮助.
这是代码:
HTML:
- <div class="yearBanner">2013</div>
- <section>
- <a id="mainImage" class="floatLeft" href="http://rads.stackoverflow.com/amzn/click/0299186342"><img height="240" width="160" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg"></a>
- <div id="prizeCategory" class="category">BIOGRAPHY</div>
- <br/>
- <cite id="prizeTitle" class="title">Son of the Wilderness: The Life of John Muir</cite>
- <br/>
- <div id="prizeArtist" class="author">Linnie Marsh Wolfe</div>
- <br/>
- <img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
- <img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
- <img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
- </section>
- <section>
- <a class="floatLeft" href="http://rads.stackoverflow.com/amzn/click/0299186342"><img height="240" width="160" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg"></a>
- <div class="category">BIOGRAPHY</div>
- <br/>
- <cite class="title">Son of the Wilderness: The Life of John Muir</cite>
- <br/>
- <div class="author">Linnie Marsh Wolfe</div>
- <br/>
- <img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
- <img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
- <img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
- </section>
CSS:
- body {
- background-color: black;
- }
- .floatLeft {
- float: left;
- padding-right: 20px;
- padding-left: 5px;
- }
- .yearBanner {
- font-size: 3em;
- color: white;
- font-family: Verdana,Arial,Helvetica,sans-serif;
- float: left;
- padding-top: 64px;
- }
- .category {
- display: inline-block;
- font-family: Consolas,sans-serif;
- font-size: 2em;
- color: Orange;
- width: 160px;
- }
- .title {
- display: inline-block;
- font-family: Calibri,Candara,serif;
- color: Yellow;
- width: 160px;
- }
- .author {
- display: inline-block;
- font-family: Courier,sans-serif;
- font-size: 0.8em;
- color: White;
- width: 160px;
- }
jQuery的:
- $('#prizeCategory').text("Changed Category");
- $('#prizeTitle').text("Changed Title that spans two rows");
- $('#prizeArtist').text("Changed Author and co-author");
- $('#mainImage img').attr("src","http://ecx.images-amazon.com/images/I/61l0rZz6mdL._SY300_.jpg");
- $('#mainImage img').attr("height","200");
解决方法
您只需使用div对项目进行分组(或者如果您想使用部分,也可以).通过一点CSS提示,您可以在包装内对项目进行分组.不幸的是,除了保持在一起之外没有这样的属性,但你可以做到:
- section.wrapper {
- min-width: 400px; /* Minimum width of your wrapper element */
- overflow: hidden;
- display: inline-block;
- }
min-width帮助您按顺序将元素保留在包装器中.选择最适合您情况的值.
溢出值隐藏让您的包装器理解并添加浮动元素的宽度和高度值.
只要有足够的空间,使用值inline-block显示让所有包装器彼此相邻,如果没有,则包装器跳转到其他行.
http://www.w3schools.com/提供了很好的资源来理解和学习CSS,HTML和Web技术.很有用.
编辑在编辑时,min-width或width在这种情况下比max-width更适合