web前端之使用弹性和外边距进行网页布局、非常有用的小技巧、flex、margin、auto

2023-12-14 22:36:21


效果图

图中效果只需要flex和margin便可以实现。


flex_margin


html

<div class="w_100_ d_f h_168 p_6 bc_c0c0c0">
    <div class="w_50 h_50 bc_e75f57 m_a"></div>
</div>

<div class="w_100_ d_f p_6 mt_20 bc_c0c0c0">
    <div class="w_50 h_50 bc_e75f57"></div>
    <div class="w_50 h_50 bc_f2b64c"></div>
    <div class="w_50 h_50 bc_55bd4c ml_a"></div>
</div>

<div class="w_100_ d_f p_6 mt_20 bc_c0c0c0">
    <div class="w_50 h_50 bc_e75f57"></div>
    <div class="w_50 h_50 bc_f2b64c"></div>
    <div class="w_50 h_50 bc_55bd4c ml_a"></div>
    <div class="w_50 h_50 bc_e75f57"></div>
</div>

<div class="w_100_ d_f p_6 mt_20 bc_c0c0c0">
    <div class="w_50 h_50 bc_e75f57"></div>
    <div class="w_50 h_50 bc_f2b64c"></div>
    <div class="w_50 h_50 bc_55bd4c ml_a mr_a"></div>
    <div class="w_50 h_50 bc_e75f57"></div>
</div>

<div class="w_100_ d_f p_6 mt_20 bc_c0c0c0">
    <div class="w_50 h_50 bc_e75f57"></div>
    <div class="w_50 h_50 bc_f2b64c ml_a mr_a"></div>
    <div class="w_50 h_50 bc_55bd4c ml_a mr_a"></div>
    <div class="w_50 h_50 bc_e75f57"></div>
</div>

<div class="w_100_ d_f p_6 mt_20 bc_c0c0c0">
    <div class="w_50 h_50 bc_e75f57"></div>
    <div class="w_50 h_50 bc_f2b64c ml_a"></div>
    <div class="w_50 h_50 bc_55bd4c ml_a"></div>
    <div class="w_50 h_50 bc_e75f57 ml_a"></div>
</div>

<div class="w_100_ d_f fw_w p_6 mt_20 bc_c0c0c0">
    <div class="item w_50 h_50 bc_e75f57"></div>
    <div class="item w_50 h_50 bc_f2b64c"></div>
    <div class="item w_50 h_50 bc_55bd4c"></div>
    <div class="item w_50 h_50 bc_e75f57"></div>
    <div class="item w_50 h_50 bc_f2b64c"></div>
    <div class="item w_50 h_50 bc_55bd4c"></div>
    <div class="item w_50 h_50 bc_e75f57"></div>
    <div class="item w_50 h_50 bc_f2b64c"></div>
    <div class="item w_50 h_50 bc_55bd4c"></div>
    <div class="item w_50 h_50 bc_e75f57"></div>
    <div class="item w_50 h_50 bc_f2b64c"></div>
    <div class="item w_50 h_50 bc_55bd4c"></div>
    <div class="item w_50 h_50 bc_e75f57"></div>
    <div class="item w_50 h_50 bc_f2b64c"></div>
    <div class="item w_50 h_50 bc_55bd4c"></div>
    <div class="item w_50 h_50 bc_e75f57"></div>
</div>

css

.item {
    --n: 7;
    --gap: calc((100% - 50px * var(--n)) / var(--n) / 2);
    margin: 10px var(--gap);
}

解析

1、d_f:display: flex;
2、fw_w:flex-wrap: wrap;
3、m_a:margin: auto;
4、ml_a:margin-left: auto;
5、mr_a:margin-right: auto;
3、item类名比较特殊,所以单独定义,涉及到变量,不属于公共样式
4、其他类名基本是见名知意,不做过多叙述

文章来源:https://blog.csdn.net/weixin_51157081/article/details/115436259
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。