【前端知識體系】CSS佈局知識強化

来源:https://www.cnblogs.com/fecommunity/archive/2019/11/17/11874666.html
-Advertisement-
Play Games

本文介紹了CSS常見的表格、浮動、定位佈局等方式,也介紹了聖杯佈局和雙飛翼佈局高級佈局的實現方式。 ...


1.實現兩欄/三欄佈局的方法?

  1. 表格佈局
  2. float + margin佈局
  3. inline-block佈局
  4. flexbox佈局(相容性的問題)

1.1 基礎佈局

<style>
    * {
        margin: 0;
        padding: 0;
    }
    .layout {
        margin-top: 10px;
    }
    .layout div{
        min-height: 100px;
    }
</style>
<body>
  <!--1.浮動的方式來實現佈局-->
  <section class="layout float">
      <style>
          .layout.float .left {
              float: left;
              width: 300px;
              background-color: #48adff;
          }
          .layout.float .main {
              background-color: #ff4344;
          }
      </style>
      <article class="left-main">
          <div class="left"></div>
          <div class="main">
              <h1>浮動兩欄佈局</h1>
              <p>兩欄佈局的中間部分</p>
              <p>兩欄佈局的中間部分</p>
          </div>
      </article>
  </section>

  <!--2.定位的方式來實現佈局-->
  <section class="layout absolute">
      <style>
          .layout.absolute .left-main {
              width: 100%;
          }
          .layout.absolute .left {
              left : 0;
              width: 300px;
              background-color: #48adff;
              position: absolute;
          }
          .layout.absolute .main {
              /*預設是以正常文檔流的方式來展現的*/
              background-color: #ff4344;
              margin-left: 300px;
              right: 0;
          }
      </style>
      <article class="left-main">
          <div class="left"></div>
          <div class="main">
              <h1>絕對定位兩欄佈局</h1>
              <p>兩欄佈局的中間部分</p>
              <p>兩欄佈局的中間部分</p>
          </div>
      </article>
  </section>

  <!--3.flex佈局的實現-->
  <section class="layout flex">
      <style>
          .layout .left-main {
              display: flex;
          }
          .layout .left {
              width: 300px;
              background-color: #48adff;
          }
          .layout .main {
              flex: 1;
              background-color: #ff4344;
          }
      </style>
      <article class="left-main">
          <div class="left"></div>
          <div class="main">
              <h1>flex兩欄佈局</h1>
              <p>兩欄佈局的中間部分</p>
              <p>兩欄佈局的中間部分</p>
          </div>
      </article>
  </section>

  <!--4.table佈局的實現-->
  <section class="layout table">
      <style>
          .layout .left-main {
              display: table;
              width: 100%;
          }
          .layout .left {
              display : table-cell;
              width: 300px;
              background-color: #48adff;
          }
          .layout .main {
              background-color: #ff255f;
          }
      </style>
      <article class="left-main">
          <div class="left"></div>
          <div class="main">
              <h1>table兩欄佈局</h1>
              <p>兩欄佈局的中間部分</p>
              <p>兩欄佈局的中間部分</p>
          </div>
      </article>
  </section>

  <!--5.grid佈局-->
  <section class="layout grid">
      <style>
          .layout.grid .left-main {
              display: grid;
          }
          .layout.grid .left-main {
              grid-template-rows : 100px;
              /*按照順序指定盒子的寬度*/
              grid-template-columns : 300px  auto;
          }
          .layout.grid .left {
              background-color: #48adff;
          }
          .layout.grid .main {
              background-color: #ff4344;
          }
      </style>
      <article class="left-main">
          <div class="left"></div>
          <div class="main">
              <h1>grid兩欄佈局</h1>
              <p>兩欄佈局的中間部分</p>
              <p>兩欄佈局的中間部分</p>
          </div>
      </article>
  </section>
</body>

