【问题标题】:Layout Issue (floats/Padding)布局问题(浮动/填充)
【发布时间】:2016-06-12 03:01:23
【问题描述】:

我遇到了布局问题,试图了解浮动以及如何定位元素。我对 css/html 很陌生。有人可以帮我理解这个布局是如何工作的。一些问题:

1) 无法在不破坏布局的情况下更改页脚高度,页脚似乎被向上推。

2) 当我通过 chrome 检查页面时,当页脚 div 位于代码外部时,页脚 div 包含在“包装器”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;
}

干杯

【问题讨论】:

    标签: html css layout css-float padding


    【解决方案1】:

    我确实使用 +200px 页脚制作了​​ +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>

    【讨论】:

    • padding-bottom / top 没问题。当我添加左或右填充时,它似乎中断了
    • 当然它的宽度是 100%,如果你添加更多,你会得到一个比屏幕更大的元素。除非你使用box-sizing: border-box;
    • 现在更新了所有方向的盒子大小和填充;请重新检查。
    • 谢谢!是时候研究一下盒子大小了!
    【解决方案2】:

    html 的宽度和高度不必指定为 100%,css 中的 body 或 div 宽度默认为 100%。仅在需要时才可以提及宽度。

    为页脚添加高度只需添加 clear:both 用于页脚选择器

    页脚{背景颜色:橙色;清晰:两者;高度:50px;}

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

    【讨论】:

    • 当我删除高度 100% 布局中断时尝试过这个 - 我的内容都被向上推
    猜你喜欢
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多