2015-04-23 142 views
0

中心浮動元素

.key { 
 
    width: 40px; 
 
    height: 40px; 
 
    background: red; 
 
    float: left; 
 
    border-radius: 50%; 
 
    text-align: center; 
 
} 
 
.clearFloat { 
 
    clear: both; 
 
}
<div class="keyWrap"> 
 
    <div class="key"><span>1</span> 
 
    </div> 
 
    <div class="key">2</div> 
 
    <div class="key">3</div> 
 

 
    <div class="clearFloat"></div> 
 

 
    <div class="key">4</div> 
 
    <div class="key">5</div> 
 
    <div class="key">6</div> 
 
    <div class="clearFloat"></div> 
 

 
    <div class="key">0</div> 
 
</div>

我怎樣才能讓圓圈中的數字中心的中心?我不能做填充或保證金,因爲它會響應。

回答

3

您可以嘗試line-height

.key { 
 
    line-height:40px; 
 
    width: 40px; 
 
    height: 40px; 
 
    background: red; 
 
    float: left; 
 
    border-radius: 50%; 
 
    text-align: center; 
 
} 
 
.clearFloat { 
 
    clear: both; 
 
}
<div class="keyWrap"> 
 
    <div class="key"><span>1</span> 
 
    </div> 
 
    <div class="key">2</div> 
 
    <div class="key">3</div> 
 

 
    <div class="clearFloat"></div> 
 

 
    <div class="key">4</div> 
 
    <div class="key">5</div> 
 
    <div class="key">6</div> 
 
    <div class="clearFloat"></div> 
 

 
    <div class="key">0</div> 
 
</div>

0

你需要在頂部

.key { 
 
    width: 40px; 
 
padding-top:10px; 
 
    height: 30px; 
 
    background: red; 
 
    float: left; 
 
    border-radius: 50%; 
 
    text-align: center; 
 
} 
 
.clearFloat { 
 
    clear: both; 
 
}
<div class="keyWrap"> 
 
    <div class="key"><span>1</span> 
 
    </div> 
 
    <div class="key">2</div> 
 
    <div class="key">3</div> 
 

 
    <div class="clearFloat"></div> 
 

 
    <div class="key">4</div> 
 
    <div class="key">5</div> 
 
    <div class="key">6</div> 
 
    <div class="clearFloat"></div> 
 

 
    <div class="key">0</div> 
 
</div>

+0

它被specificaly提到,您不能使用填充,現在你在這裏使用它.. –

1

的給填充最好的解決方案是,您可以使用display:table-cellvertical-align:middle。而且它也是響應式的。

有一件事是,如果你改變身高,那麼它也會保持垂直中間。您也可以使用line-height,但在增加圈子高度時會發生變化。

使用它像以下:

.key { 
 
    width: 40px; 
 
    height: 40px; 
 
    background: red; 
 
    float: left; 
 
    border-radius: 50%; 
 
    text-align: center; 
 
    display:table; 
 
} 
 
.clearFloat { 
 
    clear: both; 
 
} 
 

 
.key > span{ 
 
    display:table-cell; 
 
    vertical-align:middle; 
 
    }
<div class="keyWrap"> 
 
    <div class="key"><span>1</span> 
 
    </div> 
 
    <div class="key"><span>2</span></div> 
 
    <div class="key"><span>3</span></div> 
 

 
    <div class="clearFloat"></div> 
 

 
    <div class="key"><span>4</span></div> 
 
    <div class="key"><span>5</span></div> 
 
    <div class="key"><span>6</span></div> 
 
    <div class="clearFloat"></div> 
 

 
    <div class="key"><span>0</span></div> 
 
</div>

+0

我怎麼能拉伸以適應父的寬度圈? –

+0

你想如何拉伸圓圈? – ketan

+0

我找到了一個方法,在這裏http://jsfiddle.net/5adjhd1x/但我怎麼能讓他們之間的空間?我其實試圖模仿iphone解鎖密碼屏幕。 –

0

解決方法1:line-height

.key { 
 
    width: 40px; 
 
    height: 40px; 
 
    background: red; 
 
    float: left; 
 
    border-radius: 50%; 
 
    text-align: center; 
 
    line-height: 40px; 
 
} 
 
.clearFloat { 
 
    clear: both; 
 
}
<div class="keyWrap"> 
 
    <div class="key"><span>1</span> 
 
    </div> 
 
    <div class="key">2</div> 
 
    <div class="key">3</div> 
 

 
    <div class="clearFloat"></div> 
 

 
    <div class="key">4</div> 
 
    <div class="key">5</div> 
 
    <div class="key">6</div> 
 
    <div class="clearFloat"></div> 
 

 
    <div class="key">0</div> 
 
</div>

溶液2:flex

.key { 
 
    width: 40px; 
 
    height: 40px; 
 
    background: red; 
 
    float: left; 
 
    border-radius: 50%; 
 
    display: inline-flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
} 
 
.clearFloat { 
 
    clear: both; 
 
}
<div class="keyWrap"> 
 
    <div class="key"><span>1</span> 
 
    </div> 
 
    <div class="key">2</div> 
 
    <div class="key">3</div> 
 

 
    <div class="clearFloat"></div> 
 

 
    <div class="key">4</div> 
 
    <div class="key">5</div> 
 
    <div class="key">6</div> 
 
    <div class="clearFloat"></div> 
 

 
    <div class="key">0</div> 
 
</div>