【问题标题】:CSS centre a <ul> tag within div problemsCSS在div问题中居中<ul>标签
【发布时间】:2015-05-19 16:22:14
【问题描述】:

我试图将 UL 标记置于 DIV 中,但遇到了一些问题。我已经尝试了很多东西。代码附在下面。 UL 是一个水平列表。

<!-- Header -->
    <div id="header">
        <div class="container">

            <!-- Nav -->
                <nav id="nav">
                                            <ul>
                        <li <?php if($currenturl == 'index.php')  { ?>class="active"<?php } ?>><a href="index.php">Home</a></li>
                        <li <?php if($currenturl == 'limitededitions.php')  { ?>class="active"<?php } ?>><a href="limitededitions.php">Limited Editions</a></li>
                        <li <?php if($currenturl == 'left-sidebar.php')  { ?>class="active"<?php } ?>><a href="left-sidebar.php">Products</a></li>
                        <li <?php if($currenturl == 'services.php')  { ?>class="active"<?php } ?>><a href="services.php">Services</a></li>
                        <li <?php if($currenturl == 'suppliers.php')  { ?>class="active"<?php } ?>><a href="suppliers.php">Suppliers</a></li>
                        <li <?php if($currenturl == 'right-sidebar.php')  { ?>class="active"<?php } ?>><a href="right-sidebar.php">About</a></li>
                        <li <?php if($currenturl == 'no-sidebar.php')  { ?>class="active"<?php } ?>><a href="no-sidebar.php">Contact</a></li>
                        <li><a target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_cart&amp;business=WEM898SE24RZL&amp;display=1">View Cart</a></li>
                    </ul>

                </nav>

        </div>
    </div>
<!-- Header -->


/*********************************************************************************/
/* Header                                                                        */
/*********************************************************************************/

#header
{
    position: relative;
    background: #FFF;
}

    #header .container
    {
        position: relative;
        padding: 3em 0em;
    }


/*********************************************************************************/
/* Nav                                                                           */
/*********************************************************************************/

#nav
{
    position: relative;
    top: 1em;
}

    #nav > ul > li
    {
        float: left;
    }

        #nav > ul > li:last-child
        {
            padding-right: 0;
        }

        #nav > ul > li > a,
        #nav > ul > li > span
        {
            display: block;
            padding: 0.80em 1.2em;
            letter-spacing: 0.06em;
            text-decoration: none;
            text-transform: uppercase;
            font-size: 1.1em;
            outline: 0;
            color: #333;
        }

        #nav li.active a
        {
            background: #8B5A2B;
            border-radius: 5px;
            color: #FFF;
        }

        #nav > ul > li > ul
        {
            display: none;
        }

任何帮助将不胜感激!谢谢。

【问题讨论】:

  • 如何你想把这个居中?我在您的代码中没有看到任何 text-align:centermargin:0 auto 属性。甚至没有&lt;center&gt;
  • 我试过 margin-left: auto;边距右:自动;文本对齐:居中;边距:0 自动;左:50% 和左:-50%;都在很多地方。我是css的初学者这是我最差的技能哈哈。我只是很困惑,因为它是一个 div 中的一个 div,我已经认真尝试了两个小时,但不知道该放在哪里。

标签: html css class center centering


【解决方案1】:

我无法完全说出您想如何将其居中。你的浮动没有帮助,它是 100% 必要的吗?无论如何,这里有一个代码笔向您展示一种方法,但不确定它是否是您想要的方式。

我假设它是一种传统的导航,需要在页面中间居中并内联。

http://codepen.io/anon/pen/qEQodo

更改的 CSS:

#nav {
    position: relative;
    top: 1em;
    text-align:center;
    margin:0 auto;
}

#nav > ul {
    margin:0 auto;
    text-align:center;
    position:relative;
    display:block;
}

#nav > ul > li {
    display:inline;
    list-style-type:none;
    margin:10px; 
}

【讨论】:

  • 感谢您的回复。上面的代码将列表集中在一列中。我有一个连续的列表(例如 foo - bar - bat - hoo - moo)。删除左侧的浮动似乎再次使其成为单列。
  • 如果您单击 codepen,您会看到它在一行中,而不是一列。如果这仍然是您要寻找的东西,请创建一个 codepen,我会为您查看。
  • 谢谢。我没看。我已经添加了您的代码,还必须更改 display: block;在我的代码中的#nav > ul > li > span 下
猜你喜欢
  • 2015-10-27
  • 2012-12-24
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 2017-03-28
  • 2012-05-21
  • 1970-01-01
相关资源
最近更新 更多