1.2 聖杯佈局

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>實現三欄水平佈局之聖杯佈局</title>
    <style type="text/css">
      /*基本樣式*/
      .left, .right, .main {
        min-height: 300px;
      }
      .left {
        width: 200px;
        background-color:thistle;
      }
      .main {
        background-color: #999;
      }
      .right {
        width: 300px;
        background-color: violet;
      }
      /* 聖杯佈局關鍵代碼 */
      .left, .main, .right {
        float: left;
        position: relative;
      }
      .main {
        width: 100%;
      }
      .container {
        padding-left: 200px;
        padding-right: 300px;
      }
      .left {
        margin-left: -100%;
        left: -200px;
      }
      .right {
        margin-left: -300px;
        right: -300px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="main">main</div>
      <div class="left">left</div>
      <div class="right">right</div>
    </div>
  </body>
</html>

1.3 雙飛翼佈局

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>雙飛翼佈局</title>
    <style>
      .left,
      .right,
      .main {
        min-height: 200px;
      }
      .left {
        width: 200px;
        background-color: thistle;
      }
      .main {
        background: #999;
      }
      .right {
        width: 300px;
        background-color: violet;
      }
      /* 雙飛翼佈局重點 */
      .left,
      .main,
      .right {
        float: left;
      }
      .main {
        width: 100%;
      }
      .main-inner {
        margin-left: 200px;
        margin-right: 300px;
      }
      .left {
        margin-left: -100%;
      }
      .right {
        margin-left: -300px;
      }
    </style>
  </head>
  <body>
    <div class="main"><div class="main-inner">中心區</div></div>
    <div class="left">left</div>
    <div class="right">right</div>
  </body>
</html>

2.absolute/fixed/static/sticky定位?

  1. 前者相對於最近的absolute/relative
  2. 後者相對於屏幕進行定位
  3. fixed是相對於屏幕的可視區域的,也會直接脫離於文檔流獨立存在的
  4. 元素未滾動,在當前可視區域他的top值不生效,只有margin生效,滾動起來後margin失效,top值生效
<style type="text/css">
  p{
  font-size:11pt;
  color:#363636;
  text-indent:2em;
  }
  .parent{
  width:500px;
  height:150px;
  margin-top:20px;
  margin-left:20px;
  border:solid 1px #555555;
  background:#aaaaaa;
  }
  .parent div{
  width:100px;
  height:80px;
  float:left;
  background:#708090;
  border:dashed 1px #008B8B;
  font-size:12pt;
  font-weight:bold;
  color:#104E8B;
  }
</style>
</head>
<body>
  <!--相對定位!-->
  <h2>relative</h2>
  <p>相對定位是一個非常容易掌握的概念。如果對一個元素進行相對定位,它將出現在它所在的位置上。然後,可以通過設置垂直或水平位置,讓這個元素“相對於”它的起點進行移動。</p>
  <div class="parent">
  <div>child 1</div>
  <div style="position:relative;left:20px;top:20px;">child 2</div>
  <div>child 3</div>
  </div>
 
<!--絕對定位!-->
<h2>absolute</h2>
<p>絕對定位的元素的位置相對於最近的已定位祖先元素,如果元素沒有已定位的祖先元素,那麼它的位置相對於最初的包含塊。
對於定位的主要問題是要記住每種定位的意義。</p>
<p>絕對定位是“相對於”最近的已定位祖先元素,如果不存在已定位的祖先元素,那麼“相對於”最初的包含塊。所以如果要設定元素與其父元素的絕對位置定位就必須設定父元素的定位。</p>
<p>註釋:根據用戶代理的不同,最初的包含塊可能是畫布或 HTML 元素。</p>
<div class="parent" style="position:relative;"<!--如果該處不定位,那麼child5框的定位是相對於最初的包含塊!-->>
<div>child 4</div>
<div style="position:absolute;left:20px;top:20px;">child 5</div>
<div>child 6</div>
</div>
 
<!--相對定位!-->
<h2>fixed</h2>
<p>元素框的表現類似於將 position 設置為 absolute,不過其包含塊是視窗本身。</p>
<div class="parent">
<div>child 7</div>
<div style="position:fixed;right:20px;top:20px;">child 8</div>
<div>child 9</div>
</div>

<!--相對定位!-->
<h2>static</h2>
<p>元素框正常生成。塊級元素生成一個矩形框,作為文檔流的一部分,行內元素則會創建一個或多個行框,置於其父元素中。</p>
<div class="parent">
<div>child 10</div>
<div style="position:static;right:20px;top:20px;">child 11</div>
<div>child 12</div>
</div>
</body>

3.什麼是層疊上下文?如何形層疊上下文?層疊順序是怎樣的?

<style>
    .father-green {
        width:500px;
        height:300px;
        background-color:green;
    }
    .son-red {
        width:200px;
        height:100px;
        background-color:red;
        display:inline-block;
    }
    .subson-yellow {
        height:50px;
        width:200px;
        background-color: yellow;
        
    }
    .son-purple {
        width: 200px;
        height:100px;
        background-color:purple;
        display:inline-block;
        margin-left:-50px;
    }
    .mather-pink {
        width: 300px;
        height:100px;
        background-color:pink;
    }
    .daughter-blue {
        width:100px;
        height:50px;
        background-color:blue;
        margin-top:-20px;
    }
</style>
<body>
    <div class="father-green">
        <div class="son-red">
            <div class="subson-yellow">
                我是孫子輩的我是孫子輩的我是孫子輩的
            </div>
        </div>
        
        <div class="son-purple">
            我是第二個子元素
        </div>
    </div>
    <div class="mather-pink"><div class="daughter-blue">daughter-blue</div>
    </div>
</body>    

頁面顯示的層級關係

3.1 形成層疊上下文的方法?

  • 根元素
  • position值為 absolute|relative,且 z-index值不為 auto
  • position 值為 fixed|sticky
  • z-index 值不為 auto 的flex元素,即:父元素 display:flex|inline-flex
  • opacity 屬性值小於 1 的元素
  • transform 屬性值不為 none的元素
  • mix-blend-mode 屬性值不為 normal 的元素
  • filter、 perspective、 clip-path、 mask、 mask-image、 mask-border、 motion-path 值不為none 的元素
  • perspective 值不為 none 的元素
  • isolation 屬性被設置為 isolate 的元素
  • will-change 中指定了任意 CSS 屬性,即便你沒有直接指定這些屬性的值
  • -webkit-overflow-scrolling 屬性被設置 touch的元素

[!NOTE]

  • 層疊上下文可以包含在其他層疊上下文中,並且一起組建了一個有層級的層疊上下文
  • 每個層疊上下文完全獨立於它的兄弟元素,當處理層疊時只考慮子元素,這裡類似於BFC
  • 每個層疊上下文是自包含的:當元素的內容發生層疊後,整個該元素將會在父級疊上下文中按順序進行層疊

3.2 CSS層疊上下優先順序

層疊上下優先順序順序

  • 1.形成堆疊上下文環境的元素的背景與邊框
  • 2.擁有負 z-index 的子堆疊上下文元素 (負的越高越堆疊層級越低)
  • 3.正常流式佈局,非 inline-block,無 position 定位(static除外)的子元素
  • 4.無 position 定位(static除外)的 float 浮動元素
  • 5.正常流式佈局, inline-block元素,無 position 定位(static除外)的子元素(包括 display:table 和 display:inline )
  • 6.擁有 z-index:0 的子堆疊上下文元素
  • 7.擁有正 z-index: 的子堆疊上下文元素(正的越低越堆疊層級越低)

3.3 層疊上下文的堆疊順序?

層疊上下文的堆疊順序

[!NOTE]
總結:層疊上下文(border/background)< 負z-index < block塊狀盒子 < 浮動的盒子 < inline/inline-block水平盒子 < z-index:auto 或者 z-index:0 < 正z-index(定位並設定了正的z-index值,z-index值越大 層級越高)

4.如何解決inline-block 的間隙問題?

4.1 字元間距問題

<style>
*{
        margin: 0;
        padding: 0;
    }
    ul{
        list-style: none;
    }
    li{
        display: inline-block;
        width: 100px;
        height: 100px;
        background: red;    
    }
</style>
<ul>
    <li>111</li>
    <li>222</li>
    <li>333</li>


    <li>444</li>
    <li>555</li>
</ul>

CSS字元間距問題

4.2 解決方法

  • 1.解決方法1:直接刪除換行符(IE1像素殘留)
  • 2.設置父元素的font-size為0,在子元素重新設置字體大小(低版本safari 相容性)
  • 3.父元素 設置font-size:0 ;letter-spacing:-3px ,子元素重新設置font-size(推薦方案)

5.BFC是什麼?如何清除浮動?

BFC:浮動元素和絕對定位元素,非塊級盒子的塊級容器(例如 inline-blocks, table-cells, 和 table-captions),以及overflow值不為“visiable”的塊級盒子,都會為他們的內容創建新的BFC(塊級格式上下文)。它是指一個獨立的塊級渲染區域,只有Block-level BOX參與,該區域擁有一套渲染規則來約束塊級盒子的佈局,且與區域外部無關

5.1 如何觸發BFC?

  • float的值不為none
  • overflow的值不為visible
  • display的值為inline-block、table-cell、table-caption
  • position的值為absolute或fixed

5.2 BFC佈局規則

  1. 內部的Box會在垂直方向,一個接一個地放置。
  2. Box垂直方向的距離由margin決定。屬於同一個BFC的兩個相鄰Box的margin會發生重疊(高頻考點)
  3. 每個元素的margin box的左邊, 與包含塊border box的左邊相接觸(對於從左往右的格式化,否則相反)。即使存在浮動也是如此。
  4. BFC的區域不會與float box重疊。
  5. BFC就是頁面上的一個隔離的獨立容器,容器裡面的子元素不會影響到外面的元素。反之也如此。
  6. 計算BFC的高度時,浮動元素也參與計算

5.3 如何清除浮動?

  1. 原因:浮動的元素不會占據父元素的佈局空間(父元素佈局不會管浮動元素)
  2. 清除方式: 讓盒子負責自己的佈局:

5.3.1 添加額外標簽

<div class="main left">.main{float:left;}</div>
<div class="side left">.side{float:right;}</div>
<div style="clear:both;"></div>
</div>
<div class="footer">.footer</div>

5.3.2 父元素設置 overflow:hidden

<div class="wrap" id="float3" style="overflow:hidden; *zoom:1;">
<h2>3)父元素設置 overflow </h2>
<div class="main left">.main{float:left;}</div>
<div class="side left">.side{float:right;}</div>
</div>
<div class="footer">.footer</div>

