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

CSS样式表
CSS教程:制作网页中常用的5个CSS属性
网页制作理论:精确还原设计稿
CSS教程:Firefox浏览器下margin-top问题
CSS教程:li标记在IE下产生间距
CSS实例教程:CSS制作虚线的2种方法
CSS教程:网页中英文字体的设置
根据IE版本不同调用不同CSS样式文件
CSS网页布局:tbody标签与thead和tfoot标签
网页制作技巧:CSS网页布局中文排版心得
网页制作CSS教程:自适应圆角
Css Hack:Pixel Perfect
OOcss教程:认识OOCSS和简单应用OOCSS
网页制作教程:面向对象的CSS应用
CSS写法:不同组合间的优先级及浏览器的支持性
CSS sprite实例教程:li:hover修改密码
利用负边距技术制作自适应宽度布局的网页
WEBJX收集漂亮的大背景网页设计实例
使用一张或两张图片创建大背景网站
CSS教程:CSS选择符的用法和实例
CSS实例:a:hover伪类在IE6下的问题

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 52 ::
收藏到网摘: 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.