简体   繁体   English

如何使2 div的宽度为100%

[英]how to make 100% width for 2 div

I have 2 div in on page 我的页面上有2个div

<div id="main"></div>
<div id="panel"></div>

and using float to make main at left, and panel at right 并使用float将main置于左侧,panel置于右侧

#photos-main {
float: left;
width: 800px;
position: relative;
}

#panel {
float: left;
margin-left: 830px;
position: absolute;
}

I want to let panel fill with the left page space, which css should I use? 我想让面板填充左侧页面空间,我应该使用哪个CSS?

Just don't float it, and make it relatively positioned. 只是不要浮动它,并使其相对定位。 Take out the margin as well. 还要取出边距。 Floating "main" means that it will simply be to the left of "panel" all the time. 浮动的“ main”表示它始终一直位于“ panel”的左侧。 If you define "main" how you want, "panel" will automatically take up the remaining space. 如果根据需要定义“ main”,“ panel”将自动占用剩余空间。

#photos-main {
float: left;
width: 800px;
position: relative;
}

#panel {
}

Looks like you're trying to build some layout, right? 看起来您正在尝试构建一些布局,对吗? If that's the case, consider implementing (hence learning from) some of the techniques presented in these links: 如果是这种情况,请考虑实施(从中学习)这些链接中介绍的一些技术:

40 Tutorials and tips about CSS Layouts 40关于CSS布局的教程和技巧

CSS Positioning CSS定位

The perfect three columns layout 完美的三列布局

Hope it helps! 希望能帮助到你!

You could do it with floating with this approach: 您可以使用以下方法进行浮动:

#photos-main {
 float: left;
 width: 800px;
}

#panel {
 float: right; /*to have the panel on the right side*/
 width: 100px; /*with a width of 100px*/
}

Then you have to wrap the two Tags with another , which get a total width of both elements. 然后,必须用另一个包装两个Tag,这两个标签的总宽度相同。

To clarify this two column layout and put eg a footer beneath, put another in your HTML-Structure and set into the css simple a "clear:both;", so the floating will be stopped. 为了弄清这两列的布局并在其下方放置一个页脚,请在HTML结构中放置另一个页脚,并在css中设置一个简单的“ clear:both;”,以便停止浮动。

Complete Sample 完整样本

HTML 的HTML

<div id="wrap">
  <div id="photos-main"></div>
  <div id="panel"></div>
  <div id="clear"></div>
</div>

CSS 的CSS

#wrap {
 width: 900px;
}

#photos-main {
 float: left;
 width: 800px;
}

#panel {
 float: right; /*to have the panel on the right side*/
 width: 100px; /*with a width of 100px*/
}

#clear {
 clear:both;
}

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

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