gpt4 book ai didi

css - 是否可以将一组 html 元素分组以便它们一起移动?

转载 作者:太空狗 更新时间:2023-10-29 13:26:45 25 4
gpt4 key购买 nike

当浏览器尺寸改变/在不同尺寸的设备上时,我需要一组在语义上都相关的 html 元素保持在一起并在一个 block 中移动。也就是说,如果其中一个元素由于宽度不足以包含整个分组而移动到下一个“行”,则所有元素都应该向下移动。

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"

所以总而言之,如果“行”中的第二个“列”没有足够的空间,而不是保留第一个“列”并将第二列中的元素向下移动到下一个“行” ,"第一列中的应该坚持其 sibling ,并始终与他们保持在同一“行”(我将“行”和“列”放在引号中,因为我没有使用 html 表,并且它们存在仅在虚拟意义上)。

如果您发现这有点难以想象(我不怪您),请查看 fiddle :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>

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 对元素进行分组(或者如果您想使用 section,也可以)。借助 CSS 的一点提示,您可以在包装器中对元素进行分组。不幸的是,没有比 keep together 这样的属性,但您可以执行以下操作:

section.wrapper {
min-width: 400px; /* Minimum width of your wrapper element */
overflow: hidden;
display: inline-block;
}

min-width 帮助您保持元素在 wrapper 中的顺序。选择最适合您情况的值。
overflow 的值hidden 让您的包装器能够理解并添加内部 float 元素的宽度和高度值。
display with value inline-block 让所有包装器彼此相邻排序,只要有足够的空间,如果没有,包装器跳转到另一行。

http://www.w3schools.com/为理解和学习 CSS、HTML 和一般网络技术提供了很好的资源。非常有用。

编辑
在我编辑时,min-widthwidthma​​x-width

更适合这种情况

关于css - 是否可以将一组 html 元素分组以便它们一起移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17128784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com