2016-02-29 98 views
0

我有布局问题,试图了解浮动和如何定位元素。我对css/html非常陌生。有人能帮我理解这种布局是如何工作的。一些问题:布局问题(浮动/填充)

1)无法改变页脚的高度而不破坏布局,页脚似乎被推向上。

2)当我通过chrome查看页面时,页脚div被包含在'wrapper'div中,当它位于代码之外时。

3)添加填充到内容DIV似乎也打破了布局

<!DOCTYPE html> 

<html lang="en"> 

<head> 
    <title>DCW</title> 
    <link rel="stylesheet" type="text/css" href="styles.css" /> 
</head> 

<body> 

    <!--NAV BAR --> 

    <nav> 
     <ul> 
     <div class="float-left"> 
       <li><a class="active" href="default.asp">PD</a></li> 
       <li><a href="news.asp">Projects</a></li> 
       <li><a href="About.asp">About</a></li> 
       <li><a href="Contact.asp">Contact</a></li> 
      </div> 
      <div class="float-right"> 
       <li><a href="#">Hire Us!</a></li> 
      </div> 
     </ul> 

    </nav> 

    <!-- HEADER --> 

    <header> 

     <div id="title_wrapper"> 

     </div> 

    </header> 

    <!-- BODY CONTENT --> 

    <div id="wrapper"> 

     <div id="content"> 

      <div class="info"> 
      col1 

      </div> 

      <div class="info"> 
      col2 

      </div> 

     </div> 

     <!-- SIDEBAR --> 

     <div id="sidebar"> 

      <div id="sidebar-text"> 
        content 2 

      </div> 

     </div> 

     </div> 

    <!-- FOOTER --> 

    <footer> 
    Footer 

    </footer> 

</body>  

CSS:

html, body 
{ 
width: 100%; 
height: 100%; 
margin:0; 
padding: 0; 
} 

p.thick { 
font-weight: bold; 
} 

h1, p { 
padding: 0; 
margin: 0; 
} 



/*Nav */ 


nav ul { 
list-style-type: none; 
margin: 0; 
padding: 0; 
overflow: hidden; 
background-color: #333; 
} 

nav li { 
float: left; 
} 

nav li a { 
display: block; 
color: white; 
text-align: center; 
padding: 14px 16px; 
text-decoration: none; 
} 

/* Change the link color to #111 (black) on hover */ 
nav li a:hover { 
background-color: #111; 
} 

.active { 
background-color: #4CAF50; 
} 

.float-left { 
float:left; 
} 
.float-right { 
float:right; 
} 


/* Header */ 

header #title_text{ 

font-size: 100px; 
font-family: arial; 

} 

header #title_wrapper{ 
text-align:center; 
position: relative; 
top: 200px; 

} 

header { 

background-color: orange; 
position: relative; 
height:100%; 
width: 100%; 
color:white; 
margin:0; 




} 

/*Content*/ 

#wrapper{ 

min-height: 70%; 
height: auto; 
height: 70%; 

} 

#content{ 
float:left; 
width:100%; 
height: 100%; 
text-align: center; 

} 


#sidebar{ 

float: left; 
background-color: lightgrey; 
height: 75%; 
text-align: center; 
width: 100%; 

} 

#sidebar-text{ 
font: 14px/22px normal helvetica, sans-serif; 
padding-top: 30px; 
text-decoration: none; 
line-height: 200%; 
} 



.img-circle { 
border-radius: 50%; 
} 


.info{ 
width: 50%; 
float: left; 
} 


blockquote.style2 { 
font: 14px/22px normal helvetica, sans-serif; 
margin-top: 10px; 
margin-bottom: 10px; 
margin-left: 50px; 
padding-left: 15px; 
border-left: 3px solid #ccc; 
outline-style: double; 
outline-color: lightgrey; 

} 

/* footer */ 
footer{ 
background-color: orange; 
} 

干杯

回答

0

我没有做+ 200像素填充内容为+ 200px页脚。除了内容上的display:inline-block加上box-sizing: border-box;之外没有改变任何东西。

html, body 
 
{ 
 
width: 100%; 
 
height: 100%; 
 
margin:0; 
 
padding: 0; 
 
} 
 

 
p.thick { 
 
font-weight: bold; 
 
} 
 

 
h1, p { 
 
padding: 0; 
 
margin: 0; 
 
} 
 

 

 

 
/*Nav */ 
 

 

 
nav ul { 
 
list-style-type: none; 
 
margin: 0; 
 
padding: 0; 
 
overflow: hidden; 
 
background-color: #333; 
 
} 
 

 
nav li { 
 
float: left; 
 
} 
 

 
nav li a { 
 
display: block; 
 
color: white; 
 
text-align: center; 
 
padding: 14px 16px; 
 
text-decoration: none; 
 
} 
 

 
/* Change the link color to #111 (black) on hover */ 
 
