/* Generic Styles */
body {
background: #ffffff;
color: #222222;
font: 1em;
}
.container {
\t margin: auto;
\t width: 90%;
}
img {
\t max-width: 100%;
}
/* Layout Styles */
header {
\t background-image: url(../images/rocket.png);
\t background-repeat: no-repeat;
\t background-size: contain;
\t background-position: left;
\t background-color: #003151;
}
header img {
\t float: left;
\t margin-top: 30px;
}
header h3 {
\t clear: both;
}
nav {
\t float: right;
\t width: 100%;
\t font-size: 1.250em;
\t font-weight: bold;
\t text-transform: uppercase;
\t margin-top: 62px;
\t }
nav ul {
\t list-style: none;
}
nav ul li {
\t float: left;
\t display: block;
\t margin-right: 04%;
}
nav ul li:last-child {
\t margin-right: 0;
}
nav ul li a {
\t color: #ffffff;
\t text-decoration: none;
}
<header>
<div class="container">
<a href="#" title="Home page"><img src="images/logo.png" alt="Logo" /></a>
<nav>
<ul>
<li><a href="#" title="Home page" class="current">Home</a></li>
<li><a href="portfolio" title="See some of my featured work">Portfolio</a></li>
<li><a href="services" title="Learn more about my services">Services</a></li>
<li><a href="about" title="Learn more about me">About</a></li>
<li><a href="blog" title="View latest posts">Blog</a></li>
<li><a href="faqs" title="View latest FAQS">FAQS</a></li>
<li><a href="contact" title="Get in touch">Contact</a></li>
</ul>
</nav>
<h3>BlahBlahBlahH3</h3>
<p>BlahBlahBlah</p>
<a href="#" class="btn" title="Get in touch">Get in touch</a>
</div>
<!-- End Container -->
</header>
<!-- End Header -->
儘管我盡了最大努力就好像在nav
元素具有某種填充或保證金的(它沒有)。如果將寬度設置爲100%以阻止nav
溢出到兩條線上,它會跳到徽標下面,而不是在徽標的同一行上浮動到右邊。現在從大量的試驗和錯誤的代碼可能有點雜亂,但任何人都可以解釋爲什麼如果我沒有設置nav
元素的寬度100%,nav
溢出到兩條線或爲什麼如果它被設置爲100%不會一直飄到右邊?
奇怪的是,這似乎是對的 - 我可以使用PX超過%沒有它打破成兩行一個大得多的利潤率。你知道這是爲什麼嗎? – user4349555
當您未定義寬度並且一直都有浮動元素時,邊際百分比可能會很奇怪。這些元素的寬度是動態計算的,並且不考慮%-margin(因爲在它可以是數字之前它需要知道寬度)。這使得不可預知的佈局。嘗試給你的標誌10%,導航90%的寬度,然後你的UL寬度爲100%。這樣可以在不依賴浮點元素寬度計算的情況下定義容器。它將避免不規則的包裝。 –
感謝您的解釋 – user4349555