html - 如何使包含 UL 的 DIV 的高度自动扩展

标签 html css formatting height

我正在使用下面的 HTML 来显示一个选项卡式列表(我通过专注于最长的列表来简化它,因为这是导致我的问题的那个)。我怎样才能使包含uldiv根据ul的长度自动扩展到它的高度。 CSS 位于 HTML 下方:

<div class="col-lg-offset-2 col-lg-8">
    <section class="tabs">
        <h2 class="centered">SAMPLE MENU</h2>
        <input id="tab-1" type="radio" name="radio-set" class="tab-selector-1"     checked="checked"    />
        <label for="tab-1" class="tab-label-1">Casual Eating</label>
        <input id="tab-2" type="radio" name="radio-set" class="tab-selector-2" />
        <label for="tab-2" class="tab-label-2">Dining</label>
        <input id="tab-3" type="radio" name="radio-set" class="tab-selector-3" />
        <label for="tab-3" class="tab-label-3">Afternoon Tea</label>
        <input id="tab-4" type="radio" name="radio-set" class="tab-selector-4" />
        <label for="tab-4" class="tab-label-4">Desserts</label>
        <div class="clear-shadow"></div>
        <div class="content">
            <div class="content-2">
                <ul class="menu_items">
                    <li>
                        <h3>Wild Boar Bons</h3>
                        <p>wensleydale wild boar, pickled mushrooms & quince</p>
                    </li>
                    <li>
                        <h3>Tangled Tiger Prawns</h3>
                        <p>chilli & garlic prawns, straw pastry, spiced coconut broth</p>
                    </li>
                    <li>
                        <h3>Wood Pigeon Nest</h3>
                        <p>local wood pigeon, potato & parsnip nest, foraged fruit</p>
                    </li>
                    <li>
                        <h3>Avocado Mango Burratta</h3>
                        <p>buffalo mozzarella, alfonso mango, citrus rye toast</p>
                    </li>
                    <li>
                        <h3>Seared Black Salmon</h3>
                        <p>cucumber salad, soy, ginger & mirin</p>
                    </li>
                    <li>
                        <h3>Rose Veal Rib</h3>
                        <p>aged veal chop, rosemary desiree potato & shallots</p>
                    <li>
                        <h3>Crispy Rabbit Cassoulet</h3>
                        <p>fried leg, loin, chorizo cassoulet & celeriac slaw</p>
                    </li>
                    <li>
                        <h3>Morel Butter Turbot</h3>
                        <p>turbot loin poached in morel butter, capers & samphire</p>
                    </li>
                    <li>
                        <h3>Wild Garlic Raviolo</h3>
                        <p>green herb pasta, goats cheese, confit shallots</p>
                    </li>
                    <li>
                        <h3>Pink Lady Pig</h3>
                        <p>belly & loin, king scallop & apple salad</p>
                    </li>
                    <li>
                        <h3>Pecan Maple Goose</h3>
                        <p>wild goose, streaky bacon, roast swede & chesnuts</p>
                    </li>
                    <li>
                        <h3>Lobster Fennel Pie</h3>
                        <p>whole lobster tail, saffron mash & baby fennel</p>
                    </li>
                    <li>
                        <h3>Girolle Truffle Brioche</h3>
                        <p>girolles, quails eggs, asparagus, black truffle & toasted brioche</p>
                    </li>
                    <li>
                        <h3>Pan Fried Fillet</h3>
                        <p>oxtail fritter ,smoked garlic& sirloin butter, roast onion</p>
                    </li>
                </ul>
            </div>
        </div>
    </section>
</div>

上面代码的CSS:

