是否可以对一组html元素进行分组,以便它们一起移动?

前端之家收集整理的这篇文章主要介绍了是否可以对一组html元素进行分组,以便它们一起移动?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当浏览器大小改变/在不同大小的设备上,我需要一组html元素,这些元素在语义上都相关,保持在一起并在块中移动.也就是说,如果其中一个元素移动到下一个“行”,因为它们的宽度不足以包含整个分组,所有元素都应该向下移动.

IOW,这有点像文字处理文档中的一些项目分组所具有的“保持在一起”属性.

更具体一点,说我有以下元素的集合:

  1. 1) an anchor tag,filling out a first "column"
  2. 2) a collection of tags,to the right of the anchor tag,consisting of:
  3. (a) a div,followed by a <br/>
  4. (b) a cite,followed by a <br/>
  5. (c) another div,followed by a <br/>
  6. (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:

  1. <div class="yearBanner">2013</div>
  2. <section>
  3. <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>
  4.  
  5. <div id="prizeCategory" class="category">BIOGRAPHY</div>
  6. <br/>
  7. <cite id="prizeTitle" class="title">Son of the Wilderness: The Life of John Muir</cite>
  8.  
  9. <br/>
  10. <div id="prizeArtist" class="author">Linnie Marsh Wolfe</div>
  11. <br/>
  12. <img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
  13. <img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
  14. <img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
  15. </section>
  16. <section>
  17. <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>
  18.  
  19. <div class="category">BIOGRAPHY</div>
  20. <br/>
  21. <cite class="title">Son of the Wilderness: The Life of John Muir</cite>
  22.  
  23. <br/>
  24. <div class="author">Linnie Marsh Wolfe</div>
  25. <br/>
  26. <img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
  27. <img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
  28. <img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
  29. </section>

CSS:

  1. body {
  2. background-color: black;
  3. }
  4. .floatLeft {
  5. float: left;
  6. padding-right: 20px;
  7. padding-left: 5px;
  8. }
  9. .yearBanner {
  10. font-size: 3em;
  11. color: white;
  12. font-family: Verdana,Arial,Helvetica,sans-serif;
  13. float: left;
  14. padding-top: 64px;
  15. }
  16. .category {
  17. display: inline-block;
  18. font-family: Consolas,sans-serif;
  19. font-size: 2em;
  20. color: Orange;
  21. width: 160px;
  22. }
  23. .title {
  24. display: inline-block;
  25. font-family: Calibri,Candara,serif;
  26. color: Yellow;
  27. width: 160px;
  28. }
  29. .author {
  30. display: inline-block;
  31. font-family: Courier,sans-serif;
  32. font-size: 0.8em;
  33. color: White;
  34. width: 160px;
  35. }

jQuery的:

  1. $('#prizeCategory').text("Changed Category");
  2. $('#prizeTitle').text("Changed Title that spans two rows");
  3. $('#prizeArtist').text("Changed Author and co-author");
  4. $('#mainImage img').attr("src","http://ecx.images-amazon.com/images/I/61l0rZz6mdL._SY300_.jpg");
  5. $('#mainImage img').attr("height","200");

解决方法

您只需使用div对项目进行分组(或者如果您想使用部分,也可以).通过一点CSS提示,您可以在包装内对项目进行分组.不幸的是,除了保持在一起之外没有这样的属性,但你可以做到:
  1. section.wrapper {
  2. min-width: 400px; /* Minimum width of your wrapper element */
  3. overflow: hidden;
  4. display: inline-block;
  5. }

min-width帮助您按顺序将元素保留在包装器中.选择最适合您情况的值.
溢出值隐藏让您的包装器理解并添加浮动元素的宽度和高度值.
只要有足够的空间,使用值inline-block显示让所有包装器彼此相邻,如果没有,则包装器跳转到其他行.

http://www.w3schools.com/提供了很好的资源来理解和学习CSS,HTML和Web技术.很有用.

编辑在编辑时,min-width或width在这种情况下比max-width更适合

猜你在找的HTML相关文章