css在一个div中定位2个div
我有一个主 div 宽度为 100%,其中有 2 个 div。我的意思是,一个宽度为 200px,另一个宽度为 100%-200px;
-----------------this is main div -------------
| |
| ----subdiv1---- -------------subdiv2----------|
|| | | ||
| -------------- ---------------------------- |
|-----------------------------------------------|
subdiv1 有 200 px,subdiv2 的宽度将是剩余的空白空间。我在谷歌上搜索但找不到。
i have a main div has 100% width, and 2 divs in it. one has 200px width and other will be 100%-200px, i mean;
-----------------this is main div -------------
| |
| ----subdiv1---- -------------subdiv2----------|
|| | | ||
| -------------- ---------------------------- |
|-----------------------------------------------|
subdiv1 has 200 px, subdiv2's width will be rest of empty space. I search on google but couldnt find.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我使用
float: left
规则为最左边的 div 和margin-left
规则为右边的 div 编写的一种可能的解决方案: http://jsfiddle.net/P4xMj/示例 HTML:
示例 CSS(背景颜色仅用于可见性):
Here's one possible solution I hacked up using a
float: left
rule for the left-most div, and amargin-left
rule for the right div: http://jsfiddle.net/P4xMj/Example HTML:
Example CSS (the background colors are just for visibility):
您需要将
float:left;
添加到subdiv1
中。这里有几行代码将产生您所显示的内容。简而言之,在您的
subdiv1
上使用float:left;
You're going to want to add
float:left;
to yoursubdiv1
. Here is a few lines of code that will produce what you have shown.In short, use
float:left;
on yoursubdiv1
您可以将左侧
div
float: left
,并在右侧div
上设置margin-left: 200px
。http://jsfiddle.net/SpxH9/
HTML:
CSS:
您可以使用另一种技术,即替换
margin-left
带有溢出:隐藏
< /a>.这很有用,因为您不必在其中两次放置维度,并且它更容易适应变化。例如,使用
10px
边框:http://jsfiddle.net/SpxH9/1/如果您尝试使用我提到的第一种技术执行相同的操作,您会发现必须手动计算内容:http://jsfiddle.net/SpxH9/2/(并修复: http://jsfiddle.net/SpxH9/3/)
最后,
#container
上的overflow:hidden
用于包含浮动。您可能希望改用clearfix。You can
float: left
the leftdiv
, and havemargin-left: 200px
on the rightdiv
.http://jsfiddle.net/SpxH9/
HTML:
CSS:
There's another technique you can use, which is to replace
margin-left
withoverflow: hidden
. This is useful because you don't have to have the dimension in there twice, and it adapts to changes more easily.For example, with
10px
borders: http://jsfiddle.net/SpxH9/1/If you try to do the same thing with the first technique I mentioned, you'll find that you have to manually calculate stuff: http://jsfiddle.net/SpxH9/2/ (and fixed: http://jsfiddle.net/SpxH9/3/)
Lastly,
overflow: hidden
on#container
is used to contain the floats. You might wish to use clearfix instead.