首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将父元素的高度与子元素匹配-相对位置和绝对位置

问将父元素的高度与子元素匹配-相对位置和绝对位置
EN

Stack Overflow用户
提问于 2016-04-18 09:25:27
回答 2查看 663关注 0票数 1

虽然这看上去很基本,但我无法做到这一点。

简单地说,我有一个父元素-相对位置。然后是子元素-绝对位置。

如果子级的高度大于父级,如何匹配父级的高度。意思是,如何使父级展开。

以下是我所看到的https://jsfiddle.net/wthwawcv/中的js小提琴

代码语言:javascript
复制
<div class="parent">
  dsfsdfsdfsdf<br> 
  hellow .. line 2 <br>
  hello .. line 3
  <div class="child">
    this is the child element to be diaplayed
  </div>
</div>


.parent {
  width:100%;
  background: #ff0000;
  width:200px;
  position: relative;
  overflow: hidden;
}

.child {
  width:40px;
  height:100px;
  background: #ffff00;
  position: absolute;
  right: 0;
  top: 0;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-18 09:37:45

与绝对定位不同,您可以使用浮动和清除修补程序:

​

代码语言:javascript
复制
.parent {
  width:100%;
  background: #ff0000;
  width:200px;
  position: relative;
}
.parent:after {
  content: '.';
  color: transparent;
  clear: right;
  overflow: hidden;
  display: block;
  height: 0;
}

.child {
  float: right;
  width:40px;
  background: #ffff00;
}
代码语言:javascript
复制
<div class="parent">
<div class="child">
    this is the child element to be displayed
  </div>
  Child is<br> 
  longer than<br>
  parent content.
</div>
<br>
<div class="parent">
<div class="child">
    child
  </div>
  Child is<br> 
  shorter than<br>
  parent content.
</div>

​

票数 1
EN

Stack Overflow用户

发布于 2016-04-18 09:36:31

如果您可以使用jquery,那么请在您的文件中添加以下代码,并从子类中删除高度。这是工作小提琴演示

代码语言:javascript
复制
var min_parent_height = jQuery(".child").height()+"px";
console.log(min_parent_height);
jQuery(".parent").css({'min-height':min_parent_height});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36690039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档