特色泡酒 發表於 2022-4-17 23:34:00

CSS学习手册

<p>学习别人的CSS,这是一个传送门</p>
<h2 id="布局">布局</h2>
<h3 id="实现水平垂直居中">实现水平垂直居中</h3>
<p>在flex 格式化上下文中,设置了<code>margin: auto</code>的元素,在通过<code>justify-content</code>和<code>align-self</code>进行对齐之前,任何正处于空闲的空间都会分配到该方向的自动 margin 中去。</p>
<pre><code class="language-html">                &lt;div class="box"&gt;
                        &lt;div class="content"&gt;&lt;/div&gt;
                &lt;/div&gt;

                &lt;style&gt;
                        *{
                                margin: 0;
                                padding: 0;
                        }
                        .box{
                                width: 100vw;
                                height: 100vh;
                                display: flex;
                        }
                        .content{
                                margin: auto;
                                width: 50px;
                                height: 50px;
                                background-color: #000;
                        }
                &lt;/style&gt;
</code></pre>
<p>效果如下:</p>
<p><img src="https://gitee.com/windzzzz/cloud-image/raw/master/img/image-20220417163941036.png" alt="image-20220417163941036" loading="lazy"></p>
<h3 id="双飞翼布局">双飞翼布局</h3>
<p>双飞翼布局实现左中右三列布局,渲染时中间列比左右两列提前渲染,左右两列定宽,中间列自适应剩余宽度。</p>
<p>双飞翼布局的中间容器多了一个子容器,通过控制这个子容器的<code>margin</code>或者<code>padding</code>空出左右两列的宽度。</p>
<pre><code class="language-html">                &lt;div class="box"&gt;
                        &lt;div class="middle"&gt;
                                &lt;div class="content"&gt;#content&lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div class="left"&gt;#left&lt;/div&gt;
                        &lt;div class="right"&gt;#right&lt;/div&gt;
                &lt;/div&gt;


                &lt;style&gt;
                        *{
                                margin: 0;
                                padding: 0;
                        }
                        .box{
                                min-width: 500px;
                                background-color: blue;
                                height: 100px;
                        }
                        .middle{
                                width: 100%;
                                height: 100px;
                                background-color: pink;
                                float: left;
                        }
                        .content{
                                margin-left: 100px;
                                margin-right: 100px;
                        }
                        .left,.right{
                                width: 100px;
                                float: left;
                                height: 100px;
                        }
                        .left{
                                margin-left: -100%;
                                background-color: red;
                        }
                        .right{
                                margin-left: -100px;
                                background-color: yellow;
                        }
                &lt;/style&gt;
</code></pre>
<p>效果如下:</p>
<p><img src="https://gitee.com/windzzzz/cloud-image/raw/master/img/image-20220417205502516.png" alt="image-20220417205502516" loading="lazy"></p>
<h3 id="grid布局配合clip-path实现gta5封面">grid布局配合clip-path实现GTA5封面</h3>
<p>可以使用grid布局配合clip-path来实现造型怪异的网格。</p>
<p><img src="https://gitee.com/windzzzz/cloud-image/raw/master/img/111740149-34267a00-88bf-11eb-8275-efc28a9b958b.png" alt="image" loading="lazy"></p>
<pre><code class="language-vue">&lt;template&gt;
                &lt;div class="parent"&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/1.png"&gt;
                        &lt;/div&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/2.png"&gt;
                        &lt;/div&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/3.png"&gt;
                        &lt;/div&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/4.png"&gt;
                        &lt;/div&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/9.png"&gt;
                        &lt;/div&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/6.png"&gt;
                        &lt;/div&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/7.png"&gt;
                        &lt;/div&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/8.png"&gt;
                        &lt;/div&gt;
                        &lt;div class="child"&gt;
                                &lt;img src="../assets/images/5.png"&gt;
                        &lt;/div&gt;
                &lt;/div&gt;
&lt;/template&gt;

&lt;style scoped lang="scss"&gt;
        body{
                box-sizing: border-box;
        }
img{
width:100%;
height: 100%;
object-fit: cover;
object-position: 40% 0;
}

.parent{
padding: .8rem;
background: black;
height: 95vh;
min-height: 500px;
width: 100%;
max-width: 600px;
margin: auto;
margin-top: 2.5vh;
border: 1px solid #c9b473;
overflow: hidden;
display: grid;

grid-template-columns: 1fr .7fr .3fr 1fr;
grid-template-rows: 20% 40% 20% 20%;
grid-template-areas: 'one two two three'
    'four five five five'
    'six five five five'
    'six seven eight eight';
}
.child{

&amp;:nth-child(1),
&amp;:nth-child(2),
&amp;:nth-child(3){
    img{
      width:120%;
      height: 120%;
    }
}
&amp;:first-child{
    grid-area: one;
    clip-path: polygon(0% 0%, 93.24% 0%, 105.04% 110.16%, 0% 90%);
}
&amp;:nth-child(2){
    grid-area: two;
    clip-path: polygon(0% 0%, 108.28% 0%, 96.45% 110.13%, 10.55% 110.93%);
}
&amp;:nth-child(3){
    grid-area: three;
    clip-path:polygon(15.05% 0%, 100% 0%, 99.35% 91.7%, 3.08% 108.48%);
}
&amp;:nth-child(4){
    grid-area: four;
    clip-path: polygon(0% -0.85%, 106.34% 9.98%, 121.32% 65.63%, 99.66% 109.89%, 1.86% 124.41%);

    img{
      width: 135%;
      height: 135%;
    }
}
&amp;:nth-child(5){
    grid-area: five;
    clip-path: polygon(6.4% 6.48%, 47.24% 5.89%, 100% 0%, 98.41% 96.85%, 53.37% 100%, 53% 63.21%, 3.23% 73.02%, 14.30% 44.04%);
}
&amp;:nth-child(6){
    grid-area: six;
    clip-path:polygon(2.14% 29.3%, 99.34% 15.42%, 98.14% 100.82%, 1.57% 101.2%);
}
&amp;:nth-child(7){
    grid-area: seven;
    clip-path: polygon(7.92% 33.47%, 96.31% 23.39%, 95.38% 100%, 5.30% 100.85%);
}
&amp;:nth-child(8){
    grid-area: eight;
    clip-path: polygon(2.5% 22.35%, 100% 0%, 100% 100%, 1.55% 100%);
}
&amp;:nth-child(9){
    grid-row-start: 3;
    grid-row-end: 4;
    grid-column-start: 2;
    grid-column-end: 4;

    clip-path:polygon(5.94% 28.66%, 100.61% -0.67%, 101.1% 108.57%, 5.4% 126.28%);

    img{
      object-position: 30% 50%;
      height: 135%;
    }
}
}
&lt;/style&gt;
</code></pre>
<p>效果如下:</p>
<p><img src="https://gitee.com/windzzzz/cloud-image/raw/master/img/image-20220417233011671.png" alt="image-20220417233011671" loading="lazy"></p><br><br>
来源:https://www.cnblogs.com/Small-Windmill/p/16157902.html
頁: [1]
查看完整版本: CSS学习手册