gpt4 book ai didi

css - ReactJs - 对齐导航栏元素

转载 作者:行者123 更新时间:2023-11-28 01:41:35 25 4
gpt4 key购买 nike

目标
我正在使用 ReactJS,我正在尝试动态调整我的 Nav/Navbar 中的所有元素,如下面的示例所示。

例如:
enter image description here

问题:
我研究了一些选项,例如将“justified”附加到 Nav 标记,但这最终将我所有的 NavItems/MenuItems 挤在一起。我尝试将我的 Nav 包裹在 div 中,给它一个 className,然后尝试使用以下方法修改对齐方式:text-align : center 在 .css 文件中。不幸的是,这并没有改变对齐方式

导航组件

/* Basic Functionality Imports */
import React from 'react';
import { render } from 'react-dom';
import { Navbar, Nav, NavItem, MenuItem, NavDropdown } from 'react-bootstrap';
import { BrowserRouter as Router, Route, Link, NavLink } from "react-router-dom";
import { getUserGroup } from './Auth.jsx';

/* All page imports */
import GenReports from '../pages/GenReports';
import IntlReports from '../pages/IntlReports';

var isDev = getUserGroup() === "dev" ? true : false;
var isAdmin = getUserGroup() === "admin" ? true : false;
var isDataAnalyst = getUserGroup() === "analyst" ? true : false;

/*
* Below is an example of conditional rendering
* Using the above variables as shorthand, each "render" performs a check against the user role
* to determine if the navigation bar should render that piece of the navigation component.
* The final navbar pushed to the component that utilizes the Navigation componenet will never
* know the existance of the non-rendered pieces, helping to prevent potential security issues
*/
var renderUserVerify = !isDataAnalyst ? <MenuItem eventKey={1.1}><NavLink to="/UserVerfication">USER VERIFICATION</NavLink></MenuItem> : null;
var renderPkgLookup = !isDataAnalyst ? <MenuItem eventKey={1.2}><NavLink to="/PkgInqueryn">PACKAGE INQUERY</NavLink></MenuItem> : null;
var renderQuerySearch = !isDataAnalyst ? <NavDropdown eventKey={1} title="QUERY SEARCHES" id="query-nav-dropdown">
{renderUserVerify}
{renderPkgLookup}
</NavDropdown> : null;

var renderUpdateUser = isDev || isAdmin ? <NavItem eventKey={2}><NavLink to="/UpdateUser">UPDATE USER</NavLink></NavItem> : null;

var renderGenReports = isDev || isAdmin || isDataAnalyst ? <MenuItem eventKey={3.1}><NavLink to="/reports/GenReports">GENERAL REPORTS</NavLink></MenuItem> : null;
var renderIntlReports = isDev || isAdmin || isDataAnalyst ? <MenuItem eventKey={3.2}><NavLink to="/reports/IntlReports">INTERNATIONAL REPORTS</NavLink></MenuItem> : null;
var renderReports = isDev || isAdmin || isDataAnalyst ? <NavDropdown eventKey={3} title="REPORTS" id="reports-nav-dropdown">
{renderGenReports}
{renderIntlReports}
</NavDropdown> : null;
var renderUserPref = !isDataAnalyst ? <NavItem eventKey={4}><NavLink to"/UserPref">USER PREFERENCE</NavLink></NavItem> : null;

