2017-02-26 72 views
0

我正在爲我的DofE考察隊建立一個網站,但在正常工作時遇到了一些麻煩。當前漂浮在主容器右側的文本應該位於導航部分的旁邊,但是可能會嘗試使其成爲迄今爲止不成功的文本。對此有何想法?文本漂浮到右側,而不是旁邊導航

如果有幫助,我想它是這個樣子:W3schools CSS Help Code

@charset "utf-8"; 
 
/* CSS Document */ 
 
* { 
 
    box-sizing: border-box; 
 
} 
 
body { 
 
\t background-color: #EEE; 
 
} 
 
#header { 
 
\t background-color: #66CCFF; \t 
 
\t margin: auto; 
 
\t text-align:center; 
 
\t font-family: Helvetica; 
 
} 
 
#container { 
 
\t background-color: white; 
 
\t width: 800px; 
 
\t margin-left: auto; 
 
\t margin-right: auto; \t 
 
\t font-family: Helvetica; 
 
} 
 
.main { 
 
\t font-family: Helvetica; 
 
\t width: 70%; 
 
\t margin-left: auto; 
 
\t float: right; 
 
\t background-color:#f2f2f2; 
 
\t clear: left; 
 
\t display: inline; 
 
\t /*margin: 1px;*/ 
 
} 
 
#main2 { 
 
\t width: 799px; 
 
\t font-family: Helvetica; 
 
\t width: 50%; 
 
\t display: inline; 
 

 
\t background-color:#f2f2f2; 
 
\t /*margin: 1px;*/ 
 
} 
 
#nav{ 
 
\t padding: 3px; 
 
\t font-family: Helvetica; 
 
\t list-style-type: none; 
 
} 
 

 
#nav li a:hover { 
 
\t color: red; 
 
} 
 

 
#nav li { 
 
\t text-decoration: none; 
 
\t display: block; 
 
\t padding: 5px; 
 
\t margin: 5px; 
 
\t margin-left: 0px; 
 
\t background-color: #dddddd; 
 
\t width: 25%; 
 
} 
 
#nav ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    fl 
 
} 
 
#content { 
 
\t padding:10px; 
 
} 
 
#footer{ 
 
\t clear: both; 
 
\t color: white; 
 
\t background-color: grey; 
 
\t text-align: right; 
 
\t padding: 0.5px; 
 
} 
 
#footer-margin{ 
 
\t margin: 5px; 
 
} 
 
img { 
 
\t /*margin-left: auto; 
 
\t margin-right: auto;*/ 
 

 
\t text-align:center; 
 
\t border-style: solid; 
 
\t border-width: 1px; 
 
\t border-color: black; 
 
\t clear: right; 
 
} 
 
.clearfix::after { 
 
    content: ""; 
 
    clear: both; 
 
    display: table; 
 
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
<title>DofE:Expedition Aim</title> 
 
<link rel="stylesheet" type="text/css" href="style.css"> 
 
</head> 
 

 
<body> 
 
<div id = "container"><!--start container--> 
 
\t <div id = "header"> 
 
    <h1>DofE Expedition Aim</h1> 
 
    </div> 
 
\t \t <div id = "content"> 
 
    \t \t <div id = "nav"><!-- start nav tag --> 
 
     \t \t <ul> 
 
     \t \t \t <li><a href="index.html">Home</a></li> 
 
        <li><a href="page_1.html">Our Aim</a></li> 
 
        <li><a href="page_2.html">How we acheived it</a></li> 
 
        <li><a href="page_3.html">Extra info (tbd)</a></li> 
 
     \t \t </ul> 
 
     \t </div><!-- nav close tag --> 
 
      <div class = "clearfix"> 
 
      <div class = "main"> 
 
      <p>This page shows what happened on out DofE expedition and the things that we did when not on expedition.</p> 
 
      <p>This includes doing map work, fitness and orginising and running events localy.</p> 
 
      <p>On the following pages, you can find the various evidence to show you what we did and how it benefited us. (or didn't)</p> 
 
      
 
      </div><!-- "main" end tag --> 
 
      <!--<div id = "main2"> 
 
      <p>This is a test of a second column that should float to the right of the other.</p> 
 
      </div>--> 
 
      </div> 
 
      <!-- <div id = "img.1"> 
 
      <img src="logo.jpg" alt="DofE Logo goes here" height="100" width="73"> 
 
      </div>--> 
 
    \t </div> 
 
\t \t <div id = "footer"> 
 
\t \t \t <div id = "footer-margin"> 
 
\t \t \t <p>Copyright &copy; Dom Brown 2017</p> 
 
\t \t \t </div> 
 
\t \t </div> 
 
</div> 
 
</body> 
 
</html>

+0

設置你的'content'顯示Flex和刪除列表的寬度 – Roljhon

回答

0

你需要玩填充等,但這會得到導航和主要內容並排。基本上你有你的clearfix容器包裹在只有nav的全寬。我還將float: left;應用於nav和main。

@charset "utf-8"; 
 
/* CSS Document */ 
 
* { 
 
    box-sizing: border-box; 
 
} 
 
body { 
 
\t background-color: #EEE; 
 
} 
 
#header { 
 
\t background-color: #66CCFF; \t 
 
\t margin: auto; 
 
\t text-align:center; 
 
\t font-family: Helvetica; 
 
} 
 
#container { 
 
\t background-color: white; 
 
\t width: 800px; 
 
\t margin-left: auto; 
 
\t margin-right: auto; \t 
 
\t font-family: Helvetica; 
 
} 
 
