1

我在另一个 div 中有一个 div。外部 div 有一个百分比宽度(80%),我希望内部 div 的宽度比外部 div 的宽度小 30px。我该怎么做呢?我假设我需要使用javascript?提前致谢。

4

2 回答 2

7

使用边距:

​<div style="width:80%;"> 
    <div style="margin-left:15px;margin-right:15px;"> inner </div>
</div>​​​​​​​​​​​​​

示范

于 2012-10-04T14:30:10.950 回答
0

@dystroy 有一个很好的答案,尽管它缺乏动态定制。

我的看法,使用 CSS 变量。

你必须做这样的事情:

#parent-div
{
    var-width: 100px; /* Or something else? */
    //Notice that though the attribute is named var-width, the browser will
    //treat it as width and will use the `var-` prefix as directive
    //so that the calculations can be performed by using it as variable.
}
#child-div
{
    width: calc(var(width) - 30);
}

这将使 CSS 计算孩子的宽度。

更多关于这个,在这里。

于 2012-10-04T15:18:50.310 回答