将文字放入Div

时间:2014-10-17 13:44:38

标签: html css

我有这个代码用于在网站上构建导航栏和标题。

<div id="wrap">
    <ul id="nav">
    <div id="head">
        <li><a href="transitionyear.html">Home</a></li>
        <li><a href="#">TY Projects</a></li>
        <li><a href="#">News</a></li>
        <li><a href="#">Build a Bank</a></li>
        <li><a href="#">Enterprise Awards</a></li>
        <li><a href="#">Gaisce</a></li>
        <li><a href="#">Social Inclusion</a></li>
    </div>
    </ul>

#nav {
  width: 100%;
  float: left;
  margin: 0 0 3em 0;
  padding: 0;
  list-style: none;
  background-color: #f2f2f2;
  border-bottom: 1px solid #ccc; 
  border-top: 1px solid #ccc; 
  }
   #nav li {
  float: left; 
  }
   #nav li a {
  display: block;
  padding: 8px 15px;
  text-decoration: none;
  font-weight: bold;
  color: #069;
  border-right: 1px solid #ccc; 
  }
   #nav li a:hover {
  color: #c00;
  background-color: #fff; 
  }
      #wrap {
  width: 750px;
  margin: 0 auto;
  background-color: #fff;
  }
    #head {
width: 100%;
margin-top: 30px;
background-color: #fff;
border:  1px solid #ccc;
}

我无法将文本放入div(head div)的顶部。我想在导航栏上方添加“Marist College Transition Year”。

1 个答案:

答案 0 :(得分:0)

首先,正确设置HTML,可能如下所示。

您的li需要嵌套在ul元素中。

#nav {
  width: 100%;
  float: left;
  margin: 0 0 3em 0;
  padding: 0;
  list-style: none;
  background-color: #f2f2f2;
  border-bottom: 1px solid #ccc;
  border-top: 1px solid #ccc;
}
#nav li {
  float: left;
}
#nav li a {
  display: block;
  padding: 8px 15px;
  text-decoration: none;
  font-weight: bold;
  color: #069;
  border-right: 1px solid #ccc;
}
#nav li a:hover {
  color: #c00;
  background-color: #fff;
}
#wrap {
  width: 750px;
  margin: 0 auto;
  background-color: #fff;
  overflow: auto; /* to prevent the float from interfering with the rest of the layout */
}
#head {
  width: 100%;
  margin-top: 30px;
  background-color: #fff;
  border: 1px solid #ccc;
}
<div id="wrap">
  <div id="head">
    <h2>Marist College Transition Year</h2>
  </div>
  <ul id="nav">
    <li><a href="transitionyear.html">Home</a></li>
    <li><a href="#">TY Projects</a></li>
    <li><a href="#">News</a></li>
    <li><a href="#">Build a Bank</a></li>
    <li><a href="#">Enterprise Awards</a></li>
    <li><a href="#">Gaisce</a></li>
    <li><a href="#">Social Inclusion</a></li>
  </ul>
</div>