开发者

Why my CSS effect is like this? (not what I want)

My test.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitio开发者_StackOverflownal//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="test.css" />
<title>test</title>
</head>
<body>
    <div id="header">header</div>
    
    <div id="container">
      <h1>content</h1>
      <p>test</p>
      <p class="last">...</p>
    </div>
    
    <div id="sidebar">
      <h1>sidebar</h1>
      <ul>
        <li>link one</li>
        <li>link two</li>
      </ul>
    </div>
    
    <div id="footer">footer</div>
</body>
</html>

my test.css:

@CHARSET "ISO-8859-1";
#header {
  background: #d7dabd;
}
#container {
  width: 100%;
  float: right;
  margin-left: -200px;
}
#sidebar {
  width: 200px;
  float: left;
}
#footer {
  background: #d7dabd;
  clear: both; 
}

The final effect is:

Why my CSS effect is like this? (not what I want)

I expect #sidebar on the left and #container on the right of the page. but they are mixed in the left


Just remove the width: 100%; from the css for #container and it should look like you wanted

Here's a demo http://jsbin.com/ohebo3


Try removing the negative margin left, and - possibly - the width


First, change the order of the divs, so that sidebar comes first:

<div id="header">header</div>

<div id="sidebar">
  <h1>sidebar</h1>
  <ul>
    <li>link one</li>
    <li>link two</li>
  </ul>
</div>

<div id="container">
  <h1>content</h1>
  <p>test</p>
  <p class="last">...</p>
</div>

<div id="footer">footer</div>

Then make both, container and sidebar float left:

#container {
   float: left;
}
#sidebar {
   width: 200px;
   float: left;
}


It will be easier if you put the sidebar div before the content div in the HTML.


Here is your CSS modified for a simple fluid layout.

Demo: http://jsfiddle.net/W6T6n/


#container
{
    width: 75%;
    float: right;
}
#sidebar
{
    width: 25%;
    float: left;
}


You could put the #sidebar div inside the #container div.

Example: http://jsbin.com/ogobe4

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