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

时间:2013-12-16 12:08:19

标签: html css html5 css-position

我目前正在我的网站上完成一些事情,但是我注意到了一个问题。 我有一个顶部栏菜单,其位置是固定的。

主体和标题的最小宽度均为1150,以防止布局变得错误(Primairly因为管理部分)。

但似乎固定位置不仅要确保它会粘在顶部,而且还要向左移动。

无论如何都要让酒吧坚持到顶部,而不是向左,所以它仍然可以从左向右滚动,以确保我能看到其他菜单项?

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

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

亲切的问候,
Larssy1


我更喜欢仅使用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;
}

2 个答案:

答案 0 :(得分:1)

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

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

这样在屏幕调整大小时它就会停留。 将最大宽度更改为您想要的值。 但话说再说它可能不会留在顶端

答案 1 :(得分:0)

您需要从width 100%中移除header,然后将leftright添加为0,然后制作margin:0 auto

#header {
background: #fff;
height: 50px;
border-bottom: 3px solid #ff6633;
margin-bottom: 20px;
position: fixed;
top: 0px;
left: 0px;
right: 0;
width: auto;
min-width: 1150px;
z-index: 9999;
margin: 0 auto;
display: inline-block;
}