css2.01中要使单行文本居中,为什么包裹单行文本的父盒子一定要是block元素?

目录

父盒子有宽高

父盒子为标准流元素

当父盒子为block元素时

当父盒子为inline元素时

父盒子为脱离标准流元素

当父盒子为block元素转化为脱标元素时

当父盒子为inline元素转化为脱标元素时

父盒子有高无宽

父盒子为标准流元素

当父盒子为block元素时

当父盒子为inline元素时

父盒子为脱离标准流元素

当父盒子为block元素转化为脱标元素时

当父盒子为inline元素转化为脱标元素时

父盒子有宽无高

父盒子为标准流元素

当父盒子为block元素时

当父盒子为inline元素时

父盒子为脱离标准流元素

当父盒子为block元素转化为脱标元素时

当父盒子为inline元素转化为脱标元素时

父盒子无宽无高

父盒子为标准流元素

当父盒子为block元素时 

当父盒子为inline元素时

父盒子为脱离标准流元素

当父盒子为block元素转化为脱标元素时

当父盒子为inline元素转化为脱标元素时


 重点总结:(总结基于探究)

1.脱标元素居中结果都一样,不分原来是block元素脱标的,还是inline元素脱标

2.当父盒子是block元素时,不设置宽,自动有宽,此宽默认为其父亲的宽,如果父盒子的父亲是网页,默认为网页的宽

3.当父盒子是inline元素或是脱标元素,不设置宽,宽度都默认为盒子内容的宽,text-align自然在此种情况下无效。

4.line-height自己能够撑出来高,在父盒子的任何情况下都有效。


以下所有情况都默认包裹单行文本的标签是块级标签,例如:<p>

父盒子有宽高

父盒子为标准流元素

  • 当父盒子为block元素时

  1. 单行文本,左右居中(水平居中):
    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            width: 600px;
            height: 600px;
            background-color: pink;
            text-align: center;
            
        }
 </style>

 结果为:

  1. 2单行文本上下居中:
    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            width: 600px;
            height: 600px;
            background-color: pink;
            line-height:60px;            
        }
 </style>

  结果为:

  1. 3单行文本水平竖直居中: 

    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            width: 600px;
            height: 600px;
            background-color: pink;
            line-height:60px; 
            text-align:center;           
        }
 </style>

   结果为:

  • 当父盒子为inline元素时

  1. 单行文本左右居中:
<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
            width: 600px;
            height: 600px;
            background-color: pink;
            text-align: center;
            
        }
    </style>

  结果为:

  1.  2单行文本上下居中:
<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>

<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
            width: 600px;
            height: 600px;
            background-color: pink;
            line-height: 600px; 
        }
    </style>

   结果为:

  1.  3单行文本水平竖直垂直居中
<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
            width: 600px;
            height: 600px;
            background-color: pink;
            line-height: 600px; 
            text-align:center;
        }
    </style>

    结果为:

父盒子为脱离标准流元素

  • 当父盒子为block元素转化为脱标元素时

1.单行文本左右居中(水平居中)

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width: 600px;
            height: 600px;
            background-color: pink;
            
            text-align:center;           
        }
 </style>

    结果为:

1.2单行文本上下居中

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width: 600px;
            height: 600px;
            background-color: pink;
            line-height:600px; 
                     
        }
 </style>

     结果为:

1.3单行文本水平竖直垂直居中

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width: 600px;
            height: 600px;
            background-color: pink;
            line-height:600px;
            text-align: center;
                     
        }
 </style>

     结果为:

  • 当父盒子为inline元素转化为脱标元素时

1.单行文本左右居中(水平居中)

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width:600px;
            height: 600px;
            background-color: pink;
            text-align:center;  
                  
        }
 </style>

   结果为:

1.2单行文本上下居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width:600px;
            height: 600px;
            background-color: pink;
           
            line-height:600px;         
        }
 </style>

   结果为:

1.3单行文本水平竖直垂直居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width:600px;
            height: 600px;
            background-color: pink;
            text-align:center;  
            line-height:600px;         
        }
 </style>

   结果为:

 

父盒子有高无宽

父盒子为标准流元素

  • 当父盒子为block元素时

  1. 单行文本左右居中:
    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            height: 600px;
            background-color: pink;
            text-align:center;           
        }
 </style>

    结果为:

  1.  2单行文本上下居中:
    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            height: 600px;
            background-color: pink;
            line-height:600px          
        }
 </style>

   结果为:

  1. 3当单行文本水平竖直垂直居中:
    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
 
            height: 600px;
            background-color: pink;
            line-height:60px; 
            text-align:center;           
        }
 </style>

    结果为:

  • 当父盒子为inline元素时

  1. 单行文本左右居中
<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>

<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
           
            height: 600px;
            background-color: pink;
            
            text-align:center;
        }
    </style>

     结果为:

  1.  2单行文本上下居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
            
            height: 600px;
            background-color: pink;
            line-height: 600px; 
           
        }
    </style>

   结果为: 

 1.3单行文本水平竖直垂直居中:

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
           
            height: 600px;
            background-color: pink;
            line-height: 600px; 
            text-align:center;
        }
    </style>

   结果为: 

 

父盒子为脱离标准流元素

  • 当父盒子为block元素转化为脱标元素时

1.单行文本左右居中(水平居中)

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            height: 600px;
            background-color: pink;
          
            text-align:center;           
        }
 </style>

  结果为: 

1.2单行文本上下居中

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            height: 600px;
            background-color: pink;
            line-height:600px; 
                      
        }
 </style>

  结果为: 