5.3.3 使用:after 偽元素

<style type="text/css">
 .clearfix:after {  
   content: ".";
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;  
    }  
.clearfix {
  display: inline-block;
  *zoom:1;
  }  /* for IE/Mac */  
</style>
<!--[if IE]>
 <style type="text/css">
 .clearfix {zoom: 1;/* triggers hasLayout */  display: block;/* resets display for IE/Win */} </style>
<![endif]-->

5.3.4 雙偽元素清除法(推薦)

.clearfix:before,.clearfix:after{
   content:"";
   display:table;
}
.clearfix:after{
  clear:both;
}
.clearfix{
  *zoom:1;
}

6.如何適配移動端?

6.1 viewport進行縮放

<meta name="viewport" content="width=device-width,initial-scale=1">

6.2 使用rem

rem是什麼(CSS3新增),初始值:1rem=16px?
rem(font size of the root element)是指相對於根元素的字體大小的單位。簡單的說它就是一個相對單位

[!NOTE]
rem(1rem = 16px) / viewport(固定寬度) / media query(屏幕大小自適應)

6.3 設計上(響應式方法)

隱藏(移動端隱藏元素) 折行(橫排變縱排) 自適應(留下自適應的空間)(media query)

6.4 固定寬度做法

定寬佈局(版心)

