2

我试图将 div 保持#chat-inner在 div 的中间#chat-main,我可以通过使用百分比值来做到这一点,但我的问题是应该存在2px百分比值无法提供的间隙(相对于宽度和高度)。为了清楚理解,请参阅 CSS 代码中的注释。

JSbin

HTML

<div id="chat-outline">
  <div id="chat-main">
    <div id="chat-inner">
    </div>
  </div>
</div>

CSS

#chat-outline
{background-color:grey;width:30%;height:40%;
 position:fixed;bottom:5px;right:5px;
 padding:2px;}

#chat-main
{
  width:100%;
  height:100%;
  background-color:silver;
  overflow:hidden;
}

#chat-inner
{
  width:95%;
  height:97%;

  /*How can I give pixels here? I need 2px value*/
  margin:2.5% 2.5% 2.5% 2.5%;  

  /*margin:2px;*/

  background-color:cornflowerblue;
}
4

2 回答 2

1

尝试这样的事情(演示):

#chat-outline
{ background-color:grey;width:30%;height:40%;
  position:fixed;bottom:5px;right:5px;
padding:2px;}

#chat-main
{
  width:100%;
  height:100%;  
  background-color:silver;
  overflow:hidden;
  position:relative;
}

#chat-inner
{  
  top:2px;
  bottom:2px;
  right:2px;
  left:2px;  
  background-color:cornflowerblue;
  position:absolute;
}
于 2012-11-22T06:31:41.163 回答
1

您还可以通过以下方式实现您想要的结果padding:-

#chat-outline
{background-color:grey;width:30%;height:40%;
  position:fixed;bottom:5px;right:5px;
padding:2px;}

#chat-main
{
  width:100%;
  height:100%;
  background-color:silver;
  overflow:hidden;
  padding:2px;
  -moz-box-sizing:border-box;
  box-sizing:border-box;
  -webkit-box-sizing:border-box;
}

#chat-inner
{
  width:100%;
  height:100%;
  background-color:cornflowerblue;
}

演示

于 2012-11-22T06:40:26.523 回答