質問

I've been fighting with getting my CSS-only menu working in all versions of IE, and I think I've almost gotten it down, with the exception of IE7.

On the site in question, I'm using the following CSS:

.header-bar {
  position:fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 0;
  background: #333;
  font-size: 1.2em;
  font-weight: bold;
  z-index: 1000;
}
.header-bar a:hover {
  text-decoration: none;
}
.header-bar a {
  display: inline-block;
  padding: 7px 10px 7px 10px;
  color: #2C8FAA;
  font-size:1.1em;
}
.header-bar ul {
  list-style-type: none;
  display: inline-block;
  position: relative;
  height: auto;
}
.header-bar ul li {
  background: #333;
}
.header-bar ul li:hover {
  background: #444;
  color: #34AACB;
}
.header-bar ul.nav > li {
  display: inline-block;
}
.header-bar ul ul {
  display: none;
}
.header-bar ul ul li {
  border-top: 1px solid black;
}
.header-bar ul li:hover > ul {
  margin: 0;
  display: block;
  position: absolute; /* overrides other settings */
  /* top: 28px; */
  height: 28px;
  min-width: 200px;
}
.header-bar ul ul li:hover ul {
  top: 0; /* match position of parent, which should be position: relative; */
  position: absolute;
  margin-left: 199px; /* 150px min-width -1 px border */
  border-left: 1px solid black;
}
.header-bar ul li a { 
  display: block;
}
.header-bar ul li ul li {
  position: relative;
}
.header-bar ul ul li:hover ul ul {
  position: absolute;
  top: -48px; /* 28 + 1 for border */
  margin-left: 199px; /* 150px min-width -1 px border */
  border-left: 1px solid black;
}
.header-bar ul ul li:hover ul ul {
  top: 0;
}

To style HTML that looks like this:

<div class="header-bar">
  <div class="container">
    <ul class="nav">
      <li>
        <a href="#">Menu Item 1</a>
        <ul>
          <li>
            <a href="#">Menu Item 1, Submenu 1</a>
            <ul>
              <li><a href="#">Menu Item 1, Submenu 1, Sub-submenu 1</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>

This appears to work for the child menus, but the parent menu items appear to be adopting a block layout, rather an inline-block. I'm at my wits' end trying to fight IE at this point, and would appreciate a couple extra pairs of eyes.

役に立ちましたか?

解決

Try to close your li tags properly:

<div id="header-bar">
  <div class="container">
    <ul class="nav">
      <li><a href="#">Menu Item 1</a></li>
        <ul>
          <li><a href="#">Menu Item 1, Submenu 1</a></li>
            <ul>
              <li><a href="#">Menu Item 1, Submenu 1, Sub-submenu 1</a></li>
            </ul>
        </ul>
    </ul>
  </div>
</div>

--EDIT--

Use this at your css:

.header-bar ul.nav > li {
      display: inline-block;
      *float:left
}

It should make the trick.

他のヒント

A few things.

  1. The <LI> tags are not properly closed.
  2. The display: inline-block; is not supported.

To do:

  1. Close all the <li> tags properly.
  2. Use this CSS:

    .header-bar ul.nav > li {
        display: inline-block;
        *display: inline;
        *zoom: 1;
    }
    

Validation Issues

If still you are really worried about not having a hack, you can do this. Use Paul Irish's conditional HTML Classes this way:

<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->

And in the CSS, you can give this:

.ie6 .header-bar ul.nav > li,
.ie7 .header-bar ul.nav > li {
    display: inline;
    zoom: 1;
}

Now your HTML and CSS both are valid.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top