css在一个div中定位2个div

发布于 2024-12-29 22:54:59 字数 465 浏览 2 评论 0原文

我有一个主 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2025-01-05 22:54:59

这是我使用 float: left 规则为最左边的 div 和 margin-left 规则为右边的 div 编写的一种可能的解决方案: http://jsfiddle.net/P4xMj/

示例 HTML:

<div id="container">
    <div id="left">
        Some content here
    </div>
    <div id="right">
        Some more content goes over here on the right. Let's make this
        content really long to see what happens when we wrap more than
        one or two line's worth. Extra text to fill the void.
    </div>
</div>

示例 CSS(背景颜色仅用于可见性):

#container {
    background: #FF0;
    overflow: auto;
    padding: 10px;
}

#left {
    background: #0F0;
    float: left;
    width: 200px;
}

#right {
    background: #F00;
    margin-left: 210px;
}

Here's one possible solution I hacked up using a float: left rule for the left-most div, and a margin-left rule for the right div: http://jsfiddle.net/P4xMj/

Example HTML:

<div id="container">
    <div id="left">
        Some content here
    </div>
    <div id="right">
        Some more content goes over here on the right. Let's make this
        content really long to see what happens when we wrap more than
        one or two line's worth. Extra text to fill the void.
    </div>
</div>

Example CSS (the background colors are just for visibility):

#container {
    background: #FF0;
    overflow: auto;
    padding: 10px;
}

#left {
    background: #0F0;
    float: left;
    width: 200px;
}

#right {
    background: #F00;
    margin-left: 210px;
}
零度° 2025-01-05 22:54:59

您需要将 float:left; 添加到 subdiv1 中。这里有几行代码将产生您所显示的内容。

<div>
  <div style="float:left;width:200px;background:#0F0">
  SUBDIV1
  </div>
  <div style="background:#F00;">
  SUBDIV2
  </div>
</div>

简而言之,在您的 subdiv1 上使用 float:left;

You're going to want to add float:left; to your subdiv1. Here is a few lines of code that will produce what you have shown.

<div>
  <div style="float:left;width:200px;background:#0F0">
  SUBDIV1
  </div>
  <div style="background:#F00;">
  SUBDIV2
  </div>
</div>

In short, use float:left; on your subdiv1

定格我的天空 2025-01-05 22:54:59

您可以将左侧divfloat: left,并在右侧div上设置margin-left: 200px

http://jsfiddle.net/SpxH9/

HTML:

<div id="container">
    <div id="left">left</div>
    <div id="right">right</div>
</div>

CSS:

#container {
    overflow: hidden;
}
#left {
    float: left;
    width: 200px;
}
#right {
    margin-left: 200px;
}

您可以使用另一种技术,即替换 margin-left 带有溢出:隐藏< /a>.这很有用,因为您不必在其中两次放置维度,并且它更容易适应变化。

例如,使用 10px 边框:http://jsfiddle.net/SpxH9/1/

#container {
    overflow: hidden;
}
#left {
    float: left;
    width: 200px;
}
#right {
    overflow: hidden;
}

如果您尝试使用我提到的第一种技术执行相同的操作,您会发现必须手动计算内容:http://jsfiddle.net/SpxH9/2/(并修复: http://jsfiddle.net/SpxH9/3/

最后,#container 上的overflow:hidden 用于包含浮动。您可能希望改用clearfix

You can float: left the left div, and have margin-left: 200px on the right div.

http://jsfiddle.net/SpxH9/

HTML:

<div id="container">
    <div id="left">left</div>
    <div id="right">right</div>
</div>

CSS:

#container {
    overflow: hidden;
}
#left {
    float: left;
    width: 200px;
}
#right {
    margin-left: 200px;
}

There's another technique you can use, which is to replace margin-left with overflow: 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/

#container {
    overflow: hidden;
}
#left {
    float: left;
    width: 200px;
}
#right {
    overflow: hidden;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文