flex+margin布局方法

news/2024/10/17 12:21:14/

元素上下左右居中
方式:【具体代码1】

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.box{width: 100%;height: 100vh;}.box1{width: 100%;height: 30vh;background-color: blue;display: flex;justify-content: center;align-items: center;}.content1{width: 50%;height: 50%;background-color: lightgray;}</style>
</head>
<body><div class="box"><div class="box1"><div class="content1"></div></div></div>
</body>
</html>

【具体代码2】:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.box{width: 100%;height: 100vh;}.box1{width: 100%;height: 30vh;background-color: blue;display: flex;justify-content: center;align-items: center;}.content1{width: 50%;height: 50%;background-color: lightgray;margin: auto;}</style>
</head>
<body><div class="box"><div class="box1"><div class="content1"></div></div></div>
</body>
</html>

绿色的盒子到右边
【具体代码】

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.box{width: 100%;height: 100vh;}.box1{width: 100%;height: 30vh;background-color: blue;display: flex;}.content1{width: 20%;height: 30%;background-color: lightgray;}.content2{width: 20%;height: 30%;background-color: lightgoldenrodyellow;}.content3{width: 20%;height: 30%;background-color: pink;margin-left: auto;}</style>
</head>
<body><div class="box"><div class="box1"><div class="content1"></div><div class="content2"></div><div class="content3"></div></div></div>
</body>
</html>

右边两个移动到右边
【代码具体实现】:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.box{width: 100%;height: 100vh;}.box1{width: 100%;height: 30vh;background-color: blue;display: flex;}.content1{width: 20%;height: 30%;background-color: lightgray;}.content2{width: 20%;height: 30%;background-color: lightgoldenrodyellow;}.content3{width: 20%;height: 30%;background-color: pink;margin-left: auto;}.content4{width: 20%;height: 30%;background-color: purple;}</style>
</head>
<body><div class="box"><div class="box1"><div class="content1"></div><div class="content2"></div><div class="content3"></div><div class="content4"></div></div></div>
</body>
</html>

在这里插入图片描述

【代码实现】:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.box{width: 100%;height: 100vh;}.box1{width: 100%;height: 30vh;background-color: blue;display: flex;}.content1{width: 20%;height: 30%;background-color: lightgray;}.content2{width: 20%;height: 30%;background-color: lightgoldenrodyellow;}.content3{width: 20%;height: 30%;background-color: pink;margin-left: auto;}.content4{width: 20%;height: 30%;background-color: purple;}.content5{width: 20%;height: 30%;background-color: lightgray;}.content6{width: 20%;height: 30%;background-color: lightgoldenrodyellow;}.content7{width: 20%;height: 30%;background-color: pink;}.content8{width: 20%;height: 30%;background-color: purple;}.content9{width: 20%;height: 30%;background-color: lightgray;}.content10{width: 20%;height: 30%;background-color: lightgoldenrodyellow;}.content11{width: 20%;height: 30%;background-color: pink;}.content12{width: 20%;height: 30%;background-color: purple;}.content13{width: 20%;height: 30%;background-color: lightgray;}.content14{width: 20%;height: 30%;background-color: lightgoldenrodyellow;}.content15{width: 20%;height: 30%;background-color: pink;}.content16{width: 20%;height: 30%;background-color: purple;}#item{/* --n:7;--$ap: calc((100%-50px*7) / 7/2);margin: 18px var(--$ap); */margin: 10px calc(100% - 50px*7)/7/2;}</style>
</head>
<body><div class="box"><div class="box1"><div class="content1" id="item"></div><div class="content2" id="item"></div><div class="content3" id="item"></div><div class="content4" id="item"></div><div class="content4" id="item"></div><div class="content5" id="item"></div><div class="content6" id="item"></div><div class="content7" id="item"></div><div class="content8" id="item"></div><div class="content9" id="item"></div><div class="content10" id="item"></div><div class="content11" id="item"></div><div class="content12" id="item"></div><div class="content13" id="item"></div><div class="content14" id="item"></div><div class="content15" id="item"></div><div class="content16" id="item"></div></div></div>
</body>
</html>

