代码之家  ›  专栏  ›  技术社区  ›  pts

如何在不显示的情况下将DIV水平居中:表格?

  •  4
  • pts  · 技术社区  · 14 年前

    可以将多行居中吗 <div> 水平放置,内容保持左对齐( text-align:left )而不指定 display:table 而且不需要知道 <DIV & GT; (应根据需要变宽)?它必须在火狐3和Chrome中工作。

    我有以下工作方案,其中包含 <DIV & GT; 在另一个里面 <DIV & GT; 所以我只想找一个不需要内在的解决方案 <DIV & GT; .

    <style type="text/css">
    div.showtitle {
      margin-left:auto;  
      margin-right:auto;
      display:table;
    }
    div.showtitle > div {
      background:#7777ff;
      color:ffffff;
      text-align:left;
      padding:.34em;
      font-size:140%;
    }
    </style>
    <div class="showtitle"><div>Centered div<br>with left-aligned text</div></div>
    

    仅供参考为什么 显示:表 font-size:140% 不能组合在一起 <DIV & GT; 在铬合金中,它对 font-size 它的容器在javascript中被更改。

    6 回复  |  直到 14 年前
        1
  •  2
  •   Mikhail    14 年前
    <div style="margin:0 auto; width:300px">
      Text<br />
      text
    </div>
    
        2
  •  2
  •   Jacob    14 年前

    你真正需要的是在 div 并设置自动页边距。你不需要外皮 div ;元素将水平居中于包含它的任何块级元素中,包括主体。

    .showtitle 
    { 
        with: 350px; 
        margin-left: auto; 
        margin-right: auto; 
        text-align: left; 
    }
    

    更新 :

    不清楚您所说的希望DIV的宽度是动态的,但又是多行的意思。什么时候内容会自动换行,或者您只想手动换行?如果您要进行内容包装,那么当必须包装时,DIV会坚持100%吗?如果不希望宽度为100%,最大宽度应设置为什么?

    您提到您希望内容保持对齐,但是如果它包含在一个自动调整大小的居中块中,那么您基本上需要一个居中的内联元素。如果要将内容换行并左对齐,则必须指定宽度或使用100%的宽度。如果你想让文本行自动平衡,然后调整容器大小以适应,我相信就CSS提供的内容而言,你运气不好。

        3
  •  1
  •   Harmen    14 年前
    <div class="showtitle">Centered div<br>with left-aligned text</div>
    

    具有

    .showtitle {
        margin: 0 auto;
        width: ...;
    }
    

    在火狐和Chrome中应该可以正常工作。

        4
  •  1
  •   Dave Kiss    14 年前

    我可能读错了,但你应该能申请 margin: 0 auto; width:960px; 去你的表演中心,做个金色的。

        5
  •  1
  •   eyelidlessness    14 年前
    <style>
        #outer {
            text-align: center;
        }
        #inner {
            display: -moz-inline-box; /* Firefox < 3 */
            display: inline-block; /* Standards */
            /* IE < 8, inline + hasLayout */
            *display: inline;
            *zoom: 1;
            text-align: left;
        }
    </style>
    <div id="outer"><div id="inner">Your centered content here</div></div>
    
        6
  •  0
  •   pts    14 年前

    似乎不可能没有 display:table 或内部 <div> 或指定 <DIV & GT; .