我是一名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
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%?
伊格纳西奥
答案:
我意识到在类型和标题周围分别放置包装器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的边框。