CSS结构好的话,没有必要使用过多的类或者标识选择符。这是因为你可以指定在选择符内的选择符,而不必使用CSS嵌套。(或者更好的说法,上下文选择符--译者著)
1、比如:
ExampleSourceCode#top{ background-color:#ccc padding:1em } #toph1{ color:#ff0 } #topp{ color:red font-weight:bold }
2、这就减去不必要的类或者标识选择符,如果应用到像这样的HTML中:
ExampleSourceCode<dividdivid="top"> <h1>Chocolatecurry</h1> <p>Thisismyrecipeformakingcurrypurelywithchocolate</p> <p>Mmmmmmmmmm</p> </div>
这是因为,用英文半角空格间隔选择符,我们指明了在标识id内的h1有“#ff0”的颜色,而p则是红色red和粗体bold。这可能也会有些复杂(因为可能不止两级,比如在内在内在内在内等等)。有必要多加练习。
<div class="box"><div>
<div>
<div class="contont">内容</div>
</div>
</div>
</div>
一般情况下是会设置一个类名来进行设置样式的,
像这样可以通过子代选择器来设置,例如:
.box>div>div>div{color:#f00} 但是假如最里层有多个div那也会被设置当前样式,所以这样虽然可以设置到,但是不合理。
通过后代选择器来设置: .box .contont{ color:#f00} 这样就是设置自代类名为 .contont的盒子了。
这里有一个二级菜单,下拉导航条的源代码,希望对你有用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html charset=utf-08" />
<title>dropline-水平三级横向弹出菜单</title>
<style type="text/css">
/* common styling */
.hd3_nav {font-family: arial, sans-serif width:749px height:30px position:relative margin:0 font-size:11px margin:0 background:#fff position:relative}
.hd3_nav ul {padding:0 margin:0 list-style-type: none}
.hd3_nav ul li {float:left border-left:1px solid #eee width:106px}
.hd3_nav ul li a, .hd3_nav ul li a:visited {display:block float:left width:101px text-decoration:none padding:0 0 0 5px height:30px line-height:30px color:#000 background:#c9c9a7 url(../../graphics/drop.gif) bottom right no-repeat}
.hd3_nav ul li ul {display: none}
/* specific to non IE browsers */
.hd3_nav ul li:hover a {color:#fff background:#b3ab79}
.hd3_nav ul li:hover ul {display:block position:absolute width:749px top:30px left:0 background:#b3ab79 color:#fff}
.hd3_nav ul li:hover ul.right_side li {float:right border:0 border-left:1px solid #eee}
.hd3_nav ul li:hover ul.left_side li {float:left border:0 border-left:1px solid #eee}
.hd3_nav ul li:hover ul li a.hide {background:#bd8d5e url(../../graphics/drop2.gif) bottom right no-repeat color:#fff}
.hd3_nav ul li:hover ul li:hover a.hide {background:#b3ab79 color:#fff}
.hd3_nav ul li:hover ul li ul {display: none}
.hd3_nav ul li:hover ul li a {display:block background:##b3ab79 color:#fff}
.hd3_nav ul li:hover ul li a:hover {background:#dfc184 color:#000}
.hd3_nav ul li:hover ul li:hover ul {display:block position:absolute left:0 top:30px color:#000 background:#dfc184}
.hd3_nav ul li:hover ul.right li {float:right}
.hd3_nav ul li:hover ul li:hover a.hide {background:#dfc184 color:#000}
</style>
</head>
<body>
<div class="hd3_nav">
<ul>
<li><a class="hide" href="../hd3_nav/index.html">第1个</a>
<ul>
<li><a href="../hd3_nav/zero_dollars.html" title="The zero dollar ads page">zero dollars</a></li>
<li><a href="../hd3_nav/embed.html" title="Wrapping text around images">wrapping text</a></li>
<li><a href="../hd3_nav/form.html" title="Styling forms">styled form</a></li>
<li><a href="../hd3_nav/nodots.html" title="Removing active/focus borders">active focus</a></li>
<li><a href="../hd3_nav/hover_click.html" title="Hover/click with no active/focus borders">HOVER/CLICK</a></li>
<li><a href="../hd3_nav/shadow_boxing.html" title="Multi-position drop shadow">shadow boxing</a></li>
<li><a href="../hd3_nav/old_master.html" title="Image Map for detailed information">image map</a></li>
</ul>
</li>
<li><a class="hide" href="index.html">第2个</a>
<ul>
<li><a href="spies.html" title="a coded list of spies">spies hd3_nav</a></li>
<li><a href="vertical.html" title="a horizontal vertical hd3_nav">vertical hd3_nav</a></li>
<li><a href="expand.html" title="an enlarging unordered list">enlarging list</a></li>
<li><a href="enlarge.html" title="an unordered list with link images">link images</a></li>
<li><a href="cross.html" title="non-rectangular links">non-rectangular</a></li>
<li><a href="jigsaw.html" title="jigsaw links">jigsaw links</a></li>
<li><a href="circles.html" title="circular links">circular links</a></li>
</ul>
</li>
<li><a class="hide" href="../layouts/index.html">第3个</a>
<ul>
<li><a href="../layouts/bodyfix.html" title="Cross browser fixed layout">Fixed 1</a></li>
<li><a href="../layouts/body2.html" title="Cross browser fixed layout">Fixed 2</a></li>
<li><a href="../layouts/body4.html" title="Cross browser fixed layout">Fixed 3</a></li>
<li><a href="../layouts/body5.html" title="Cross browser fixed layout">Fixed 4</a></li>
<li><a href="../layouts/minimum.html" title="A simple minimum width layout">minimum width</a></li>
</ul>
</li>
<li><a class="hide" href="../boxes/index.html">第4个</a>
<ul>
<li><a href="spies.html" title="a coded list of spies">spies hd3_nav</a></li>
<li><a href="vertical.html" title="a horizontal vertical hd3_nav">vertical hd3_nav</a></li>
<li><a href="expand.html" title="an enlarging unordered list">enlarging list</a></li>
<li><a href="enlarge.html" title="an unordered list with link images">link images</a></li>
<li><a href="cross.html" title="non-rectangular links">non-rectangular</a></li>
<li><a href="jigsaw.html" title="jigsaw links">jigsaw links</a></li>
<li><a href="circles.html" title="circular links">circular links</a></li>
</ul>
</li>
<li><a class="hide" href="../mozilla/index.html">第5个</a>
<ul>
<li><a href="../mozilla/dropdown.html" title="A drop down hd3_nav">drop down hd3_nav</a></li>
<li><a href="../mozilla/cascade.html" title="A cascading hd3_nav">cascading hd3_nav</a></li>
<li><a href="../mozilla/content.html" title="Using content:">content:</a></li>
<li><a href="../mozilla/moxbox.html" title=":hover applied to a div">mozzie box</a></li>
<li><a href="../mozilla/rainbow.html" title="I can build a rainbow">rainbow box</a></li>
<li><a href="../mozilla/snooker.html" title="Snooker cue">snooker cue</a></li>
<li><a href="../mozilla/target.html" title="Target Practise">target practise</a></li>
</ul>
</li>
<li><a class="hide" href="../ie/index.html">第6个</a>
<ul class="right_side">
<li><a href="../ie/exampleone.html" title="Example one">example one</a></li>
<li><a href="../ie/weft.html" title="Weft fonts">weft fonts</a></li>
<li><a href="../ie/exampletwo.html" title="Vertical align">vertical align</a></li>
</ul>
</li>
<li><a class="hide" href="../opacity/index.html">第6个</a>
<ul class="right_side">
<li><a href="../opacity/colours.html" title="colour wheel">opaque colours</a></li>
<li><a href="../opacity/picturehd3_nav.html" title="a hd3_nav using opacity">opaque hd3_nav</a></li>
<li><a href="../opacity/png.html" title="partial opacity">partial opacity</a></li>
<li><a href="../opacity/png2.html" title="partial opacity II">partial opacity II</a></li>
<li><a href="../hd3_nav/hover_click.html" title="Hover/click with no active/focus borders">HOVER/CLICK</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>