简体   繁体   English

如何居中 <UL> 在清单中

[英]How to center <UL> in a list

I've got an issue that I can't resolve. 我有一个无法解决的问题。

I have a list of lists, like this one: 我有一个列表列表,像这样:

<ul id="nav">
    <li>
        <div  class="tabquad">
            Item 1
        </div>

        <ul>
            <li>
                <a class="item" href="index.jsp?quad=1">
                    <div  class="tabquad">
                        1.1
                    </div>
                </a>
            </li>
            <li>
                <a class="item" href="index.jsp?quad=2">
                    <div  class="tabquad">
                        1.2
                    </div>
                </a>
            </li>
            <li>
                <a class="item" href="index.jsp?quad=3">
                    <div  class="tabquad">
                        1.3
                    </div>
                </a>
            </li>
        </ul>
    </li>
    <li>
        <div  class="tabquad">
            Item 2
        </div>

        <ul>
            <li>
                <a class="item" href="index.jsp?quad=7">
                    <div  class="tabquad">
                        2.1
                    </div>
                </a>
            </li>
            <li>
                <a class="item" href="index.jsp?quad=8">
                    <div  class="tabquad">
                        2.2
                    </div>
                </a>
            </li>
            <li>
                <a class="item" href="index.jsp?quad=9">
                    <div  class="tabquad">
                        2.3
                    </div>
                </a>
            </li>
            <li>
                <a class="item" href="index.jsp?quad=10">
                    <div  class="tabquad">
                        2.4
                    </div>
                </a>
            </li>
            <li>
                <a class="item" href="index.jsp?quad=11">
                    <div  class="tabquad">
                        2.5
                    </div>
                </a>
            </li>
        </ul>
    </li>
</ul>

with this CSS: 使用此CSS:

#nav, #nav ul {
    padding: 0;
    margin: 0 auto;
    list-style: none;
}

#nav {
    width: 45em;
    margin: 0 auto;
}

#nav a {
    display: block;
    width: 15em;
}

#nav li {
    float: left;
    width: 15em;
}
#nav li ul {
    position: absolute;
    width: 15em;
    left: -500em;
}

#nav li:hover ul {
    left: auto;
}

#nav li:hover ul, #nav li.sfhover ul {
    left: auto;
}
.item{
    color:#0E4063;
    text-decoration:none;
}
.tabquad{
    color:white;
    margin:auto;
    position:relative;
    border:1px solid #000;
    border-color:rgb(82,115,154);
    width:200px;
    height:30px;
    text-align:center;
    padding-top:10px;
    top:25px;
    background-color:rgb(0,56,130);
}
    .tabquad:hover{
    background-color:rgb(49,87,132);
    cursor: hand;
}

My problem is: Item 1 list and Item 2 list are not in the center of my page, they are left-oriented. 我的问题是:项目1列表和项目2列表不在我页面的中心,它们是朝左的。

I've tried with the center parameter and with the float, without success. 我尝试使用center参数和float进行操作,但均未成功。

Any suggestion? 有什么建议吗?

You can find a fiddle here: Link 您可以在这里找到小提琴: 链接

you have to set the width of the #nav to the right value (corresponding to the width of the content). 您必须将#nav的宽度设置为正确的值(与内容的宽度相对应)。 change your example to the following value and the two menus will be centered on the page. 将您的示例更改为以下值,这两个菜单将在页面上居中。

#nav {
    width: 30em;
    margin: 0 auto;
}

updated jsFiddle: http://jsfiddle.net/F9Wrb/17/ 更新的jsFiddle: http : //jsfiddle.net/F9Wrb/17/

and just to increase cross-browser compatibility, add the following CSS to the parent node of your #nav: 只是为了提高跨浏览器的兼容性,请将以下CSS添加到#nav的父节点中:

body {
    text-align: center;
}

Here is your solution. 这是您的解决方案。 I have just changed these two styles. 我刚刚更改了这两种样式。 FYI, unless you are using html5, you cannot put div inside <a> tag; 仅供参考,除非您使用的是html5,否则不能将div放在<a>标记内; it's invalid. 这是无效的。

#nav, #nav ul {
    padding: 0;
    margin: 0 auto;
    list-style: none;
    text-align:center
}
#nav li {
    width: 15em;
    display:inline-block;
}

You might want to read about the basics of CSS page centering and page layouts. 您可能需要阅读CSS页面居中和页面布局的基础知识。

Normally, you'd have a "container" <div> that contains the entire page and defines the layout and boundaries of the page. 通常,您将有一个“容器” <div> ,它包含整个页面并定义页面的布局和边界。 You can call it anything (not necessarily container ) 您可以将其命名为任何东西(不一定是container

Now, you need to decide what layout fits you best, most commonly used layout sizes can vary anywhere between 960px to 1200px . 现在,您需要确定哪种布局最适合您,最常用的布局大小可以在960px1200px之间变化。

general styling for your container would be, 您的容器的一般样式是

.container{
    width:960px; //<-- this can be anything, based on your layout.
    margin:0 auto;
}

This ensures your page is centered almost always. 这样可以确保您的页面几乎始终居中。 You would want to use some kind of a CSS Reset to ensure you reset browser defaults 您可能想使用某种CSS重置来确保重置浏览器默认值

Three possible solutions: 三种可能的解决方案:

  1. Using absolute positioning and a negative left margin: 使用绝对定位和负的左边距:

     #nav { position: absolute; left: 50%; margin: 0 0 0 -15em; } 

    Demo 1 演示1

  2. Using auto margins with ul width specified: 使用指定了ul宽度的自动页边距:

     #nav { width: 30em; margin: 0 auto; } 

    Demo 2 演示2

  3. If you want to use the auto-margin solution and don't want to specify the ul width, declare the LIs as display: inline-block (instead of floating them). 如果要使用自动边距解决方案,并且不想指定ul宽度,则将LI声明为display: inline-block (而不是浮动它们)。 This works because the LIs will now be in the page flow instead of out of it, as they are when floated. 之所以可行,是因为LIs现在将在页面流中而不是从页面流中移出,就像它们在浮动时一样。 Answer courtesy of Kashif Raza. 回答由Kashif Raza提供。

     #nav { margin: 0 auto; } #nav > li { display: inline-block; width: 15em; } 

    Demo 3 演示3

#nav a {
    display: block;
    width: 15em;
    margin:auto;
}

#nav>li {
    width:50% !important;
}

#nav li:hover ul {
    position:relative;
    left: auto;
}

Make these changes in your CSS and lists will be in the center of your page. 在CSS中进行这些更改,列表将位于页面的中心。

Just put a width on the surrounding div, then change that to margin:0 auto, which will center it in the page. 只需在周围的div上放置一个宽度,然后将其更改为margin:0 auto,它将在页面中居中。

.center{
    width:400px;
    margin:0 auto;
}

And the html, rather than float="center" 和html,而不是float =“ center”

<div class="center">    

http://jsfiddle.net/F9Wrb/12/ http://jsfiddle.net/F9Wrb/12/

As mentioned, there is no such thing as float="center" , or for that matter, float:center; 如前所述,没有诸如float="center"类的东西,或者就没有float:center; ! For advanced positioning of elements, I'd recommend using a CSS framework such as zurb foundation or Gridpak.com 对于元素的高级定位,我建议使用CSS框架,例如zurb foundationGridpak.com

将此放置在您要居中的位置:

div align="center" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM