简体   繁体   English

如何创建固定在屏幕顶部并调整大小以适合所有屏幕尺寸的水平居中导航栏?

[英]How do I create a horizontally centered navigation bar that's fixed to the top of the screen and resizes to work on all screen sizes?

I am trying to create a simple navigation menu that is fixed to the top of the screen, and will take up 10% of any screen's height. 我正在尝试创建一个固定在屏幕顶部的简单导航菜单,它将占据任何屏幕高度的10%。 I also would like the navigation bar to be functional on all devices regardless of screen height and width; 我还希望导航栏在所有设备上都可以使用,而不管屏幕的高度和宽度如何; what I currently have gets messed up quite horribly if you should resize your screen or have a resolution that is naturally low. 如果您应该调整屏幕大小或分辨率自然较低,那么我目前所遇到的一切都非常糟糕。 Also, I would like to have the text for each tab to be centered vertically, a task I am struggling to fix, having tried many methods, such as changing the padding, vertical-align, margins, and so on. 另外,我想使每个选项卡的文本垂直居中,这是我正在努力解决的任务,尝试了许多方法,例如更改填充,垂直对齐,边距等。 I have searched and searched and tried many different approaches, such as using JavaScript to accomplish this, all without luck. 我已经搜索并尝试了许多不同的方法,例如使用JavaScript来做到这一点,而这些都没有运气。

My current css: 我当前的CSS:

        #nav {
            height:10%;
            background-color:rgb(52, 152, 219);
            position:fixed;
            top:0;
            left:0;
            width:100%;
            color:white;
            font-family: Calibri;
            font-size:24pt;
            text-align:center;
        }

        #nav a {
            display:inline-block;
            height:100%;
            padding-left:15px;
            padding-right:15px;
        }
        #nav a:hover {
            background-color:rgb(41, 128, 185);
        }

And my HTML: 而我的HTML:

     <div id="main">
        <div id="nav">
            <a>Home</a>
            <a>Page2</a>
            <a>Page3</a>
            <a>Page4</a>
            <a>Page5</a>

        </div>
    </div>

Thanks! 谢谢!

I fixed a couple things in your css and wrapped the links in div's. 我在您的CSS中修复了一些问题,并将链接包装在div中。

        #nav {
            height:10%;
            background-color:rgb(52, 152, 219);
            position:fixed;
            top:0;
            left:0;
            width:100%;
            color:white;
            font-family: Calibri;
            font-size:24pt;
            text-align:center;
        }

        #nav div {
            display:inline-block;
            padding:15px;
        }
        #nav div:hover {
            background-color:rgb(41, 128, 185);
            cursor:pointer;
        }

And the HTML 和HTML

     <div id="main">
        <div id="nav">
            <div><a>Home</a></div>
            <div><a>Page2</a></div>
            <div><a>Page3</a></div>
            <div><a>Page4</a></div>
            <div><a>Page5</a></div>
        </div>
    </div>

Looked ok on browser resize, let me know if that's still an issue. 在调整浏览器大小时看起来还可以,请告诉我这是否仍然是一个问题。

You can add to your nav css "z-index: -1;" 您可以将其添加到导航css“ z-index:-1;”。 to set it behind the rest of the content. 将其设置在其余内容的后面。 You may need to add "z-index: -2; to you body css to keep your background image behind the nav bar. That should fix it to the top of your screen at least and have it remain there while the rest of the content scrolls 您可能需要在正文css上添加“ z-index:-2;”,以将背景图像保持在导航栏的后面。这应至少将其固定在屏幕顶部,并在其余内容保留在那里卷轴

As for having the sizing go crazy, it's because you have a set font size. 至于使大小变得疯狂,是因为您有固定的字体大小。 I think setting your font size to 2em will keep it at 24 for common size screens, but will allow it to adjust to smaller windows. 我认为将字体大小设置为2em可使普通大小的屏幕保持在24em,但可以将其调整为较小的窗口。

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

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