div 填充、边距?
我是一名 iPhone 开发人员,一直被一些基本的 CSS 属性所困;) 我想展示这样的东西:
这就是我所拥有的:
<div class="cell">
<div class="cell_3x3_top">
<div class="cell_3x3_type rounded_left">type</div> <!--UPDATED:2010/09/29-->
<div class="cell_3x3_title rounded_right">title</div><!--UPDATED:2010/09/29-->
</div>
<div class="cell_3x3_content rounded_left rounded_right">content</div><!--UPDATED:2010/09/29-->
</div>
和 css:
div.cell_3x3_top{
height:20%;
margin: 0 0 0 0;
padding: 0 0 0 0;
border: none;
margin-bottom: 1px; /*to compensate space between top and content*/
text-align: center;
vertical-align: middle;
}
div.cell_3x3_type{
width:20%;
float:left;
background-color: inherit;
margin-right: -2px; /*UPDATED:2010/09/29*/
}
div.cell_3x3_title{
width:80%;
float:left;
background-color: inherit;
margin: 0 0 0 0; /* maybe not neccesary*/
padding: 0 0 0 0; /*maybe not neccesary*/
margin-left: -1px; /*UPDATED:2010/09/29 */
}
div.cell_3x3_content{
height:80%;
background-color: inherit;
}
但是当我使用上面的代码渲染内容时 title< /code> div 似乎太大了,并且出现在
type
div 下面,这是为什么? type
div 的宽度为 20%,title
的宽度为 80%,因此应该准确为 100%。我在这里忘记了任何保证金或其他指标吗? 我尝试使用边距将 title
div 移动到左侧,但仍然有问题。我想知道获得类似图片的正确方法是什么? (不完全是因为如果你仔细观察 title
div 比它应该的要短一点。看到它的右边框没有与 content
div 对齐。)
提前致谢。
编辑:2010/09/28
这实际上是我想要实现的目标:
这就是我所拥有的:
如果我没有边框 div,上面的代码(更新了一点)就可以工作。由于边框宽度为 1px,我需要的是将 type
div 宽度设置为 20%-2px(左边框 + 右边框 = 2px),并将 title
div 设置为 80%-2px
.rounded_left{
border-top-left-radius: 4px 4px;
border-bottom-left-radius: 4px 4px;
border-color:gray;
border-width: 1px;
border-style:solid;
}
(.rounded_right 类似)
我相信这与 clear:both
属性无关。我尝试过,但没有任何效果,因为我的 content
div 从一开始就很好。
简而言之:我怎样才能使一个div(包括其边框)宽度恰好为20%?
Ignacio
回答:
我意识到围绕 type 和 title 的包装 div 分别解决了这个问题。所以我的答案是这样的:
<td class="cell">
<div class="cell_3x3_top bordered">
<div class="cell_3x3_type_container"><div class="cell_3x3_type rounded_left full_height">6</div></div>
<div class="cell_3x3_title_container"><div class="cell_3x3_title rounded_right full_height">title</div></div> </div>
<div class="cell_3x3_content rounded_left rounded_right">content</div>
</td>
我在容器中设置了 20% 和 80%,在内部 div 中设置了边框。
I am a iPhone developer stuck with some basic CSS properties ;)
I want to show something like this:
This is what I have:
<div class="cell">
<div class="cell_3x3_top">
<div class="cell_3x3_type rounded_left">type</div> <!--UPDATED:2010/09/29-->
<div class="cell_3x3_title rounded_right">title</div><!--UPDATED:2010/09/29-->
</div>
<div class="cell_3x3_content rounded_left rounded_right">content</div><!--UPDATED:2010/09/29-->
</div>
and the css:
div.cell_3x3_top{
height:20%;
margin: 0 0 0 0;
padding: 0 0 0 0;
border: none;
margin-bottom: 1px; /*to compensate space between top and content*/
text-align: center;
vertical-align: middle;
}
div.cell_3x3_type{
width:20%;
float:left;
background-color: inherit;
margin-right: -2px; /*UPDATED:2010/09/29*/
}
div.cell_3x3_title{
width:80%;
float:left;
background-color: inherit;
margin: 0 0 0 0; /* maybe not neccesary*/
padding: 0 0 0 0; /*maybe not neccesary*/
margin-left: -1px; /*UPDATED:2010/09/29 */
}
div.cell_3x3_content{
height:80%;
background-color: inherit;
}
But when I render my content with above code title
div seems to be too large and it appears underneath type
div, Why is this?type
div is 20% width, title
is 80% width so it should be 100% exactly. Is any margin or other metric I am forgetting here?
I have tried to move title
div to the left using margin but is still buggy. I wonder what is the correct way of getting something like the picture?
(Not exactly because if you look closer title
div is a little bit shorter than it should be. See that its right border is not aligned with content
div.)
Thanks in advance.
EDIT: 2010/09/28
This is actually what I want to achieve:
and this is what I have:
Above code (updated a little bit) would work if I wouldn't have bordered divs. Since border width is 1px what I need is to set type
div width to 20%-2px (left border + right border = 2px) and title
div to 80%-2px
.rounded_left{
border-top-left-radius: 4px 4px;
border-bottom-left-radius: 4px 4px;
border-color:gray;
border-width: 1px;
border-style:solid;
}
(.rounded_right is similar)
This is not related to clear:both
property I believe. I tried and didn't had any effect since my content
div was good form the beginning.
In short: How can I make a div including its border to be let's say exactly 20% width?
Ignacio
ANSWER:
I realized that a wrapper div around type and title respectively solves the problem. So my answer is kind of like this:
<td class="cell">
<div class="cell_3x3_top bordered">
<div class="cell_3x3_type_container"><div class="cell_3x3_type rounded_left full_height">6</div></div>
<div class="cell_3x3_title_container"><div class="cell_3x3_title rounded_right full_height">title</div></div> </div>
<div class="cell_3x3_content rounded_left rounded_right">content</div>
</td>
I set 20% and 80% in the containers and the borders in the inner div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少一个清除 div。浮动元素不会像您期望的那样扩展
.cell_3x3_type
div。试试这个:CSS:
其余保持不变。
编辑:
对clear属性作用的一个小解释:考虑一个仅包含浮动元素的容器
div
,如下所示(为了清楚起见,使用内联CSS):(来源:fii.cz)
容器
div
的高度为 0,因为浮动元素已从文档流中取出,不再影响其容器的高度。元素上的clear: Both
属性“清除”所有浮动元素,即确保该元素放置在其之前的所有浮动元素下方:(来源:fii.cz)
如果结合上面的两个示例,您可以强制容器 div 的高度等于其中最高浮动元素的高度:
(来源:fii.cz)
You are missing a clearing div. The floating elements do not expand the
.cell_3x3_type
div as you would expect. Try this instead:CSS:
The rest remains the same.
EDIT:
A small explanation of what the clear property does: consider a container
div
that contains only floated elements, like this (using inline CSS for clarity):(source: fii.cz)
The height of the container
div
is 0 because the floating elements are taken out of the document flow and do not affect the height of their container anymore. Theclear: both
property on an element "clears" all floats, i.e. makes sure that the element is placed below all floating elements that precede it:(source: fii.cz)
If you combine the two above examples, you can force the container div to have its height equal to the height of the highest floating element in it:
(source: fii.cz)