开发者

Divs align in parent block

#Menu
{
    position: relative;
    width: 90%;
    margin: 0 auto;
    height; 50px;
    min-height: 50px;
    padding: 5px;
}

And children blocks:

#MenuItem
{
    position: relative;
    float: left;

    margin: 0 auto;
    margin-top: 2px;
    margin-left: 5px;
    margin-right: 5px;

    text-align: center;
    width: 142px;
    height: 37px;
}

And html:

    <div id="Menu">
        <div id="MenuI开发者_如何转开发tem"><span>Home</span></div>
        <div id="MenuItem"><span>Home</span></div>
        <div id="MenuItem"><span>Home</span></div>
    </div>

How to make MenuItem divs to be centrated horizontally in parent div?


<ul id="nav">
  <li><a href="/">Home</a></li>
  <li><a href="/about/">About</a></li>
  <li><a href="/work/">Work</a></li>
  <li><a href="/clients/">Clients</a></li>
  <li><a href="/contact/">Contact</a></li>
</ul>

and here is the css

#nav{
  border:1px solid #ccc;
  border-width:1px 0;
  list-style:none;
  margin:0;
  padding:0;
  text-align:center;
}
#nav li{
  display:inline;
}
#nav a{
  display:inline-block;
  padding:10px;
}


The following code should do it

#Menu
{
    position: relative;
    width: 90%;
    margin: 0 auto;
    height; 50px;
    min-height: 50px;
    padding: 5px;
    text-align: center; /* added this rule */
}

#MenuItem
{
    position: relative;
    display: inline-block;  /* changed from float:left */

    margin: 0 auto;
    margin-top: 2px;
    margin-left: 5px;
    margin-right: 5px;

    text-align: center;
    width: 142px;
    height: 37px;
}

Also, I would recommend you'd use

<ul id="Menu">
  <li>...</li>
  <li>...</li>
</ul>

instead of the nested divs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