Html元素显示在父div之外

时间:2017-05-11 05:21:58

标签: html css

我正在尝试在父容器中放置多个公式。每个公式对象都应该在右上角显示关闭按钮。

但似乎我的CSS有问题。关闭按钮将显示在父容器外部。

我无法弄清楚究竟出了什么问题。我以前从未在css工作过。请帮我弄清楚我在这里做错了什么。



.formulaObject {
  float: left;
  display: block;
  width: 250px;
  border-radius: 5px;
  border: 2px solid #4c88aa;
  height: 50px;
  line-height: 50px;
  margin: 2px 25px 2px 25px;
  background-color: #D2E9FE;
}

.formulaText {
  font-size: 15px;
  color: #4c88aa;
  font-weight: bold;
  text-align: left;
  margin-left: 10px;
  width: 150px;
}

.closeButton {
  margin-top: 0px;
  margin-right: 5px;
  top: 0;
  right: 0;
  background-color: black;
  color: white;
  text-align: center;
  border-radius: 3px;
  width: 10px;
  height: 20px;
  width: -webkit-calc(10px);
}

.formulaDiv {
  width: 300px;
  height: 500px;
  background-color: rgba(119, 136, 153, 0.2);
  overflow-x: hidden;
  overflow-y: auto;
  border: 1px solid #4c88aa;
  overflow: auto;
}

<div class="formulaDiv">
  <div class="formulaObject">
    <p class="formulaText">Cost = 0.5 * Weight </p>
    <div class="closeButton">x</div>
    <div>
      <div class="formulaObject">
        <p class="formulaText">Cost = 0.5 * Weight </p>
        <div class="closeButton">x</div>
        <div></div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

问题是.formulaObject的行高设置为50px并由按钮文本继承,但您将黑色按钮的高度设置为20px。您可以通过将该按钮中的行高设置为20px来解决此问题。

&#13;
&#13;
.formulaObject {
  float: left;
  display: block;
  width: 250 px;
  border - radius: 5 px;
  border: 2 px solid #4c88aa;
  height: 50px;
  line-height: 50px;
  margin: 2px 25px 2px 25px;
  background-color: # D2E9FE;
}

.formulaText {
  font-size: 15 px;
  color: #4c88aa;
  font-weight: bold;
  text-align: left;
  margin-left: 10px;
  width: 150px;
}

.closeButton {
  margin-top: 0px;
  margin-right: 5px;
  top: 0;
  right: 0;
  background-color: black;
  color: white;
  text-align: center;
  border-radius: 3px;
  width: 10px;
  height: 20px;
  line-height: 20px;
  width: -webkit-calc(10px);
}

.formulaDiv {
  width: 300px;
  height: 500px;
  background-color: rgba(119, 136, 153, 0.2);
  overflow-x: hidden;
  overflow-y: auto;
  border: 1px solid # 4 c88aa;
  overflow: auto;
}
&#13;
<div class="formulaDiv">
  <div class="formulaObject">
    <p class="formulaText">Cost = 0.5 * Weight </p>
    <div class="closeButton">x</div>
    <div>
      <div class="formulaObject">
        <p class="formulaText">Cost = 0.5 * Weight </p>
        <div class="closeButton">x</div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

改进了HTML格式,div未正确关闭,并使用positioning定位关闭按钮。

.formulaObject {
    float: left;
    display: block;
    width: 250px;
    border-radius: 5px;
    border: 2px solid #4c88aa;
    height: 50px;
    line-height: 50px;
    margin: 2px 25px 2px 25px;
    background-color: #D2E9FE;
    position: relative;
}

.formulaText {
  font-size: 15px;
  color: #4c88aa;
  font-weight: bold;
  text-align: left;
  margin-left: 10px;
  width: 150px;
}

.closeButton {
    margin-top: 0px;
    margin-right: 5px;
    top: 0;
    right: 0;
    background-color: black;
    color: white;
    text-align: center;
    border-radius: 3px;
    width: 10px;
    height: 20px;
    line-height: 20px;
    position: absolute;
}

.formulaDiv {
  width: 300px;
  height: 500px;
  background-color: rgba(119, 136, 153, 0.2);
  overflow-x: hidden;
  overflow-y: auto;
  border: 1px solid #4c88aa;
  overflow: auto;
}
<div class="formulaDiv">
  <div class="formulaObject">
    <p class="formulaText">Cost = 0.5 * Weight </p>
    <div class="closeButton">x</div>
  </div>
  <div class="formulaObject">
    <p class="formulaText">Cost = 0.5 * Weight </p>
    <div class="closeButton">x</div>
  </div>
</div>

答案 2 :(得分:0)

&#13;
&#13;
.formulaObject {
  float: left;
  display: block;
  width: 250px;
  border-radius: 5px;
  border: 2px solid #4c88aa;
  height: 50px;
  line-height: 50px;
  margin: 2px 25px 2px 25px;
  background-color: #D2E9FE;
}

.formulaText {
  font-size: 15px;
  color: #4c88aa;
  font-weight: bold;
  text-align: left;
  margin-left: 10px;
  display: inline-block;
  margin-top: 0;
}

.closeButton {
  margin-top: 0;
  margin-right: 5px;
  display: inline;
  background-color: black;
  color: white;
  text-align: center;
  border-radius: 3px;
  width: -webkit-calc(10px);
}

.formulaDiv {
  width: 300px;
  height: 500px;
  background-color: rgba(119, 136, 153, 0.2);
  overflow-x: hidden;
  overflow-y: auto;
  border: 1px solid #4c88aa;
  overflow: auto;
}
&#13;
<div class="formulaDiv">
  <div class="formulaObject">
    <p class="formulaText">Cost = 0.5 * Weight </p>
    <div class="closeButton">x</div>
    <div>
      <div class="formulaObject">
        <p class="formulaText">Cost = 0.5 * Weight </p>
        <div class="closeButton">x</div>
      </div>
    </div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

首先,您有多个元素标记验证错误。行高:50像素;也导致x从他们的小黑盒子里出来。以下是我修复它的方法。

    <!DOCTYPE html>
    <html>
    <head>
      <title>Page Title</title>
      <style>
         .formulaObject{
         float : left;
         display : block;
         width : 250px;
         border-radius: 5px;
         border: 2px solid #4c88aa;
         height : 50px;
         margin : 2px 25px 2px 25px;
         background-color: #D2E9FE;
         }
         .formulaText{
         font-size:15px;
         color:#4c88aa;
         font-weight: bold;
         text-align:left;
         margin-left:10px;
         width:150px;
         }
         .closeButton{
         margin-top: 0px;
         margin-right: 5px;
         top : 0;
         right: 0;
         background-color: black;
         color: white;
         text-align: center;
         border-radius: 3px;
         width : 10px;
         height: 20px;
         width: -webkit-calc(10px);
         float: right;
         }
         .formulaDiv{
         width: 300px;
         height: 500px;
         background-color: rgba(119, 136, 153, 0.2);
         overflow-x: hidden;
         overflow-y: auto;
         border : 1px solid #4c88aa;
         overflow:auto;
         }
      </style>
    </head>
    <body>
      <div class="formulaDiv">
      <div class="formulaObject">
      <div class="closeButton">x</div>
      <p class="formulaText">Cost = 0.5 * Weight </p>
      </div>
      <div class="formulaObject">
        <div class="closeButton">x</div>
      <p class="formulaText">Cost = 0.5 * Weight </p>
      </div>
      </div>
    </body>
    </html>