gpt4 book ai didi

html - 更改 float 元素的顺序

转载 作者:行者123 更新时间:2023-11-28 15:56:43 26 4
gpt4 key购买 nike

我有如下的div代码

div {
float: left;
width: 33.33%;
height: 50px;
}

#container {
width: 100%;
}

#title,
#image,
#descr {
display: inline-block;
}

#title,
#descr {
float: right;
}

@media only screen and (max-width: 480px) {
div {
width: 100%;
}
}
<div id="title">This is a title</div>
<div id="image">This is an image</div>
<div id="descr">This is a description</div>

我想要做的是,安排 div,以便在桌面上首先显示图像,然后是标题和描述。像这样:

[This is an image] [This is a title] [This is a description]

但是,当我将标题和描述向右浮动时,我得到的结果是:

[This is an image] [This is a description] [This is a title]

此外,请注意,虽然我可以更改 HTML 中 div 的顺序以获得我想要的顺序,但我希望标题在移动设备上排在第一位,然后是图像和描述,因此是顺序。

编辑:请注意布局是通过 float div 来响应的。我正在寻找一种无需完全移除 float 的解决方法。

jsfiddle

最佳答案

我会使用 flex 来做这件事,因为它会更容易:

.container {
display: flex;
/* make things line up in a column */
flex-direction: column;
}

.container>div {
width: 100%;
height: 50px;
}


/* do mobile first and media query for larger */

@media only screen and (min-width: 481px) {
.container {
/* make things line up in a row */
flex-direction: row;
}
.container>div {
width: 33.33%;
}

#title {
order: 2;
}
#image {
order: 1;
}
#descr {
order: 3;
}
}
<div class="container">
<div id="title">This is a title</div>
<div id="image">This is an image</div>
<div id="descr">This is a description</div>
</div>

关于html - 更改 float 元素的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50723116/

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