7.em和rem的區別?

  1. rem 單位翻譯為像素值是由 html 元素的字體大小決定的。 此字體大小會被瀏覽器中字體大小的設置影響,除非顯式重寫一個具體單位。
  2. em 單位轉為像素值,取決於他們使用的字體大小。 此字體大小受從父元素繼承過來的字體大小,除非顯式重寫與一個具體單位

8.垂直居中的6中實現方式?

8.1 方法一:基於視口的垂直居中

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>垂直居中</title>
  </head>
  <style>
    .wrapper {
      overflow: hidden;
      width: 1000px;
      height: 500px;
      background: #999;
    }
    .center {
      width: 18em;
      height: 10em;
      text-align: center;
      background-color: orange;
      color: #fff;


      /* 1vh = 1% * 視口高度 */
      margin: 50vh auto;
      transform: translateY(-50%);
    }

  </style>
  <body>
    <div class="wrapper">
      <div class="center">
        基於視口的垂直居中<br />
        不要求原生有固定的寬高。<br />
        但是這種居中是在整個頁面視窗內居中,不是基於父元素<br />

      </div>
    </div>
  </body>
</html>

8.2 方法二:定寬居中

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>垂直居中</title>
  </head>
  <style>
    .center {
      width: 18em;
      height: 10em;
      text-align: center;
      background-color: orange;
      color: #fff;

      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -9rem;
      margin-top: -5rem;
    }
  </style>
  <body>
    <div class="center">
        要求原生有固定的寬高。<br/>
        position: absolute;<br/>
        top和left 為 50%;<br/>
        margin上為高的一半<br/>
        margin左為寬的一半<br/>
    </div>

  </body>