http://www.ppmy.cn/news/182663.html

相关文章

运动耳机哪个品牌好,推荐几款专业跑步耳机

如今&#xff0c;年轻人喜欢让音乐陪伴他们的生活。无论他们是散步还是锻炼&#xff0c;我们总能看到一些年轻人在锻炼时戴着耳机听音乐&#xff0c;陶醉于他们喜欢的音乐。一个好的运动耳机需要良好的稳定性和较长的耐用性来满足它。那么如何选择适合自己的运动耳机呢&#xf…

跑步耳机哪款好用,运动耳机品牌排行榜

运动有时会有些枯燥&#xff0c;显然你还需要选择合适的耳机。毫无疑问&#xff0c;运动耳机绝对是跑步音乐的最佳伴侣。如果是户外运动&#xff0c;骨传导耳机更安全。如果是室内运动&#xff0c;耳朵里的蓝牙耳机更有效。我给同学们&#xff0c;挑选了几种不同的运动耳机。希…

全球加速AEB「强标」,15万元以下车型或将「释放」巨量需求

目前&#xff0c;智能驾驶技术升级&#xff0c;主要是在帮助中高端车型提升产品竞争力&#xff0c;同时为车企构建未来软件付费盈利模式的转型。 但另一方面&#xff0c;基础辅助驾驶&#xff0c;尤其主动安全&#xff0c;比如&#xff0c;AEB&#xff08;自动紧急制动&#xf…

运动装备怎么选?运动装备品牌排行榜

随着越来越多的人加入到运动锻炼当中&#xff0c;其中的用户对于高质量运动装备的需求也在增大。跑步、健身、户外运动&#xff0c;如果运动装备的质量不好&#xff0c;那用户体验可太难受了&#xff01;运动装备可以减缓人们的疲劳感&#xff0c;提升幸福感&#xff0c;是不可…

跑步耳机哪个牌子好,运动耳机品牌排行榜前十名

我不知道什么时候开始&#xff0c;我们的生活、工作和学习的节奏越来越快。我们一出生&#xff0c;就在与时间赛跑&#xff0c;所以很多人已经在20多岁的时候处于亚健康状态!为了使自己远离亚健康并保持良好的身体状况&#xff0c;许多年轻人在早上或晚上调整一些时间&#xff…

游泳耳机哪个牌子好、最好的游泳耳机品牌排行榜

这么热的天气&#xff0c;玩水才是消暑纳凉最好的选择&#xff0c;可以在室内游泳馆通过游泳锻炼&#xff0c;也可以在海滨浴场游泳日光浴。这时选购一款能够胜任玩水的游泳耳机就十分必要了&#xff0c;利用音乐可以让原本枯燥乏味的游泳运动更有动力&#xff0c;让海边戏水、…

运动用品品牌排行榜,双十一运动好物选购清单

健身运动就像打游戏一样&#xff0c;如何区分你和其他玩家的差别呢&#xff1f;有时候靠身材&#xff0c;当然有时候也会拼装备&#xff0c;那么这些运动装备能否增加buff呢&#xff1f;是否值得入手呢&#xff1f;作为一名资深的运动爱好者&#xff0c;下面我就从实用角度聊一…

运动耳机品牌排行榜、2022五大最好的运动耳机品牌推荐

我们都知道运动耳机的出现是为了人们在运动中更好的使用,减少掉落、进水等问题而出现的,所以一般的运动耳机也都具备无线、防水以及防汗功能。常见的款式大多是挂耳式的,较为适合经常运动跑步和热爱健身的人群使用。那么,市面上琳琅满目的运动耳机那么多,究竟哪个好呢?今天小编…