gpt4 book ai didi

html - CSS Responsive - 更改 div 的顺序

转载 作者:行者123 更新时间:2023-11-28 04:47:55 25 4
gpt4 key购买 nike

#div1 {
position: relative;
max-width: 100%;
height: 100px;
background-color: green;
color: white;
}

#div2 {
position: relative;
max-width: 100%;
height: 100px;
background-color: red;
color: white;
}

#div3 {
position: relative;
max-width: 100%;
height: 100px;
background-color: blue;
color: white;
}

@media (max-width: 400px) {

#div1 {
position: absolute;
max-width: 100%;
height: 100px;
background-color: green;
color: white;
}

#div2 {
position: absolute;
left: 0;
max-width: 100%;
height: 100px;
background-color: red;
color: white;
}

#div3 {
position: relative;
max-width: 100%;
height: 100px;
background-color: blue;
color: white;
}

}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title></title>
<meta charset="utf-8" />

<body>

<div id="div1">
<h1>Box 1</h1>
</div>

<div id="div2">
<h1>Box 2</h1>
</div>
<div id="div2">
<h1>Box 3</h1>
</div>

</body>
</html>

我可以根据浏览网站的设备类型更改 div 的顺序吗?我正在尝试创建响应式设计。我创建了这个非常简单的示例,它基本上有 3 个 div。它们在桌面上的顺序为 1、2、3。我在我的 CSS 中添加了媒体查询来处理移动设备。我希望顺序从 2、1、3 更改。我确信必须有一种方法可以仅使用 css 来完成此操作,但我对它还不够熟悉。有任何想法吗?谢谢

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title></title>
<meta charset="utf-8" />

<style>
#div1 {
position: relative;
max-width: 100%;
height: 100px;
background-color: green;
color: white;
}

#div2 {
position: relative;
max-width: 100%;
height: 100px;
background-color: red;
color: white;
}

#div3 {
position: relative;
max-width: 100%;
height: 100px;
background-color: blue;
color: white;
}

@media (max-width: 400px) {

#div1 {
position: absolute;
max-width: 100%;
height: 100px;
background-color: green;
color: white;
}

#div2 {
position: absolute;
left: 0;
max-width: 100%;
height: 100px;
background-color: red;
color: white;
}

#div3 {
position: relative;
max-width: 100%;
height: 100px;
background-color: blue;
color: white;
}


}
</style>

</head>


<body>

<div id="div1">
<h1>Box 1</h1>
</div>

<div id="div2">
<h1>Box 2</h1>
</div>
<div id="div2">
<h1>Box 3</h1>
</div>

</body>
</html>

最佳答案

使用flex显示元素并使用order改变顺序

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title></title>
<meta charset="utf-8" />

<style>
#div1 {
position: relative;
max-width: 100%;
height: 100px;
background-color: green;
color: white;
}

#div2 {
position: relative;
max-width: 100%;
height: 100px;
background-color: red;
color: white;
}

#div3 {
position: relative;
max-width: 100%;
height: 100px;
background-color: blue;
color: white;
}

@media (max-width: 400px) {
body{
display:flex;/*
flex-direcion:row;*/
}
#div2{
order:-1;
}

#div1 {/*
position: absolute;*/
max-width: 100%;
height: 100px;
background-color: green;
color: white;
}

#div2 {/*
position: absolute;
left: 0;*/
max-width: 100%;
height: 100px;
background-color: red;
color: white;
}

#div3 {
position: relative;
max-width: 100%;
height: 100px;
background-color: blue;
color: white;
}

}
</style>

</head>


<body>

<div id="div1">
<h1>Box 1</h1>
</div>

<div id="div2">
<h1>Box 2</h1>
</div>
<div id="div3">
<h1>Box 3</h1>
</div>

</body>
</html>

方法 2:-(适用于所有浏览器)

添加仅在 max-width:400px 时显示的隐藏 div 并隐藏原始 div。它甚至可以在 IE-9 和更低版本中工作

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title></title>
<meta charset="utf-8" />

<style>
#div1, #div1-alter {
position: relative;
max-width: 100%;
height: 100px;
background-color: green;
color: white;
}
#div1-alter{
display:none;
}

#div2 {
position: relative;
max-width: 100%;
height: 100px;
background-color: red;
color: white;
}

#div3 {
position: relative;
max-width: 100%;
height: 100px;
background-color: blue;
color: white;
}

@media (max-width: 400px) {

#div2{
order:-1;
}

#div1, #div1-alter {/*
position: absolute;*/
max-width: 100%;
height: 100px;
background-color: green;
color: white;
}

#div2 {/*
position: absolute;
left: 0;*/
max-width: 100%;
height: 100px;
background-color: red;
color: white;
}

#div3 {
position: relative;
max-width: 100%;
height: 100px;
background-color: blue;
color: white;
}
#div1{
display:none;
}
#div1-alter{
display:inherit;
}
}
</style>

</head>


<body>

<div id="div1">
<h1>Box 1</h1>
</div>

<div id="div2">
<h1>Box 2</h1>
</div>
<div id="div1-alter">
<h1>Box 1</h1>
</div>
<div id="div3">
<h1>Box 3</h1>
</div>

</body>
</html>

关于html - CSS Responsive - 更改 div 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850994/

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