当浏览器大小改变/在不同大小的设备上时,我需要一组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"
Run Code Online (Sandbox Code Playgroud)
总而言之,如果没有足够的空间容纳"行"中的第二个"列",而不是保留在第一个"列"中并将第二列中的元素向下移动到下一个"行",在第一列中应该遵守其兄弟姐妹,并始终与它们保持同一"行"(我将"行"和"列"放在引号中,因为我没有使用html表,而这些只存在于虚拟感).
如果你发现这有点难以想象(我不怪你),请查看小提琴:http://jsfiddle.net/W7CYC/8/
注意:将分组包装到html5中并没有帮助.
这是代码:
HTML:
<div class="yearBanner">2013</div>
<section>
<a id="mainImage" class="floatLeft" href="https://rads.stackoverflow.com/amzn/click/com/0299186342" rel="nofollow noreferrer"><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="https://rads.stackoverflow.com/amzn/click/com/0299186342" rel="nofollow noreferrer"><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>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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");
Run Code Online (Sandbox Code Playgroud)
您可以简单地将项目分组div(或者如果您想使用section,也可以).通过一点CSS提示,您可以在包装内对项目进行分组.不幸的是,除了保持在一起之外没有这样的属性,但你可以做到:
section.wrapper {
min-width: 400px; /* Minimum width of your wrapper element */
overflow: hidden;
display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)
min-width帮助您按顺序将元素保留在包装器中.选择最适合您情况的值.
溢出值隐藏让您的包装器理解并添加浮动元素的宽度和高度值.只要有足够的空间,使用值inline-block
显示让所有包装器彼此相邻,如果没有,则包装器跳转到其他行.
http://www.w3schools.com/提供了很好的资源来理解和学习CSS,HTML和Web技术.很有用.
编辑
正如我编辑的那样,在这种情况下,最小宽度或宽度比最大宽度更适合
| 归档时间: |
|
| 查看次数: |
18365 次 |
| 最近记录: |