</html>

8.3 方法三:calc居中

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>垂直居中</title>
  </head>
  <style>
    .center {
      width: 18em;
      height: 10em;
      text-align: center;
      background-color: orange;
      color: #fff;

      position: absolute;
      top: calc(50% - 5em);
      left: calc(50% - 9em);
    }
  </style>
  <body>
    <div class="center">
        要求原生有固定的寬高。<br/>
        position: absolute;<br/>
        top 為 calc(50% 剪 一半高)
        left 為 calc(50% 剪 一半寬)
    </div>

  </body>
</html>

8.4 方法四:transform居中

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>垂直居中</title>
  </head>
  <style>
    .center {
      width: 18em;
      height: 10em;
      text-align: center;
      background-color: orange;
      color: #fff;

      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
  <body>
    <div class="center">
        不要求原生有固定的寬高。<br/>
        position: absolute;<br/>
        top和left 為 50%;<br/>
        transform: translate(-50%, -50%);
    </div>

  </body>
</html>

8.5 方法五:flex居中方法1

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>垂直居中</title>
  </head>
  <style>
    .wrapper {
      width: 1000px;
      height: 600px;
      background: #999;

      display: flex;
    }
    .center {
      width: 18em;
      height: 10em;
      text-align: center;
      background-color: orange;
      color: #fff;

      margin: auto;
    }
  </style>
  <body>
    <div class="wrapper">
      <div class="center">
        使用flex居中<br/>
        父元素 display: flex; <br/>
        居中塊: margin: auto;
      </div>
    </div>
  </body>
</html>

8.6 方法六: flex居中方法2

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>垂直居中</title>
  </head>
  <style>
    .wrapper {
      width: 1000px;
      height: 600px;
      background: #999;

      display: flex;
      /* 盒子橫軸的對齊方式 */
      justify-content: center;
      /* 盒子縱軸的對齊方式 */
      align-items: center;
    }
    .center {
      width: 18em;
      height: 10em;
      text-align: center;
      background-color: orange;
      color: #fff;
    }
  </style>
  <body>
    <div class="wrapper">
      <div class="center">
        使用flex居中<br/>
        父元素 display: flex; <br/>
        justify-content: center;<br/>
        align-items: center;<br/>
      </div>
    </div>
  </body>
</html>

9.水平居中的4種實現方式?

9.1 方法一:text-align的center屬性

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>水平元素居中</title>
  </head>
  <style>
    .wrapper {
      text-align: center;
      height: 1000px;
    }
    .center {
      display: inline-block;
      width: 500px;
      height: 200px;
      
      background: orange;
    }
  </style>
  <body>
    <div class="wrapper">
      <div class="center">如果需要居中的元素為常規流中 inline / inline-block 元素,為父元素設置 text-align: center;</div>
    </div>
  </body>
</html>

9.2 方法二:margin的auto屬性

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>水平元素居中</title>
  </head>
  <style>
    .wrapper {
      width: 100%;
      height: 500px;

      text-align: center; /* 3 */
    }
    .center {
      width: 500px;
      text-align: left; 
      margin: 0 auto; 

      background-color: orange;
    }
  </style>
  <body>
    <div class="wrapper">
      <div class="center">
          父元素上設置 text-align: center;<br />
          居中元素上margin 為 auto。
      </div>
    </div>
  </body>
</html>

9.3 方法三:絕對定位

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>水平元素居中</title>
  </head>
  <style>
    .wrapper {
      width: 80%;
      height: 500px;
      background: #888;
      
      position: relative;
    }
    .center {
      width: 500px;
      position: absolute;
      left: 50%;
      margin-left: -250px;

      background-color: orange;
    }
  </style>
  <body>
    <div class="wrapper">
      <div class="center">如果元素positon: absolute; 那麼 0)設置父元素postion: relative 1)為元素設置寬度,2)偏移量設置為 50%,3)偏移方向外邊距設置為元素寬度一半乘以-1</div>
    </div>
  </body>
</html>

