2017-09-16 62 views
0

因此,我正在製作不同的部分,以便找到一個小型的「關於我」頁面。在所有divs中(這是最相關的),我有寬度和高度。它不會讓我做保證金:0汽車;。 margin:0 auto;當有寬度時不居中

body { 
 
background: #aaa; 
 
} 
 

 
#about { 
 
font-family: arial; 
 
width: 300px; 
 
height: 20px; 
 
text-align: center; 
 
padding-left: 10px; 
 
opacity: 0.5; 
 
padding-top: 4px; 
 
border-radius: 10px; 
 
background-color: #000; 
 
position: fixed; 
 
margin: 0 auto; 
 
overflow: hidden; 
 
font-size: 11px; 
 
-webkit-transition: all 0.8s ease-out; 
 
-moz-transition: all 0.8s ease-out; 
 
-o-transition: all 0.8s ease-out; 
 
transition: all 0.8s ease-out; 
 
} 
 

 
#about:hover { 
 
height: 250px; 
 
} 
 

 
.about-text { 
 
width: 300px; 
 
height: 220px; 
 
overflow-y: auto; 
 
margin-top: 5px; 
 
}
<div id="about"> 
 
<font face="arial" color="#fff">&#120016;&#120043;&#120056;&#120062;&#120061; &#120028;&#119942;</font> 
 

 
<div class="about-text"> 
 
<font face="arial" color="#fff"><br> 
 
Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text.</font> 
 
</div> 
 
</div>

+0

那是因爲你有'位置:fixed'。添加如下內容:'left:calc(50% - 150px);' – sideroxylon

+0

我也試過使用position:absolute並且沒有做任何改變。但是你的建議確實奏效。 – Jona

回答

0

股利是position:fixed。這需要它out of the flow,所以你需要給它一個left(或right) - 是這樣的:

body { 
 
background: #aaa; 
 
} 
 

 
#about { 
 
font-family: arial; 
 
width: 300px; 
 
height: 20px; 
 
text-align: center; 
 
padding-left: 10px; 
 
opacity: 0.5; 
 
padding-top: 4px; 
 
border-radius: 10px; 
 
background-color: #000; 
 
position: fixed; 
 
left: 50%; 
 
transform: translateX(-50%); 
 
-webkit-transform: translateX(-50%); 
 
overflow: hidden; 
 
font-size: 11px; 
 
-webkit-transition: all 0.8s ease-out; 
 
-moz-transition: all 0.8s ease-out; 
 
-o-transition: all 0.8s ease-out; 
 
transition: all 0.8s ease-out; 
 
} 
 

 
#about:hover { 
 
height: 250px; 
 
} 
 

 
.about-text { 
 
width: 300px; 
 
height: 220px; 
 
overflow-y: auto; 
 
margin-top: 5px; 
 
}
<div id="about"> 
 
<font face="arial" color="#fff">&#120016;&#120043;&#120056;&#120062;&#120061; &#120028;&#119942;</font> 
 

 
<div class="about-text"> 
 
<font face="arial" color="#fff"><br> 
 
Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text.</font> 
 
</div> 
 
</div>

0

你必須在一個div容器封裝,我已經修改了此。 :)

body { 
 
background: #aaa; 
 
} 
 
.container{ 
 
width: 100%; 
 
margin: 0 auto; 
 
display: block; 
 
} 
 
#about { 
 
font-family: arial; 
 
width: 300px; 
 
height: 20px; 
 
text-align: center; 
 
padding-left: 10px; 
 
opacity: 0.5; 
 
padding-top: 4px; 
 
border-radius: 10px; 
 
background-color: #000; 
 
position: relative; 
 
margin: 0 auto; 
 
overflow: hidden; 
 
font-size: 11px; 
 
-webkit-transition: all 0.8s ease-out; 
 
-moz-transition: all 0.8s ease-out; 
 
-o-transition: all 0.8s ease-out; 
 
transition: all 0.8s ease-out; 
 
} 
 

 
#about:hover { 
 
height: 250px; 
 
} 
 

 
.about-text { 
 
width: 300px; 
 
height: 220px; 
 
overflow-y: auto; 
 
margin-top: 5px; 
 
}
<div class="container"> 
 
    <div id="about"> 
 
    <font face="arial" color="#fff">&#120016;&#120043;&#120056;&#120062;&#120061; &#120028;&#119942;</font> 
 

 
    <div class="about-text"> 
 
    <font face="arial" color="#fff"><br> 
 
    Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text. Text.</font> 
 
    </div> 
 
    </div> 
 
    </div>

相關問題