导航栏是否根据窗口大小变化?

问题描述 投票:0回答:1

我正在整个文档中运行媒体查询,但是在这里我只想解释发生了什么。无论使用什么设备,都可以使此菜单保持居中状态?因此,如果没有足够的空间让所有菜单项都停留在一行上,它会将其余菜单项放在下面的行中,然后在屏幕上显示centres,而不是从左侧开始新行?它可能与菜单上剩余的浮点数有关,但这是使滚动条保持水平的唯一方法。谢谢。

下面的HTML和CSS,但我建议在JSFiddle上查看它:https://jsfiddle.net/rt518s7p/

HTML:

<nav id="page-navigation">
      <ul>
            <li><a href="../../index.html" title="Home">Home</a></li>
                <ul class="top-menu">
                    <li><a href="../../_portfolio/photography.html" onclick="return false">Photography</a>
                    <ul class="sub-menu">
                        <li><a href="../../_portfolio/_photography/bmc-himley-mini-show-2015.html">BMC Himley Mini Show 2015</a></li>
                        <li><a href="../../_portfolio/_photography/kinver-snow.html">Kinver Snow</a></li>
                        <li><a href="../../_portfolio/_photography/mini-runs-collection.html">"Mini Runs" Collection</a></li>
                        <li><a href="../../_portfolio/_photography/hofner-bass.html">Hofner Bass</a></li>
                        <li><a href="../../_portfolio/_photography/nature.html">Nature</a></li>
                        <li><a href="../../_portfolio/_photography/haynes-motor-museum.html">Haynes Motor Museum</a></li>
                        <li><a href="../../_portfolio/_photography/miscellaneous.html">Miscellaneous</a></li>
                        <li><a href="../../_portfolio/_photography/classic-mini.html">Classic Mini</a></li>
                    </ul>
                </li>
                    <li><a href="../../_portfolio/graphic-design.html" onclick="return false">Graphic Design</a>
                    <ul class="sub-menu">
                        <li><a href="../../_portfolio/_graphic-design/story-bag-artwork.html">"Story Bag" Artwork</a></li>
                        <li><a href="../../_portfolio/_graphic-design/business-cards.html">Business Cards</a></li>
                        <li><a href="../../_portfolio/_graphic-design/logo-design.html">Logo Design</a></li>
                        <li><a href="../../_portfolio/_graphic-design/the-mexican-job.html">"The Mexican Job"</a></li>
                        <li><a href="../../_portfolio/_graphic-design/magazine-covers.html">Magazine Covers</a></li>
                        <li><a href="../../_portfolio/_graphic-design/wpap-artwork.html">WPAP Artwork</a></li>
                        <li><a href="../../_portfolio/_graphic-design/lyric-posters.html">Lyrics Posters</a></li>
                    </ul>
                </li>
                    <li><a href="../../_portfolio/3d-modelling.html">3D Modelling</a></li>
                </ul>
            <li><a href="../../about.html">About</a></li>
            <li><a href="../../recognition.html">Recognition</a></li>
      </ul>
  </nav>

CSS:

    /*navigation*/
#page-navigation
{
    width: 70%;
    height: 53px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 10px;
}

#page-navigation ul li
{
    color: white;
    list-style: none;
    background-color: darkslategray;
    width: 9em;
    float: left;
}

li
{
    position: relative;
}

li.title
{
    display: none;   
}

li a
{
    display: block;
    color: white;
    line-height: 1.3em;
    padding: 1em;
    text-align: center;
}

li a:link
{
    text-decoration: none;
}

li a:visited
{
    text-decoration: none;
    color: white;
}

li a:hover, .top-menu > li:hover > a
{
    background-color: rgb(48,48,48);
}

li a:active
{
    background-color: dimgray;
}

ul.sub-menu
{
    width: auto;
    height: auto;
    position: absolute;
    left: -9000em;
    overflow: hidden;
}

ul.sub-menu li
{
    clear: left;
    float: none;
    margin-left: -2.5em;
    z-index: 1000;
}

.top-menu li:hover ul
{
    left: 0;
}

ul.sub-menu li a
{
    height: auto;
    border-bottom: 1px solid gray;
    padding: .4em 1em;
    background-color: dimgray;
    padding-top: 1em;
    padding-bottom: 1em;
}

ul.sub-menu li:last-child a
{
    border-bottom: none;
}

ul.sub-menu li a:hover
{
    background-color: darkslategray;
}

ul.sub-menu li a:active
{
    background-color: gray;
}

ul.top-menu
{
    padding: 0;   
}
html css css3 css-float nav
1个回答
1
投票

答案:

由于HTML文档的结构方式,由于以下原因,您无法获得预期的效果:

  • 您有一个直接嵌套在另一个无序列表中的无序列表,该列表是(1)不正确(see this discussion);但更重要的是,尽管您的导航看起来有6个顶级项,但实际上实际上只有4个。因此,无论您对它应用什么CSS,它都不会起作用。

