html - 高度以像素为单位,而不是百分比

标签 html css

我正在尝试使用以下 html 设置创建一个简单的菜单:

<div class="container">
    <div class="container-cell">
        <a href="play.html">
            <div class="menu-option no-margintop">
                <div class="play-icon">
                    <span class="helper"></span><img src="./img/icon_1.png"/>
                </div>
                <div class="menu-text"><span>Play</span></div>
            </div>
        </a>
        <a href="index.html">
            <div class="menu-option">
                <div class="play-icon">
                    <span class="helper"></span><img src="./img/icon_3.png"/>
                </div>
                <div class="menu-text"><span>Highscore</span></div>
            </div>
        </a>
        <a href="index.html">
            <div class="menu-option">
                <div class="play-icon">
                    <span class="helper"></span><img src="./img/icon_2.png"/>
                </div>
                <div class="menu-text"><span>FAQ</span></div>
            </div>
        </a>
    </div>
</div>

在创建这个过程中,我从硬像素切换到百分比。虽然设置高度的像素版本有效,但它似乎不适用于百分比。一个特定类的高度,menu-option,似乎没有被浏览器拾取。

我已经搜索了一段时间,我遇到的唯一解决方案是为 body 和 html (100%) 设置高度。我还为任何容器元素设置了高度。

参见下面的 CSS:

html, body{
    height:100%;
}

.container {
    display:table;
    width:100%;
    height:70%;
}

.container-cell {
    display:table-cell;
    vertical-align: middle;
    height:100%;
}

.menu-option{
    width:90%;
    margin-left:5%;
    border-radius:10px;
    background-color:rgba(17,23,28,0.2);
    height:6.94%;
    margin-top:5%;
    font-size:150%;
}

为什么浏览器没有选择百分比高度,如何让它使用百分比高度?

最佳答案

display: inline-block; 添加到您的菜单选项类中,以使高度属性起作用。像这样:

.menu-option{
    display: inline-block;
    width:90%;
    margin-left:5%;
    border-radius:10px;
    background-color:rgba(17,23,28,0.2);
    height:6.94%;
    margin-top:5%;
    font-size:150%;
}

这是上面的一个 jsfiddle:https://jsfiddle.net/AndrewL32/e0d8my79/17/

关于html - 高度以像素为单位,而不是百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29186199/

相关文章:

javascript - 使用 JS/jQuery 修改路径的最有效方法

javascript - 将变量值放入文本框中,而不使用 JavaScript 中的 onclick 按钮

jquery - 调整大小时弹出警报离开屏幕

javascript - 菜单图标不显示在桌面版 Safari 中或根本不显示在移动设备上

html - 将 Bootstrap 输入元素移到页面下方

html - 垂直和水平对齐的两列布局,具有可变高度

html - 很好地跨度 Bootstrap 以覆盖整个页面

html - 在悬停其他元素时更改主体背景图像/渐变

CSS 帮助,使菜单流畅

html - 悬停对 class 和 id 的影响是否相互关联?