.main { 
 
\t font-family: Helvetica; 
 
\t width: 70%; 
 
\t margin-left: auto; 
 
\t float: left; 
 
\t background-color:#f2f2f2; 
 
\t display: inline; 
 
\t /*margin: 1px;*/ 
 
} 
 
#main2 { 
 
\t width: 799px; 
 
\t font-family: Helvetica; 
 
\t width: 50%; 
 
\t display: inline; 
 

 
\t background-color:#f2f2f2; 
 
\t /*margin: 1px;*/ 
 
} 
 
#nav{ 
 
\t padding: 3px; 
 
\t font-family: Helvetica; 
 
\t list-style-type: none; 
 
\t width: 25%; 
 
    float: left; 
 
} 
 

 
#nav li a:hover { 
 
\t color: red; 
 
} 
 

 
#nav li { 
 
\t text-decoration: none; 
 
\t display: block; 
 
\t padding: 5px; 
 
\t margin: 5px; 
 
\t margin-left: 0px; 
 
\t background-color: #dddddd; 
 
} 
 
#nav ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
#content { 
 
\t padding:10px; 
 
} 
 
#footer{ 
 
\t clear: both; 
 
\t color: white; 
 
\t background-color: grey; 
 
\t text-align: right; 
 
\t padding: 0.5px; 
 
} 
 
#footer-margin{ 
 
\t margin: 5px; 
 
} 
 
img { 
 
\t /*margin-left: auto; 
 
\t margin-right: auto;*/ 
 

 
\t text-align:center; 
 
\t border-style: solid; 
 
\t border-width: 1px; 
 
\t border-color: black; 
 
\t clear: right; 
 
} 
 
.clearfix::after { 
 
    content: ""; 
 
    clear: both; 
 
    display: table; 
 
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
<title>DofE:Expedition Aim</title> 
 
<link rel="stylesheet" type="text/css" href="style.css"> 
 
</head> 
 

 
<body> 
 
<div id = "container"><!--start container--> 
 
\t <div id = "header"> 
 
    <h1>DofE Expedition Aim</h1> 
 
    </div> 
 
\t \t <div id = "content" class="clearfix"> 
 
    \t \t <div id = "nav"><!-- start nav tag --> 
 
     \t \t <ul> 
 
     \t \t \t <li><a href="index.html">Home</a></li> 
 
        <li><a href="page_1.html">Our Aim</a></li> 
 
        <li><a href="page_2.html">How we acheived it</a></li> 
 
        <li><a href="page_3.html">Extra info (tbd)</a></li> 
 
     \t \t </ul> 
 
     \t </div><!-- nav close tag --> 
 
      <div class = "main"> 
 
      <p>This page shows what happened on out DofE expedition and the things that we did when not on expedition.</p> 
 
      <p>This includes doing map work, fitness and orginising and running events localy.</p> 
 
      <p>On the following pages, you can find the various evidence to show you what we did and how it benefited us. (or didn't)</p> 
 
      
 
      </div><!-- "main" end tag --> 
 
      <!--<div id = "main2"> 
 
      <p>This is a test of a second column that should float to the right of the other.</p> 
 
      </div>--> 
 
      <!-- <div id = "img.1"> 
 
      <img src="logo.jpg" alt="DofE Logo goes here" height="100" width="73"> 
 
      </div>--> 
 
    \t </div> 
 
\t \t <div id = "footer"> 
 
\t \t \t <div id = "footer-margin"> 
 
\t \t \t <p>Copyright &copy; Dom Brown 2017</p> 
 
\t \t \t </div> 
 
\t \t </div> 
 
</div> 
 
</body> 
 
</html>

+0

我可以問一下clearfix實際上呢?我在看過其他一些網站後說,這是一個好主意。我希望這會幫助你修好它... –

+0

是的。基本上是浮動物品的容器,在默認情況下計算其高度時忽略浮動物品,爲了避免這種情況,我們將clearfix應用於容器,這會「清除」使容器達到所需高度的浮動物。如果您不應用clearfix,您的頁腳將不會位於主容器的底部。如果您想閱讀更多內容,請參閱[本文](https://css-tricks.com/snippets/css/clear-fix/) –

0

設置你的CSS這樣

#nav{ 
    padding: 3px; 
    font-family: Helvetica; 
    list-style-type: none; 
    position: absolute; 
    width: 100%; 
}