【问题标题】:How to center multiple line text in html using css, just like <center>?如何使用 css 将 html 中的多行文本居中,就像 <center> 一样?
【发布时间】:2016-09-05 04:40:25
【问题描述】:

我有这个 html 内容:

<div class="test">
  <a href=/cgi-bin/source/sourceSearch><img border=0 src=http://source-search.princeton.edu/images/SOURCE/sourcehdrBlue.gif></a>
  <h2 style='margin-bottom:0px'>BATCH</h2>
  <table border="0" cellpadding="0" width="600">
    <tr>
      <td><h3>SOURCE is now hosted by Princeton University. The supporting database was last updated on June 10, 2015. </h3> <p>This page is the batch extract interface for SOURCE.  You can input a list of GenBank Accessions, dbEST cloneIDs, UniGene ClusterIDs, UniGene gene names, UniGene gene symbols microarray Probes from various platforms (Affymetrix, Agilent, Heebo/meebo, Illumina) or coordinates (e.g.: chr1 14565 15000 - chromosome, from, to - one set per line) and retrieve data from the checklist below.  You will be given a link to the output file when processing is complete.</p> <p>You may enter the identifiers as a file or as a list in the text area below.  If you are entering an input file, it must be a text file consisting of a single column containing one of the accepted types of identifiers.</p> <p>Please see the <a href="http://source-search.princeton.edu/help/SOURCE/resultsBatchHelp.html">help</a> section for further information. Or go to <a href="http://source-search.princeton.edu/cgi-bin/source/sourceSearch">Single Entry SOURCE Search.</a> </p></td>
    </tr>
  </table>
</div>

只需添加&lt;center&gt;... &lt;/center&gt; 包围所有这些代码,这就是我想要的。 由于&lt;center&gt; 标签在 HTML 中已被弃用,我想使用 css 而不是 &lt;center&gt;。我的 css 代码是这样的:

div.test {
margin-left: auto;
margin-right: auto;
text-align: center;

}

我得到了这个:

我尝试了所有我能找到的东西,但它们都不适合我。我使用的是 windows 7 和 chrome,我见过 Why is the <center> tag deprecated in HTML?Simple center a object with css and no hacks

【问题讨论】:

  • 您担心使用已弃用的元素 &lt;center&gt;,但显然您对 bordercellpaddingwidth 属性没有任何疑虑,它们同样已被弃用!
  • 我希望我可以说您使用表格进行布局。 (这本来是使用表格的原因,虽然不是一个有效的表格。)但你不是,你使用表格根本没有任何目的!为什么?哦,未引用的 src URL 让我很痒。
  • @MrLister,放轻松,这只是一个例子。

标签: html css


【解决方案1】:

你使用&lt;table&gt;是有原因的吗?

改变你的CSS:

    div.test {
text-align: center;
}

div table{
  margin: 0 auto;
}

小提琴

click here for your code

这将回答您为什么不推荐使用 &lt;center&gt; answer here

【讨论】:

  • 哇!它对我有用,但你能给我一个解释吗?表格中的自动边距有什么作用?
  • 当您将边距设置为自动时,元素将占据指定的宽度,剩余空间将在左右边距之间平均分配。平均值前 0 - 顶部和底部边距为零。
  • 看看这个没有表格标签的例子。 jsfiddle.net/ak2o76pj/3
【解决方案2】:

.test
{
  text-align:center;
}
   div table
   {
  margin: 0 auto;
   }
<div class="test">
  <a href=/cgi-bin/source/sourceSearch><img border=0 src=http://source-search.princeton.edu/images/SOURCE/sourcehdrBlue.gif></a>
  <h2 style='margin-bottom:0px'>BATCH</h2>
  <table border="0" cellpadding="0" width="600" style="text-align:center">
    <tr>
      <td><h3>SOURCE is now hosted by Princeton University. The supporting database was last updated on June 10, 2015. </h3> <p>This page is the batch extract interface for SOURCE.  You can input a list of GenBank Accessions, dbEST cloneIDs, UniGene ClusterIDs, UniGene gene names, UniGene gene symbols microarray Probes from various platforms (Affymetrix, Agilent, Heebo/meebo, Illumina) or coordinates (e.g.: chr1 14565 15000 - chromosome, from, to - one set per line) and retrieve data from the checklist below.  You will be given a link to the output file when processing is complete.</p> <p>You may enter the identifiers as a file or as a list in the text area below.  If you are entering an input file, it must be a text file consisting of a single column containing one of the accepted types of identifiers.</p> <p>Please see the <a href="http://source-search.princeton.edu/help/SOURCE/resultsBatchHelp.html">help</a> section for further information. Or go to <a href="http://source-search.princeton.edu/cgi-bin/source/sourceSearch">Single Entry SOURCE Search.</a> </p></td>
    </tr>
  </table>
</div>

【讨论】:

    【解决方案3】:

    使用这个

    table { margin-left: auto;边距右:自动;文本对齐:居中; }

    div.test { 文本对齐:居中; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 2015-11-30
      • 2013-02-05
      • 2021-05-17
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      相关资源
      最近更新 更多