我正在使用flexbox创建导航栏 - A Codepen here。我想要左侧的徽标和导航栏右侧的链接。
然而,我无法锻炼如何使用flex-end
取得良好效果。无序列表中的链接似乎不受flexbox的影响。有没有一种特殊的方法将flexbox绑定到导航栏?
我的HTML:
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway" />
<nav>
<!-- Primary Logo -->
<div id="logo">
<img src="http://www.shinetown.com.sg/shinetown/wp-content/uploads/2016/04/logo-dummy.png" alt="NewCircle">
</div>
<!-- Primary Navigation -->
<div id="navigation">
<ul>
<li class="current"><a href="#">Home</a>
<li><a href="#">Products</a>
<li><a href="#">Pricing Plans</a>
<li><a href="#">Help</a>
</ul>
</div>
</nav>
和我的手写笔代码:
nav-height = 100px
primary-color = #FF5B4E
primary-text-color = #444
secondary-border-color = #EEE
body {
font-family: Raleway
}
nav, nav *
display: flex
align-items: center
width: 100%
nav
height: nav-height
border-bottom: 5px solid secondary-border-color
#logo
width: 300px
height: nav-height
border-right: 1px solid secondary-border-color
padding: 0 1em 0 1em
img
max-width: 100%
#navigation
height: 100%
justify-content: flex-end
ul
list-style-type: none
padding: 0
height: 100%
margin: 0
li
height: 100%
a
height: 100%
transition: color 0.4s ease
font-weight: bold
font-size: 13px
letter-spacing: 1px
text-transform: uppercase
text-align: center
text-decoration: none
color: primary-text-color
padding: 0 18px 0 18px
a:hover
color: primary-color
.current
> a
color: primary-color
答案 0 :(得分:1)
flex-end
无法解决问题,margin-left: auto
会。
nav, nav *
display: flex
align-items: center
#navigation
height: 100%
margin-left: auto
另外,在规则中你有nav *
,这会使所有nav
个后代的宽度达到100%,且#navigation
无法对齐正确的是宽度。
从width: 100%
规则中删除nav, nav *
将解决此问题。
Stack snippet
body {
font-family: Raleway;
}
nav,
nav * {
display: flex;
align-items: center;
}
nav {
height: 100px;
border-bottom: 5px solid #eee;
}
nav #logo {
width: 300px;
height: 100px;
border-right: 1px solid #eee;
padding: 0 1em 0 1em;
}
nav #logo img {
max-width: 100%;
}
nav #navigation {
height: 100%;
margin-left: auto;
}
nav #navigation ul {
list-style-type: none;
padding: 0;
height: 100%;
margin: 0;
}
nav #navigation ul li {
height: 100%;
}
nav #navigation ul li a {
height: 100%;
transition: color 0.4s ease;
font-weight: bold;
font-size: 13px;
letter-spacing: 1px;
text-transform: uppercase;
text-align: center;
text-decoration: none;
color: #444;
padding: 0 18px 0 18px;
}
nav #navigation ul li a:hover {
color: #ff5b4e;
}
nav #navigation ul .current > a {
color: #ff5b4e;
}
&#13;
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway" />
<nav>
<!-- Primary Logo -->
<div id="logo">
<img src="http://www.shinetown.com.sg/shinetown/wp-content/uploads/2016/04/logo-dummy.png" alt="NewCircle">
</div>
<!-- Primary Navigation -->
<div id="navigation">
<ul>
<li class="current"><a href="#">Home</a>
<li><a href="#">Products</a>
<li><a href="#">Pricing Plans</a>
<li><a href="#">Help</a>
</ul>
</div>
</nav>
&#13;
注意,justify-content: flex-end
应该在Flex容器上使用,而不是在flex项目上使用。如果想要在弹性项目上设置它,请使用align-self: flex-end
。
在这种情况下,flex具有行方向,因此align-self
在交叉轴上应用时不会起作用,因此margin-left: auto
将起作用。