【问题标题】:Two buttons vertically misaligning, how to align them properly?两个按钮垂直错位,如何正确对齐?
【发布时间】:2015-04-23 04:27:42
【问题描述】:

我有两个按钮,一个是 A 元素,另一个是 BUTTON 元素。它们都有相同的类 (class="button button--icon")。虽然我对它们都应用了相同的 CSS,但使用这些类时,按钮的显示方式不同。

这两个元素的区别在于 A 元素是其包含的 LI 的直接子元素,但 BUTTON 元素被包装在一个 FORM 中,该 LI 包含在一个 LI 中。

A 按钮的垂直位置高于 BUTTON 按钮。

这些我都试过了,但没有解决我的问题:

  • 将所有字体大小硬编码为 16 像素;
  • 分别对包含的 LI 和 As 应用边距或填充(这只会使我的整个 UL 向下移动)
  • 对 A 和 BUTTON 应用相等的垂直边距。
  • 也将 A 包装在一个表单中(没有效果,因为我在 FORM 上没有样式)
  • display: block !important;
  • vertical-align: baseline;
  • 删除框尺寸声明

有人知道如何将 A 元素向下移动,使其顶部与 BUTTON 的顶部在同一行吗?

我准备了一个带有简化版代码的 CodePen,它显示出了问题所在。如果 CodePen 丢失,我还在这个问题的底部列出了完全相同的代码。 http://codepen.io/MHLut/pen/gpOREP?editors=110

HTML:

<!-- Real world: this UL is normally in a table cell, not a DIV -->
<div class="example">
    <ul>
        <li>
            <a class="button button--icon" href="#">
                <img class="icon" src="http://placehold.it/120x120">
            </a>
        </li>

        <li>
            <form>
                <button class="button button--icon">
                    <img class="icon" src="http://placehold.it/120x120">
                </button>
            </form>
        </li>

        <li>
            <form>
                <button class="button button--icon">
                    <img class="icon" src="http://placehold.it/120x120">
                </button>
            </form>
        </li>
    </ul>
</div>

SCSS:

html {
    @include box-sizing(border-box);
}

*,
*:before,
*:after {
    @include box-sizing(inherit);
}

small {
    font-size: 0.75em;
    line-height: 1.5em;
}

button,
.button {
    border-radius: 0.125rem;
    background-color: #eee;
    border: 0.1em outset darken(#eee, 5%);
    color: #eee;
    line-height: 1.5em;
    padding: 0.5em 0.75em;
    &:hover, &:focus {}
    &:active {
        border-style: inset;
    }
}

.button {
    display: block;
    text-decoration: none;
}

.button--icon {
    @extend small;
    display: block;
    padding: 0.5em;
    margin-top: 0;
    .icon {
        display: block;
        height: 2em;
        width: 2em;
    }
}

.example {
    ul, form {
        margin-bottom: 0;
        padding-top: 0.25rem;
        padding-bottom: 0.25rem;
    }
    ul {
        list-style-type: none;
    }
    li {
        display: inline-block;
    }
    .button {
        @extend small;
        margin-top: 0;
    }
}

【问题讨论】:

  • 添加vertical-align: top; 以列出项目&lt;li&gt;s。
  • 由于它们(.example li) 是显示inline-block,因此您需要在它们上设置垂直对齐,因此将其设置为顶部,您在其他项目上看到的额外间距是表单中的填充元素
  • 就是这样!我知道这是一件小而愚蠢的事情。仅将垂直填充声明移至 ul 并添加了 li { vertical-align: top; }。奇迹般有效。谢谢 :) 将在几分钟内接受。

标签: html css vertical-alignment


【解决方案1】:

如果您设置这些,那么它们将对齐。您的列表项是内联块项,因此您需要设置垂直对齐顶部,否则它们将与基线对齐。我将表单设置为没有填充,以向您显示完美对齐的框

.example {
    form {
        padding: 0; /* you original also have padding top and bottom on form */
    }
    li {
        display: inline-block; /* this you already have */
        vertical-align: top;
    }
}

【讨论】:

    【解决方案2】:

    在设置垂直对齐时,我总是觉得使用 display: table 很有用:

    http://codepen.io/anon/pen/ZGEJKm

    ul {
        list-style-type: none;
        display: table;
    }
    li {
        display: table-cell;
        vertical-align: middle;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2018-10-30
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      相关资源
      最近更新 更多