我想在左侧创建一个带有我名字的标题,并使用flexbox在右侧创建一个“about”“portfolio”“contact”的导航器。
我在我的flexbox容器中使用in-range
,但项目不合适,他们留在左边,我不知道为什么。
我可以使用各种justify-content: flex-end;
,但标题保持不变。
我想知道我是如何将“Roger Anderson”这个名字留在左边,然后是“关于,投资组合,联系”。
justify-content
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
display: block;
}
body {
line-height: 1;
}
ol,
ul {
list-style: none;
}
blockquote,
q {
quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/* COMEÇADO O ESTILO */
header {
display: flex;
justify-content: flex-end;
background-color: black;
height: 50px;
align-items: center;
width: 100%;
}
.logo-name,
.nav li {
font-family: 'Abel', sans-serif;
color: white;
font-weight: 400;
text-transform: uppercase;
align-self: center;
padding-left: 5px;
}
.logo {
height: 25px;
width: 25px;
}
a {
text-decoration: none;
}
a:visited {
color: white;
}
a:link {
color: white;
}
/*
========================================
Navigation
========================================
*/
.nav {
margin-right: auto;
}
.nav li {
display: inline-block;
margin: 0 10px;
}
< !DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Roger Anderson</title><link rel="stylesheet" href="style.css"><link href="https://fonts.googleapis.com/css?family=Abel" rel="stylesheet"></head><body>< !-- Header --><header class="primary-header"><h5 class="logo-name">Roger Anderson</h5><nav class="nav"><ul><li><a href="about.html">about</a></li>< !-- --><li><a href="portfolio.html">portfolio</a></li>< !-- --><li><a href="contact.html">contact</a></li></ul></nav></header></body></html>My CSS:
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
display: block;
}
body {
line-height: 1;
}
ol,
ul {
list-style: none;
}
blockquote,
q {
quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/* COMEÇADO O ESTILO */
header {
display: flex;
justify-content: flex-end;
background-color: black;
height: 50px;
align-items: center;
width: 100%;
}
.logo-name,
.nav li {
font-family: 'Abel', sans-serif;
color: white;
font-weight: 400;
text-transform: uppercase;
align-self: center;
padding-left: 5px;
}
.logo {
height: 25px;
width: 25px;
}
a {
text-decoration: none;
}
a:visited {
color: white;
}
a:link {
color: white;
}
/*
========================================
Navigation
========================================
*/
.nav {
margin-right: auto;
}
.nav li {
display: inline-block;
margin: 0 10px;
}
答案 0 :(得分:2)
你有这个:
.nav {
margin-right: auto;
}
而是使用它:
.nav {
margin-left: auto;
}
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/* COMEÇADO O ESTILO */
header {
display:flex;
justify-content: flex-end;
background-color: black;
height: 50px;
align-items:center;
width:100%;
}
.logo-name, .nav li {
font-family: 'Abel', sans-serif;
color: white;
font-weight: 400;
text-transform: uppercase;
align-self: center;
padding-left: 5px;
}
.logo {
height: 25px;
width: 25px;
}
a{text-decoration: none; }
a:visited {color: white;}
a:link {color: white;}
/*
========================================
Navigation
========================================
*/
.nav {
/* margin-right: auto; */
margin-left: auto; /* NEW */
}
.nav li {
display: inline-block;
margin: 0 10px;
}
<header class="primary-header">
<h5 class="logo-name">Roger Anderson</h5>
<nav class="nav">
<ul>
<li><a href="about.html">about</a></li>
<li><a href="portfolio.html">portfolio</a></li>
<li><a href="contact.html">contact</a></li>
</ul>
</nav>
</header>
Flex auto
页边距消耗了边距方向的所有可用空间。
因此,当您在导航菜单上设置margin-right: auto
时,会消耗右侧的所有可用空间,将菜单固定在左侧。
但如果您使用margin-left: auto
,则会消耗左侧的所有可用空间,将菜单推离徽标并将其固定在右侧。
以下是一个更完整的解释:
justify-content
无效的原因有两个:
Flex auto
页边距优先于关键字对齐属性(例如justify-content
和align-items
)。如果设置了auto
页边距,则会获得第一个镜头并消耗该行上的所有可用空间。 justify-content
排在第二位,但没有剩余空间,所以它无能为力。
8.1. Aligning with
auto
margins在通过
justify-content
和align-self
进行对齐之前 正空闲空间分配到auto
边距 尺寸。如果可用空间分配到
auto
边距,则对齐 由于边距,属性在该维度中不起作用 屈服后会偷走剩下的所有可用空间。
即使没有auto
页边距,justify-content
正在运行,也无法正常工作,因为您将值设置为flex-end
。这会将所有项目固定在右侧。您需要使用space-between
或space-around
。
答案 1 :(得分:0)
.primary-header {
display: flex;
flex-direction: row;
justify-content: space-between;
}
答案 2 :(得分:0)
Justify-content:flex-end;将证明右侧的标题和列表项。你想使用justify-content:space-between;代替。 (A Complete Guide to Flexbox)
您可以使用flexbox将列表项放在彼此旁边。只需添加display:flex;到.nav ul。
删除display:inline-block;来自li元素和margin-right:auto;来自.nav。就个人而言,我更喜欢纯粹通过flexbox属性来解决对齐问题。
示例:
Dim myClassList as New Lsit(Of MyClass)