flex+margin布局方法

简介: flex+margin布局方法

cf890196ad874aaf93c605aa73ae9679.png

方式:【具体代码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>

e1ee878b707840ac803d9730fa9f649a.png

【具体代码】

<!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>

7895bac337e346d29df78aa8985b0a50.png

【代码具体实现】:

<!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>

93703c258b9b4270b64a8cc4370d8301.png

【代码实现】:

<!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>


相关文章
|
11月前
|
前端开发 算法 Java
(CSS)使用Flex布局,帮助你快速了解各种基本的Flex布局属性以及帮你让元素快速达到布局中的指定位置!
(CSS)使用Flex布局,帮助你快速了解各种基本的Flex布局属性以及帮你让元素快速达到布局中的指定位置!
315 1
|
11月前
|
前端开发 算法 Java
【CSS】前端三大件之一,如何学好?从基本用法开始吧!(六):全方面分析css的Flex布局,从纵、横两个坐标开始进行居中、两端等元素分布模式;刨析元素间隔、排序模式等
Flex 布局 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性。它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现。 2009年,W3C 提出了一种新的方案----Flex 布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。 一、Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。
598 0
|
设计模式 容器
13.HarmonyOS流式卡片列表实现指南:Flex多行布局详解
在现代移动应用开发中,流式卡片列表是一种常见且实用的UI设计模式。它能够自适应屏幕宽度,在有限空间内高效展示多个内容项。本教程将详细讲解如何使用HarmonyOS的ArkUI框架中的Flex组件实现一个灵活的流式卡片列表,重点关注多行布局与对齐策略的应用。
479 2
|
开发者 UED 容器
07.精通HarmonyOS Flex对齐:从基础到高级布局技巧(上)
在HarmonyOS Next的ArkUI框架中,Flex容器提供了强大而灵活的对齐系统,使开发者能够精确控制子元素在容器中的排列方式。掌握这些对齐技术,是构建专业级用户界面的关键。
443 0
|
UED 容器
5.HarmonyOS Next开发宝典:掌握Flex布局的艺术
Flex布局(弹性布局)是HarmonyOS Next中最强大的布局方式之一,它提供了一种更加高效、灵活的方式来对容器中的子元素进行排列、对齐和分配空间。无论是简单的居中显示,还是复杂的自适应界面,Flex布局都能轻松应对。
398 0
|
开发者 容器
鸿蒙开发:弹性布局Flex
在实际的开发中,需要掌握主轴与交叉轴的关系、换行规则及子元素属性,同时注意性能与兼容性问题,还有一点,Flex组件在渲染时存在二次布局过程,因此在对性能有严格要求的场景下建议使用Column、Row代替。
425 10
鸿蒙开发:弹性布局Flex
|
UED 容器
10.HarmonyOS Next布局进阶:嵌套Flex容器与空间分配策略
在HarmonyOS Next的ArkUI框架中,Flex布局是构建用户界面的核心技术之一。通过嵌套使用Flex容器,我们可以创建复杂而灵活的界面结构,满足各种应用场景的需求。本教程将深入探讨如何在HarmonyOS Next中使用嵌套Flex容器实现复杂布局,以及如何合理分配和控制空间。
447 0
|
前端开发 UED 容器
使用 Flex 布局实现垂直居中效果
【10月更文挑战第7天】
1768 57
|
前端开发 UED 容器
在 CSS 中使用 Flex 布局实现页面自适应时需要注意什么?
【10月更文挑战第22天】在使用 Flex 布局实现页面自适应时,需要对其基本原理和特性有深入的理解,同时结合具体的布局需求和场景,进行细致的调整和优化。通过合理的设置和注意事项的把握,才能实现理想的自适应效果,提升用户体验。还可以根据实际情况进行更深入的探索和实践,以不断提升 Flex 布局的应用能力。