.menu_wrapper {
    background: url(../img/bg/light_cream_bg.jpg);
}
.tabs {
    background: url("http://fmkcatering.com/wp-content/themes/fmk/images/grey-bg.jpg") repeat scroll 0 0 rgba(0, 0, 0, 0);
    position: relative;
    margin: 40px auto;
    width: 750px;
}
.tabs input {
    position: absolute;
    z-index: auto;
    width: 100%;
    height: 40px;
    left: 0px;
    top: 38px;
    opacity: 0;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    filter: alpha(opacity=0);
    cursor: pointer;
}
.tabs label {
    font-size: 15px;
    line-height: 40px;
    height: 40px;
    position: relative;
    padding: 0 20px;
    float: left;
    display: block;
    width: 187px;
    color: #385c5b;
    letter-spacing: 1px;
    text-transform: uppercase;
    font-weight: bold;
    text-align: center;
    text-shadow: 1px 1px 1px rgba(255,255,255,0.3);
    border-radius: 3px 3px 0 0;
}
.tabs label:after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 2px;
    display: block;
}
.tabs input:hover + label {
    color: #FFF;
    border: 1px solid #F90;
    background-color: #CCC;
}
.tabs label:first-of-type {
    z-index: 4;
}
.tab-label-2 {
    z-index: 3;
}
.tab-label-3 {
    z-index: 2;
}
.tab-label-4 {
    z-index: 1;
}
.tabs input:checked + label {
    z-index: 6;
}
.clear-shadow {
    clear: both;
}
.content {
    position: relative;
    width: 100%;
    height: 370px;
    z-index: 5;
    border-radius: 0 3px 3px 3px;
}
.content div {
    position: absolute;
    top: 0;
    left: 0;
    padding: 10px 40px;
    z-index: 1;
    opacity: 0;
    -webkit-transition: opacity linear 0.1s;
    -moz-transition: opacity linear 0.1s;
    -o-transition: opacity linear 0.1s;
    -ms-transition: opacity linear 0.1s;
    transition: opacity linear 0.1s;
}
.tabs input.tab-selector-1:checked ~ .content .content-1, .tabs input.tab-selector-2:checked ~ .content .content-2, .tabs input.tab-selector-3:checked ~ .content .content-3, .tabs input.tab-selector-4:checked ~ .content .content-4 {
    z-index: 100;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);
    opacity: 1;
    -webkit-transition: opacity ease-out 0.2s 0.1s;
    -moz-transition: opacity ease-out 0.2s 0.1s;
    -o-transition: opacity ease-out 0.2s 0.1s;
    -ms-transition: opacity ease-out 0.2s 0.1s;
    transition: opacity ease-out 0.2s 0.1s;
}
.content div h2, .content div h3 {
    color: #398080;
}
.content div p {
    font-size: 14px;
    line-height: 22px;
    font-style: italic;
    text-align: left;
    margin: 0;
    color: #777;
    font-family: Cambria, Georgia, serif;
    text-align: center;
}
.menu_items {
    list-style: none outside none !important;
    width: 700px;
    margin: 0;
    padding: 0;
    text-align: left;
}
.menu_items li {
    float: left;
    width: 340px;
}
.menu_items li h3 {
    font-size: 15px;
    font-size: 15px;
    margin-bottom: 5px;
    text-align: center;
}

这是 fiddle :

http://jsfiddle.net/QGyJ9/1/

最佳答案

我已经更新了示例(抱歉,没有意识到其他人将不再看到原始代码——我第一次使用 jsfiddle)。

我不得不做各种改变:

  • 删除“.content”的硬编码高度(让浏览器计算)
  • 删除“.content div”的绝对定位(不清楚你为什么需要它)
  • 最重要的是为“.content:after”添加了一条规则(这通常通过在您的 CSS 中定义一个 clearfix 类并在适当的元素上使用它来使用

    .内容:之后{ 内容: ” ”; 显示: block ; 字体大小:0; 高度:0; 明确:两者; 可见性:隐藏;

现在背景延伸到列表底部

关于html - 如何使包含 UL 的 DIV 的高度自动扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24905451/

相关文章:

javascript - Bootstrap 选项卡式导航故障

javascript - Canvas getImageData 但按比例缩小

html - 如何使用显示: table to get the same row heights in firefox as in chrome and edge

c# - 带空格的字符串格式

javascript - 使用 Javascript 将 24 小时时间转换为 12 小时时间 w/AM & PM

c++ - 格式化 C++ 控制台输出

jquery - 使用 Jquery/css 在单选按钮上切换 div 标签背景颜色单击

javascript - 如何修复 Accordion 拼贴菜单的 Tab 键顺序?

javascript - 使用 styled-components 的 createGlobalStyle() 从文件创建全局样式

html - 控制和对齐 CSS 中的段落