【问题标题】:Aligning text to bottom left corner inside a styled-element将文本与样式元素内的左下角对齐
【发布时间】:2014-03-07 10:26:37
【问题描述】:

如何使此样式元素内部的文本与左下角对齐? 我只希望框内的文本位于框的左下角。文本的其余部分应该不受影响。

我是CSS 的新手,无法弄清楚。

HTML

<body>
    <h1>example text</h1>
    <article class="box" style="background-color: #2672EC">foo bar</article>
    <h1>example text</h1>
</body>

CSS

body {
    font-family: Verdana;
    font-size: 16px;
    color: black;
}

.box {
    height: 187px;
    width: 187px;
    margin-right: 5.5px;
    margin-left: 5.5px;
    margin-bottom: 5.5px;
    margin-top: 5.5px;
    color: white;

}

这里是 JSFiddle

http://jsfiddle.net/J9hT5/8/

【问题讨论】:

    标签: html css text-alignment


    【解决方案1】:

    您可以通过两种方式做到这一点,一种是使用 CSS 定位技术,您需要将父元素设置为position: relative;,将子元素设置为position: absolute;

    Demo (使用span 元素包装文本)

    .box > span {
        position: absolute;
        bottom: 0;
        left: 0;
    }
    

    或者通过使用display: table-cell; 和vertical-align: bottom;

    Demo (标记没有变化)

    .box {
        height: 187px;
        width: 187px;
        margin-right: 5.5px;
        margin-left: 5.5px;
        margin-bottom: 5.5px;
        margin-top: 5.5px;
        color: white;
        display: table-cell;
        vertical-align: bottom;
    }
    

    另外,想重构一下你的CSS,下面的sn-p

    margin-right: 5.5px;
    margin-left: 5.5px;
    margin-bottom: 5.5px;
    margin-top: 5.5px;
    

    可以写成margin: 5.5px 5.5px 5.5px 5.5px;

    【讨论】:

    • 感谢这两个解决方案!
    • 请注意,如果父母将显示设置为 flex,则第二个选项不起作用,不幸的是
    【解决方案2】:

    尝试将目标文本包含在 span 中:

    <article class="box" style="background-color: #2672EC"><span class="bottom">foo bar</span>
    

    那么你可以使用:

    .box {
        position: relative;
    }
    .bottom {
        position: absolute;
        bottom: 0;
        left: 0;
    }
    

    Updated Fiddle

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      相关资源
      最近更新 更多