CSS两列布局不起作用

时间:2014-12-14 03:21:15

标签: html css html5 layout web

我的代码假设是两列布局,顶部是页眉,底部是页脚,左上角是顶部和底部之间的导航,最后是右侧列的主页。问题是主要与导航栏重叠。

这是我文件的CSS:

body        {   background-color: #000033;
                background-image: url(primehorizontal.png);
                color: #003300;
                font-family: Arial, Helvetica, sans-serif;
                margin-left:180px;
                padding: 0px 20px 180px 0px;}

header      {   background-repeat: no-repeat;
                height: 100px; }

h1          {   white-space: nowrap;
                overflow: hidden;}

h2          {   color: #003366; }

h3          {   padding-top: 10px;
                color: #006600; }

nav         {   float: left;
                width: 150px;
                font-weight: bold;
                font-size: 1.2em; }

nav a       {   text-decoration: none;
                text-align: center;
                color: #FFFFCC;
                font-weight: bold;
                border: 3px outset #CCCCCC;
                padding: 5px; }

nav a:link  {   background-color: #003366; }
nav a:visited{  background-color: #48751A; }
nav a:hover {   border: 3px inset #333333; }

nav ul      {   list-style-type:none;
                margin: 0;
                padding-left: 0; }

dd          {   font-style: italic;
                font-size: .90em;
                height: 200%; }

.contact    {   font-weight: bold;
                font-size: .90em;
                font-family: "Times New Roman", sans-serif; }

.floatleft  {   float: left;
            padding: 0 20px 20px 0; }

.clear      {   clear:left; }

footer      {   font-size: .60em;
                clear:both;
                margin-left: 180px;}

img         {   border-style:none; }

#wrapper    {   background-color:#FFFFCC;
                min-width: 700px;
                max-width: 960px;
                padding: 0px 0px 20px 30px;
                border: 1px ridge blue;
                width: 80%;
                margin-right: auto;
                margin-left: auto;
                box-shadow: inset -3px -3px 3px 3px #00332B;}

header, nav, main, footer {display:block;}

这是我的一个html5文件:

<!DOCTYPE html>
<html lang="en">

<head>
    <link type="text/css" rel="stylesheet" href="prime.css"/>
    <title>Prime Properties :: Financing</title>
</head>

<body>

<div id="wrapper">

<header>
    <h1><img src="primelogo.gif" width="650" height="100" alt="Prime Logo"></h1>
</header>

<nav>
    <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="listings.html">Listings</a></li>
        <li><a href="financing.html">Financing</a></li>
        <li><a href="contact.html">Contact</a></li>
    </ul>
</nav>

<main>
    <h2>Financing</h2>
    <p>We work with many area mortgage and finance companies.</p>
    <h3>Morgages FAQs</h3>
    <dl>
        <dt>What amount of morgage do I qualify for?</dt>
            <dd>The total basic monthly housing cost is normally based on 29% to 41%   of your gross monthly income<dd>
        <dt>Which percentage is most often used?</dt>
            <dd>The perecentage used depends on the lending institution and type of financing.</dd>
        <dt>How do I get started?</dt>
            <dd>Contact us today to help you arrange financing for your home</dd>
    </dl>
</main>

<footer>
Copyright &copy; 2014 Prime Properties<br>
<a href="mailto:123@123.com">Paul Clef Ube</a>
</footer>

3 个答案:

答案 0 :(得分:1)

添加:

main{
    float:left;
    width:calc(100% - 150px);
}

我添加了新的宽度(你可能想要添加一个后备),所以main总是适合你的固定大小的导航(加上包装器上的填充)

答案 1 :(得分:0)

我看到你有一个班级“floatleft”,但你永远不会把它分配给任何东西。 尝试给出元素位置。例如,如果你想要一些东西浮动,请告诉它如何从左边开始。在你的CSS中你可以尝试这样的东西.floatleft {float:left;左:10px}。 .floatright {float:right;右:10px;}。

答案 2 :(得分:0)

解决了问题。只是稍微使用了你的代码,认为你可能觉得它很有用。

虽然calc()很诱人,但由于比较问题,它不一定是最佳选择。

Styles

*{
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html, body {
  height: 100%;
  width: 100%;
}
body {   
  background-color: #000033;
  background-image: url(primehorizontal.png);
  color: #003300;
  font-family: Arial, Helvetica, sans-serif;
}

header {
  position: relative;
  background: #003366 url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/156843/worldmap.png) no-repeat center 35%;
  height: 100px;
  margin-left: -21px;
  margin-right: -21px;
  overflow: hidden;
}
header img {
  position: absolute;
  top: 0;
  left: 24px;
  bottom: 0;
  right: auto;
  margin: auto;
  display: inline-block;
  max-width: 140px;
  height: 65px;
}

h1 { 
  padding-left: 55px; 
  font-size: 25px; 
  line-height: 100px; 
  color: white; 
}


h2 { 
  margin-bottom: 5px; color: #003366; 
}

h3 {
  margin: 2px 0px 6px;
  padding-top: 15px;
  color: #006600; 
}

nav {
  float: left;
  display: inline-block;
  margin: 0;
  padding-top: 20px;
  width: 150px;
  font-size: 1.2em; }

nav a {
  display: inline-block;
  width: 100%;
  text-decoration: none;
  text-align: center;
  color: #FFFFCC;
  font-weight: bold;
  border: 3px outset #CCCCCC;
  padding: 5px; 
  margin: 2px 0; 
}

nav a:link  {
   background-color: #003366; 
}
nav a:visited { 
  background-color: #48751A;
}
nav a:hover { 
  border: 3px inset #333333; 
}

nav ul { 
  list-style-type:none;
}
dt {

}
dd {   
  display: inline-block;
  margin: 4px 0 10px 18px;
  line-height: 1.2;
  font-style: italic;
  font-size: .90em;
  height: 200%;
}
dt:first-child {
  margin-top: 15px;
}
.contact { 
  font-weight: bold;
  font-size: .90em;
  font-family: "Times New Roman", sans-serif; 
}

.floatleft  {
  float: left;
  padding: 0 20px 20px 0; 
}

.clear { 
  clear:left; 
}

footer {
  font-size: .60em;
  width: 100%;
  clear: both; 
}

img {
   border-style: none;
}

#wrapper {  
  background-color: #FFFFCC;
  width: 80%;
  min-width: 700px;
  max-width: 960px;
  padding: 30px 20px 15px;
  border: 1px ridge blue;
  margin: auto;
  box-shadow: inset -3px -3px 3px 3px #00332B;
}

main {
  padding: 20px 0px 20px 20px;
  display: inline-block;
  width: 100%;
  max-width: 77%;
}