9

それで...これは私を悩ませ始めています...そして私はここに来て助けを求めようと思いました..

ボックスの周りのメインの div には幅があります...次にテキストがあります...そして div の中央に必要なボタンがあります..

それは<a href>ボタンです..しかし、それは中央にありません。

外側のdivには幅があるので、幅を指定する必要があるとは思いませんでした..margin:0 auto; text-align:centerなどを試してみましたが、何も機能しません

<h2 class="service-style color_service">Annoyed</h2>
<div class="service-text text1">
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Lorem ipsum dolor sit amet…
    </p>

    <a href="" class="button button-large"><i class="fa fa-credit-card" style="padding-right:10px;"></i> Learn More</a>
</div>

ここにフィドルリンクがあります

http://jsfiddle.net/2gMQ9/

4

9 に答える 9

12

問題があるのはdisplay:inline-block...ここを参照

cssのこのセクションで、次を追加します。

marginクラスで設定されているすべての を削除し、次のbuttonように修正します。

/*  --------------------------------------------------
    :: Button Configuration
    -------------------------------------------------- */
 .button {
    display:block; /* change this from inline-block */
    width:20%; /* setting the width */
    margin:0 auto; /* this will center  it */
    position:relative;
    font-style:normal;
    font-weight:normal;
    font-family:"Open Sans";
    font-size:14px;
    outline:none;
    color:#fff;
    border:none;
    text-decoration:none;
    text-align:center;
    cursor:pointer;
    -webkit-border-radius:2px;
    -moz-border-radius:2px;
    border-radius:2px;
    background-color:#96aa39;
    border-bottom:1px solid #7b8b2f;
}

EDIT : 幅を設定せずにインラインブロックを使用するには

インラインブロックのデモ

インラインブロック要素を中央に配置する唯一の解決策は、使用することですtext-align:center

あなたのCSSクラスで:

.service-text {
text-align: center; /* add this to center the button */
}

次に追加します。

.service-text p{
 text-align:left /* and then add this to left align the text inside the para*/
}

この方法では、与える必要はなく、問題を解決するwidthだけです!padding

于 2013-12-24T07:32:52.240 に答える
5

ボタンはブロック レベルの要素である必要があります。最も簡単な修正:

a.button-large{  
    padding : 15px 30px 14px 30px;
    margin  : 5px auto;
    display : block;
    width   : 100px;
}

http://jsfiddle.net/jqvbM/

a.button-large iまた、「詳細」テキストを中央に配置するために、10 ピクセルの右マージンを削除します。

于 2013-12-24T07:34:41.070 に答える
1

ここでフィドルをチェックしてください

以下のように CSS を変更する必要があります: -クラスmarginから削除 -クラスでa.button-large変更display:table.button

a.button-large{  
    padding:15px 30px 14px 30px;    
}


.button{
display:table;
position:relative;
font-style:normal; 
font-weight:normal; 
font-family:"Open Sans"; 
font-size:14px;
margin:0 auto;
outline:none;
color:#fff;
border:none;
text-decoration:none;
text-align:center;
cursor:pointer;
-webkit-border-radius:2px;
-moz-border-radius:2px;
border-radius:2px;
}
于 2013-12-24T07:31:35.033 に答える
1

少しダイヤルを戻して、フレームワークや使用しているものから離れることをお勧めします。物事を水平方向に中央揃えするには、2 つの方法があります。

ボタンを配置するかどうかに基づいて決定する必要があります:インライン、インラインブロック、またはブロック。モバイルと指とそのすべて(2014)については、インラインブロックまたはブロックをお勧めします。そう -

インライン ブロックの場合は、テキストのように扱うことができます。親をに設定しますtext-align center;

ブロックの場合は使用できますmargin-right: auto; margin-left: auto;が、それには他の問題があります。他のすべての人が考えているように、それらが互いに浮かんですべてを台無しにしないように、適切に整理する必要があります。次のようにすることをお勧めします:jsfiddle

PS - フィドルにそれほど多くのコードを表示する必要はありません。必要最低限​​のものだけで問題を解決する方が簡単です。幸運を。

HTML

<aside class="service">

    <header>
        <h2 class="">Title of module thing</h2>
    </header>

    <div class="text-wrapper">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Lorem ipsum dolor sit amet…&lt;/p>
        <a href="#" class="button button-large"><i class="fa fa-credit-card" style="padding-right:10px;"></i> Learn More</a>
    </div> <!-- .text-wrapper -->

</aside> <!-- .service -->

CSS

*, *:before, *:after {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
} /* start your projext off right */


.service {
    border: 1px solid #2ecc71;
    font-family: helvetica;
    text-align: center;
}

.service header {
    background-color: #2ecc71;  
}

.service header h2 {
    font-size: 1.3em;
    font-weight: 400;
    color: white;
    /* text-align: center; */ /* the parent should have this - not the element */
    /* width: 100%; */ /* this is already default because it is a block element */
    margin: 0;
    padding: .5em;
}

.service .text-wrapper {
    padding: 1em;
}

.service p {
    text-align: left;
    font-size: .9em;
}

.button {
    display: inline-block; /*so it's like... "inline" - and can be centered */
    background-color: #2ecc71;
    text-decoration: none;
    color: white;
    text-transform: uppercase;
    padding: 1em 2em;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
于 2013-12-24T08:10:24.390 に答える
1

text-align:center; を配置する必要があります。あなたの場合のボタンの親要素はdiv.service-textです。

上の P を text-align:right; にしたい場合。ボタンに別のPを使用してください

覚えて。あなたの要素がdisplay:block;の場合 したがって、margin: 0 auto; を指定する必要があります。あなたの要素に。要素が display:inline-block; の場合。text-align:center; を指定する必要があります。親要素に。

于 2013-12-24T07:31:12.997 に答える
1

手っ取り早い方法は、 に追加する"text-align: center;"こと.service-textです。

于 2013-12-24T07:34:41.490 に答える
0

ボタンを div に含めて、以下のように更新してみてCSSください

<div class="check">
     <a href="" class="button button-large"><i class="fa fa-credit-card" style="padding-right:10px;"></i> Learn More</a>
 </div>

`CSS`
.check
{
    width: 150px;
    margin:auto;

}
于 2013-12-24T07:29:12.340 に答える
0

ボタンを要素でラップし、それを与えますtext-align:center

書く:

<div class="center">
        <a href="" class="button button-large"><i class="fa fa-credit-card" style="padding-right:10px;"></i> Learn More</a>
</div>

.center {
    text-align:center;
}

ここでフィドルを更新しました。

于 2013-12-24T07:29:24.750 に答える
0

これを試して

<div style="text-align:center;">
    <a href="" class="button button-large"><i class="fa fa-credit-card" style="padding-right:10px;"></i> Learn More</a>
</div>

デモ

于 2013-12-24T07:30:03.827 に答える