悬停背景显示不正确?

时间:2014-03-28 10:58:32

标签: html css menu hover

我想实现这个目标: enter image description here

这就是我所取得的成就:LINK

将鼠标悬停在导航链接上时,应显示底部带有蓝色边框的白色背景。它没有出现。为什么会这样?

标记:

<header>
    <div>
        <h1>CertsPlan</h1>
        <section>
            <ul>
                <li><a href="javascript:void(0)">+ All Vendors</a></li>
                <li>
                    <article id="header-cart">
                        <img src="img/cart-icon.png" alt="Cart Icon"> 000
                    </article>
                </li>
                <li>
                    <article id="header-search">
                            <form action="#">
                              <input type="text" name="search" value="Exam Code">
                            </form>
                      </article>
                </li>
                <li><a href="javascript:void(0)">Sign In/Register</a></li>
            </ul>
        </section>
        <nav>
            <ul>
                <li><a href="index.html"><img src="img/home.png" alt="Home Icon"></a></li>
                <li><a href="javascript:void(0)">Bundle</a></li>
                <li><a href="javascript:void(0)">Faqs</a></li>
                <li><a href="javascript:void(0)">Contact Us</a></li>
            </ul>
        </nav>
    </div>
</header>

CSS:

/* Global Styles */
body {
    border-top: 2px solid #ffffff;
    background: #ffffff;
    font-family: 'Arial', sans-serif;
    color: #57616a;
}

p {
    margin: 5px 0;
    line-height: 25px;
}

strong {
    font-weight: bold;
}

/* Section Styles */

/* Header */
header {
    width:auto;
    margin: 2 auto 0 auto;
    background-color: #f7f7f7;
    border-bottom: solid 5px #dedede;
}

header div {
    width:auto;
    max-width:1040px;
    margin: auto;
    overflow: hidden;
}

header h1 {
    font-size: 30px;
    font-weight: 400;
    color: #2a323a;
    font-family: 'PT Sans', sans-serif;
    vertical-align: middle;
    margin-top: 30px;
    margin-bottom: 25px;
    float:left;
}


/* Navigation */
nav {
    float:right;
    margin:none;
}

header ul {
    list-style:none;
    margin: 0;
    overflow: hidden;
}

nav ul li {
  display: block;
  float: left;
  margin-top: 38px;
  margin-bottom: 35px;
}

nav ul li a {
  font-family: 'PT Sans', sans-serif;
  text-transform: uppercase;
  font-size: 14px;
  font-weight: 400;
  color: #2a323a;
  padding: 38px 20px 35px 20px;
  text-decoration:none;
}

nav ul li a:hover {
    background-color: #ffffff;
    border-bottom: solid 5px #00acdd;
}

ul li a:active {
    background-color: #ffffff; 
    border-bottom: solid 5px #00acdd !important;
}

/* Header top right area */

header section {
    float:right;
}

header section li {
    display:block;
    float:right;
    margin: 37px 0px 28px 7px;
}

header section a {
    padding: 10px;
    background: #31bbe2;
    font-family: 'PT Sans', sans-serif;
    font-size: 12px;
    font-weight: 400;
    color: #ffffff;
    text-decoration:none;
    border-radius: 5px;
}

3 个答案:

答案 0 :(得分:1)

你有两个父元素overflow: hidden,它隐藏了边框之间的所有内容:(图像上的蓝色)
 enter image description here
a标记上的蓝色边框超出了父级边框。

以下是解决方案:

  1. 应从overflow: hidden删除header div,然后添加min-height: 89px
  2. 也从overflow: hidden删除header ul
  3. nav ul li a中将padding更改为:38px 20px 33px
  4. 就是这样。

    修改

    如果您希望在调整浏览器大小时使其正常工作,请在div之后添加nav,如下所示:

    <nav>
       [...]
    </nav>
    <div class="clear"></div>
    

    clear div的样式:

    .clear {
       clear: both;
       display: table;
    }
    

答案 1 :(得分:0)

只需更改你的css类

nav ul li a
{
    font-family: 'PT Sans', sans-serif;
    text-transform: uppercase;
    font-size: 14px;
    font-weight: 400;
    color: #2a323a;
    padding: 38px 20px 30px 20px;  // This is where you are wrong
    text-decoration: none;
    height: 93px;
}

这是因为您在底部设置了太多填充,不允许出现边框

它会起作用

答案 2 :(得分:0)

<style>
    /* Global Styles */
body
{
    border-top: 2px solid #ffffff;
    background: #ffffff;
    font-family: 'Arial', sans-serif;
    color: #57616a;}

p
{
    margin: 5px 0;
    line-height: 25px;}

strong
{
    font-weight: bold;}

/* Section Styles */

/* Header */
header
{
    width:auto;
    margin: 2 auto 0 auto;
    background-color: #f7f7f7;
    border-bottom: solid 5px #dedede;
     height: 93px;
}

header div {
    width:auto;
    max-width:60%;
    margin: auto;
    overflow: hidden;
}

header h1
{
    font-size: 30px;
    font-weight: 400;
    color: #2a323a;
    font-family: 'PT Sans', sans-serif;
    vertical-align: middle;
    margin-top: 30px;
    margin-bottom: 25px;
    float:left;
}


/* Navigation */
nav {
    float:right;
    margin:none;
}

header ul
{
list-style:none;
margin: 0;
overflow: hidden;
}

nav ul li {
  display: block;
  float: left;
  margin-top: 38px;
  margin-bottom: 35px;
}

nav ul li a {
  font-family: 'PT Sans', sans-serif;
  text-transform: uppercase;
  font-size: 14px;
  font-weight: 400;
  color: #2a323a;
  padding: 38px 20px 35px 20px;
  text-decoration:none;
}

nav ul li a:hover {
  background-color: #ffffff;
  border-bottom: solid 8px #00acdd;
}

ul li a:active {background-color: #ffffff; border-bottom: solid 5px #00acdd !important;}

/* Header top right area */

header section {
    float:right;
}

header section li {
    display:block;
    float:left;
    margin: 37px 0px 28px 7px;
}

header section a {
    padding: 10px;
    background: #31bbe2;
    font-family: 'PT Sans', sans-serif;
    font-size: 12px;
    font-weight: 400;
    color: #ffffff;
    text-decoration:none;
    border-radius: 5px;
}
</style>
<header>
    <div>
        <h1>CertsPlan</h1>
        <section>
            <ul>
                <li><a href="javascript:void(0)">+ All Vendors</a></li>

                <li>
                    <article id="header-search">
                            <form action="#">
                              <input type="text" name="search" value="Exam Code">
                            </form>
                      </article>
                </li>
                <li>
                    <article id="header-cart">
                        <img src="img/cart-icon.png" alt="Cart Icon"> 000
                    </article>
                </li>
                <li><a href="javascript:void(0)">Sign In/Register</a></li>
            </ul>
        </section>
        <nav>
            <ul>
                <li><a href="index.html"><img src="img/home.png" alt="Home Icon"></a></li>
                <li><a href="javascript:void(0)">Bundle</a></li>
                <li><a href="javascript:void(0)">Faqs</a></li>
                <li><a href="javascript:void(0)">Contact Us</a></li>
            </ul>
        </nav>
    </div>
</header>