如何将按钮移动到导航栏的右侧?

huangapple go评论126阅读模式
英文:

How could I move the button to the right side of the navigation bar?

问题

"Client Area"按钮需要移动到导航栏的右侧,但是当我尝试了一些方法后,它没有从当前位置移动。我尝试了一些方法,但无法使它移动。有什么好的解决方法来解决这个问题。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
/* 主要网站样式 */

* {
  background-color: #232323;
  color: white;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Kumbh Sans', sans-serif;
}

/* 导航栏 */

/* 导航栏项目 */

.navbar ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: #000f14;
  display: flex;
  align-items: center;
}

.navbar li a {
  display: block;
  text-align: center;
  padding: 12px 16px;
  text-decoration: none;
  background: #000f14;
}

.navbar img {
  width: 50%;
  height: 50%;
}

.navbar a {
  background: #000f14;
}

/* 导航栏 客户区按钮 */

.btn {
  border: 2.5px solid #053F74;
  background-color: #000f14;
  color: white;
  padding: 12px 16px;
  font-size: 16px;
  cursor: pointer;
  border-radius: 15px;
  text-decoration: none;
}

.btn:hover {
  background: #053F74;
}

<!-- language: lang-html -->
<nav class="navbar">
  <div class="navbar__container">
    <div class="navbar__toggle" id="mobile-menu">
      <span class="bar"></span>
      <span class="bar"></span>
      <span class="bar"></span>
    </div>
    <ul>
      <li>
        <a href="/index.html"><img src="/images/AquaTech navbar logo.png" alt="navbar__logo"></a>
      </li>
      <li><a href="index.html">首页</a></li>
      <li><a href="services.html">服务</a></li>
      <li><a href="clients.html">客户</a></li>
      <li><a href="contact.html">联系我们</a></li>
      <li><a href="about.html">关于我们</a></li>
      <a href="login.html"><button class="btn">客户区</button></a>
    </ul>
  </div>
</nav>

<!-- end snippet -->

如何将按钮移动到导航栏的右侧?
enter image description here

英文:

enter image description hereThe "Client Area" button needs to move to the right side of the navigation bar, but when I tried a few things I couldn't get it to move from the place it is in as of right now.
I tried some things but it would not work. What is a good way to resolve this issue. I couldn't a sollution to get this working.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/* Main website */
* {
background-color: #232323;
color: white;
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: &#39;Kumbh Sans&#39;, sans-serif;
}
/* Navigation bar */
/* Navigation items */
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background: #000f14;
display: flex;
align-items: center;
}
.navbar li a {
display: block;
text-align: center;
padding: 12px 16px;
text-decoration: none;
background: #000f14;
}
.navbar img {
width: 50%;
height: 50%;
}
.navbar a {
background: #000f14;
}
/* Navigation  Client area Button */
.btn {
border: 2.5px solid #053F74;
background-color: #000f14;
color: white;
padding: 12px 16px;
font-size: 16px;
cursor: pointer;
border-radius: 15px;
text-decoration: none;
}
.btn:hover {
background: #053F74;
}

<!-- language: lang-html -->

&lt;nav class=&quot;navbar&quot;&gt;
&lt;div class=&quot;navbar__container&quot;&gt;
&lt;div class=&quot;navbar__toggle&quot; id=&quot;mobile-menu&quot;&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;/index.html&quot;&gt;&lt;img src=&quot;/images/AquaTech navbar logo.png&quot; alt=&quot;navbar__logo&quot;&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;index.html&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;services.html&quot;&gt;Services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;clients.html&quot;&gt;Clients&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;contact.html&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About us&lt;/a&gt;&lt;/li&gt;
&lt;a href=&quot;login.html&quot;&gt;&lt;button class=&quot;btn&quot;&gt;Client Area&lt;/button&gt;&lt;/a&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;

<!-- end snippet -->

如何将按钮移动到导航栏的右侧?
enter image description here

答案1

得分: 1

你可以将margin-inline: auto 0应用到你的锚元素。关于如何使用边距的一些信息,可以从Kevin Powell的这里这里找到。

以下是代码部分,不进行翻译:

/* Main website */

* {
  background-color: #232323;
  color: white;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Kumbh Sans', sans-serif;
}

/* Navigation bar */

/* Navigation items */

.navbar ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: #000f14;
  display: flex;
  align-items: center;
}

.navbar li a {
  display: block;
  text-align: center;
  padding: 12px 16px;
  text-decoration: none;
  background: #000f14;
}

.navbar img {
  width: 50%;
  height: 50%;
}

.navbar a {
  background: #000f14;
}

/* Navigation  Client area Button */

