css - div高度不符合内容高度

标签 css

我正在尝试自动调整主 div 的高度,但我无法让它工作。我输入了 620px,以便您可以看到内容,但我想让它适合它的内容。

CSS 代码:

#distributors {
}

#distributors input {
    height: 2.5em;
    visibility: hidden;
}

#distributors input.visible {
    visibility: visible !important;
}

#distributors label {
    background: #f9f9f9; 
    border-radius: .25em .25em 0 0;
    color: #888;
    cursor: pointer;
    display: block;
    float: left;
    font-size: 1em;
    height: 2.5em;
    line-height: 2.5em;
    margin-right: .25em;
    padding: 0 1.5em;
    text-align: center;
}

#distributors input:hover + label {
    background: #ddd; 
    color: #666;
}

#distributors input:checked + label {
    background: #f1f1f1; 
    color: #444;
    position: relative;
    z-index: 6;
    /*
    -webkit-transition: .1s;
    -moz-transition: .1s;
    -o-transition: .1s;
    -ms-transition: .1s;
    */
}

#content {
    background: #f1f1f1;
    border-radius: 0 .25em .25em .25em;
    min-height: 20em;
    position: relative;
    overflow: auto;
    width: 100%;
    height: 620px;
    z-index: 5;
}

#content .store-card {
    min-height: 240px;
    margin-bottom: 15px;
    display: inline-block;
}

#content .store-card img {
    margin-bottom: 10px;
}

#content .store-card h4 {
    font-weight: 700;
    margin-bottom: 0;
}

#content .store-card p {
    margin-bottom: 0;
}

#content .store-card a {
    font-weight: 700;
}

#content-1, #content-2, #content-3, #content-4 {
    opacity: 0;
    padding: 1.5em;
    position: absolute;
    display: block;
    z-index: -100;
    transition: all linear 0.3s;
}

#distributors input#tab-1:checked ~ #content #content-1,
#distributors input#tab-2:checked ~ #content #content-2,
#distributors input#tab-3:checked ~ #content #content-3,
#distributors input#tab-4:checked ~ #content #content-4 {
    opacity: 1;
    z-index: 100;
}

请检查以下 jsfiddle 并尽可能修复: https://jsfiddle.net/uydomdub/

最佳答案

您需要从子内容 div 中删除绝对定位,更改 fiddle 中的最后样式并从内容 div 中删除固定高度:

#content-1,
#content-2,
#content-3,
#content-4 {
  opacity: 0;
  padding: 1.5em;
  display: none;
  transition: all linear 0.3s;
}

#distributors input#tab-1:checked ~ #content #content-1,
#distributors input#tab-2:checked ~ #content #content-2,
#distributors input#tab-3:checked ~ #content #content-3,
#distributors input#tab-4:checked ~ #content #content-4 {
  opacity: 1;
  z-index: 100;
  display:block;
}

Updated fiddle

关于css - div高度不符合内容高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42925230/

相关文章:

jquery - :not([attr ="value"]) and [attr! ="value"之间的性能有区别吗?

html - 如何使表格行固定在顶部

html - 制作部分不在屏幕上的 div,它不会向屏幕添加水平滚动

javascript - 处理时的 jQuery 数据表和动画 SVG

css - iPhone 忽略所有媒体查询

javascript - 我的 href 链接按钮无法联机使用

javascript - 用 JS 制作自定义文本编辑器(styler)

jquery - 平滑滚动形成并专注于文本区域

html - 在字段集图例标题旁边添加图标

html - 最大宽度防止网格填满所有空间