export default class Navigation extends React.Component {
render() {
return (
<Router>
<div>
<Navbar inverse fluid collapseOnSelect>
<Navbar.Header>
<Navbar.Brand>
<><NavLink to="/"> HOME </NavLink>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav justified>
{renderQuerySearch}
{renderUpdateUser
{renderReports}
{renderUserPref}
<NavItem eventKey={5}><NavLink to="/Help">HELP</NavItem>
<NavItem eventKey={6}><NavLink to="/logout">LOGOUT</NavItem>
</Nav>
</Navbar.Collapse>
</Navbar>

<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
<Route path="/GenReports" component={GenReports} />
<Route path="/IntlReports" component={IntlReports} />
</div>
</Router>
);
}
}

const Home = () => (
<div>
<h2>I am your Overlord! Fear me!</h2>
</div>
);

const About = () => (
<div>
<h2>About</h2>
</div>
);

const Topics = ({ match }) => (
<div>
<h2>Topics</h2>
<NavLink to={`${match.url}/rendering`}>Rendering with React</NavLink><br />
<NavLink to={`${match.url}/components`}>Components</NavLink><br />
<NavLink to={`${match.url}/props-v-state`}>Props v. State</NavLink><br />

<Route path={`${match.url}/:topicId`} component={Topic} />
<Route
exact
path={match.url}
render={() => <h3>Please select a topic.</h3>}
/>
</div>
);

const Topic = ({ match }) => (
<div>
<h3>{match.params.topicId}</h3>
</div>
);

示例报告页面(目前看起来都很相似)

import React from 'react';

export default class IntlReports extends React.Component {
render() {
return (
<h1>You have reached the International Reports Page</h1>
);
}
}

输出 HTML

<html lang="en">
<head>
<meta charset="utf-8" >
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="/manifest.json">
<link rel="shortcut icon" href="/favicon.ico">
<!--
Notice the use of in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<link type="text/css" href="/node_modules/bootstrap/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<title>React App</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}

.centerMe {
margin: 0;
float: none;
text-align: center;
}
</style>

<style type="text/css">
.App {
text-align: center;
}

.App-logo {
-webkit-animation: App-logo-spin infinite 20s linear;
animation: App-logo-spin infinite 20s linear;
height: 80px;
}

.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}

.App-title {
font-size: 1.5em;
}

.App-intro {
font-size: large;
}

@-webkit-keyframes App-logo-spin {
from { -webkit-transform: rotate(0deg); transform: rotate(0deg); }
to { -webkit-transform: rotate(360deg); transform: rotate(360deg); }
}

@keyframes App-logo-spin {
from { -webkit-transform: rotate(0deg); transform: rotate(0deg); }
to { -webkit-transform: rotate(360deg); transform: rotate(360deg); }
}

</style>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"><div class="App">
<header class="App-header">
<img src="/static/media/logo.5d5d9eef.svg" class="App-logo" alt="logo">
<h1 class="App-title">Welcome to React</h1>
</header>
<div>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand active" aria-current="true" href="/"> HOME </a>
<button type="button" class="navbar-toggle collapsed">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="centerMe nav navbar-nav">
<li class="dropdown">
<a id="query-nav-dropdown" role="button" class="dropdown-toggle" aria-haspopup="true" aria-expanded="false" href="#">QUERY SEARCHES <span class="caret"></span></a>
<ul role="menu" class="dropdown-menu" aria-labelledby="query-nav-dropdown">
<li role="presentation" class="">
<a role="menuitem" tabindex="-1" href="#"><a aria-current="false" href="/PkgLookup">PACKAGE LOOKUP</a></a>
</li></ul></li>
<li role="presentation" class=""><a role="button" href="#"><a aria-current="false" href="/Help">HELP</a></a></li>
<li role="presentation" class=""><a role="button" href="#"><a aria-current="false" href="/logout">LOGOUT</a></a></li>
</ul></div></div></nav>
<div><h2>I am your Overlord! Fear me!</h2></div></div></div></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<script rel="stylesheet" type="javascript" href="../node_modules/react-bootstrap/react-bootstrap.min.js"></script>
<script type="text/javascript" src="/static/js/bundle.js"></script>

</body>
</html>

index.css

body {
margin: 0;
padding: 0;
font-family: sans-serif;
}

App.css

.App {
text-align: center;
}

.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}

.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}

.App-title {
font-size: 1.5em;
}

.App-intro {
font-size: large;
}

@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}


渲染/输出 CSS

element.style {
}

html {
font-size: 10px;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
html {
font-family: sans-serif;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html[Attributes Style] {
-webkit-locale: "en";
}
user agent stylesheet
html {
display: block;
}
:after, :before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
:after, :before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}


在复制过程中,您可能需要手动更改 isDevisAdmin 和/或 isAnalyst 以使其成为 true 帮助修改渲染

问题:
有谁知道动态调整/居中 Navbar 元素的正确方法?

最佳答案

从文档中有一个名为 justify, setting 的 Nav Prop

<Nav className="justify-content-end nav" justify="true">
...
</Nav>

并将导航类设置为 flex: 1;这样它就可以占用 Navbar 中的所有空间。

关于css - ReactJs - 对齐导航栏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50415189/

25 4 0