.btn {
  border: 2.5px solid #053F74;
  background-color: #000f14;
  color: white;
  padding: 12px 16px;
  font-size: 16px;
  cursor: pointer;
  border-radius: 15px;
  text-decoration: none;
}

.btn:hover {
  background: #053F74;
}

/* added this class */
.align-right {
  margin-inline: auto 0;
}
<nav class="navbar">
  <div class="navbar__container">
    <div class="navbar__toggle" id="mobile-menu">
      <span class="bar"></span>
      <span class="bar"></span>
      <span class="bar"></span>
    </div>
    <ul>
      <li>
        <a href="/index.html"><img src="/images/AquaTech navbar logo.png" alt="navbar__logo"></a>
      </li>
      <li><a href="index.html">Home</a></li>
      <li><a href="services.html">Services</a></li>
      <li><a href="clients.html">Clients</a></li>
      <li><a href="contact.html">Contact</a></li>
      <li><a href="about.html">About us</a></li>
      <a class='align-right' href="login.html"><button class="btn">Client Area</button></a><!-- added the class 'align-right' to the <a> element -->
    </ul>
  </div>
</nav>
/* Main website */

* {
  background-color: #232323;
  color: white;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Kumbh Sans', sans-serif;
}

/* Navigation bar */

/* Navigation items */

.navbar ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: #000f14;
  display: flex;
  align-items: center;
}

.navbar li a {
  display: block;
  text-align: center;
  padding: 12px 16px;
  text-decoration: none;
  background: #000f14;
}

.navbar img {
  width: 50%;
  height: 50%;
}

.navbar a {
  background: #000f14;
}

/* Navigation  Client area Button */

.btn {
  border: 2.5px solid #053F74;
  background-color: #000f14;
  color: white;
  padding: 12px 16px;
  font-size: 16px;
  cursor: pointer;
  border-radius: 15px;
  text-decoration: none;
}

.btn:hover {
  background: #053F74;
}

/* added this class */
.spacer {
  flex-grow: 2;
}
<nav class="navbar">
  <div class="navbar__container">
    <div class="navbar__toggle" id="mobile-menu">
      <span class="bar"></span>
      <span class="bar"></span>
      <span class="bar"></span>
    </div>
    <ul>
      <li>
        <a href="/index.html"><img src="/images/AquaTech navbar logo.png" alt="navbar__logo"></a>
      </li>
      <li><a href="index.html">Home</a></li>
      <li><a href="services.html">Services</a></li>
      <li><a href="clients.html">Clients</a></li>
      <li><a href="contact.html">Contact</a></li>
      <li><a href="about.html">About us</a></li>
      <li class='spacer'></li><!-- added this spacer element -->
      <a href="login.html"><button class="btn">Client Area</button></a>
    </ul>
  </div>
</nav>
英文:

You can use margin-inline: auto 0 to your anchor element. Some info on how to use margins from Kevin Powell here and here.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/* Main website */
* {
background-color: #232323;
color: white;
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: &#39;Kumbh Sans&#39;, sans-serif;
}
/* Navigation bar */
/* Navigation items */
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background: #000f14;
display: flex;
align-items: center;
}
.navbar li a {
display: block;
text-align: center;
padding: 12px 16px;
text-decoration: none;
background: #000f14;
}
.navbar img {
width: 50%;
height: 50%;
}
.navbar a {
background: #000f14;
}
/* Navigation  Client area Button */
.btn {
border: 2.5px solid #053F74;
background-color: #000f14;
color: white;
padding: 12px 16px;
font-size: 16px;
cursor: pointer;
border-radius: 15px;
text-decoration: none;
}
.btn:hover {
background: #053F74;
}
/* added this class */
.align-right {
margin-inline: auto 0;
}

<!-- language: lang-html -->

&lt;nav class=&quot;navbar&quot;&gt;
&lt;div class=&quot;navbar__container&quot;&gt;
&lt;div class=&quot;navbar__toggle&quot; id=&quot;mobile-menu&quot;&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;/index.html&quot;&gt;&lt;img src=&quot;/images/AquaTech navbar logo.png&quot; alt=&quot;navbar__logo&quot;&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;index.html&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;services.html&quot;&gt;Services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;clients.html&quot;&gt;Clients&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;contact.html&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About us&lt;/a&gt;&lt;/li&gt;
&lt;a class=&#39;align-right&#39; href=&quot;login.html&quot;&gt;&lt;button class=&quot;btn&quot;&gt;Client Area&lt;/button&gt;&lt;/a&gt;&lt;!-- added the class &#39;align-right&#39; to the &lt;a&gt; element --&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;

<!-- end snippet -->

The other way I do this sometimes is to add a dummy element as a spacer and use flex-grow to push the elements beside it to the left and right.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/* Main website */
* {
background-color: #232323;
color: white;
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: &#39;Kumbh Sans&#39;, sans-serif;
}
/* Navigation bar */
/* Navigation items */
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background: #000f14;
display: flex;
align-items: center;
}
.navbar li a {
display: block;
text-align: center;
padding: 12px 16px;
text-decoration: none;
background: #000f14;
}
.navbar img {
width: 50%;
height: 50%;
}
.navbar a {
background: #000f14;
}
/* Navigation  Client area Button */
.btn {
border: 2.5px solid #053F74;
background-color: #000f14;
color: white;
padding: 12px 16px;
font-size: 16px;
cursor: pointer;
border-radius: 15px;
text-decoration: none;
}
.btn:hover {
background: #053F74;
}
/* added this class */
.spacer {
flex-grow:2;
}

<!-- language: lang-html -->

&lt;nav class=&quot;navbar&quot;&gt;
&lt;div class=&quot;navbar__container&quot;&gt;
&lt;div class=&quot;navbar__toggle&quot; id=&quot;mobile-menu&quot;&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;/index.html&quot;&gt;&lt;img src=&quot;/images/AquaTech navbar logo.png&quot; alt=&quot;navbar__logo&quot;&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;index.html&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;services.html&quot;&gt;Services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;clients.html&quot;&gt;Clients&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;contact.html&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About us&lt;/a&gt;&lt;/li&gt;
&lt;li class=&#39;spacer&#39;&gt;&lt;/li&gt;&lt;!-- added this spacer element  --&gt;
&lt;a href=&quot;login.html&quot;&gt;&lt;button class=&quot;btn&quot;&gt;Client Area&lt;/button&gt;&lt;/a&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;

<!-- end snippet -->

答案2

得分: 0

你可以在"Client area"之前创建一个元素,并使用margin:autoflex-grow: 1

/* 主要网站样式 */

* {
  background-color: #232323;
  color: white;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Kumbh Sans', sans-serif;
}

/* 导航栏 */

/* 导航项目 */

.navbar ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: #000f14;
  display: flex;
  align-items: center;
}
.navbar ul .spacer {
  flex-grow: 1;
}
.navbar li a {
  display: block;
  text-align: center;
  padding: 12px 16px;
  text-decoration: none;
  background: #000f14;
}
<nav class="navbar">
  <div class="navbar__container">
    <div class="navbar__toggle" id="mobile-menu">
      <span class="bar"></span>
      <span class="bar"></span>
      <span class="bar"></span>
    </div>
    <ul>
      <li>
        <a href="/index.html"><img src="/images/AquaTech navbar logo.png" alt="navbar__logo"></a>
      </li>
      <li><a href="index.html">Home</a></li>
      <li><a href="services.html">Services</a></li>
      <li><a href="clients.html">Clients</a></li>
      <li><a href="contact.html">Contact</a></li>
      <li><a href="about.html">About us</a></li>
      <div class="spacer"></div>
      <a href="login.html"><button class="btn">Client Area</button></a>
    </ul>
  </div>
</nav>
英文:

You can create an element right before "Client area" and use margin:auto or use flex-grow: 1.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/* Main website */
* {
background-color: #232323;
color: white;
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: &#39;Kumbh Sans&#39;, sans-serif;
}
/* Navigation bar */
/* Navigation items */
.navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background: #000f14;
display: flex;
align-items: center;
}
.navbar ul .spacer {
flex-grow: 1;
}
.navbar li a {
display: block;
text-align: center;
padding: 12px 16px;
text-decoration: none;
background: #000f14;
}

<!-- language: lang-html -->

&lt;nav class=&quot;navbar&quot;&gt;
&lt;div class=&quot;navbar__container&quot;&gt;
&lt;div class=&quot;navbar__toggle&quot; id=&quot;mobile-menu&quot;&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;bar&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;/index.html&quot;&gt;&lt;img src=&quot;/images/AquaTech navbar logo.png&quot; alt=&quot;navbar__logo&quot;&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;index.html&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;services.html&quot;&gt;Services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;clients.html&quot;&gt;Clients&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;contact.html&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About us&lt;/a&gt;&lt;/li&gt;
&lt;div class=&quot;spacer&quot;&gt;&lt;/div&gt;
&lt;a href=&quot;login.html&quot;&gt;&lt;button class=&quot;btn&quot;&gt;Client Area&lt;/button&gt;&lt;/a&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月13日 23:48:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定