固定在顶部栏上的 CSS 必须保持在左侧

标签 css html css-position

我目前正在整理网站上的一些内容,但我注意到了一个问题。 我有一个固定位置的顶部栏菜单。

正文和标题的最小宽度为 1150,以防止布局出现错误(主要是因为管理部分)。

但是固定位置好像不仅要贴在上面,还要贴在左边。

有没有什么方法可以使栏保持在顶部,而不是左侧,这样它仍然可以从左向右滚动以确保我可以看到其他菜单项?

实时预览:http://www.evesaints.com

只需调整窗口大小,注意当您向右滑动时,菜单也会移动,而不是粘在左边。

亲切的问候,
拉西1


我更喜欢纯 CSS 解决方案,我知道如何使用 JQuery 修复它,但我认为它很讨厌。


预览代码:http://jsfiddle.net/Jm297/

HTML:

<div id="container">
    <div id="header">
    <img src="http://www.evesaints.com/images/header.png" alt="EVE Probing Tracker" title="EVE Probing Tracker">
    <ul id="menu">
        <li><a href="http://www.evesaints.com/overview/">Overview</a></li>
        <li><a href="http://www.evesaints.com/news/">News</a></li>
        <li><a href="http://www.evesaints.com/how_to_play/">How to play?</a></li>
        <li><a href="http://www.evesaints.com/contact/">Contact</a></li>
    </ul>
    <a href="http://www.ignitecoding.com">
        <img src="http://www.evesaints.com/images/ignite_header.png" alt="IgniteCoding" title="IgniteCoding">
    </a>
    <ul id="menuAccount">
        <li id="topBarIskContainer"><a href="#"><span id="topBarIskBalance">CURRENT BALANCE</span><br>57,515,000 ISK</a></li>
        <li><a href="http://www.evesaints.com/dashboard/">Dashboard</a></li>
        <li><a href="http://www.evesaints.com/logout/">Logout</a></li>
    </ul>
</div>

CSS:

/*
    RESET
*/
* {
    margin: 0px;
    padding: 0px;
    font-family: "arial";
    font-size: 12px;
    z-index: 1;
}

html, body, #header {
    height: 100%;
    color: #fff;
    min-width: 1145px;
}

h4 {
    font-size: 14px;
}

body {
    background: #1d1d1d;
}

input, select, textarea {
    padding: 5px;
    display: block;
    margin: 5px;
}

.ui-autocomplete-input {
    margin: 5px;
}

.quote {
    padding: 10px;
    margin: 10px;
    background: #2b2b2b;
}

input[type="submit"],
input[type="button"] {
    background: #ff6633;
    border: 0px;
    padding: 10px;
    display: block;
    width: 150px;
    cursor: pointer;
    color: #fff;
}

#menu {
    float: left;
    list-style: none;
    display: inline;
}

#menuAccount {
    float: right;
    list-style: none;
    display: inline;
}

#header #menu li {
    display: inline-block;
}

#header #menu li a:link,
#header #menu li a:hover,
#header #menu li a:active,
#header #menu li a:visited,
#header #menu li a:focus {
    display: block;
    padding: 18px;
    color: #1d1d1d;
    text-decoration: none;
}

#header #menu li a:hover,
#header #menu li a:active {
    background-color: #ff6633;
    color: #fff;
}

#header #menuAccount > li {
    display: inline-block;
}

#header #menuAccount li a:link,
#header #menuAccount li a:hover,
#header #menuAccount li a:active,
#header #menuAccount li a:visited,
#header #menuAccount li a:focus {
    display: block;
    padding: 18px;
    background-color: #ff6633;
    color: #fff;
    text-decoration: none;
}

#header #menuAccount #topBarIskContainer a:link,
#header #menuAccount #topBarIskContainer a:hover,
#header #menuAccount #topBarIskContainer a:active,
#header #menuAccount #topBarIskContainer a:visited,
#header #menuAccount #topBarIskContainer a:focus {
    display: block;
    padding: 10px;
    background: none;
    color: #1d1d1d;
    text-decoration: none;
    cursor: default;
    text-align: center;
}
#header #menuAccount #topBarIskContainer #topBarIskBalance {
    font-size: 10px;
    font-weight: bold;
}
#header #menuAccount li:hover > ul {
    display: block;
    position: absolute;
    top: 51px;
    background: #fff;
    border-bottom: 3px solid #ff6633;
}
#header #menuAccount ul li:hover {
    background: #f9f9f9;
}
#header #menuAccount ul li {
    list-style: none;
}
#header #menuAccount ul li a:link,
#header #menuAccount ul li a:active,
#header #menuAccount ul li a:visited,
#header #menuAccount ul li a:focus {
    float: left;
    background: none !important;
    color: #1d1d1d !important;
}
#header #menuAccount ul li a:hover {
    background: #f1f1f1;
}
#header #menuAccount ul {
    display: none;
    z-index: 5;
}
#header {
    background: #fff;
    height: 50px;
    border-bottom: 3px solid #ff6633;
    margin-bottom: 20px;
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    z-index: 9999;
}
#header a:link img {
    border: none;
    margin: 10px;
}
#header a:link,
#header a:hover,
#header a:active,
#header a:visited,
#header a:focus {
    /*position: absolute;
    right: 0px;*/
    float: right;
}

#header > img {
    margin: 9px;
    float: left;
}

h1 {
    font-size: 30px;
    color: #ff6633;
}

h2 {
    font-size: 24px;
    color: #fff;
}

h2 a:link,
h2 a:hover,
h2 a:focus,
h2 a:visited,
h2 a:active {
    font-size: 24px;
    color: #fff;
}

h3 {
    font-size: 16px;
}

p {
    margin: 8px 0;
}

input {
    width: 266px;
}

a:link,
a:hover,
a:active,
a:visited,
a:focus {
    color: #ff6633;
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

.clear {
    clear: both;
}

.ui-button {
    width: 97px;
}

#container {
    width: 100%;
    min-width: 960px;
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -115px;
}

最佳答案

在底部的CSS文件中写一个媒体查询

@media screen and (max-width: 700px) {
   #header {
     position: absolute;
   }
}

这种方式在调整屏幕大小时保持不变。 将最大宽度更改为您想要的值。 但话又说回来,它可能不会留在顶部

关于固定在顶部栏上的 CSS 必须保持在左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20610602/

相关文章:

html - 如何从我网站的图像创建导航菜单?

javascript - 预呈现不等待动态页面标题和元标记呈现

html - 为什么 <h3> 和 <p> 之间有这么多空间

css - 绝对定位的内部div与父div的内容重叠

javascript - 如何向左打开(扩展) block 元素?

CSS:带有 position:absolute 的 UL 显示带有 position:fixed 和 overflow:hidden 的外部容器

css - 在没有垂直滚动的情况下嵌入 Gist

html - 为什么 Chrome 80 会导致这个 grid-template-rows : auto problem

javascript - javascript输入错误的意外结束

javascript - D3条形图图形表示问题