2016-11-13 142 views
6

中心這是我的html:對齊元素到另一個元素

.HCSI { 
 
    background-color: #fff; 
 
    height: auto; 
 
    width: 100%; 
 
    text-align: ; 
 
    position: fixed; 
 
    left: 0; 
 
    top: 0; 
 
    z-index: 0; 
 
    border: 2px solid #000; 
 
    border-radius: 25px; 
 
} 
 
.home, 
 
.csgo, 
 
.steam, 
 
.info { 
 
    z-index: 1; 
 
    background-color: rgba(50, 150, 250, 0.5); 
 
    border: 2px solid #000; 
 
    padding: 10px 15px; 
 
    border-radius: 20px; 
 
    float: center; 
 
} 
 
.home:hover { 
 
    background-color: rgba(50, 150, 250, 1); 
 
} 
 
.HCSI, 
 
li { 
 
    color: #000; 
 
    padding: 0px; 
 
    display: inline-block; 
 
    font-size: 21px; 
 
    font-weight: 100; 
 
    letter-spacing: 2.5px; 
 
    word-spacing: 90px; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>VusicVoid</title> 
 
    <link href="https://fonts.google.com/specimen/Shrikhand" rel="stylesheet"> 
 
</head> 
 

 
<body> 
 
    <div class="HCSI"> 
 
    <ul> 
 
     <a href=""> 
 
     <li class="home">Home</li> 
 
     </a> 
 
     <a href="http://store.steampowered.com/app/730/"> 
 
     <li class="csgo">Csgo</li> 
 
     </a> 
 
     <a href=""> 
 
     <li class="steam">Steam</li> 
 
     </a> 
 
     <a href=""> 
 
     <li class="info">Info</li> 
 
     </a> 
 
    </ul> 
 
    </div> 
 
</body> 
 

 
</html>

如果你在一個代碼測試儀把這些,應該有一個白色的內4個藍色箱子,但我的問題是我不能讓所有4個盒子與白色中心對齊。我試圖讓藍色框的所有邊上的填充是相同的。

+0

? – Gacci

+0

親愛的@Vusic Void請不要忘記接受答案,如果任何人給出的答案是正確的,這可能是對他人的啓發 –

回答

2

剛剛修復text-align 使其text-align:center;

.HCSI { 
    background-color: #fff; 
    height: auto; 
    width: 100%; 
    text-align:center ; 
    position: fixed; 
    left: 0; 
    top: 0; 
    z-index: 0; 
    border: 2px solid #000; 
    border-radius: 25px; 
} 
2

嘗試用這樣的回答給予UL 文本對齊:中心;它會使錨點居中並添加文字修飾:直通;標籤,使得垂直居中行

/* styleSheet */ 
 

 
.HCSI { 
 
\t background-color: #fff; 
 
\t height: auto; 
 
\t width: 100%; 
 
text-align:; 
 
\t position: fixed; 
 
\t left: 0; 
 
\t top: 0; 
 
\t z-index: 0; 
 
\t border: 2px solid #000; 
 
\t border-radius: 25px; 
 
} 
 
.HCSI ul { 
 
\t padding-left: 0; 
 
\t text-align: center; 
 
} 
 
.HCSI ul a { 
 
\t text-decoration:line-through; 
 
} 
 
.home, .csgo, .steam, .info { 
 
\t z-index: 1; 
 
\t background-color: rgba(50, 150, 250, 0.5); 
 
\t border: 2px solid #000; 
 
\t padding: 10px 15px; 
 
\t border-radius: 20px; 
 
\t float: center; 
 
} 
 
.home:hover { 
 
\t background-color: rgba(50, 150, 250, 1); 
 
} 
 
.HCSI, li { 
 
\t color: #000; 
 
\t padding: 0px; 
 
\t display: inline-block; 
 
\t font-size: 21px; 
 
\t font-weight: 100; 
 
\t letter-spacing: 2.5px; 
 
\t word-spacing: 90px; 
 
}
<!-- HTML --> 
 

 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<title>VusicVoid</title> 
 
<link href="https://fonts.google.com/specimen/Shrikhand" rel="stylesheet"> 
 
</head> 
 
<body> 
 
<div class="HCSI"> 
 
    <ul> 
 
    <a href=""> 
 
    <li class="home">Home</li> 
 
    </a> <a href="http://store.steampowered.com/app/730/"> 
 
    <li class="csgo">Csgo</li> 
 
    </a> <a href=""> 
 
    <li class="steam">Steam</li> 
 
    </a> <a href=""> 
 
    <li class="info">Info</li> 
 
    </a> 
 
    </ul> 
 
</div> 
 
</body> 
 
</html>

+0

這是否解決了您的問題 –

1

如果你想在中心聚集了限定的間隙箱子,這將這樣的伎倆

.HCSI ul { 
    display: table; 
    margin: 0 auto; 
    padding-left: 0; 
} 

.HCSI ul a { 
    /* Adjust this value to adjust the spacing around the buttons */ 
    padding: 20px; 
    display: table-cell; 
} 

如果您希望箱子均勻分佈在整個酒吧,然後你可以添加這個代替

.HCSI ul { 
    display: table; 
    margin: 0 auto; 
    padding-left: 0; 
    width: 100%; 
} 

.HCSI ul a { 
    padding: 20px; 
    display: table-cell; 
    text-align: center; 
} 
2

您可以使用CSS Flexbox

只需將Flex容器(display: flex)屬性與Flex Justify(justify-content: center)和Flex對齊(align-items: center)屬性一起應用即可。

對我是如何用他們在下面的代碼看看:

/* Parent Element (Flex Container) */ 
 
.HCSI { 
 
    display: flex; 
 
    justify-content: center; /* Center the content horizontaly */ 
 
    padding: 20px; 
 
    border: 2px solid #000; 
 
    border-radius: 25px; 
 
} 
 

 
/* Resetting <ul> (Flex Container) */ 
 
ul { 
 
    list-style: none; 
 
    display: flex; 
 
    justify-content: center; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
/* <li> Styles */ 
 
ul li { 
 
    color: #000; 
 
    margin: 0 10px; 
 
    font-size: 21px; 
 
    font-weight: 100; 
 
    letter-spacing: 2.5px; 
 
    background-color: rgba(50, 150, 250, 0.5); 
 
    border: 2px solid #000; 
 
    border-radius: 20px; 
 
} 
 

 
/* <a> Styles */ 
 
ul li a { 
 
    display: block; 
 
    color: #000; 
 
    padding: 10px 15px; 
 
    border-radius: 18px; 
 
    text-decoration: none; 
 
} 
 

 
/* <a> Hover Styles */ 
 
ul li a:hover { 
 
    text-decoration: none; 
 
    background-color: rgba(50, 150, 250, 1); 
 
}
<div class="HCSI"> 
 
    <ul> 
 
    <li class="home"><a href="">Home</a></li> 
 
    <li class="csgo"><a href="http://store.steampowered.com/app/730/">Csgo</a></li> 
 
    <li class="steam"><a href="">Steam</a></li> 
 
    <li class="info"><a href="">Info</a></li> 
 
    </ul> 
 
</div>

瞭解更多關於CSS Flexbox

希望這有助於!垂直居中或水平居中?