2016-11-17 79 views
3

我正在使用The Odin Project學習CSS,並製作了Google.com主頁的樣機。一切看起來都很完美,直到我調整了瀏覽器窗口的大小和元素移動的位置。如何在調整網頁大小時保持div居中?

我一直在嘗試很多不同的方法,如何在頁面調整大小時保持div(Google徽標,搜索欄和按鈕)居中。

#header { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    margin-left: -150px; 
 
    margin-top: -125px; 
 
    width: 300px; 
 
    padding: 10px; 
 
} 
 
#topbar { 
 
    font-family: arial, sans-serif; 
 
} 
 
.centered { 
 
    width: 960px 
 
} 
 
.footer { 
 
    position: absolute; 
 
    right: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    padding: 0.15rem; 
 
    background-color: #f2f2f2; 
 
    font-family: arial, sans-serif; 
 
    font-size: 15px; 
 
    color: #666; 
 
} 
 
.alignleft { 
 
    float: left; 
 
} 
 
.alignright { 
 
    float: right; 
 
} 
 
p { 
 
    font-family: Arial, Helvetica, sans-serif; 
 
    font-size: 14px; 
 
    color: #7D7676; 
 
}
<title>Google</title> 
 
<link rel="stylesheet" type="text/css" href="google-homepage-css.css"> 
 
<!-- Top bar --> 
 

 
<body> 
 
    <div id="topbar"> 
 
    <p align="right"><a href="https://mail.google.com" style="color:#7D7676;text-decoration: none">Gmail</a>&nbsp;&nbsp;<a href="https://images.google.com" style="color:#7D7676;text-decoration: none">Images</a>&nbsp;&nbsp; 
 
     <img style="vertical-align:middle" src="http://i.imgur.com/xmMdSIp.png">&nbsp;</p> 
 
    </div> 
 
    <br> 
 
    <!-- Google Center Image --> 
 
    <div id="centered"> 
 
    <div id="logo"> 
 
     <img id="header" src="http://i.imgur.com/hs6n5hR.png" width="400"> 
 
    </div> 
 
    </div> 
 

 
    <!-- Search Bar and Buttons --> 
 
    <div id="search"> 
 
    <form id="searchbar" width="500"> 
 
     <br> 
 
     <input type="text"> 
 
     <br> 
 
    </form> 
 
    <br> 
 
    <button type="button" id="firstbutton">Google Search</button> 
 
    <button type="button" id="secondbutton">I'm Feeling Lucky</button> 
 
    </div> 
 

 
    <!-- Footer --> 
 
    <div class="footer"> 
 
    <p class="alignleft"><a href="https://www.google.com/intl/en/ads/" style="color:#666;text-decoration: none">&nbsp;&nbsp;Advertising</a> 
 
     &nbsp;&nbsp;&nbsp; 
 
     <p class="alignleft"><a href="https://www.google.com/services/" style="color:#666;text-decoration: none">Business</a> 
 
     &nbsp;&nbsp;&nbsp; 
 
     <p class="alignleft"><a href="https://www.google.com/intl/en/about/" style="color:#666;text-decoration: none">About</a> 
 
     </p> 
 
     <p class="alignright"><a href="https://www.google.com/intl/en/policies/privacy/" style="color:#666;text-decoration: none">Privacy</a> 
 
      &nbsp;&nbsp; 
 
      <a href="https://www.google.com/intl/en/policies/terms/" style="color:#666;text-decoration: none">Terms</a> 
 
      &nbsp;&nbsp; 
 
      <a href="https://www.google.com/preferences" style="color:#666;text-decoration: none">Settings&nbsp;&nbsp;</a> 
 
     </p> 
 
    </div>


jsFiddle


我覺得應該是這樣的,但我不知道:

div { 
    position: relative 
    margin-left:auto; 
    margin-right:auto; 
} 

注意:我將在搜索欄和按鈕之後調整大小和添加顏色,這是我在瀏覽器頁面重新調整大小時,如何將搜索欄和按鈕保持在Google徽標下居中的最大擔憂。

+0

