2016-10-04 79 views
1

我想把我的按鈕放在中間。我嘗試使用如何將我的鏈接(按鈕)放在頁面中間?

margin: 0 auto; 

但沒有任何反應。代碼如下:

Why does margin: 0 auto; doesnt work?

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<style> 
 
div { 
 
    margin: 0 auto; <!--Why margin: 0 auto; doesnt work? --> 
 
} 
 
a:link, a:visited { 
 
    background-color: #f44336; 
 
    color: white; 
 
    padding: 14px 25px; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    display: inline-block; 
 
} 
 

 

 
a:hover, a:active { 
 
    background-color: red; 
 
} 
 
</style> 
 
</head> 
 
<body> 
 

 
<div><a href="https://www.google.com" target="_blank">This is a link</a></div> 
 

 
</body> 
 
</html>

回答

0

下面的代碼。保證金0 auto不工作,因爲你的div使用身體寬度的100%。一旦您將其設置爲小於100%,就會自動進行調整。

body, html{ 
 
    width: 99%; 
 
} 
 

 
div { 
 
    margin: 0 auto; 
 
    width: 134px; 
 
} 
 
a:link, a:visited { 
 
    background-color: #f44336; 
 
    color: white; 
 
    padding: 14px 25px; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    display: inline-block; 
 
} 
 

 

 
a:hover, a:active { 
 
    background-color: red; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 

 
</head> 
 
<body> 
 

 
<div><a href="https://www.google.com" target="_blank">This is a link</a></div> 
 

 
</body> 
 
</html>

編輯的片段

+0

請問你的解決方案中心的按鈕?現在你可以向側面滾動,剩下更多的空白。 – stoicho184

+0

編輯剪下。上面已經解釋了它的中心。 – Stormhashe

相關問題