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

CSS样式表
CSS教程:闭合CSS浮动元素的几种方法
细说CSS3中的选择符
网页超级链接该以什么方式打开?
背投广告设计:用最少的时间来做最效率的事情
css中用javascript判断浏览器版本
css教程:经常保持CSS的整洁度和有序性
css教程:选择合适的、有意义的元素描述内容
CSS编写中灵活运行注释的意义
css教程:CSS文件应该保持整洁和统一
DIV CSS常用的网页布局代码
完全掌握纯CSS布局网页
serif和sans serif:西方国家的字母体系
基于XTHML标准的DIVCSS布局对SEO的影响
CSS合理的编码与组织技巧
网站变黑白灰色的4种代码详细讲解
css布局实例:网页布局的方法
YUI 中的 Grids CSS值得关注和学习的
CSS代码是否合理?是否优化?
css教程:网页中Span和Div的区别
div css网页适应不同分辨率技巧

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


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