display: table-cell的一些用法

1.水平垂直居中

(1).块级元素包含行列元素


    <style>
    .wp {
          width: 500px;
          height: 500px;
          background-color: blue;
          display: table-cell;
          text-align: center;
          vertical-align: middle;
        }
    </style>

  </head>
  <body>
    <div class="wp">
      <span>123123</div>
  </div>
  </body>

在这里插入图片描述
( 2)块级元素包含块级元素

    .wp {
         width: 500px;
         height: 500px;
         background-color: blue;
         display: table-cell;
         text-align: center;
         vertical-align: middle;
       }
       .content{
           width: 200px;
         height: 200px;
         background-color: rgb(196, 13, 89); 
       }
   </style>

 </head>
 <body>
   <div class="wp">
     <div class="content">123123</div>
 </div>
 </body>

在这里插入图片描述

2.元素水平平均排列

1.虽然css3加入display:flex,但兼容性较差,所以在需要的情况下可以用table进行布局
(缺陷:无法换行)

<style>
    <style>
        .table {
            width: 1000px;
            display: table;
            border-spacing: 30px 0;
        }

        .tableCell {
            display: table-cell;
            height: 30px;
            line-height: 30px;
            text-align: center; 
            color: white;
            background: #6ee0b6;
        }

    </style>
</head>
<body>
<div class="table">
    <div class="tableCell">
        <span>1</span>
    </div>

    <div class="tableCell">
        <span>2</span>
    </div>

    <div class="tableCell">
        <span>3</span>
    </div>
</div>

在这里插入图片描述

3.自动占满剩余空间

<style>
    .table {
        display: table;
    }

    .demo2 {
        width: 400px;
        height: 300px;
    }

    .demo2 div.table-header-group {
        padding: 5px 20px;
        background: #10CFBD;
    }

    .demo2 .main {
        height: 100%;
        background: #48B0F7;
    }

    .demo2 div.table-footer-group {
        padding: 5px 20px;
        background: #10CFBD;
    }

    .table-row {
        display: table-row;
    }
</style>

</head>

<body>
    <h3>自动占满剩余空间</h3>
    <div class="table demo2">
        <div class="table-header-group">Table Header</div>
        <div class="main table-row">自动占满剩余空间</div>
        <div class="table-footer-group">Table Footer</div>
    </div>
</body>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值