I'm having trouble centering the links in my navigation bar using flexbox, and I'm not sure how to fix it

Struggling with creating a navigation bar for my portfolio-building project. The "Who needs a quote" element is correctly displaying on the left, but I can't figure out how to center align the links "Sports, business, politics". Tried using flexbox options without success. Any help would be greatly appreciated. Below is my code. Thanks!

//js file
import React, {Component} from 'react';
import {Navbar, Nav,NavDropdown, Form, FormControl, Button} from 'react-bootstrap';

import './Topbar.css'
import img from './logo.png'

class Topbar extends Component {
  render() {
    return (
      <div className = "OuterDiv">
        <div>
        <a class="active" href="/">Who Needs A Quote!?</a>
        </div>
        <div class = "topnavcenter">

        <a href="/SportsPage">Sports</a>
        <a href="/BusinessPage">Business</a>
        <a href="/PoliticsPage">Politics</a>
        </div>
      </div>

    )
  }
}

export default Topbar;

//css file

.OuterDiv {
  display: flex;
  flex-direction: row;
  background-color: #FFFF00;
  padding: 14px 16px;
  font-size: 17px;

}



.topnavcenter {
  float: center;

}

Thanks again!

Answer №1

It is possible that there was some confusion in understanding the question, but I will do my best to provide an answer. It seems like you may need to include the following code for the topnavcenter block:
-ms-align-self: center;
align-self: center;

Answer №2

There are multiple approaches to centrally aligning links, each dependent on the styling of the navigation bar.

text-align:center

To quickly center a basic set of text-only links, you can use text-align:center on the .topnavcenter element.

.OuterDiv {
  display: flex;
  flex-direction: row;
  background-color: #FFFF00;
  padding: 14px 16px;
  font-size: 17px;
}

.topnavcenter {
  text-align: center;
}
<div className="OuterDiv">
  <div>
    <a class="active" href="/">Who Needs A Quote!?</a>
  </div>
  <div class="topnavcenter">
    <a href="/SportsPage">Sports</a>
    <a href="/BusinessPage">Business</a>
    <a href="/PoliticsPage">Politics</a>
  </div>
</div>

justify-content:center

If you're aiming for a more elaborate navigation bar with button-styled links, it's recommended to utilize flexbox. By making .topnavcenter a flex container and applying justify-content:center, you can easily center its child elements.

.OuterDiv {
  display: flex;
  flex-direction: row;
  background-color: #FFFF00;
  padding: 14px 16px;
  font-size: 17px;
}

.topnavcenter {
  display: flex;
  justify-content: center;
}
<div className="OuterDiv">
  <div>
    <a class="active" href="/">Who Needs A Quote!?</a>
  </div>
  <div class="topnavcenter">
    <a href="/SportsPage">Sports</a>
    <a href="/BusinessPage">Business</a>
    <a href="/PoliticsPage">Politics</a>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble running Next.js on iOS? An error occurred while trying to execute the 'insertBefore' function on the Node, resulting in a NotFoundError with the message insertBefore([native code]). It seems like the object you are

After extensive searching, I finally found a solution to an error that was plaguing our iOS Mobile users and a few Android users. This issue was causing severe 500 Application errors on our Nextjs app for our users. Has anyone else encountered a similar pr ...

Utilizing the map() function to parse a Json object

Looking for guidance with working with a JSON object structured like this: {"Title":"asb","Date":"2019","Other":"not important"} How can I correctly read this object and render it in <ul><li> format? Please Note: I have attempted to assign th ...

Retrieve the scope object using angular.element and the $ID parameter

In order to transfer data from an angular application to scripts that operate outside of angular, I am seeking a solution since I cannot modify the code of the angular app. By utilizing Angular Batarang and NG-Inspector extensions in Chrome, I have identi ...

Loading a page in Next.js with Redux before the completion of fetching data actions in getInitialProps

