【问题标题】:centering div with css用css居中div
【发布时间】:2012-11-26 03:53:05
【问题描述】:

这是我的css文件中的sn-p

#centered{
    position: relative;
    text-align: center;
    width: 50%;
    margin-left: auto;
    margin-right: auto;
    }
#table{
    text-align: center;
    font-size: 1.5em;
    font-weight: 900;
    background-color: #5E9DC8;
}

这是我尝试使用的 html 部分:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
    <title>Bicycle Store Database</title>
    <link rel="stylesheet" type="text/css" href="web.css" />
</head>
<body>

    <h1>ACME BICYCLE SHOP</h1>
    <h2>GET IN GEAR!</h2>

    <div id="centered">
    <table id="table" border="0" cellpadding="10"> 
        <tr>
            <td><a href="search_inv.php" class="navTest">Go Shopping!</a><br/><br/>
            <a href="search_service.html" class="navTest">Check a Service Ticket</a></td>
        </tr>
    </table>
    </div>

    <p><br/><a href="index.html">HOME</a></p>
</body>
</html>

这是结果:

我读过的所有内容都表明我已经正确地做到了这一点,但它在我所有的浏览器中都偏离了中心。有什么想法吗?

【问题讨论】:

  • 你那里没有任何表格数据,你不应该使用表格元素。
  • 使用table 设置页面样式被认为是一种不好的做法;当需要显示表格数据时使用它。

标签: html css alignment


【解决方案1】:

您为什么要使用表格?有什么具体原因吗?就不能这么干吗?

<div class="center">
  <a href="#">Go Shopping</a>
  <br>
  <a href="#"></a>
</div>

.center {
   margin: auto;
   /* Other styles goes here, width height background etc */
}

【讨论】:

  • 谢谢大家,这一种总结了一切。我摆脱了桌子并修复了我的css,一切都被拨号了。
【解决方案2】:

您正在以#centered 为中心,但不是其中的表格。 将margin:0 auto; 添加到#table

【讨论】:

    【解决方案3】:

    div 居中。

    表格在 div 中左对齐。

    添加 table { margin-left: auto; margin-right: auto; } 以使表格在 div 中居中。

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2014-08-31
      • 1970-01-01
      • 2015-05-06
      • 2012-08-26
      • 1970-01-01
      • 2011-06-24
      • 2013-07-20
      • 2020-11-07
      相关资源
      最近更新 更多