我有一个div box-container,它是viewBlocks div中的一个子项.

我遇到的问题是,box-containerviewBlocks中溢出了.我试着把position: relative加到viewBlocks上,但没有效果,我在这个网站上找不到任何其他问题的答案:/.

.viewBlocks {
    text-align: center;
    float: right;
    height: 700px;
    width: 190px;
    border: 1px solid black;
    background-color: darkgray;
}
.box {
    position: absolute;
    width: 250px;
    height: 250px;
    background-color: #f1f1f1;
    border: 1px solid #d3d3d3;
    border-radius: 5px;
    resize: both;
    overflow: auto;
    z-index: 1;
    text-align: center;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
  
.box h2 {
    margin: 0;
    padding: 10px;
}
    <div class="viewBlocks">
    <!-- Class box -->
      <div id="box-container">
        <div class="box">
          <h2>Class</h2>
          <input class="class-name-form">
          <button class="class-name-btn" disabled>Set Class Name</button>
          <button disabled>Add Method</button>
          <button class="add-variable" disabled>Add instance variable</button>
        </div>
      </div>
      <!-- End Class Box -->
    
    </div>

推荐答案

position: relative是不够的.您必须定义应该将子项放置在父项中的位置.

默认情况下,子对象的位置是left: 0top: 0,这会使您的框溢出(因为子对象的宽度是250px,而父对象的宽度只有190px).

请阅读以下代码以获得更好的解释:

.viewBlocks {
    text-align: center;
    float: right;
    height: 700px;
    width: 190px;
    border: 1px solid black;
    background-color: darkgray;
    /* Use this and the children will be positioned relatively within this element*/
    position: relative;
}
.box {
    /* Define the placement here*/
     right: 0;
    top: 0;
    position: absolute;
    /* Change this to 100% if you want the element to fill its parent's width*/
    width: 100%; 
    height: 250px;
    background-color: #f1f1f1;
    border: 1px solid #d3d3d3;
    border-radius: 5px;
    resize: both;
    overflow: auto;
    z-index: 1;
    text-align: center;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
  
.box h2 {
    margin: 0;
    padding: 10px;
}
    <div class="viewBlocks">
    <!-- Class box -->
      <div id="box-container">
        <div class="box">
          <h2>Class</h2>
          <input class="class-name-form">
          <button class="class-name-btn" disabled>Set Class Name</button>
          <button disabled>Add Method</button>
          <button class="add-variable" disabled>Add instance variable</button>
        </div>
      </div>
      <!-- End Class Box -->
    
    </div>

Html相关问答推荐

Chrome和Safari浏览器中的CSS3动画不同

输入宽度在表格显示上不起作用FLEX溢出隐藏Firefox

禁用与行分开的边框折叠span

在 Select 前后更改选项卡的背景

首字下沉非字母字符

按钮之间的HTML文本居中不正确

Angular -表和源映射中未显示的数据错误

在css中,如何在英雄版块的标题后面做一个粘性的导航栏?

在每个列宽由带有固定值的minmax()函数定义的网格中,CSS如何为列分配空间?

使用CSS和Slick Carcass使图像适合屏幕

Swift WKUserContentController 和 WKScriptMessageHandler 设置不正确

如何在悬停时更改同级元素 CSS

如何使用 Flutter 构建 Chrome 扩展?

如何将背景与之前的内容正确对齐

标签背景 colored颜色 的 html 和 css 技巧说明

如何在没有容器的情况下沿基线将 div 中的元素居中?

我应该如何使这个带有标签的隐藏复选框可访问

shinydashboard 标题中的位置标题

禁用的 Select 标签没有在 chrome 上的 css 中指定的 colored颜色

更改包含图标高度的 div