如何仅使用纯CSS引用多级元素?

时间:2014-11-05 17:33:35

标签: css drop-down-menu

将纯css用于水平菜单。 original code来自cssscript.com:

这是我的标记:

<ul class="main-navigation">
  <li><a href="#">Home</a></li>
  <li><a href="#">Front End Design</a>
    <ul>
      <li><a href="#">HTML</a></li>
      <li><a href="#">CSS</a>
        <ul>
          <li><a href="#">Resets</a></li>
          <li><a href="#">Grids</a></li>
          <li><a href="#">Frameworks</a></li>
        </ul>
      </li>
      <li><a href="#">JavaScript</a>
        <ul>
          <li><a href="#">Ajax</a></li>
          <li><a href="#">jQuery</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li><a href="#">WordPress Development</a>
    <ul>
      <li><a href="#">Themes</a></li>
      <li><a href="#">Plugins</a></li>
      <li><a href="#">Custom Post Types</a>
        <ul>
          <li><a href="#">Portfolios</a></li>
          <li><a href="#">Testimonials</a></li>
        </ul>
      </li>
      <li><a href="#">Options</a></li>
    </ul>
  </li>
  <li><a href="#">About Us</a></li>
</ul>

这是我的CSS:

.main-navigation{
  list-style: none;
  padding: 0;
  margin: 0;
  background: red;
}

.main-navigation ul li {
  display: block;
  position: relative;
  float: left;
  background: #1bc2a2;
}

/* This hides the dropdowns */


.main-navigation li ul { display: none; }

.main-navigation ul li a {
  display: block;
  padding: 1em;
  text-decoration: none;
  white-space: nowrap;
  color: #fff;
}

.main-navigation ul li a:hover { background: #2c3e50; }

/* Display the dropdown */


.main-navigation li:hover > ul {
  display: block;
  position: absolute;
}

.main-navigation li:hover li { float: none; }

.main-navigation li:hover a { background: #1bc2a2; }

.main-navigation  li:hover li a:hover { background: #2c3e50; }

.main-navigation li ul li { border-top: 0; }

/* Displays second level dropdowns to the right of the first level dropdown */


.main-navigation ul ul ul {
  left: 100%;
  top: 0;
}

/* Simple clearfix */



.main-navigation ul:before,
.main-navigation ul:after {
  content: " "; /* 1 */
  display: table; /* 2 */
}

.main-navigation ul:after { clear: both; }

这是原作:

ul {
  list-style: none;
  padding: 0;
  margin: 0;
  background: #1bc2a2;
}

ul li {
  display: block;
  position: relative;
  float: left;
  background: #1bc2a2;
}

/* This hides the dropdowns */


li ul { display: none; }

ul li a {
  display: block;
  padding: 1em;
  text-decoration: none;
  white-space: nowrap;
  color: #fff;
}

ul li a:hover { background: #2c3e50; }

/* Display the dropdown */


li:hover > ul {
  display: block;
  position: absolute;
}

li:hover li { float: none; }

li:hover a { background: #1bc2a2; }

li:hover li a:hover { background: #2c3e50; }

.main-navigation li ul li { border-top: 0; }

/* Displays second level dropdowns to the right of the first level dropdown */


ul ul ul {
  left: 100%;
  top: 0;
}

/* Simple clearfix */



ul:before,
ul:after {
  content: " "; /* 1 */
  display: table; /* 2 */
}

ul:after { clear: both; }

我担心如果我在页面的其他地方使用它们,这将会影响其他ul li等。它们与菜单无关。如何仅使用菜单引用这些样式?

1 个答案:

答案 0 :(得分:1)

CSS选择器中的空格定义了后代层次结构,因此当您更改它时:

ul li {

对此:

.main-navigation ul li {

您正在ul标记内查找列表项,该列表项位于具有.main-navigation类的元素内。

相反,要使其工作,您需要使用主类选择器替换原始样式表的根ul定义:

.main-navigation {
  list-style: none;
  padding: 0;
  margin: 0;
  background: #1bc2a2;
}

.main-navigation li {
  display: block;
  position: relative;
  float: left;
  background: #1bc2a2;
}

/* This hides the dropdowns */


.main-navigation li ul { display: none; }

.main-navigation li a {
  display: block;
  padding: 1em;
  text-decoration: none;
  white-space: nowrap;
  color: #fff;
}

/* And so on.... */

如果您需要进一步澄清,请与我们联系。