当前位置: 首页 > 图文教程 > 网页制作 > CSS样式表 > 英文教程:用CSS控制和操作BODY标记

CSS样式表
图片的alt(替换文本)属性描述
CSS布局实例代码 两列布局实例
将PSD网站模板转换为XHTML+CSS
div+css 定位浅析
CSS 浏览器专用
css设置z-index 失效的解决方法
Div+Css实现屏蔽效果
css 鼠标经过文字变色
不用JS只用CSS制作的网页下拉菜单
CSS 类名的长命名和短命名
css ul li 的使用及浏览器兼容问题
div中英文无法自动换行的解决办法
CSS 基础教程 在网页中使用CSS
CSS技巧 使用标签来创建导航菜单(滑动门教程)
用css创建一个类似按扭的导航
css 横向菜单实现代码
使用CSS做出一个嵌套导航.
css 列表菜单的设计
网页中图片应用CSS的滤镜的效果
CSS 漂亮搜索框美化代码

CSS样式表 中的 英文教程:用CSS控制和操作BODY标记


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 45 ::
收藏到网摘: n/a

Let’s say you want to change the color of your links on just your contact page to red. They are blue on every other page, but it just makes sense for them to be red on your contact page (for some reason). There are a couple ways you could go about this.

  • You could declare a separate stylesheet for your contact page. This isn’t ideal, because it’s redundant. If you make any other changes, you’ll always have to make them both on the main stylesheet and the contact page stylesheet.
  • You could give all those links a unique class on that page. This isn’t ideal, because it isn’t very semantic and it’s also redundant. Why apply a class to every single link on the page when they really aren’t any different from links elsewhere on the site, contextually speaking?
  • The best solution is to give your the body a unique ID. This solves the problem perfectly. You can use the same stylesheet and target just the links you want to with a single CSS selector.

 

How it’s done

Simple, literally just apply the ID to the body tag:

 ...
</head>
<body id="contact-page"> ...

Now for our example of making all links on the contact page red instead of blue, just use some CSS like this:

a {
color: blue;
}
#contact-page a { color: red;
}

 

How about a more practical example?

You got it. One of the most useful implementations of this technique is within navigation. Take a look at this sample navigation:

tabbednav.jpg

See how the forums tab is the “active” tab? Certainly that’s just a slight change in CSS, probably just a shift in the position of a background image. Perhaps the XHTML looks something like this:

...
<li><a href="/fieldtips">Field tips</a></li>
<li class="active"><a href="/forums">Forums</a></li>
...

The “active” class applied to the list item is what shifts the background image. That’ll do the trick, but what about when we move to the Field Tips page? We will have to remove the active class from the forums tab and apply it to the the Field Tips tab. That’s not very convenient. That means the code for the navigation block is unique on every single page of the site. So let’s say down the road we want to add a Contact tab, we’ll have to alter the code on every single page. No fun.

Let’s do this a little smarter. First we don’t want to include the navigation block of code on every page, we want to include it, probably with a simple PHP include like this:

<?php include_once("nav.html"); ?>

 

But then how do we apply the “active” class to the current navigation list element?

This is where apply what we just learned about giving unique ID’s to the body! Instead of applying a class to only the active list element, let’s apply a unique class to each separate list item as well as give our body an unique ID.

 ...
</head>
<body id="field-tips"> ... <li class="fieldtips"><a href="/fieldtips">Field tips</a></li> <li class="forums"><a href="/forums">Forums</a></li> ...

Now we can target specific elements in the navigation with some clever CSS:

#field-tips li.fieldtips, #forums li.forums { background-position: bottom;
}

This means that the code for the navigation block can stay the same on every page, yet only the navigation element native to that page will be affected by this CSS and “flip” to the active state.

 

Let’s get even more dynamic

Reader Brian left an awesome comment on how you can use PHP to apply the unique ID to to the body element:

<body id="<?= basename(

Let’s say you want to change the color of your links on just your contact page to red. They are blue on every other page, but it just makes sense for them to be red on your contact page (for some reason). There are a couple ways you could go about this.

  • You could declare a separate stylesheet for your contact page. This isn’t ideal, because it’s redundant. If you make any other changes, you’ll always have to make them both on the main stylesheet and the contact page stylesheet.
  • You could give all those links a unique class on that page. This isn’t ideal, because it isn’t very semantic and it’s also redundant. Why apply a class to every single link on the page when they really aren’t any different from links elsewhere on the site, contextually speaking?
  • The best solution is to give your the body a unique ID. This solves the problem perfectly. You can use the same stylesheet and target just the links you want to with a single CSS selector.

 

How it’s done

Simple, literally just apply the ID to the body tag:

 ...
</head>
<body id="contact-page"> ...

Now for our example of making all links on the contact page red instead of blue, just use some CSS like this:

a {
color: blue;
}
#contact-page a { color: red;
}

 

How about a more practical example?

You got it. One of the most useful implementations of this technique is within navigation. Take a look at this sample navigation:

tabbednav.jpg

See how the forums tab is the “active” tab? Certainly that’s just a slight change in CSS, probably just a shift in the position of a background image. Perhaps the XHTML looks something like this:

...
<li><a href="/fieldtips">Field tips</a></li>
<li class="active"><a href="/forums">Forums</a></li>
...

The “active” class applied to the list item is what shifts the background image. That’ll do the trick, but what about when we move to the Field Tips page? We will have to remove the active class from the forums tab and apply it to the the Field Tips tab. That’s not very convenient. That means the code for the navigation block is unique on every single page of the site. So let’s say down the road we want to add a Contact tab, we’ll have to alter the code on every single page. No fun.

Let’s do this a little smarter. First we don’t want to include the navigation block of code on every page, we want to include it, probably with a simple PHP include like this:

<?php include_once("nav.html"); ?>

 

But then how do we apply the “active” class to the current navigation list element?

This is where apply what we just learned about giving unique ID’s to the body! Instead of applying a class to only the active list element, let’s apply a unique class to each separate list item as well as give our body an unique ID.

 ...
</head>
<body id="field-tips"> ... <li class="fieldtips"><a href="/fieldtips">Field tips</a></li> <li class="forums"><a href="/forums">Forums</a></li> ...

Now we can target specific elements in the navigation with some clever CSS:

#field-tips li.fieldtips, #forums li.forums { background-position: bottom;
}

This means that the code for the navigation block can stay the same on every page, yet only the navigation element native to that page will be affected by this CSS and “flip” to the active state.

 

Let’s get even more dynamic

Reader Brian left an awesome comment on how you can use PHP to apply the unique ID to to the body element:

___FCKpd___6

This will return the name of the PHP file being executed as the ID (e.g. body id=”index.php”). To leave off the .php part, just remove the “.php” part.

SERVER['PHP_SELF'], ".php")?>">

This will return the name of the PHP file being executed as the ID (e.g. body id=”index.php”). To leave off the .php part, just remove the “.php” part.