My current issue involves fetching data from an API by triggering a 'getItems' action in getInitialProps on a page (let's call it page A). The data is successfully fetched when I navigate to page A from another page using Link(next/link). ...

How can I make the fullcalendar 'moreLink' popover stay open when clicking outside?

Currently, I am working with FullCalendar v6 on nextjs. One built-in feature that caught my attention is the event popover that appears when there are too many events, accessible by clicking "more." However, I am facing a challenge in preventing this pop ...

CSS to target every second visible tr element using the :nth-child(2n)

My table has a unique appearance (shown below) thanks to the application of CSS nth-child(2n). tr:nth-child(2n) {background-color: #f0f3f5;} I made some elements hidden on the vID, ID, and MO_Sub tr. <tr style="display:none"> The table's new ...

When clicking on the CardActionArea component from Material UI, the React Component fails to render

Encountering a unique problem I've never faced before. Utilizing Material UI components, specifically using CardActionArea in conjunction with Redirect from react-router-dom When the user clicks on the CardActionArea, the intention is to direct th ...

Centering <th> elements is key for achieving a visually appealing print layout

Is there a way to center align the header and body of a table when printing it? I am using datatable. Here is how it currently looks: Check out this screenshot I have tried adding classes to the th tags in the HTML table, but they still appear aligned t ...

In order for each child item within a list to be properly identified in a table component in React, it is crucial that each one

Can anyone help me figure out where the error in the console is coming from? Everything looks fine to me, but I can't seem to pinpoint the issue. Error with Unique Key <TableBody> {announcements.map((announcement, index) => ( <T ...

Swapping out data within a container

I've been experimenting with creating a hangman game using JavaScript and HTML. However, I'm facing an issue where clicking on a letter doesn't replace the "_" placeholder. var myList=["Computer","Algorithm","Software","Programming","Develop ...

Get rid of the strange border on the material dialog

I am struggling with the Angular material 6 dialog component as it is displaying a strange border. I have attempted to remove it using the code below, without success. Interestingly, when I apply the style inline in the browser, it works fine. Any suggesti ...

Automatically close the menu in ReactJS when transitioning to a new page

I have created a nested menu, but I am facing an issue with it. Here is the structure: Parent_Menu Parent1 > Child_Menu1 > Child_Menu2 Parent2 Currently, when I click on, for example, Child_Menu1, I am redirected to the correct page ...

What is the best way to transfer an argument from a parsed JSON value to an onclick function?

In our dataset, we have a specific table that contains valuable information. My main objective is to transfer an argument extracted from parsed JSON data to a separate JavaScript function known as newStory(value['stories']) using the onclick meth ...

Struggling to modify a string within my React component when the state is updated

Having a string representing my file name passed to the react-csv CSVLink<> component, I initially define it as "my-data.csv". When trying to update it with data from an axios request, I realize I may not fully understand how these react components w ...

The functionality of opening a new tab when clicking on a link image is not functioning correctly on Mozilla, whereas it

Check out my code below: <a target="_blank" href="#" onclick="window.open('https://www.google.com','_blank');"> <img src="#{request.contextPath}/resources/img/landing-page/terdaftar-kominfo.png" /> </a> ...

utilizing stateful react elements multiple times

Looking for advice as a newcomer to React. The issue I'm facing involves several component buttons that trigger a modal, where additional selection buttons are available. Home Screen Buttons (components) <Button value="First"></button> ...

Learn how to iterate over an array and display items with a specific class when clicked using jQuery

I have an array with values that I want to display one by one on the screen when the background div is clicked. However, I also want each element to fade out when clicked and then a new element to appear. Currently, the elements are being produced but th ...

Programmatically Expand and Collapse All nodes in MUI DataGrid using ReactJS

Is there a way to programmatically expand or collapse all rows in MUI ReactJS DataGrid? What have I tried? I attempted to use the defaultGroupingExpansionDepth property as suggested in the MUI documentation: export const EXPAND_ALL = -1; export const COLL ...

Steps for incorporating npm-check-update as a customized script in package.json file

Essentially, my code looks like this: "scripts": { "checkUpdate": "npm-check-updates", "updateConfig": "ncu-u" }, However, when I try to run npm checkUpdate, I receive the following output: where ...

Issues arise with AJAX authentication request

Currently, I'm working on incorporating a basic login form with an AJAX request that forwards the user to a page for querying my database. Depending on the results, the user is then redirected to the main index page or prompted back to the login form. ...