HTML:替代

问题描述 投票:7回答:5

我一直认为用<center>替换<div style="text-align:center;">标签会得到相同的结果。显然我错了。

这是我的HTML的一部分:(您也可以在我为此问题创建的页面中看到它的运行情况:http://www.catmoviez.com/ErrorPageSO.aspx

<div style="margin: 0 auto; background-color:red;border:5px solid black;margin-top:5px;width:750px;text-align:center;">
    <span style="width:560px;padding-right:10px;text-align:left;float:left;">
    <h1>Oops... We're sorry.</h1>

    <h3>You've just encountered an unknown error. <br /></h3>
    This site is a work-in-progress, we have already been informed of the error and will do our best to fix it. <br />
    We would be thankful if you could contact us through the appropriate button and elaborate on what caused this error to appear.<br />
    <br />
    <h3>
    You can go back to the <a style="text-decoration:underline;" href="Default.aspx">Home page</a> and continue using Moviez.NET.       
    </h3>
    </span><span style="width:180px;float:left;"><img src="Resources/Images/404.jpg" /></span>
</div>

我想做两件事:

  1. 摆脱<center>标签,同时保持div在页面的中心。
  2. 确保外部DIV背景颜色和边框影响内部跨度。

更新:目标1完成。目标#2的时间。

html xhtml
5个回答
10
投票

在封闭的margin: 0 auto;上使用<div>

<div style="margin: 0 auto; background-color:red;border:5px solid black;margin-top:5px;width:750px;text-align:center;">
  <span style="width:560px;padding-right:10px;text-align:left;">
  <h1>Oops... We're sorry.</h1>

  <h3>You've just encountered an unknown error. <br /></h3>
  This site is a work-in-progress, we have already been informed of the error and will do our best to fix it. <br />
  We would be thankful if you could contact us through the appropriate button and elaborate on what caused this error to appear.<br />
  <br />
  <h3>
  You can go back to the <a style="text-decoration:underline;" href="Default.aspx">Home page</a> and continue using Moviez.NET.           
  </h3>
  </span><span style="width:180px;"><img src="Resources/Images/404.jpg" /></span>
</div>

See it in action

参考:CSS: centering things


6
投票

如果你想简单地将文本居中,你就是这种css风格:

text-align:center;

但是,如果您希望将元素或div本身居中,那么有很多解决方案,一个在下面:

.mydiv
{
  margin:0 auto;
}

或者甚至用这样的东西:

.mydiv
{
  width:300px; // the width can sometimes be ignored based on inherent size of element.
  margin-left:auto;
  margin-right:auto;
}

或者甚至用这样的东西:

.mydiv
{
  margin-left:50%;
  margin-right:50%;
}

所以你看,有更多的可能性。


2
投票

内联内容与text-align对齐,块内容与边距对齐(对于居中的情况设置为auto)。见Centring Using CSS


-1
投票

如果你试图将div放在页面中心,我通常会使用这种方法将主包装div放在页面中心。

使左侧定位为50%,然后将后半部分的宽度调整为div。

以下示例。

#mainspace {
    position:absolute;
    left:50%;
    margin-left:-450px;
    height:auto;
    width:900px;
    border:none;
    }

-1
投票

试试这个,当我希望我还有那个时,它对我有用

<center>

标签

< p style="text:align-center" >  example image or text < / p >
© www.soinside.com 2019 - 2024. All rights reserved.