English 中文(简体)
Creating a CSS-rule to work for both a one and two column layout
原标题:
  • 时间:2009-11-15 11:21:26
  •  标签:
  • html
  • css
  • web

This is what I m trying to do:

Example http://img689.imageshack.us/img689/5761/cssautowidth.th.jpg (Larger image.)

When the <nav> element is present in the design, I want it to look like the example below. Is it possible to do this while the #content div has got a percentage value set? I m just curious to see whether this is possible without using two different styles for the #content (both with different width values.)
Just floating doesn t seem to do it.

The reason I want the #content to have a percentage value in the first example is because I have a background image in #body that creates the illusion of an outer glow.

Edit: I just removed the need for using the width percentage by using margins instead.

问题回答

Check the example here: http://css.maxdesign.com.au/floatutorial/tutorial0816.htm

What you should do is to set float:right and width on your <nav> element, and leave #content without any float or width, just set margin. This way content will try to occupy all given space and wont fall into navigation.

Then, if you hide <nav> element, content will automatically resize (but also you will need to remove padding from the right).

This is example code:

<style type="text/css">
    #container { width:700px; margin:0 auto; border:1px solid #000; }
    #nav  { display:none; }
    .double #nav { width:10%; float:right; display:block; }
    #content { margin-right:10%; border-right:1px solid #000; }
</style>

<div id="container" class="double">
    <div id="nav">nav content</div>
    <div id="content">page content</div>
</div>

Now, if you removed class="double" from container element you will see content is correctly resized to take 90% of given space. If you want to take 100% - just add .double before #content in style.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签