你試過嗎? ''''' –

+0

無論屏幕大小如何,google徽標都居中。複製你爲此做的任何事情? – imjared

+1

您可以將margin-left/margin-right設置爲「auto」,但您需要指定div的寬度以允許broawser計算邊距。 – Dexter0015

回答

1

您可以將房產text-align: center添加到您的<div>祖先,並且其ID爲#search

#search{ text-align: center; } 

,因爲他們保留自己的特色inline這將居中<input><button>元素。

#header { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    margin-left: -150px; 
 
    margin-top: -125px; 
 
    width: 300px; 
 
    padding: 10px; 
 
} 
 
#topbar { 
 
    font-family: arial, sans-serif; 
 
} 
 
#search { 
 
    text-align: center; 
 
} 
 
.centered { 
 
    width: 960px 
 
} 
 
.footer { 
 
    position: absolute; 
 
    right: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    padding: 0.15rem; 
 
    background-color: #f2f2f2; 
 
    font-family: arial, sans-serif; 
 
    font-size: 15px; 
 
    color: #666; 
 
} 
 
.alignleft { 
 
    float: left; 
 
} 
 
.alignright { 
 
    float: right; 
 
} 
 
p { 
 
    font-family: Arial, Helvetica, sans-serif; 
 
    font-size: 14px; 
 
    color: #7D7676; 
 
}
<title>Google</title> 
 
<link rel="stylesheet" type="text/css" href="google-homepage-css.css"> 
 
<!-- Top bar --> 
 

 
<body> 
 
    <div id="topbar"> 
 
    <p align="right"><a href="https://mail.google.com" style="color:#7D7676;text-decoration: none">Gmail</a>&nbsp;&nbsp;<a href="https://images.google.com" style="color:#7D7676;text-decoration: none">Images</a>&nbsp;&nbsp; 
 
     <img style="vertical-align:middle" src="http://i.imgur.com/xmMdSIp.png">&nbsp;</p> 
 
    </div> 
 
    <br> 
 
    <!-- Google Center Image --> 
 
    <div id="centered"> 
 
    <div id="logo"> 
 
     <img id="header" src="http://i.imgur.com/hs6n5hR.png" width="400"> 
 
    </div> 
 
    </div> 
 

 
    <!-- Search Bar and Buttons --> 
 
    <div id="search"> 
 
    <form id="searchbar" width="500"> 
 
     <br> 
 
     <input type="text"> 
 
     <br> 
 
    </form> 
 
    <br> 
 
    <button type="button" id="firstbutton">Google Search</button> 
 
    <button type="button" id="secondbutton">I'm Feeling Lucky</button> 
 
    </div> 
 

 
    <!-- Footer --> 
 
    <div class="footer"> 
 
    <p class="alignleft"><a href="https://www.google.com/intl/en/ads/" style="color:#666;text-decoration: none">&nbsp;&nbsp;Advertising</a> 
 
     &nbsp;&nbsp;&nbsp; 
 
     <p class="alignleft"><a href="https://www.google.com/services/" style="color:#666;text-decoration: none">Business</a> 
 
     &nbsp;&nbsp;&nbsp; 
 
     <p class="alignleft"><a href="https://www.google.com/intl/en/about/" style="color:#666;text-decoration: none">About</a> 
 
     </p> 
 
     <p class="alignright"><a href="https://www.google.com/intl/en/policies/privacy/" style="color:#666;text-decoration: none">Privacy</a> 
 
      &nbsp;&nbsp; 
 
      <a href="https://www.google.com/intl/en/policies/terms/" style="color:#666;text-decoration: none">Terms</a> 
 
      &nbsp;&nbsp; 
 
      <a href="https://www.google.com/preferences" style="color:#666;text-decoration: none">Settings&nbsp;&nbsp;</a> 
 
     </p> 
 
    </div>


Revised jsFiddle

1

<div>是塊元素並默認它需要它的父的100%的寬度。

如果您嘗試使用margin: 0 auto居中,那麼您將無法獲得預期結果。您必須設置寬度才能應用邊距。

請參見下面的例子:

.parent{ 
 
    width: 400px; 
 
    height: 100px; 
 
    border: 1px solid blue; 
 
} 
 

 
.item{ 
 
    width: 100px; 
 
    margin: 0 auto; 
 
    border: 1px solid green; 
 
}
<div class="parent"> 
 
    <div class="item"> 
 
    <span>some text</span> 
 
    </div> 
 
</div>

相關問題