如何在React Bootstrap 5中将一个项目对齐到右侧

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

How to align one item to the right In React Bootstrap 5

问题

I want to align an item called Login to the right of the NavBar. How should I do it? Thanks

Here is the code:

import React from 'react';
import { Nav, Navbar, NavLink } from 'react-bootstrap';
import { Link } from 'react-router-dom';

const Navigationbar = () => {
  return (
    <div>
      <Navbar collapseOnSelect expand="sm" bg="primary" variant="dark">
        <Navbar.Toggle aria-controls="navbarScroll" data-bs-target="#navbarScroll" />
        <Navbar.Collapse id="navbarScroll">
          <Nav>
            <NavLink href="/">Home</NavLink>
            <NavLink href="/about">About</NavLink>
            <NavLink href="/contact">Contact</NavLink>
            <NavLink style={{ justifyContent: 'flex-end' }}>Login</NavLink>
          </Nav>
        </Navbar.Collapse>
      </Navbar>
    </div>
  );
};

export default Navigationbar;

I tried using style={{ justifyContent: 'flex-end' }} and it is not working.

英文:

I want to align an item called Login to the right of the NavBar. How should I do it? Thanks

Here is the code:

import React from &#39;react&#39;
import {Nav, Navbar, NavLink}  from &#39;react-bootstrap&#39;
import {Link} from &#39;react-router-dom&#39;

const Navigationbar = () =&gt; {
  return (
    &lt;div&gt;
      &lt;Navbar collapseOnSelect expand=&quot;sm&quot; bg=&quot;primary&quot; variant=&quot;dark&quot;&gt;
        &lt;Navbar.Toggle aria-controls=&#39;navbarScroll&#39; data-bs-target=&quot;#navbarScroll&quot;/&gt;
        &lt;Navbar.Collapse id=&quot;navbarScroll&quot;&gt;
          &lt;Nav&gt;
            &lt;NavLink href=&quot;/&quot;&gt;Home&lt;/NavLink&gt;
            &lt;NavLink href=&quot;/about&quot;&gt;About&lt;/NavLink&gt;
            &lt;NavLink href=&quot;/contact&quot;&gt;Contact&lt;/NavLink&gt;
           ** &lt;NavLink style={{justifyContent:&quot;flex-end&quot;}}&gt;Login&lt;/NavLink&gt;**
          &lt;/Nav&gt;
        &lt;/Navbar.Collapse&gt;
      &lt;/Navbar&gt;
    &lt;/div&gt;
  )
}

export default Navigationbar

I tried using tyle={{justifyContent:"flex-end"}} and is not working.

答案1

得分: 0

&lt;Nav&gt;设为全宽,并对链接应用margin-left: auto

<Nav style={{ width: '100%', marginLeft: 'auto' }}>

请注意,上述代码中的&lt;&gt;是HTML实体编码,用于在HTML中表示<>字符。在实际代码中,应使用原始的<Nav>标签和CSS属性,而不是HTML实体编码。

英文:

Make the &lt;Nav&gt; full width and then apply margin-left: auto to the link:

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

<!-- language: lang-js -->

const { Container, Nav, Navbar, NavLink } = ReactBootstrap;

ReactDOM.createRoot(document.getElementById(&#39;app&#39;)).render(
  &lt;div&gt;
    &lt;Navbar collapseOnSelect expand=&quot;sm&quot; bg=&quot;primary&quot; variant=&quot;dark&quot;&gt;
      &lt;Navbar.Toggle aria-controls=&#39;navbarScroll&#39; data-bs-target=&quot;#navbarScroll&quot;/&gt;
      &lt;Navbar.Collapse id=&quot;navbarScroll&quot;&gt;
        &lt;Nav style={{ width: &#39;100%&#39; }}&gt;
          &lt;NavLink href=&quot;/&quot;&gt;Home&lt;/NavLink&gt;
          &lt;NavLink href=&quot;/about&quot;&gt;About&lt;/NavLink&gt;
          &lt;NavLink href=&quot;/contact&quot;&gt;Contact&lt;/NavLink&gt;
          &lt;NavLink className=&quot;ms-auto&quot;&gt;Login&lt;/NavLink&gt;
        &lt;/Nav&gt;
      &lt;/Navbar.Collapse&gt;
    &lt;/Navbar&gt;
  &lt;/div&gt;
)

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js&quot; integrity=&quot;sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js&quot; integrity=&quot;sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-bootstrap/2.8.0/react-bootstrap.min.js&quot; integrity=&quot;sha512-hSAnp8t2uUf7f1vHEtw6AfJisBLZEC61Aqrz6MZtf0J5iZn+9DYctEdqRxHd09jjFC16n2LvpRFcp7iYmrPZcw==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css&quot; integrity=&quot;sha512-t4GWSVZO1eC8BM339Xd7Uphw5s17a86tIZIj8qRxhnKub6WoyhnrxeCIMeAqBPgdZGlCcG2PrZjMc+Wr78+5Xg==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;

&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月25日 21:52:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550740.html
匿名

发表评论

匿名网友

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

确定