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

display:table

时间:2015-09-06 12:25:45      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

      display:table的CSS声明能够让一个HTML元素和它的子节点像table元素一样。使用基于表格的CSS布局,使我们能够轻松定义一个单元格的边界、背景等样式,而不会产生因为使用了table那样的制表标签所导致的语义化问题。

  以下是display:的相关属性值:

table (类似 <table>)此元素会作为块级表格来显示,表格前后带有换行符。
inline-table (类似 <table>)此元素会作为内联表格来显示,表格前后没有换行符。
table-row-group (类似 <tbody>)此元素会作为一个或多个行的分组来显示。
table-header-group (类似 <thead>)此元素会作为一个或多个行的分组来显示。
table-footer-group (类似 <tfoot>)此元素会作为一个或多个行的分组来显示。
table-row (类似 <tr>)此元素会作为一个表格行显示。
table-column-group (类似 <colgroup>)此元素会作为一个或多个列的分组来显示。
table-column (类似 <col>)此元素会作为一个单元格列显示。
table-cell (类似 <td> 和 <th>)此元素会作为一个表格单元格显示。
table-caption (类似 <caption>)此元素会作为一个表格标题显示。

  display:table

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>display: table</title>
</head>
<body>

<style type="text/css" rel="stylesheet">
    .table {
        display: table;
        border: 1px solid red;
        margin: 5px;
        /*display: table时padding会失效*/
    }
    .row {
        display: table-row;
        border: 1px solid #000;
        /*display: table-row时margin、padding同时失效*/
    }
    .cell {
        display: table-cell;
        border: 1px solid yellow;
        padding: 5px;
        /*display: table-cell时margin会失效*/
    }
</style>
<div class="table">
    <div class="row">
        <div class="cell">您好</div>
        <div class="cell">世界</div>
        <div class="cell">!!</div>
    </div>
    <div class="row">
        <div class="cell">您好</div>
        <div class="cell">世界</div>
        <div class="cell">!!</div>
    </div>
</div>
</body>
</html>

1、让块级标签实现行内效果,即浮动至同一横轴

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>display:table实现浮动效果</title>
</head>
<body>

<style type="text/css" rel="stylesheet">
    .table {
        display: table;
        margin: 5px;
        width: 1000px;
    }
    .row {
        display: table-row;
    }
    .cell {
        display: table-cell;
        padding: 10px;
    }
</style>
<div class="table">
    <div class="row">
        <div class="cell">努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力</div>
        <div class="cell">努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力</div>
        <div class="cell">努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力</div>
    </div>
</div>
</body>
</html>

2、实现垂直居中

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>display:table的垂直居中</title>
</head>
<body>

<style type="text/css" rel="stylesheet">
    .table {
        display: table;
        margin: 5px;
        width: 500px;
        height: 300px;
        background-color: #ccc;
    }

    .cell {
        display: table-cell;
        padding: 10px;
        vertical-align: middle;/*该属性是定义行内元素垂直对齐的,只有行内元素会生效。在表单元格中,这个属性会设置单元格框中的单元格内容的对齐方式。*/
    }
</style>
<div class="table">
        <div class="cell">努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力努力</div>
</div>
</body>
</html>

 

  

display:table

标签:

原文地址:http://www.cnblogs.com/zhangyanxia/p/4784856.html

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