父元素不跟随子元素

时间:2017-08-24 08:59:28

标签: html css css-float

当我们设置父元素并增加子元素的内容时,父母的大小是否应该自动缩放以适合孩子?

相反,已应用css属性的父级(例如给定的背景)不会缩放父级。这是我尝试创建一个3列简单布局,并且父级没有缩放,因此未应用边框底部:

REPL.IT

正如您所看到的,内容溢出了父级,并且所有内容都未包含在父级中。我该如何解决这个问题?

5 个答案:

答案 0 :(得分:5)

您需要的是#bodytext上的clearfix,因为您的所有内容都会浮动。

将以下行添加到#bodytext

#bodytext{
  overflow: auto;
}

为了更好地理解,为什么需要clearfix read this answer

答案 1 :(得分:1)

您尚未正确清除 floats - 将overflow: hidden添加到bodyText或添加使用after的{​​{1}}元素:

clear: left

见下面的演示:



#bodytext:after{
  clear: left;
  content: '';
  display: block;
}

@font-face {
  font-family: "handlee";
  src: url("handlee.ttf");
}

@font-face {
  font-family: "berkshirestyle";
  src: url("berkshire.ttf");
}

body {
  background-image: url("glass.jpg");
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-size: cover;
}

#FirstHeader {
  float: left;
  clear: left;
  padding-left: 100px;
  color: white;
  font-size: 70px;
  color: rgb(122, 38, 0);
  text-decoration: underline;
  font-family: handlee;
  font-style: italic;
}

#bodytext {
  padding: 30px;
  font-family: berkshirestyle, cursive;
  font-size: 1em;
  color: #BCBCBC;
  overflow: hidden;
}

#bodytext:after{
  clear: left;
  content: '';
  display: block;
}

#bodytext {
  clear: left;
  margin-left: 30px;
  margin-bottom: 50px;
  border-bottom: 10px solid red;
  background-color: red;
}

#bodytext div {
  display: inline-block;
  width: 30%;
  margin-right: 20px;
  float: left;
}

form {
  clear: both;
}




答案 2 :(得分:0)

您可以使用display:inline-block

#bodytext {

    display:inline-block;

}

答案 3 :(得分:0)

你需要做两件事:首先将css overflow:hidden提供给#bodytext,然后将所有3 div提升为第二个,即FirstParagraph,SecondParagraph,ThirdParagraph,你放<div class="clear"></div>,css为clear class {{ 1}}。

答案 4 :(得分:0)

只需使用 library(Matrix) library(pracma) columncrossprod <- function(x){ x_s <- Matrix(x, sparse = TRUE) tt <- x_s %*% t(x_s) tt <- as.matrix(tt) tt <- tt[row(tt) <= col(tt)] return(tt) } v_i = apply(b,2,columncrossprod) # repeat the matrix m across the space of y_i Mr<-repmat(M,ncol(y_i),1) Mr <- t(Mr) # perform the inner subtraction nom <- Mr - y_i # cross product nom2 <- nom %*% nom div中的float:left来解决此问题,请查看下面的代码段。

&#13;
&#13;
#bodytext
&#13;
@font-face{
    font-family: "handlee";
    src : url("handlee.ttf");
}
@font-face{
    font-family:"berkshirestyle";
    src: url("berkshire.ttf");
}
body{

    background-image: url("glass.jpg");
    background-attachment: fixed;
    background-repeat:no-repeat;
    background-size:cover;

}
#FirstHeader{
    float:left;
    clear:left;
    padding-left:100px;
    color:white;
    font-size: 70px;
    color: rgb(122, 38, 0);
    text-decoration: underline;
    font-family: handlee;
    font-style: italic;
}

#bodytext{
    padding: 30px;
    font-family: berkshirestyle, cursive;
    font-size: 1em;
    color: #BCBCBC;
    float: left;
}



#bodytext{
    clear:left;
    margin-left: 30px;
    margin-bottom: 50px;
    border-bottom: 10px solid red;
    background-color: red;

}

#bodytext div{
    display: inline-block; 
    width: 30%;
    margin-right: 20px;
    float: left;

}


form{
    clear:both;

}
&#13;
&#13;
&#13;

相关问题