td中的多个并排的div水平居中

本文介绍了一种使HTML表格单元格(td)内的多个并排div水平居中的方法。通过在外层使用相对定位和left/right属性,解决了float布局与align属性冲突的问题,实现了跨浏览器兼容的居中效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

td中的多个并排的div水平居中lin49940

水平排列的div,因为要设置了float: left; , float 的优先级比 align 高,并且跟align 有冲突,所以就算td table 设置了align=“center”, td 里面的多个div 也无法居中。

解决方法参照了baihe107 同学在帮助别人的答案,“就是常见的位置宽度居中布局咯,可以使用传统的相对定位,不过这三个 div 需要外面增加层 div 来辅助布局”。


1. 在IE6,IE8和Firefox 测试可行,IE9 测试可行(以上浏览器都没有开兼容性视图)

2. 必须的样式,一个都不能少的

<style>
  .wrap1 {
    clear: left;
    float: left;
    position: relative;
    left: 50%;
  }  
  .content1 {
    float: left;
    position: relative;
    right: 50%;
  }
</style>

3.  布局, 需要在原来的div 上面套多2层div, 注意,放置div 的td 宽度太小的话,最右边的div 会被挤压到下一行的

<td  colspan="4">
  <div class="wrap1">
     <div class="content1">
      

             水平排列的多个div。。。。


     </div>
   </div>
</td>


demo 页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> td中的多个并排的div水平居中 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
</HEAD>

<BODY>
<style>
  .wrap1 {
    clear: left;
    float: left;
    position: relative;
    left: 50%;
  }  
  .content1 {
    float: left;
    position: relative;
    right: 50%;
  }
</style>
<table WIDTH="804px" BORDER="1" CELLSPACING="0" CELLPADDING="0">
<tr>
<td width="257" height="35"> </td>
<td  colspan="4">
  <div class="wrap1">
     <div class="content1">
      <div style="background: #6699cc none repeat scroll 0% 0%;float: left;">  
             首页
       </div>  
	   <div style="background: #6699ff none repeat scroll 0% 0%;float: left;">  
             首页2
       </div>  
     </div>
   </div>
</td>
<td width="157" height="35"> </td>
</tr>
</table>

</BODY>
</HTML>


<!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>EDA-Robot</title> <style> body { margin: 0; padding: 0; font-family: Arial, sans-serif; } .container { max-width: 800px; margin: 0 auto; padding: 20px; text-align: center; } h1 { text-align: center; } .button { display: inline-block; height: 30px; width: 300px; margin-top: 20px; padding: 10px 20px; background-color: deepskyblue; color: #fff; border: none; border-radius: 20px; /* 添加圆角 */ text-decoration: none; line-height: 2; /* 通过调整line-height的值来调整文字的垂直位置 */ text-align: center; /* 文字居中 */ box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2); /* 添加立体感 */ transition: all 0.3s ease; /* 添加过渡效果 */ } .button:hover { background-color: skyblue; /* 鼠标悬停时的背景颜色 */ transform: translateY(2px); /* 点击效果 */ box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3); /* 添加更多立体感 */ } .search-box { margin-top: 20px; display: inline-block; height: 30px; width: 300px; padding: 5px 10px; background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 20px; text-align: center; /* 文字居中 */ } .hidden { display: none; /* 初始隐藏 */ } </style> </head> <body> <form action='/connect' method='POST'> <div class='container'> <h1>EDA-Robot设备配置页</h1> <p>本项目基于ESP8266主控开发</p> <input type='text' name='ssid' placeholder='输入WIFI212名称' class='search-box'> <input type='password' name='pass' placeholder='输入WIFI密码' class='search-box'> <input type='uid' name='uid' placeholder='输入bilibili用户ID' class='search-box'> <input type='api' name='api' placeholder='输入心知天气API密钥' class='search-box'> <input type='city' name='city' placeholder='输入城市拼音小写' class='search-box'> <input type='submit' style="height: 50px;width: 320px" class='button' value="保存"> <a href="https://lceda.cn/"> <table class="container button" style="height: 200px"> <tr> <th>设备名称:</th> <td>EDA-Robot</td> </tr> <tr> <th>内存大小:</th> <td>4MB</td> </tr> <tr> <th>控制台版本:</th> <td>V1.0</td> </tr> <tr> <th>作者:</th> <td> 臣除君</td> </tr> </table> </a> </div> </form> </body> </html>为什么这里的 <tr> <th>设备名称:</th> <td>EDA-Robot</td> </tr>是横向排版的?
最新发布
08-12
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值