【问题标题】:How do I put one <div> element below another <div>如何将一个 <div> 元素放在另一个 <div> 下方
【发布时间】:2015-12-16 03:34:48
【问题描述】:

我刚刚用 Codecademy 完成了 HTML/CSS。那里的“项目”之一是制作自己的简历。我从那个项目中获取了 HTML/CSS,我正在对其进行调整以使简历看起来更好。我目前正在尝试将一个div - 简历中有关我的职业目标的文字部分放在另一个div header 下。但是,它不起作用。 “目标”的div 当前位于标题的div 后面。我到底如何获得第二个 div 以使目标低于第一个 div

我读到了一些关于我应该如何将float header div 放在左侧,然后将clear:both; 放在div 中以实现目标,但这不起作用。

HTML

<div id="header">
        <p id="name">My Name</p>
        <a href="mailto:myemail@email.com"><p id="email">myemail@email.com</p></a>
    </div>
    
    <div id="objective"></div>
    
    
    <div class="left"></div>
    <div class="right"></div>
    
    <div id="footer">
        <p>1234 Anywhere Street, Brooklyn NY 11216 | Tel: (123) 456-7890</p>
    </div>

CSS

div {
border-radius: 5px;
}

#header {
z-index:1;
position: fixed;
width: 98%;
margin-top: -20px;
height: 60px;
background-color: #668284;
margin-bottom: 10px;
float:left;
}

#name {
float:left;
margin-left: 5px;
padding-top: 5px;
font-size: 16px;
font-family: Verdana, sans-serif;
color: #ffffff;
}

#email{
float:right;
margin-right: 5px;
padding-top: 5px;
font-size: 16px;
font-family: Verdana, sans-serif;
color: #ffffff;
}


.right p {
margin-left: 5px;
margin-right: 5px;
margin-top: -10px;
font-family: Garamond, serif;
color: #000000;
}


a:hover {
font-weight: bold;
}

#objective {

height: 50px;
background-color: #668284;
font-family: Verdana, sans-serif;
font-size: 14px;
text-align: center;
clear:both;
color: #ffffff;
}


.left {
position: relative;
float: left;
margin-top: 50px;
width: 49%;
height: 400px;
background-color: #B9D7D9;
margin-bottom: 10px;
}

.right {
position: relative;
float: right;
margin-top: 50px;
width: 49%;
height: 400px;
background-color: #F4EBC3;
margin-bottom: 10px;
}

#footer {
position: relative;
height: 50px;
background-color: #668284;
clear: both;
font-family: Verdana, sans-serif;
font-size: 14px;
text-align: center;
color: #ffffff;
}

#footer p {
position: relative;
padding-top: 15px;
}

【问题讨论】:

    标签: html css css-float floating


    【解决方案1】:

    例如:

     <div class="div1">KSD;JSFAJ;SSD;</div>
     <div class="div2">KSD;JSFAJ;SSdfaD;</div>
    

    带有浮动的CSS:

     .div1 {
         float: none; 
      }
      .div2 {
         float: none; 
      }
    

    带显示的CSS:

     .div1 {
         display: inline;
      }
      .div2 {
         display: inline; 
      }
    

    【讨论】:

      【解决方案2】:

      这是更新后的 HTML:

      <div id="header">
          <p id="name">My Name</p>
          <a href="mailto:myemail@email.com"><p id="email">myemail@email.com</p></a>
      </div>
      <div style="height:50px;width:98%;">
      </div>
      <div id="objective">Objective goes here</div>
      <div class="left"></div>
      <div class="right"></div>
      <div id="footer">
          <p>1234 Anywhere Street, Brooklyn NY 11216 | Tel: (123) 456-7890</p>
      </div>
      

      这将在header div 下方显示objective div

      这也是一个link 供您参考。

      【讨论】:

        【解决方案3】:

        这里是更新 CSS,这显示了你的 html 响应

        *{
                        padding: 0;
                        margin: 0;
                        box-sizing: border-box;
                    }
                        div {
                    border-radius: 5px;
                    }
        
                    #header {
                    width: 98%;
                    margin: 0 auto;
                    height: 60px;
                    background-color: #668284;
                    margin-bottom: 10px;
                    }
        
                    #name {
                    float:left;
                    margin-left: 5px;
                    padding-top: 5px;
                    font-size: 16px;
                    font-family: Verdana, sans-serif;
                    color: #ffffff;
                    }
        
                    #email{
                    float:right;
                    margin-right: 5px;
                    padding-top: 5px;
                    font-size: 16px;
                    font-family: Verdana, sans-serif;
                    color: #ffffff;
                    }
        
        
                    .right p {
                    margin-left: 5px;
                    margin-right: 5px;
                    margin-top: -10px;
                    font-family: Garamond, serif;
                    color: #000000;
                    }
        
        
                    a:hover {
                    font-weight: bold;
                    }
        
                    #objective {
                    height: 50px;
                    background-color: #668284;
                    font-family: Verdana, sans-serif;
                    font-size: 14px;
                    text-align: center;
                    clear:both;
                    color: #ffffff;
                    }
        
        
                    .left {
                    position: relative;
                    float: left;
                    margin-top: 50px;
                    width: 49%;
                    height: 400px;
                    background-color: #B9D7D9;
                    margin-bottom: 10px;
                    }
        
                    .right {
                    position: relative;
                    float: right;
                    margin-top: 50px;
                    width: 49%;
                    height: 400px;
                    background-color: #F4EBC3;
                    margin-bottom: 10px;
                    }
        
                    #footer {
                    position: relative;
                    height: 50px;
                    background-color: #668284;
                    clear: both;
                    font-family: Verdana, sans-serif;
                    font-size: 14px;
                    text-align: center;
                    color: #ffffff;
                    }
        
                    #footer p {
                    position: relative;
                    padding-top: 15px;
                    }
        

        不要忘记添加此代码

          *{
                        padding: 0;
                        margin: 0;
                        box-sizing: border-box;
                    }
        

        这样你的 div 上就不会有空白空间

        DEMO

        【讨论】:

          【解决方案4】:

          我认为使用引导程序更容易,这是链接http://getbootstrap.com/css/

          bootstrap 的作用是创建包装您网站内容的容器。它将站点按行划分。为此,您需要和 .使用此引导程序,您可以将行划分为 12 个单元格。

          这是我如何将我的作品集分成 3 列,每列 4 个空格的示例

          <div class="row">
                  <div class="col-md-12">
                    <hr>
                  </div>
                </div>
                <div class="row text-center">
                  <div class="col-md-4">
                    <h3 class="text-body"><u>Block vs Inline</u>
                    </h3>
                      <p class="p-text"><span>Block Elements</span> are those who take the complete line and full width of the page creating a "box".<br>
                        <span>Inline Elements</span> are those who doesn´t affect the layout, just the element inside the tag.
                      </p>
                  </div>
                  <div class="col-md-4">
                    <h3 class="text-body"><u>Selectors</u></h3>
                      <p class="p-text"><span>Class selectors</span> are used to target elements with specific attributes<br>On the other hand, <span>id selectors</span> are just for unique elements.</p>
                  </div>
                  <div class="col-md-4">
                    <h3 class="text-body"><u>Responsive Layout</u></h3>
                    <p class="p-text"><span>Responsive Layout</span> is the combination of html and css design to make the website look good in terms of enlargement, shrink and width in any screen (<em>computers, laptops, netbooks, tablets, phones</em>). </p>
                  </div>
                </div>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-08-29
            • 2021-12-20
            • 2022-11-18
            • 1970-01-01
            • 2013-09-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多