nav li a:hover { 
 
background-color: #111; 
 
} 
 

 
.active { 
 
background-color: #4CAF50; 
 
} 
 

 
.float-left { 
 
float:left; 
 
} 
 
.float-right { 
 
float:right; 
 
} 
 

 

 
/* Header */ 
 

 
header #title_text{ 
 

 
font-size: 100px; 
 
font-family: arial; 
 

 
} 
 

 
header #title_wrapper{ 
 
text-align:center; 
 
position: relative; 
 
top: 200px; 
 

 
} 
 

 
header { 
 

 
background-color: orange; 
 
position: relative; 
 
height:100%; 
 
width: 100%; 
 
color:white; 
 
margin:0; 
 

 

 

 

 
} 
 

 
/*Content*/ 
 

 
#wrapper{ 
 

 
min-height: 70%; 
 
height: auto; 
 
height: 70%; 
 

 
} 
 

 
#content{ 
 
float:left; 
 
width:100%; 
 
height: 100%; 
 
text-align: center; 
 
padding:100px; 
 
box-sizing: border-box; 
 
} 
 

 

 
#sidebar{ 
 

 
float: left; 
 
background-color: lightgrey; 
 
height: 75%; 
 
text-align: center; 
 
width: 100%; 
 

 
} 
 

 
#sidebar-text{ 
 
font: 14px/22px normal helvetica, sans-serif; 
 
padding-top: 30px; 
 
text-decoration: none; 
 
line-height: 200%; 
 
} 
 

 

 

 
.img-circle { 
 
border-radius: 50%; 
 
} 
 

 

 
.info{ 
 
width: 50%; 
 
float: left; 
 
} 
 

 

 
blockquote.style2 { 
 
font: 14px/22px normal helvetica, sans-serif; 
 
margin-top: 10px; 
 
margin-bottom: 10px; 
 
margin-left: 50px; 
 
padding-left: 15px; 
 
border-left: 3px solid #ccc; 
 
outline-style: double; 
 
outline-color: lightgrey; 
 

 
} 
 

 
/* footer */ 
 
footer{ 
 
display: inline-block; 
 
background-color: orange; 
 
height:200px; 
 
width: 100%; 
 
}
<!DOCTYPE html> 
 

 
<html lang="en"> 
 

 
<head> 
 
    <title>DCW</title> 
 
    <link rel="stylesheet" type="text/css" href="styles.css" /> 
 
</head> 
 

 
<body> 
 

 
    <!--NAV BAR --> 
 

 
    <nav> 
 
     <ul> 
 
     <div class="float-left"> 
 
       <li><a class="active" href="default.asp">PD</a></li> 
 
       <li><a href="news.asp">Projects</a></li> 
 
       <li><a href="About.asp">About</a></li> 
 
       <li><a href="Contact.asp">Contact</a></li> 
 
      </div> 
 
      <div class="float-right"> 
 
       <li><a href="#">Hire Us!</a></li> 
 
      </div> 
 
     </ul> 
 

 
    </nav> 
 

 
    <!-- HEADER --> 
 

 
    <header> 
 

 
     <div id="title_wrapper"> 
 

 
     </div> 
 

 
    </header> 
 

 
    <!-- BODY CONTENT --> 
 

 
    <div id="wrapper"> 
 

 
     <div id="content"> 
 

 
      <div class="info"> 
 
      col1 
 

 
      </div> 
 

 
      <div class="info"> 
 
      col2 
 

 
      </div> 
 

 
     </div> 
 

 
     <!-- SIDEBAR --> 
 

 
     <div id="sidebar"> 
 

 
      <div id="sidebar-text"> 
 
        content 2 
 

 
      </div> 
 

 
     </div> 
 

 
     </div> 
 

 
    <!-- FOOTER --> 
 

 
    <footer> 
 
    Footer 
 

 
    </footer> 
 

 
</body>

+0

填充底部/顶部是好的。它似乎打破了,当我添加左或右填充 –

+0

当然它的宽度是100%,如果你添加更多,你会有一个比屏幕更大的元素。除非你使用'box-sizing:border-box;' –

+0

现在已经在所有方向上用框大小和填充更新;请重新检查。 –

0

这是没有必要给宽度和高度为100%为在CSS宽度html和身体或DIV是100%默认情况下。只有在需要时才可以提及宽度。

用于给出高度到页脚只需添加明确的:既为页脚选择

页脚{背景色:橙;明确:两者;高度:50像素;}

这将解决问题1 & 2。当您添加填充时,设计会中断,因为每次添加宽度时,都会添加到已赋予相同div或元素的当前宽度。这可以通过重新调整元素宽度或使用框大小:边框来解决。

+0

当我删除高度100%的布局休息时尝试过 - 我的内容全部被向上推动 –