【问题标题】:How to align SPAN inside a DIV vertically and horizontally [duplicate]如何在 DIV 内垂直和水平对齐 SPAN [重复]
【发布时间】:2014-10-15 03:11:55
【问题描述】:

我有以下 HTML 代码:

<div id="slideClick">
    <div style="padding: 0; margin: 0 auto; width: 100%; height: 100%; text-align: center; border: 1px solid blue;">
        <span class="sideMsg">MESSAGES</span>
    </div>
</div>

CSS:

.sideMsg
{
        color:#333;
    -webkit-transform:rotate(90deg);
    -moz-transform:rotate(90deg);
    -o-transform: rotate(90deg);
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
    white-space:nowrap;
    display:inline-block;
    bottom:0;
    font-family: ‘Trebuchet MS’, Helvetica, sans-serif;
    font-size:14px;
    font-weight:normal;
    text-shadow: 0px 0px 1px #333;
    border: 1px solid red;
}
#slideClick {
    float:left;
    height:90px;
    width:40px;
    background:#EE8D40;
    -webkit-border-top-left-radius: 10px;
    -webkit-border-bottom-left-radius: 10px;
    -moz-border-radius-topleft: 10px;
    -moz-border-radius-bottomleft: 10px;
    border-top-left-radius: 10px;
    border-bottom-left-radius: 10px;
    cursor: pointer;
    text-align: center;
}

显示这个:

如何将SPAN 在蓝色DIV 内水平和垂直居中?

【问题讨论】:

标签: html css


【解决方案1】:

如上所述,这是 CSS 中的一个常见问题,但是当您仅旋转文本跨度时,我看到了棘手的部分。旋转包装器元素,然后从那里居中应该是饼图。

#slideClick{
  -webkit-transform: rotate(90deg);
}

例如,由于设置了高度,因此使用 line-height。

jsfiddle:http://jsfiddle.net/1vnbyL1t/3/

【讨论】:

    【解决方案2】:

    由于您的 div 具有固定大小,因此您可以使用内部跨度的相对位置:

    position:relative;
    top:35px;
    right:17px;
    

    并相应地调整它:http://jsfiddle.net/38dgooxj/2/

    【讨论】:

    • 我调整了我的代码以匹配你的,但由于某种原因,文本在 IE8 中的橙色框之外:/
    • 尝试left:10px而不是正确的,如果它不起作用,继续进行更改直到它起作用:)
    • 谢谢。我现在投了赞成票:)
    【解决方案3】:

    我创建了一个JSFiddle,这可能是你想要的

    <div id="slideClick">
        <div>
            <span class="sideMsg">MESSAGES</span>
        </div>
    </div>
    

    我创建了一个JSFiddle,这可能是你想要的

    <div id="slideClick">
        <div>
            <span class="sideMsg">MESSAGES</span>
        </div>
    </div>
    
    
    .sideMsg
    {
            color:#333;
        -webkit-transform:rotate(90deg);
        -moz-transform:rotate(90deg);
        -o-transform: rotate(90deg);
        filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
        white-space:nowrap;
        display:inline-block;
        bottom:0;
        font-family: ‘Trebuchet MS’, Helvetica, sans-serif;
        font-size:14px;
        font-weight:normal;
        text-shadow: 0px 0px 1px #333;
        border: 1px solid red;
    }
    
    #slideClick {
        float:left;
        height:190px;
        width:200px;
        background:#EE8D40;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        -webkit-box-direction: normal;
        -moz-box-direction: normal;
        -webkit-box-orient: horizontal;
        -moz-box-orient: horizontal;
        -webkit-flex-direction: row;
        -ms-flex-direction: row;
        flex-direction: row;
        -webkit-flex-wrap: nowrap;
        -ms-flex-wrap: nowrap;
        flex-wrap: nowrap;
        -webkit-box-pack: center;
        -moz-box-pack: center;
        -webkit-justify-content: center;
        -ms-flex-pack: center;
        justify-content: center;
        -webkit-align-content: stretch;
        -ms-flex-line-pack: stretch;
        align-content: stretch;
        -webkit-box-align: center;
        -moz-box-align: center;
        -webkit-align-items: center;
        -ms-flex-align: center;
        align-items: center;
    }
    

    【讨论】:

    • 谢谢。是否也兼容IE8和IE9?
    • 是的,应该是这样,但你需要交叉检查,我没有 Windows pc 来测试它,但我已经包含 -ms-..... 来处理 IE
    • 谢谢。我会测试一下
    • 好的,我想知道你的测试结果
    • 我暂时投了赞成票 :)
    猜你喜欢
    • 2020-10-21
    • 2017-09-28
    • 2013-11-25
    • 1970-01-01
    • 2017-07-01
    • 2011-04-26
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    相关资源
    最近更新 更多