<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      設置元素水平、垂直居中有哪2種方式

      [摘要]做一個水平和垂直居中的模態彈框這么一個小需求,對于我們這些前端來說,應該是常事! ≡赾ss3出來以前,我們要想讓元素既水平居中又要垂直居中只有一個辦法(我能想到的),就是通過js計算,把它們定位到屏幕中間位置。這方法比較笨,也麻煩。  下面兩種方式,可以讓元素快速定位到屏幕中間!  lex布...
      做一個水平和垂直居中的模態彈框這么一個小需求,對于我們這些前端來說,應該是常事。

        在css3出來以前,我們要想讓元素既水平居中又要垂直居中只有一個辦法(我能想到的),就是通過js計算,把它們定位到屏幕中間位置。這方法比較笨,也麻煩。

        下面兩種方式,可以讓元素快速定位到屏幕中間。 

        flex布局


      <style>
          .flex-mask {
              display: flex;
              position: fixed;
              z-index: 1;
              top: 0;
              left: 0;
              bottom: 0;
              right: 0;
              align-items: center;  // 垂直居中
              justify-content: center;  // 水平居中
              background: rgba(0,0,0,.5);
          }
          .flex-box {
              width: 500px;
              height: 300px;
              background-color: #fff;
              border-radius: 10px;
          }
      </style>
      
      <!-- 元素 -->
      <p class="flex-mask">
          <p class="flex-box"></p>
      </p>

        使用translate


      <style>
          .transform-box {
              position: fixed;
              z-index: 2;
              top: 50%;
              left: 50%;
              width: 300px;
              height: 150px;
              background-color: red;
              border-radius: 10px;
              transform: translate(-50%, -50%);
          }
      </style>
      <p class="transform-box"></p>

      以上就是設置元素水平、垂直居中有哪兩種方式 的詳細內容,更多請關注php中文網其它相關文章!


      網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。




      日韩精品一区二区三区高清