码迷,mamicode.com
首页 > 其他好文 > 详细

块级元素水平居中的问题

时间:2018-07-07 20:18:00      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:position   inline   auto   body   generator   des   pos   parent   keyword   

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>块级元素居中问题</title>
  <style type="text/css">
         .one{
           width:100px;
           margin:0 auto;
           background:#ff6699;
         }
         .two{
            margin:0 auto;
            background:#ff3366;
         }
         .t-parent{
           text-align:center;
         }
         .three{
           display:inline;
           background:#3333ff;
         }

         .f-parent{
           position:relative;
           float:left;
           left:50%;
         }
         .four{
           position:relative;
           left:-50%;
           background:#000033;
         }
  </style>
 </head>
 <body>
      <!--给宽度 直接设置margin:0 auto;-->
     <div  class="one">定宽块级元素水平居中</div>
      <!--table 标签是根据里面的文本来撑开的  有文字就有宽度
            在设置 margin:0 auto;-->
     <table class="two">
         <tr>
             <td>    <div >不定宽块级元素水平居中</div></td>
         </tr>
     </table>
     <!--父元素 设置 text-align:center  子元素 设置为inline-->
      <div class="t-parent">
     <div class="three">不定宽块级元素水平居中</div>
     </div>
     <!--
         父元素设置
         position:relative;
         float:left;
         left:50%;
         子元素设置
         position:absolute;
         float:-50%;
        
     -->
     <div class="f-parent">
     <div class="four">不定宽块级元素水平居中</div>
     </div>
 </body>
</html>

 

块级元素水平居中的问题

标签:position   inline   auto   body   generator   des   pos   parent   keyword   

原文地址:https://www.cnblogs.com/liveoutfun/p/9277643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!