9.4 方法四:相對定位

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>水平元素居中</title>
  </head>
  <style>
    .wrapper {
      width: 80%;
      height: 500px;
      background: #888;
    }
    .center {
      width: 500px;
      position: relative;
      left: 50%;
      margin-left: -250px;

      background-color: orange;
    }
  </style>
  <body>
    <div class="wrapper">
      <div class="center">如果元素positon: relative。 那麼 1)為元素設置寬度,2)偏移量設置為 50%,3)偏移方向外邊距設置為元素寬度一半乘以-1</div>
    </div>
  </body>
</html>

10 居中問題要點總結

10.1 被居中元素寬高固定

10.1.1 絕對定位+margin

top和left 為 50%, margin的left和top為自身寬高一半

.center {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -9rem;
  margin-top: -5rem;
}

10.1.2 絕對定位+calc

top和lefe為父元素一半剪自身一半

.center {
  position: absolute;
  top: calc(50% - 5em);
  left: calc(50% - 9em);
}

10.2 被居中元素寬高不定

10.2.1 transform變換

使用CSS3 的 transform將位置在中心點平移自身寬高一半

.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

10.2.2 flex佈局+auto

.wrapper {
  display: flex;
}
.center {
  margin: auto;
}

10.2.3 flex佈局+align

父元素指定子元素居中。

.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
}

10.3 在瀏覽器視窗中居中

10.3.1 基於視口的垂直居中

不要求原生有固定的寬高,但是這種居中是在整個頁面視窗內居中,不是基於父元素

.center{
  margin: 50vh auto;
  transform: translateY(-50%);
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 幾個月前,我的任務是將我們組的 Vue.js 項目構建配置升級到 Webpack 4。我們的主要目標之一是利用 tree shaking 的優勢,即 Webpack 去掉了實際上並沒有使用的代碼來減少包的大小。現在,tree shaking 的好處將根據你的代碼庫而有所不同。由於我們的幾個架構決策, ...
  • text transform屬性介紹 屬性就是設置 頁面中的標簽裡面的文本大小寫, 屬性常用的屬性值有三種: 、`uppercase lowercase`,不常用的屬性值在這筆者就不進行一一說明瞭。 text transform屬性值說明表 屬性值 |描述 | none | 預設。定義帶有小寫字母和 ...
  • text decoration屬性介紹 屬性是用來設置文本修飾線呢, 屬性一共有4個值。 text decoration屬性值說明表 值|作用 | none |去掉文本修飾線 underline | 設置下劃線 overline|設置上劃線 line through|設置刪除線 HTML標簽自帶修飾 ...
  • 前言 筆者所做的一個項目需要做一個前端的樹形菜單,後端返回的數據是一個平行的list,list中的每個元素都是一個對象,例如 的值為 ,每個元素都指定了父元素,生成的菜單可以無限級嵌套。一開始找的插件需要手動將生成好的樹形數組傳進去才能使用(儘管後來找到了一個UI框架,可以直接傳list進去,只需要 ...
  • 1. 定位 定位有三種:相對定位、絕對定位、固定定位 1.1 相對定位 現象和使用: 1.如果對當前元素僅僅設置了相對定位,那麼與標準流的盒子什麼區別。 2.設置相對定位之後,我們才可以使用四個方向的屬性: top、bottom、left、right。 特性:1.不脫標 2.形影分離 3.老家留坑 ...
  • 1. 盒模型 在CSS中,"box model"這一術語是用來設計和佈局時使用,然後在網頁中基本上都會顯示一些方方正正的盒子。我們稱為這種盒子叫盒模型。 盒模型有兩種:標準模型和IE模型。我們在這裡重點講標準模型。 1.1 盒模型示意圖 1.2 盒模型的屬性 width:內容的寬度 height: ...
  • [TOC] JavaScript簡介 JavaScript是前端的一門編程語言 node.js 支持前端js代碼可以跑在後端伺服器上 JavaScript 是腳本語言 JavaScript 是一種輕量級的編程語言。 JavaScript 是可插入 HTML 頁面的編程代碼。 JavaScript 插 ...
  • 1. CSS介紹 現在的互聯網前端分三層: HTML:超文本標記語言。從語義的角度描述頁面結構。 CSS:層疊樣式表。從審美的角度負責頁面樣式。 JS:JavaScript 。從交互的角度描述頁面行為 CSS:Cascading Style Sheet,層疊樣式表。CSS的作用就是給HTML頁面標簽 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...