html - 内联 block div 元素未按预期排列

标签 html css

<分区>

我有一些 HTML 和 CSS 可以创建内联 block 元素 (divs),人们可能会在登录页面上找到这些元素。但是,只有当它们在 div 中包含一些内容(无序列表)时,它们才会显示为正确垂直对齐。如果 div 中没有内容,则该元素会被下推。这是一个jsfiddle .这是代码。谁能解释为什么第三个 div block 没有垂直对齐?

编辑:虽然我很满意这个问题的“修复”是确保每个 div 在样式中使用“vertical-align:top”,但我还是有点困惑至于为什么我首先需要使用这种样式。我认为无论 div 中的内容如何,​​div 元素总是会均匀排列。

<html>
  <head>
<style type="text/css">
    body {
        font-family: Helvetica;
    }

    h1 {
        margin: 0px;
        padding: 10px;
        font-weight: bold;
        border-bottom: 1px solid #aaaaaa;
        font-size: 12px;
    }

    a {
        text-decoration: none;
    }

    ul {
        padding-left: 20px;
    }

    li {
        list-style-type: none;
        font-size: 12px;
    }

    .landing-block {
        display: inline-block;
        background-color: #eeeeee;
        margin-right: 30px;
        width: 192px;
        height: 140px;
        border: 1px solid #aaaaaa;
        -moz-box-shadow: 3px 3px 5px #535353;
        -webkit-box-shadow: 3px 3px 5px #535353;
        box-shadow: 3px 3px 5px #535353;
    }

    .header {
        padding: 10px;
        background-color: red;
        border-bottom: 1px solid #aaaaaa;
        color: #ffffff;
    }

    a:hover {
        text-decoration:underline; 
    }

    h1 > a {
        color: #ffffff;
    }

    h1 > a:hover { 
        color:#ffffff;
    }

    li > a {
        color: #000000;
    }

    li > a:hover { 
        color: #000000;
    }
   </style>
    </head>
    <body>
    <div>
        <div class='landing-block'>
            <h1 style='background-color: #3991db;'>
                <a href='#'>COMPANIES</a>
            </h1>
            <ul>
                <li><a href='#'>Search Companies</a></li>
                <li><a href='#'>New Company</a></li>
            <ul>
        </div>
        <div class='landing-block'>
            <h1 style='background-color: #9139db;'>
                <a href='#'>PEOPLE</a>
            </h1>
            <ul>
                <li><a href='#'>Search People</a></li>
                <li><a href='#'>New Person</a></li>
            <ul>
        </div>
        <div class='landing-block'>
            <h1 style='background-color: #c2db39;'>
                <a href='#'>Products</a>
            </h1>
        </div>
    <div>
</body>
</html>

最佳答案

行内 block 元素默认为 vertical-align:baseline;。将其更改为 vertical-align:top;

 .landing-block {
        display: inline-block;
        background-color: #eeeeee;
        margin-right: 30px;
        width: 192px;
        height: 140px;
        border: 1px solid #aaaaaa;
        -moz-box-shadow: 3px 3px 5px #535353;
        -webkit-box-shadow: 3px 3px 5px #535353;
        box-shadow: 3px 3px 5px #535353;
        vertical-align:top; /* add this rule */

    }

关于html - 内联 block div 元素未按预期排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680595/

相关文章:

jquery - 框阴影效果不适用于幻灯片

html - 将 pdf、doc、ppt 转换为 html5

html - CSS 继承 : parents and childrens' children

html - 使用一个空的 div (<div/>) 和仅 css 创建特定元素

javascript - 浏览器什么时候初始化闪存?

c# - Accordion 中的自动高度 True 不起作用

javascript - 中心部门

html - 无法更改导航栏 Bootstrap 的背景

html - 如果容器也有以百分比表示的最小高度,则以百分比表示的 CSS 最小高度将不起作用

javascript - 如何为具有相同id的多个div设置jQuery mouseleave函数