html - 如何让行内 block 占据整行?

标签 html css menu

我正在构建一个菜单,其中每个下拉项都具有其内容的宽度。为了解决这个问题,我必须将这些元素制作成内联 block ,但这意味着它们最终都在同一行上。有没有办法在不使用边距的情况下让它们都在一行上,或者有没有办法让 block 具有其内容的宽度?

谢谢

示例菜单项 HTML:

    <div class="menuItemWrap">
        <a href="#" class="menuItemHead">resources</a>
        <div class="menuItemContentWrap">
        <a href="#" class="menuItemContentItem">articles</a>
        <a href="#" class="menuItemContentItem">web reviews</a>
        <a href="#" class="menuItemContentItem">bookstore</a>
        <a href="#" class="menuItemContentItem">powerpoint</a>
        <a href="#" class="menuItemContentItem">schwab forms</a>
        <a href="#" class="menuItemContentItem">blunck forms</a>


        </div>    

    </div>

CSS:

.menuItemWrap{
    display:inline-block;
    float:left;
}

.menuItemHead{
    padding:14px 22px;
    margin: 0px;
    border:0px;
    background:#2389DD; 
}

.menuItemContentWrap{
    position:absolute;
    display:none;
    width:100px;
    background:#A6CFD1;
}

.menuItemContentItem{
    display:block;
    padding: 8px 14px;
    background: red;
    color:#333;
    position:relative;
    float:left;
}

最佳答案

float 删除元素的宽度和高度。为了使元素出现在单独的行上,使用 clear属性:

float: left;
clear: left;

引自MDN:

The clear CSS property specifies whether an element can be next to floating elements that precede it or must be moved down (cleared) below them.

Demo

关于html - 如何让行内 block 占据整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25429860/

相关文章:

javascript - 进度条、最大值并重置为 100%

javascript - 如何使用jquery动态改变css的属性

css - 如何防止背景图像覆盖边框图像

javascript - Jquery菜单: default active DIV container

menu - 在 Awesome WM 菜单中切换用户

html - 将滚动条定位在屏幕底部,并对齐列

javascript - 具有服务器端呈现的 HTML 片段的主干模型

html - 如何在 Bootstrap 中将此 <a> 标记置于此模式的中心?

html - 表格单元格中的溢出文本

ios - 从侧面菜单推送 ViewController 不起作用