簡體   English   中英

將div元素水平居中對齊

[英]Align span elements in div horizontally center

我的問題是,我無法在div或a元素內部水平對齊span元素的水平中心或中間位置。

我嘗試了什么,但沒有成功:

.fejlec_kosar{text-align:center}
.fejlec_kosar a{display:block;text-align:center}

我究竟做錯了什么? 我的代碼如下所示:

<div class="col-md-2 col-xs-4 fejlec_kosar">
                <a href="<?php echo $host; ?>/kosar" title="Kosár">
                    <span class="header_kosar_text"><i class="fa fa-shopping-cart fejlec_kosar_ikon" aria-hidden="true"></i> Kosár</span>
                    <span id="header_kosar_text"></span>
                    <div class="clearfix"></div>
                </a>
            </div>


    .fejlec_kosar {
    margin-top: 18px;
    -webkit-transition: .3s linear;
    -o-transition: .3s linear;
    transition: .3s linear;
    border: none;
    border-radius: 25px;
    background-color: #fbab35;
    color: #173f62;
    padding: 10px 0;
}
.fejlec_kosar_ikon {
    color: #173f62;
    margin-right: 10px;
    font-size: 20px;
}
.header_kosar_text {
    color: #173f62;
    font-size: 16px;
    font-weight: 500;
    float: left;
    line-height: 30px;
}
#header_kosar_text {
    border-radius: 50%;
    background-color: #d5902b;
    color: #fff;
    font-size: 12px;
    font-weight: bold;
    display: block;
    float: left;
    height: 30px;
    width: 30px;
    text-align: center;
    line-height: 30px;
    margin-left: 10px;
}

如您在評論中所讀,您可以使用flexbox實現此目的,您需要做的是

.fejlec_kosar {
    margin-top: 18px;
    -webkit-transition: .3s linear;
    -o-transition: .3s linear;
    transition: .3s linear;
    border: none;
    border-radius: 25px;
    background-color: #fbab35;
    color: #173f62;
    padding: 10px 0;
    display: flex; //ADDED
    justify-content: center; //ADDED
}

那應該行得通, 這里有一篇文章可以幫助您了解flexbox的工作方式。

您可以用其他方式創建相同的內容。 請嘗試以下代碼段。

 .try-btn{ width: 40%; margin-top: 18px; -webkit-transition: .3s linear; -o-transition: .3s linear; transition: .3s linear; border: none; border-radius: 25px !important; background-color: #fbab35; color: #173f62; padding: 10px 0; } 
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div class="col-md-2 col-xs-4"> <a href="#" class="btn btn-lg btn-default try-btn"><span class="fa fa-shopping-cart"></span> Kosár</a> </div> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM