gpt4 book ai didi

CSS 导航栏未使用 Flex 向右对齐

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:08 26 4
gpt4 key购买 nike

我正在尝试使用 CSS 实现“flex”。我无法让导航栏向右移动。我已经尝试了几个小时使用 margin 、更改显示、使用 float 。它不会移动到中间...抱歉我还在学习

我已经粘贴了指向我的代码笔的链接,以便向您展示更好的图片:

https://codepen.io/Saharalara/pen/pGyQWZ

HTML

 <div id="page-wrapper">
<header>
<div class="logo">
<img id="header-img" src="https://upload.wikimedia.org/" alt="Minnie Pic">
<span id="logo-name">Minnie & Friends Inc</span>
</div>
<nav id="nav-bar">
<ul id="nav-ul">
<li class="nav-link"><a href="#stories">Stories</a><li>
<li class="nav-link"><a href="#toys">Toys</a><li>
<li class="nav-link"><a href="#suscribe">Suscribe</a></li>
</ul>
</nav>
</header>
<body>
<container id="container1">
<section>
<h1 id="stories">Minnie & Friends Stories</h1>
<p>
<a href="#"> Here</a> you will find all of the best Minnie & Friends toys
and stories.
Choose from a huge variety of stories and the happy gang and they go on
many adventures.
</p>
<section>
<iframe id="video" height="180" src="https://www.youtube.com/watch?
v=crJ1CwD_TX0" frameborder="1" allowfullscreen ></iframe>
</section>
<h2 id="toys">Minnie & Friends Toys</h2>
<p>
<a href="#">Here</a> you will also find many of your favourite characters
to choose from and order to arrive at your doorstep to continue their
adventures with you.
</p>
<h3 id="suscribe">Suscribe to our newletter</h3>
</section>
</container>
</body>

CSS

#page-wrapper {
position: relative;
color: black;
margin: -9px;
padding: 10px;
border: 4px solid;
border-radius: 3px;
background-color: rgba(223, 42, 42, 0.20);
padding: 10px 20px 20px 20px;
}

header {
display: flex;
font-size: 1.3em;
margin-left: -10px;
margin-right: -10px;
background-image: url('https://cdn2.vectorstock.com/i/1000x1000/07/66/pink-
white-star-polka-dots-background-vector-7590766.jpg');
opacity: 0.80;
}

#header-img {
height: 120px;
}

#logo-name {
font-size: 2em;
color: black;
}

h1,
h2,
h3 {
font-size: 2em;
}

//navigation bar

#nav-bar {
display: flex;
justify-content: flex-end; //**not working***
}

#nav-ul {
list-style-type: none;
display: flex;
}

nav li {
padding: 4px;

}

//body

p {
font-size: 1.2em;
}

#video {
border: 5px solid;
border-radius: 3px;
color: pink;
}

最佳答案

修复 1

(对我而言)解决此问题的最佳方法是在 flex 容器(父级)级别控制布局。它简单明了。

header {

justify-content: space-between;
}

修复 2

另一种方法是将 flex-grow: 1 添加到您的 #nav-bar

#nav-bar {

flex-grow: 1;
}

在这种情况下,将 flex-grow 设置为 1 会告诉元素占用其 flex 容器中的可用空间。

修复 3

非 flexbox 的方式是在 #nav-bar 上添加一个左边的自动边距,有效地将它推到最右边。

#nav-bar {

margin-left: auto;
}

关于CSS 导航栏未使用 Flex 向右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54410608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com