如何将一个<div>元素放在另一个<div>下面

时间:2015-12-16 03:34:49

标签: html css css-float floating

我刚刚用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;
}

4 个答案:

答案 0 :(得分:1)

例如:

ListBox

带浮动的Css:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
    </Style>
</ListBox.ItemContainerStyle>

带显示的Css:

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

答案 1 :(得分:1)

以下是更新的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>

这会在objective div下面显示header div

这也是link供您参考。

答案 2 :(得分:0)

这是更新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

答案 3 :(得分:0)

我认为使用bootstrap更容易,这里是链接{{3}}

引导程序的作用是创建包装站点内容的容器。它按行划分网站。要做到这一点,你需要和。使用此引导程序,您可以将行划分为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>
相关问题