推荐:

  1. 首先仅在顶部导航项上使用适当的类来固定HTML文档的结构,然后正确嵌套导航项。*

  2. 我建议您重组信息架构,以使菜单上的导航项更少。例如,在您的“关于”页面中进行识别将很有意义。如果这是一个投资组合类型的网站,那么将您的摄影,图形设计和3D建模整合到项目中将会很好。而且,如果您担心分离,则将在页面内作为子导航进行。

  3. 如果您设置为保留导航结构,建议您将菜单折叠到移动设备上的选择菜单或汉堡菜单中,因为导航会占用某人的很大一部分移动设备屏幕,对于您的用户。最重要的是,您必须考虑到用户无法在移动设备上“悬停”,并且这些下拉列表的大小最多很难导航。

*解决方案: Demo

HTML(固定):

<nav id="page-navigation">
  <ul>
    <li><a href="../../index.html" title="Home">Home</a></li>
    <li class="top-menu"><a href="../../_portfolio/photography.html" onclick="return false">Photography</a>
      <ul class="sub-menu">
        <li><a href="../../_portfolio/_photography/bmc-himley-mini-show-2015.html">BMC Himley Mini Show 2015</a></li>
        <li><a href="../../_portfolio/_photography/kinver-snow.html">Kinver Snow</a></li>
        <li><a href="../../_portfolio/_photography/mini-runs-collection.html">"Mini Runs" Collection</a></li>
        <li><a href="../../_portfolio/_photography/hofner-bass.html">Hofner Bass</a></li>
        <li><a href="../../_portfolio/_photography/nature.html">Nature</a></li>
        <li><a href="../../_portfolio/_photography/haynes-motor-museum.html">Haynes Motor Museum</a></li>
        <li><a href="../../_portfolio/_photography/miscellaneous.html">Miscellaneous</a></li>
        <li><a href="../../_portfolio/_photography/classic-mini.html">Classic Mini</a></li>
      </ul>
    </li>
    <li class="top-menu"><a href="../../_portfolio/graphic-design.html" onclick="return false">Graphic Design</a>
      <ul class="sub-menu">
        <li><a href="../../_portfolio/_graphic-design/story-bag-artwork.html">"Story Bag" Artwork</a></li>
        <li><a href="../../_portfolio/_graphic-design/business-cards.html">Business Cards</a></li>
        <li><a href="../../_portfolio/_graphic-design/logo-design.html">Logo Design</a></li>
        <li><a href="../../_portfolio/_graphic-design/the-mexican-job.html">"The Mexican Job"</a></li>
        <li><a href="../../_portfolio/_graphic-design/magazine-covers.html">Magazine Covers</a></li>
        <li><a href="../../_portfolio/_graphic-design/wpap-artwork.html">WPAP Artwork</a></li>
        <li><a href="../../_portfolio/_graphic-design/lyric-posters.html">Lyrics Posters</a></li>
      </ul>
    </li>
    <li><a href="../../_portfolio/3d-modelling.html">3D Modelling</a></li>
    <li><a href="../../about.html">About</a></li>
    <li><a href="../../recognition.html">Recognition</a></li>
  </ul>
</nav>

CSS(已修复和更新):

/*navigation*/

#page-navigation {
  width: 100%;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 10px;
}

#page-navigation ul {
  text-align: center;
}

#page-navigation ul li {
  color: white;
  list-style: none;
  background-color: darkslategray;
  width: 9em;
  /* float: left removes any possibility of it centering */
  display: inline-block;
}

li {
  position: relative;
}

li.title {
  display: none;
}

li a {
  display: block;
  color: white;
  line-height: 1.3em;
  padding: 1em;
  text-align: center;
}

li a:link {
  text-decoration: none;
}

li a:visited {
  text-decoration: none;
  color: white;
}

li a:hover,
.top-menu > li:hover > a {
  background-color: rgb(48, 48, 48);
}

li a:active {
  background-color: dimgray;
}

ul.sub-menu {
  width: auto;
  height: auto;
  position: absolute;
  left: -9000em;
  overflow: hidden;
}

ul.sub-menu li {
  clear: left;
  float: none;
  margin-left: -2.5em;
  z-index: 1000;
}

.top-menu:hover ul {
  left: 0;
}

ul.sub-menu li a {
  height: auto;
  border-bottom: 1px solid gray;
  padding: .4em 1em;
  background-color: dimgray;
  padding-top: 1em;
  padding-bottom: 1em;
}

ul.sub-menu li:last-child a {
  border-bottom: none;
}

ul.sub-menu li a:hover {
  background-color: darkslategray;
}

ul.sub-menu li a:active {
  background-color: gray;
}

ul.top-menu {
  padding: 0;
}

仍有一些较小的样式可以调整,但这将根据您的问题为您提供想要的东西。

© www.soinside.com 2019 - 2024. All rights reserved.