简体   繁体   English

2列CSS布局

[英]2Column CSS Layout

I have the following: 我有以下内容:

<div id="aboutus">
<div id="header"></div>
<div id="headerbar"><p>ABOUT US</p></div>
<div id="contentarea">
<p>CONTENT ABOUT US</div>
<div id="clear"></div></div>

<div id="contact">
<div id="header"></div>
<div id="headerbar"><p>CONTACT US</p></div></div>
<div id="contentarea">CONTENT CONTACT US</div>
<div id="clear"></div>
</div>

and: 和:

#aboutus {
    float:left;
    width:100%;
    height:100%;
}
#contact{
    float:left;
    width:100%;
    height:100%;
}
#headerbar {
    float:left;
    width:25%;
    height:100px;
    text-align:right;
    padding-right:5px;
}
#contentarea{
    float:left;
    width:70%;
    height:100px;
}
#clear{
    clear: both;
}
#header{
    background:url(bg.png) no-repeat center center;
    background-size:contain;
    width:100%;
    height:200px;
}

I'm totally new to this so I'm sure I'm doing something silly. 我对此完全陌生,所以我确定自己在做些愚蠢的事情。 Basically its two 2 collumn layouts on top of one another, the aboutus div is fine however the contact div displays within the about us div. 基本上它的两个2柱状图布局彼此重叠,aboutus div很好,但是contact div在about us div中显示。 Any suggestions on what I'm doing wrong? 关于我在做什么错的任何建议吗?

If you would like to specify two divs on top of another, can specify display:block; 如果要在另一个div上指定两个div,可以指定display:block; with the width:100%; width:100%; like below 像下面

.div{
    display: block;
    width: 100%;
}

You don't have to use floats. 您不必使用浮点数。 Of course, there's a plenty of ways you can achieve, this is one of the way to do that. 当然,有很多方法可以实现,这是实现此目的的方法之一。 You can play around about this here http://jsfiddle.net/qiqiabaziz/mrrQU/ 您可以在这里http://jsfiddle.net/qiqiabaziz/mrrQU/

Your code should be: 您的代码应为:

<div id="aboutus">
<div class="header"><div class="headerbar"><p>ABOUT US</p></div></div>
<div class="contentarea">
<p>CONTENT ABOUT US</div>
</div>
<div id="clear"></div>
<div id="contact">
<div class="header"><div class="headerbar"><p>CONTACT US</p></div></div>
<div class="contentarea">CONTENT CONTACT US</div></div>
<div id="clear"></div>
</div>

And: 和:

#aboutus {
    float:left;
    width:100%;
    height:100%;
}
#contact{
    float:left;
    width:100%;
    height:100%;
}
#headerbar {
    float:left;
    text-align:right;
    padding-right:5px;
}
.contentarea{
    float:left;
    width:70%;
    height:100px;
}
#clear{
    clear: both;
}
.header{
    width:25%;
    height:100px;    
    background:url(bg.png) no-repeat center center;
    background-size:contain;
    float: left;
}
div {border:1px black solid;
}

I added borders so you can see where your divs are. 我添加了边框,以便您可以看到div的位置。 Floats are aways tricky to deal with, and more complicated when you use it 100% of your screen. 浮点数很难处理,并且在屏幕的100%使用浮点数时会更加复杂。

Tip: use ID for unique objects on your code, and CLASS for elements that repeat on the same page (#id is for single elements, .class is for when you want to apply the same style to more than one element on your page). 提示:ID用于代码中的唯一对象,CLASS用于在同一页面上重复的元素(#id用于单个元素,.class用于要将同一样式应用于页面上多个元素的情况)。 。

Play here: http://jsfiddle.net/VPzyy/ 在这里播放: http : //jsfiddle.net/VPzyy/

Good luck! 祝好运!

If you run your html through a validator (it's a good practice), you'll see that you have an extra </div> tag in there. 如果通过验证程序运行html(这是一种很好的做法),则会看到其中有一个额外的</div>标记。 If I'm understanding what you're trying to do correctly, it's one of the two after your second headerbar div. 如果我了解您要正确执行的操作,则它是第二个标题栏div之后的两者之一。 That extra </div> tag is closing your contact div when you don't want it to close yet. 当您不希望关闭联系人div时,该额外的</div>标签将关闭您的联系人div。

I personally always leave a comment when I'm closing a div so that I can see exactly what </div> is closing what. 关闭div时,我个人总是发表评论,以便可以准确地看到</div>关闭的内容。 Like this: 像这样:

<div id = "outerdiv">
     <div class = "innerdiv">
          <p>text</p>
          <div class = "innerdiv2">
               <p>more text</p>
          </div><!-- .innerdiv2 -->
     </div><!-- .innerdiv -->
</div><!-- #outerdiv-->

This way, even if I mess up the indentation, it's still easy to see exactly what's going on. 这样,即使我弄乱了缩进,也仍然可以很容易地看到正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM