让div以table的形式显示

本文介绍了一种使用HTML和CSS实现的三列等高表格布局方法,重点在于如何通过不同的CSS类来控制各列的宽度,并提供了针对Internet Explorer浏览器的兼容性解决方案。

<html>
<head>
<style>
 .equal {
  display:table;
  border-collapse:separate;
 }
 .row {
  display:table-row;
 }
 .row div {
  display:table-cell;
 }
 .row .one {
  width:200px;
 }
 .row .two {
  width:200px;
 }
 .row .three {
 
 }
</style>
<!--[if IE]>
<style type="text/css" media="all">
.equal, .row {
    display:block;
}
.row {
    padding:10px;
}
.row div {
    display:block;
    float:left;
    margin:0;
}
.row .two {
    margin-left:10px;
}
.row .three {
    width:160px;
    float:right;
}
.ieclearer {
    float:none;
    clear:both;
    height:0;
    padding:0;
    font-size: 2px;
    line-height:0;
}
    </style>
<![endif]-->

</head>
<body>
<div class="equal">
    <div class="row">
        <div class="one">dddddddddddddddd</div>
        <div class="two">dfffffffffffffffffff</div>
        <div class="three">faaaaaaaaaaaaaaaaaaaaaa</div>
        <!--[if IE]>
        <div class="ieclearer"></div>
        <![endif]-->
    </div>
</div>
</body>
</html>
 

### 使用CSS实现div元素的居中显示 #### 方法一:通过`margin: 0 auto;`实现水平居中 当需要让一个固定宽度的块级元素在其父容器内水平居中时,可以使用`margin: 0 auto;`属性。这种方法适用于已知宽度的情况。 ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>水平居中</title> <style> body { text-align: center; } .div { margin: 0 auto; width: 400px; height: 100px; border: 1px solid #F00; } </style> </head> <body> <div class="div">DIVCSS5实例</div> </body> </html> ``` 此方法仅能实现水平方向上的居中[^1]。 --- #### 方法二:通过`display: table-cell;`和`vertical-align: middle;`实现垂直居中 如果希望子元素相对于其父容器在垂直方向上居中,则可以通过设置父容器为表格单元格的方式完成: ```css .parent { display: table-cell; vertical-align: middle; height: 300px; /* 设置父容器的高度 */ border: 1px solid black; /* 方便观察效果 */ } .child { width: 200px; height: 100px; background-color: lightblue; } ``` 这种方式适合于静态布局场景下的简单垂直居中需求[^2]。 --- #### 方法三:利用绝对定位(`position: absolute`)配合百分比偏移量以及变换函数(transform) 对于更复杂的嵌套结构或者动态尺寸的内容来说,采用绝对定位加transform可能是更好的解决方案之一: ```css .parent { position: relative; min-height: 300px; /* 或者其他指定高度 */ outline: thin dotted gray; /* 显示边界辅助调试 */ } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 200px; height: 100px; background-color: salmon; } ``` 这里的关键在于将`.child`放置到距离页面顶部与左侧各一半的位置之后再向反方向移动自身大小的一半来达到精确中心位置的效果[^2]。 --- #### 方法四:借助Flexbox模型达成灵活简便的双向(水平+垂直)居中目的 现代浏览器广泛支持flexible box layout module (简称 Flexbox),它允许开发者轻松创建弹性空间分布关系从而简化了许多传统难以处理的设计模式比如项目间的间隔调整等等。下面展示了一个典型例子说明怎样运用这一特性使内部对象既能在横向上也能纵向上处于正中央部位: ```css .container { display: flex; justify-content: center; /* 主轴方向(通常是横向)对齐方式 */ align-items: center; /* 交叉轴方向(通常是纵向)对齐方式 */ height: 400px; /* 定义外部区域的高度 */ border: 2px dashed navy; /* 添加视觉指示器帮助理解布局 */ } .item { padding: 2em; font-size: larger; color: white; background-color: darkcyan; } ``` 注意该方案默认情况下不被Internet Explorer版本低于10的产品所接受因此部署前需考虑目标受众群体的技术环境限制条件[^2]。 --- #### 总结 以上介绍了几种常见的基于纯CSS技术手段去解决不同情境下关于HTML文档片段内的特定区块如何做到屏幕中间呈现的问题。每种都有各自适用范围及优缺点,在实际开发过程中应根据具体业务逻辑选择最合适的一种或组合形式加以实施。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值