CSS 容器DIv 高度。 浮动DIV问题

发布于 2024-08-02 00:51:58 字数 775 浏览 2 评论 0原文

你能强制容器 DIV 的高度来容纳两个浮动的 div 子项吗? 我可以用一个奇特的技巧来做到这一点吗? 我正在尝试在父 div 内制作两个大小相等的 div。 我希望它们并排出现,并且它们之间有一点空白。 Child2 往往会弹出并低于 Child1。 注意 Child2 包含一个表。 我应该漂浮吗?

HTML:

<div id="parent">
  <div id="child1"></div>
  <div id="child2">
    <table><tr><td>content</td></tr></table>
</div>
</div>

CSS:

div#parent
{
    background-color: #C6E4E0;
    border: solid 3px #017E6F;
    font-family: Arial;
    font-size: 10pt;
    font-weight: bold;
    padding-left: 5px;
    padding-right: 5px;
    width:99%;
}

div#parent div
{
    width:49%;
    float:right;  
    padding:3px;  
}

div#parent div:first-child
{
    float:left;
}

Can you force a container DIV height to accomodate two floated div children? Is there a fancy trick I can use to do that? I am trying to make two equally sized divs inside the parent div. I would like them to appear side by side with a little whitespace between them. Child2 tends to pop out and go below Child1. Note Child2 contains a table. Should I be floating?

HTML:

<div id="parent">
  <div id="child1"></div>
  <div id="child2">
    <table><tr><td>content</td></tr></table>
</div>
</div>

CSS:

div#parent
{
    background-color: #C6E4E0;
    border: solid 3px #017E6F;
    font-family: Arial;
    font-size: 10pt;
    font-weight: bold;
    padding-left: 5px;
    padding-right: 5px;
    width:99%;
}

div#parent div
{
    width:49%;
    float:right;  
    padding:3px;  
}

div#parent div:first-child
{
    float:left;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

瞎闹 2024-08-09 00:51:58

这不是一个明确的问题,他的问题是他的两个浮动 div 没有并排出现。

首先,您不需要设置父级 div 的宽度,div 是块元素,这意味着它们会自动调整宽度以占据其父级的整个宽度(在这种情况下,大概 div#parent 的父级是身体)。

因为您明确设置宽度并为其提供填充,所以它可能会超出正文。 这并不重要,但如果您将相同的知识应用于子浮动 div,您就会明白为什么正确的 div 可能会被撞到底部。

首先,如果您将 div 的宽度显式设置为百分比,则不需要添加填充。 因为您正在处理百分比宽度,所以最好将填充添加到 div 的内容而不是 div 本身,因为填充已添加到宽度。 因此,如果您向 100px 父级中宽度为 49% 的 div 添加 10px 内边距,则其宽度将为 49px + 10px + 10px(2 条边),计算得出的总宽度为 69px。

由于您没有发布您的标记和内容或您正在测试的浏览器,我无法确切地说出 div 被降低的原因。 有两种可能的可能性。

  1. 您正在使用 IE,它允许表扩展到其父 div 之外,这将导致损坏。 尝试将表宽度显式设置为其父级的百分比或类似的值。
  2. 49% 宽度 + 填充 = 大于 [parent-width] - [left-div-width]。 这将导致它向下碰撞,因为左 div 和右 div 对于父宽度来说太宽。

This is not a clearfix issue guys, his problem is that his two floated divs are not appearing side by side.

First of all, you do not need to set the width of the parent div, divs are block elements which means they automatically adjust their width to take up the full width of their parent (in this case, presumably the parent of div#parent is the body).

Because you are setting the width explicitly AND giving it padding, it can potentially extend BEYOND the body. That doesn't really matter, but if you apply this same knowledge to the child floated divs and you can see why the right one might get bumped down to the bottom.

First, if you are explicitly setting the widths of the divs to a percentage, you do not need to add padding. Because you are dealing with percentage widths, it is better to add padding to the content of the divs rather than the divs themselves, because padding is ADDED to the width. Therefore, if you added 10px padding to a div that had a 49% width in a 100px parent, it would have a width of 49px + 10px + 10px (2 sides) for a total calculated width of 69px.

Since you didn't post your markup and content or which browser you are testing in, I can't say exactly why the div is being bumped down. There are two likely possibilities.

  1. You are using IE, which allows tables to extend beyond its parent div which will cause breakage. Try explicitly setting the table width to a percentage of its parent or something like that.
  2. The 49% width + padding = greater than [parent-width] - [left-div-width]. This will cause it to get bumped down because the left div and right div are too wide for the parent width.
陈独秀 2024-08-09 00:51:58

我使用clearfix 类。

.clearfix:after {
    content: "."; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;
}

.clearfix {
    display: inline-block;
}

/* Hides from IE-mac \*/
/** html .clearfix {height: 1%;}*/

.clearfix {
    display: block;
}

/* End hide from IE-mac */

然后只需在每个浮动元素容器中使用该类即可。

I use the clearfix class.

.clearfix:after {
    content: "."; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;
}

.clearfix {
    display: inline-block;
}

/* Hides from IE-mac \*/
/** html .clearfix {height: 1%;}*/

.clearfix {
    display: block;
}

/* End hide from IE-mac */

then just use the class in every floated-element container.

ゃ懵逼小萝莉 2024-08-09 00:51:58
#container { width:200px; }
.floated   { width:100px; float:left; }
.clear     { clear:both; }

<div id="container">
  <div class="floated">A</div>
  <div class="floated">B</div>
  <div class="clear"></div>
</div>
#container { width:200px; }
.floated   { width:100px; float:left; }
.clear     { clear:both; }

<div id="container">
  <div class="floated">A</div>
  <div class="floated">B</div>
  <div class="clear"></div>
</div>
终陌 2024-08-09 00:51:58

我不喜欢 clear: Both;,我宁愿在 Jonathan Sampsons 的示例中这样做:

#container { width:200px; overflow: hidden; }
.floated   { width:100px; float:left; }

 

<div id="container">
  <div class="floated">A</div>
  <div class="floated">B</div>
</div>

I am not a fan of clear: both;, I rather do this in Jonathan Sampsons example:

#container { width:200px; overflow: hidden; }
.floated   { width:100px; float:left; }

 

<div id="container">
  <div class="floated">A</div>
  <div class="floated">B</div>
</div>
痴者 2024-08-09 00:51:58

顺便说一句,您想要

div#parent > div { 浮动:左; }

而不是

div#parent div:first-child { float:left; }

这仍然对 IE6 不友好,但它会使两个子 DIV 浮动。

By the way, you want

div#parent > div { float:left; }

instead of

div#parent div:first-child { float:left; }

which is still not IE6 friendly, but it will float both child DIVs.

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