Center
  • into div

2019-01-02 17:40发布

After some search, I did not find a proper way to center a list of <li> into a fixed width div.

Take a look at the page.. It doesn't work either!

16条回答
人间绝色
2楼-- · 2019-01-02 18:02

use oldschool center-tags

<div> <center> <ul> <li>...</li> </ul></center> </div>

:-)

查看更多
伤终究还是伤i
3楼-- · 2019-01-02 18:04

Here is the solution I could find:

#wrapper {
  float:right;
  position:relative;
  left:-50%;
  text-align:left;
}
#wrapper ul {
  list-style:none;
  position:relative;
  left:50%;
}

#wrapper li{
  float:left;
  position:relative;
}
查看更多
柔情千种
4楼-- · 2019-01-02 18:05

If you know the width of the ul then you can simply set the margin of the ul to 0 auto;

This will align the ul in the middle of the containing div

Example:

HTML:

<div id="container">
 <ul>
  <li>Item1</li>
  <li>Item2</li>
 </ul>
<div>

CSS:

  #container ul{
    width:300px;
    margin:0 auto;
  }
查看更多
萌妹纸的霸气范
5楼-- · 2019-01-02 18:06

Try

div#divID ul {margin:0 auto;}
查看更多
栀子花@的思念
6楼-- · 2019-01-02 18:08
<div id="container">
  <table width="100%" height="100%">
    <tr>
      <td align="center" valign="middle">
        <ul>
          <li>item 1</li>
          <li>item 2</li>
          <li>item 3</li>
        </ul>
      </td>
    </tr>
  </table>
</div>
查看更多
临风纵饮
7楼-- · 2019-01-02 18:10

To center a block object (e.g. the ul) you need to set a width on it and then you can set that objects left and right margins to auto.

To center the inline content of block object (e.g. the inline content of li) you can set the css property text-align: center;.

查看更多
登录 后发表回答