导航栏获得水平布局

时间:2018-01-23 18:31:07

标签: css reactjs bootstrap-4 reactstrap

我在React应用程序中有一个导航栏的代码。当浏览器窗口很宽时,导航栏内容看起来很好,但是一旦我超过navbar-expand-lg截止点,图标就会垂直对齐。我该如何预防呢? (见下面的代码截图)

import React, {Component} from 'react';
import {
  Badge,
  Nav,
  Navbar,
  NavbarBrand,
  NavbarToggler,
  NavItem,
  NavLink,
} from 'reactstrap';
import FontAwesome from '@fortawesome/react-fontawesome';

class Header extends Component {

  constructor(props) {
    super(props);
  }

  render() {
    return (
      <Navbar dark color="dark" expand="lg" className="w-100 p-1">
        <NavbarToggler className="d-flex"/>
        <NavbarBrand className="ml-3" href="/">Logo</NavbarBrand>
        <Nav className="d-flex ml-auto" horizontal="end" navbar>
          <NavItem>
            <NavLink href="#">
              <FontAwesome icon={["fas","bell"]} className="text-light" size="lg" />
              <Badge pill color="danger">5</Badge>
            </NavLink>
          </NavItem>
          <NavItem>
            <NavLink href="#">
              <FontAwesome icon={["fas","clipboard"]} className="text-light" size="lg" />
              <Badge pill color="warning">5</Badge>
            </NavLink>
          </NavItem>
          <NavItem>
            <NavLink href="#">
              <i className="icon-location-pin"></i>
              <Badge pill color="info">5</Badge>
            </NavLink>
          </NavItem>
        </Nav>
        <NavbarToggler className="d-flex ml-3"/>
      </Navbar>
    );
  }
}

export default Header;

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

只需将navbar-expand-lg替换为navbar-expand

即可

答案 1 :(得分:1)

要实现您的目标,您需要使用 try { var connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyLearn\ExcelWorkBook.xls;Extended Properties=Excel 8.0"; var sqlText = "CREATE TABLE TimeData ([HH] INT, [MM] INT,[AM / PM] VARCHAR(10))"; using (var excelConnection = new OleDbConnection(connectionString)) { object HHValue = TimeInHH.Text; object MMValue = TimeInMM.Text; object AMPMValue = Combo1AMPM.Text; // Executing this command will create the worksheet inside of the workbook // the table name will be the new worksheet name using (var command = new OleDbCommand(sqlText, excelConnection)) { if (excelConnection.State != ConnectionState.Open) { excelConnection.Open(); //check if table already exists. var exists = excelConnection.GetSchema("Tables", new string[4] { null, null, "TimeData", "TABLE" }).Rows.Count > 0; if(!exists) command.ExecuteNonQuery(); } } // Add (insert) data to the worksheet var commandText = $"Insert Into TimeData ([HH], [MM], [AM / PM]) Values (@PropertyOne, @PropertyTwo, @PropertyThree)"; using (var command = new OleDbCommand(commandText, excelConnection)) { // We need to allow for nulls just like we would with // sql, if your data is null a DBNull.Value should be used // instead of null command.Parameters.AddWithValue("@PropertyOne", HHValue ?? DBNull.Value); command.Parameters.AddWithValue("@PropertyTwo", MMValue ?? DBNull.Value); command.Parameters.AddWithValue("@PropertyThree", AMPMValue ?? DBNull.Value); command.ExecuteNonQuery(); } } } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); } finally { Console.WriteLine("Executing finally block."); } } 类将通常为flex列的内容转换为弹性行。

以下是导航栏的完整工作代码,包括两个汉堡包菜单(我已在flex-row屏幕上隐藏了lg类的正确菜单项:

d-lg-none

图标右侧的填充调整了填充右侧类<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <button class="navbar-toggler mr-3" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="#">Navbar</a> <div class="ml-auto order-lg-last"> <ul class="navbar-nav flex-row"> <li class="nav-item pr-3 pr-lg-0"> <a class="nav-link" href="#"> <i class="fa fa-bell" aria-hidden="true"></i> <span class="badge badge-pill badge-danger">5</span> </a> </li> <li class="nav-item pr-3 pr-lg-0"> <a class="nav-link" href="#"> <i class="fa fa-envelope" aria-hidden="true"></i> <span class="badge badge-pill badge-warning">6</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="#"> <i class="fa fa-telegram" aria-hidden="true"></i> <span class="badge badge-pill badge-primary">8</span> </a> </li> </ul> </div> <button class="navbar-toggler ml-3" type="button" data-toggle="collapse" data-target="#navbarSupportedContent2" aria-controls="navbarSupportedContent2" aria-expanded="false" aria-label="Toggle navigation2"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item active"> <a class="nav-link" href="#">Left Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">Left Link 1</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Left Link 2</a> </li> </ul> </div> <div class="collapse navbar-collapse" id="navbarSupportedContent2"> <ul class="navbar-nav mr-auto d-lg-none"> <li class="nav-item active"> <a class="nav-link" href="#">Right Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">Right Link 1</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Right Link 2</a> </li> </ul> </div> </nav> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>,即pr-3 pr-lg-0屏幕上的零或默认填充,以及较小屏幕上的3个单位右填充。

另请注意lg类如何在order-lg-last及更高屏幕上切换。