如何居中一个浮动元素?

110 阅读1分钟

⽗元素和⼦元素同时左浮动,然后⽗元素相对左移动50%,再然后⼦元素相对右移动50%,或者⼦元素相对左移动-50% 也就可以了。

<style>
   .p { 
       position:relative; 
       left:50%; 
       float:left; 
    }
  .c { 
      position:relative;
      float:left; 
      right:50%;
     }
   </style>
  
   <body>
      <div clsaa='p'>
           <h1 class='c'> hello world </h1>
      </div>
   </body>