1.3单行文本水平竖直垂直居中

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            height: 600px;
            background-color: pink;
            line-height:60px; 
            text-align:center;           
        }
 </style>

  结果为: 

  • 当父盒子为inline元素转化为脱标元素时

1.单行文本左右居中(水平居中)

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
           
            height: 600px;
            background-color: pink;
            text-align:center;  
                  
        }
 </style>

  结果为:  

1.2单行文本上下居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
           
            height: 600px;
            background-color: pink;
           
            line-height:600px;         
        }
 </style>

  结果为:  

1.3单行文本水平竖直垂直居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
           
            height: 600px;
            background-color: pink;
            text-align:center;  
            line-height:600px;         
        }
 </style>

  结果为:  

父盒子有宽无高

父盒子为标准流元素

  • 当父盒子为block元素时

  1. 单行文本左右居中:
    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            width: 600px;
            background-color: pink;
            text-align:center;           
        }
 </style>

 结果为:

  1. 2单行文本上下居中: 
    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            width: 600px;
            background-color: pink;
            line-height:400px          
        }
 </style>

 结果为:(当没有设置高,但是设置了line-height的数值后,自动撑起了一个高)

  1.  3单行文本水平竖直垂直居中:
    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
 
            width: 600px;
            background-color: pink;
            line-height:400px; 
            text-align:center;           
        }
 </style>

 结果为:

  • 当父盒子为inline元素时

1.单行文本左右居中(水平居中):

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>

<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
          
            width: 600px;
            background-color: pink;
            
            text-align:center;
        }
    </style>

  结果为:

1.2单行文本上下居中:

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
           
            width: 600px;
            background-color: pink;
            line-height: 600px; 
           
        }
    </style>

  结果为:

1.3单行文本水平竖直居中:

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
           
            width: 600px;
            background-color: pink;
            line-height: 600px; 
            text-align:center;
        }
    </style>

  结果为:

父盒子为脱离标准流元素

  • 当父盒子为block元素转化为脱标元素时

1.单行文本左右居中(水平居中)

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {  
            float:left;
            width: 600px;
           
            background-color: pink;
            
            text-align:center;           
        }
 </style>

 结果为:

1.2单行文本上下居中

    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {  
            float:left;
            width: 600px;
           
            background-color: pink;
            
            line-height:600px;           
        }
 </style>

 结果为:

 

1.3单行文本水平竖直垂直居中

    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {  
            float:left;
            width: 600px;
           
            background-color: pink;
            line-height:600px
            text-align:center;           
        }
 </style>

 结果为:

 

  • 当父盒子为inline元素转化为脱标元素时

1.单行文本左右居中(水平居中)

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width:600px;
           
            background-color: pink;
            text-align:center;  
                    
        }
 </style>

 结果为: 

1.2单行文本上下居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width:600px;
           
            background-color: pink;
              
            line-height:600px;         
        }
 </style>

 结果为: 

 

1.3单行文本水平竖直垂直居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            width:600px;
          
            background-color: pink;
            text-align:center;  
            line-height:600px;         
        }
 </style>

 结果为:

父盒子无宽无高

父盒子为标准流元素

  • 当父盒子为block元素时 

  1. 单行文本左右对齐:
    <div class="father">
        <p>我是单行文本</p>
    </div>

 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {

            background-color: pink;
           
            text-align:center;           
        }
 </style>

  结果为:

  1. 2单行文本上下对齐:
    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {

            background-color: pink;
            line-height:400px;
                     
        }
 </style>

   结果为:

  1. 3单行文本水平竖直垂直居中:
    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
 
            
            background-color: pink;
            line-height:400px; 
            text-align:center;           
        }
 </style>

    结果为:

  • 当父盒子为inline元素时

1.单行文本左右居中(水平居中):

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
           
            
            background-color: pink;
           
            text-align:center;
        }
    </style>

   结果为:

1.2单行文本上下居中:

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
           
            
            background-color: pink;
            line-height: 600px; 
            
        }
    </style>

   结果为:

1.3单行文本水平竖直居中:

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
<style>
        * {
            margin: 0;
            padding: 0;

        }

        a.father {
           
           
            background-color: pink;
            line-height: 600px; 
            text-align:center;
        }
    </style>

   结果为:

父盒子为脱离标准流元素

  • 当父盒子为block元素转化为脱标元素时

1.单行文本左右居中(水平居中)

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            background-color: pink;
            
            text-align:center;           
        }
 </style>

    结果为:

1.2单行文本上下居中

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            background-color: pink;
            line-height:600px; 
                     
        }
 </style>

    结果为:

 

1.3单行文本水平竖直垂直居中

    <div class="father">
        <p>我是单行文本</p>
    </div>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
            background-color: pink;
            line-height:600px; 
            text-align:center;           
        }
 </style>

    结果为:

 

  • 当父盒子为inline元素转化为脱标元素时

1.单行文本左右居中(水平居中)

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
          
            background-color: pink;
            text-align:center;  
                 
        }
 </style>

   结果为: 

1.2单行文本上下居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
           
            background-color: pink;
            
            line-height:600px;         
        }
 </style>

   结果为: 

1.3单行文本水平竖直垂直居中

<body>
    <a class="father">
        <p>我是单行文本</p>
    </a>
</body>
 <style>
        * {
            margin: 0;
            padding: 0;

        }

        div.father {
            float:left;
           
            background-color: pink;
            text-align:center;  
            line-height:600px;         
        }
 </style>

   结果为: 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值