我意识到有很多关于如何做到这一点的问题,但他们所有的解决方案似乎都没有回应我目前的情况。我的网站顶部有一个横幅,里面有两个浮动的列。右栏包含导航菜单,我觉得这可能是罪魁祸首。
我已经设置了以下jfiddle:https://jsfiddle.net/Lhd0soL5/1/
.container {
max-width: 940px;
margin: 0 auto;
margin-top: 0px;
padding-top: 0px;
box-sizing: border-box;
position: relative;
}
.high {
background-color: #FFF;
}
.high .left-col {
width: 33%;
float: left;
height: auto;
padding: 20px 0px;
}
.high .left-col p {
font-size: 140%;
}
.high .right-col {
width: 67%;
float: right;
height: auto;
top: 50%;
bottom: 50%;
}
.site-nav {
float: right;
}
.site-nav li {
list-style: none;
float: left;
border-right: 1.5px solid;
}
.site-nav li:last-child {
border: none;
}
.site-nav ul {
-webkit-padding-start: 0px;
}
.site-nav a {
display: block;
text-decoration: none;
padding: 5px 20px;
}
/* This is the clear fix method */
.group:before,
.group:after {
content: "";
display: table;
}
.group:after {
clear: both;
}
.group {
zoom: 1;
}

<div class="high group">
<div class="container">
<div class="left-col">
<p>Company Name</p>
</div>
<aside class="right-col">
<nav class="site-nav">
<ul class="group">
<li><a href="draft1-1.html">Home</a></li>
<li><a href="contact.html" class="last">Contact Us</a></li>
<li><a href="#about">About</a></li>
<li><a href="#methodology">Methodology</a></li>
<li><a href="#">Products</a></li>
</ul>
</nav>
</aside>
</div>
</div>
&#13;
谢谢!
答案 0 :(得分:1)
您可以在父元素上使用display: table
,然后在每个子元素上display: table-cell
和vertical-align: middle
执行此操作。
此实现不使用float
属性。
像这样:
/* line 45, ../scss/screen1-2.scss */
.container {
max-width: 940px;
margin: 0 auto;
margin-top: 0px;
padding-top: 0px;
box-sizing: border-box;
position: relative;
display: table;
}
/* line 90, ../scss/screen1-2.scss */
.high {
background-color: #FFF;
}
/* line 92, ../scss/screen1-2.scss */
.high .left-col {
width: 30%;
display: table-cell;
vertical-align: middle;
padding: 20px 0px;
}
/* line 95, ../scss/screen1-2.scss */
.high .left-col p {
font-size: 140%;
}
/* line 99, ../scss/screen1-2.scss */
.high .right-col {
width: 70%;
display: table-cell;
vertical-align: middle;
top: 50%;
bottom: 50%;
}
/* line 108, ../scss/screen1-2.scss */
.site-nav {
float: right;
}
/* line 110, ../scss/screen1-2.scss */
.site-nav li {
list-style: none;
float: left;
border-right: 1.5px solid;
}
/* line 116, ../scss/screen1-2.scss */
.site-nav li:last-child {
border: none;
}
/* line 120, ../scss/screen1-2.scss */
.site-nav ul {
-webkit-padding-start: 0px;
}
/* line 124, ../scss/screen1-2.scss */
.site-nav a {
display: block;
text-decoration: none;
padding: 5px 20px;
}
/* This is the clear fix method */
/* line 410, ../scss/screen1-2.scss */
.group:before,
.group:after {
content: "";
display: table;
}
/* line 415, ../scss/screen1-2.scss */
.group:after {
clear: both;
}
/* line 419, ../scss/screen1-2.scss */
.group {
zoom: 1;
}
<div class="high group">
<div class="container">
<div class="left-col">
<p>Company Name</p>
</div>
<aside class="right-col">
<nav class="site-nav">
<ul class="group">
<li><a href="draft1-1.html">Home</a></li>
<li><a href="contact.html" class="last">Contact Us</a></li>
<li><a href="#about">About</a></li>
<li><a href="#methodology">Methodology</a></li>
<li><a href="#">Products</a></li>
</ul>
</nav>
</aside>
</div>
</div>
注意:我将每个元素的宽度分别从33%和67%更改为30%和70%,因为导航栏不适合。
答案 1 :(得分:1)
如果您可以使用flexbox,则可以使用display:flex
将容器设置为justify-content:space-between
(项目均匀分布在行中;第一项位于起始行,最后一项位于结束线)和align-items:center
(项目在横轴中居中)。请参阅下面的演示,我稍微简化了HTML。
<强> jsFiddle 强>
.container {
max-width: 940px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 0;
border: 1px solid;
}
.site-logo {
font-size: 150%;
}
.site-nav ul {
padding: 0;
margin: 0;
}
.site-nav li {
list-style: none;
display: inline-block;
border-right: 1.5px solid;
margin: 0 0 0 10px;
padding: 0 14px 0 0;
}
.site-nav li:last-child {
border: none;
padding-right: 0;
}
&#13;
<div class="container">
<div class="site-logo">Company Name</div>
<nav class="site-nav">
<ul>
<li><a href="draft1-1.html">Home</a></li>
<li><a href="contact.html" class="last">Contact Us</a></li>
<li><a href="#about">About</a></li>
<li><a href="#methodology">Methodology</a></li>
<li><a href="#">Products</a></li>
</ul>
</nav>
</div>
&#13;