Align a div to center

后端 未结 14 2061
别跟我提以往
别跟我提以往 2020-12-02 13:20

I want to float a div to center. Is it possible? text-align: center is not working in IE.

相关标签:
14条回答
  • 2020-12-02 13:32

    This worked for me.

    I included an unordered list on my page twice. One div class="menu" id="vertical" the other to be centered was div class="menu" id="horizontal". Since the list was floated left, I needed an inner div to center it. See below.

    <div class=menu id="horizontal">
    <div class="fix">
      Centered stuff
    </div>
    </div>
    
    .menu#horizontal { display: block;  float: left; margin: 0px; padding: 0 10px; position: relative; left: 50%; }
    #fix { float: right; position: relative; left: -50%; margin: 0px auto; }
    
    0 讨论(0)
  • 2020-12-02 13:33

    this could help you..:D

    div#outer {
      width:200px;
      height:200px;
      float:left;
      position:fixed;
      border:solid 5px red;
    }
    div#inner {
      border:solid 5px green;
    }
    <div id="outer">
      <center>
        <div id="inner">Stuff to center</div>
      </center>
    </div>

    0 讨论(0)
  • 2020-12-02 13:34

    this works nicely

    width:40%; // the width of the content div
    right:0;
    margin-right:30%; // 1/2 the remaining space
    

    This resizes nicely with adaptive layouts also..

    CSS example would be:

    .centered-div {
       position:fixed;
       background-color:#fff;
       text-align:center;
       width:40%;
       right:0;
       margin-right:30%;
    }
    
    0 讨论(0)
  • 2020-12-02 13:42

    The usual technique for this is margin:auto

    However, old IE doesn't grok this so one usually adds text-align: center to an outer containing element. You wouldn't think that would work but the same IE's that ignore auto also incorrectly apply the text align center to block level inner elements so things work out.

    And this doesn't actually do a real float.

    0 讨论(0)
  • 2020-12-02 13:45

    Following solution worked for me.

      .algncenterdiv {
        display: block;
        margin-left: auto;
        margin-right: auto;
        }
    
    0 讨论(0)
  • 2020-12-02 13:45

    Use "spacer" divs to surround the div you want to center. Works best with a fluid design. Be sure to give the spacers height, or else they will not work.

    <style>
    div.row{width=100%;}
    dvi.row div{float=left;}
    #content{width=80%;}
    div.spacer{width=10%; height=10px;}
    </style>
    
    <div class="row">
    <div class="spacer"></div>
    <div id="content">...</div>
    <div class="spacer"></div>
    </div>
    
    0 讨论(0)
提交回复
热议问题