Having difficulty aligning the SVG in the navbar component

Struggling with centering the SVG icon in a sideways navbar using React.js. Despite trying various solutions and researching similar issues, I can't seem to make it work. Any insights on why this is happening? Apologies if the solution is straightforward, as I am still learning React.js.

Below is the code snippet:

import './App.css';
import NavBar from './navbar.js'

function App() {
  return (
    <div className="NavBar">
      <NavBar />
    </div>
  )
}

export default App;

navbar.js


import React from 'react';
import './navbar.css';

function NavBar() {
  return (
    <div className="navbar">
        <a href="/" className="navbar-button">Home</a>
        <a href="/create-language" className="navbar-button">Languages</a>
        <a href="/tutorials" className="navbar-button">Tutorials</a>
        <a href="/community" className="navbar-button">
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" className="bi bi-archive button-svg" viewBox="0 0 16 16">
                <path d="M0 2a1 1 0 0 1 1-1h14a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1v7.5a2.5 2.5 0 0 1-2.5 2.5h-9A2.5 2.5 0 0 1 1 12.5V5a1 1 0 0 1-1-1V2zm2 3v7.5A1.5 1.5 0 0 0 3.5 14h9a1.5 1.5 0 0 0 1.5-1.5V5H2zm13-3H1v2h14V2zM5 7.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5z"/>
            </svg>
            Community
        </a>
        <a href="/about" className="navbar-button">About</a>
        <a href="/contact" className="navbar-button">Contact</a>
    </div>
  );
}

export default NavBar;

navbar.css

.navbar {
    width: 5rem; /* Adjust the width to your preference */
    height: 100%; /* Make the navbar the full height of the viewport */
    position: fixed;
    top: 0;
    left: 0;
    background-color: #F5F5F5; /* Background color of the navbar */
    color: black;
    padding: 10px;
    display: flex;
    flex-direction: column; /* Display the links vertically */
    box-shadow: inset 0 0 10px 2px rgba(0, 0, 0, 0.08);
    justify-content: space-between;
    overflow-y: auto;

  }
  
  .navbar a {
    text-decoration: none;
    color: black;
    padding: 10px;
    font-size: small;
    vertical-align: center;
    
  }
  
  .navbar a:hover {
    background-color: #8a8a8a; /* Background color when hovering over links */
  }

  .button-svg {
    display: inline-flex; /* Make the SVG an inline-block element */
    align-items: center; /* Vertically center the SVG within the line height of the text */
    margin-right: 5px; /* Add margin between the SVG and text */
  }
  

Answer №1

I inserted the svg into a div and applied flex styling to it, enabling me to position it at the center.

navbar.js

  <a href="/community" className="navbar-button">
    <div className="svg-container">
      <svg
        xmlns="http://www.w3.org/2000/svg"
        width="24"
        height="24"
        fill="currentColor"
        className="bi bi-archive button-svg"
        viewBox="0 0 16 16"
      >
        <path d="M0 2a1 1 0 0 1 1-1h14a1 1 0 0 1 1 1v2a1 1 0 0 1-1 1v7.5a2.5 2.5 0 0 1-2.5 2.5h-9A2.5 2.5 0 0 1 1 12.5V5a1 1 0 0 1-1-1V2zm2 3v7.5A1.5 1.5 0 0 0 3.5 14h9a1.5 1.5 0 0 0 1.5-1.5V5H2zm13-3H1v2h14V2zM5 7.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5z" />
      </svg>
    </div>
    Community
  </a>

navbar.css

.svg-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

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 with Bootstrap's "hidden-xs" class not working and struggling to create a sticky footer for small viewports

Trying to tackle the challenge of making my footer stick to the bottom of the page on smaller screens has been quite troublesome. As a temporary fix, I decided to experiment with hiding the div entirely until I figure out a proper solution. The HTML < ...

I'm experimenting with incorporating images into a card component using the map function in JavaScript. All aspects of the card are functioning properly except for the image

import React from 'react' import Card from '@material-ui/core/Card'import CardMedia from '@material-ui/core/CardMedia'; import CardContent from '@material-ui/core/CardContent'; import {makeStyles} from '@materia ...

"To enhance the functionality of your website in Chrome, incorporate Bootstrap affix and

When I add a website name as a prefix, the name is initially hidden. As the user scrolls down, the website name becomes visible, and as they scroll back up, it disappears again. I have implemented this feature using jQuery. $(document).ready(function(){ ...

A miniature triangle-shaped bubble crafted with 1px of CSS3 styling

Currently, I am experimenting with CSS3 and creating bubble shapes. However, I have encountered an issue. I am trying to create a triangle with a 1px border, but the result is a thicker triangle than desired. You can view my code on this fiddle : FIDDLE ...

What is the proper way to input a valid path?

I am facing an issue with the path to the CSS folder in the linked picture. The HTML code is working fine, but the CSS seems to be not loading correctly. I suspect that the problem lies in the path. How can I rectify this? I'm unsure of what steps to ...

Creating a centered and beautifully styled picture with a specific maximum size using React

I recently completed the development of a new website, which can be viewed at Upon inspection of the website, it is evident that the photo is not centered and appears too large on mobile phones. Despite my efforts to align it using various methods outline ...

Unexpected Issues with the Ant Design Sidebar Layout Demo

My React webapp is utilizing the Ant design component framework version 4. I attempted to integrate this example from the Antd documentation into my webapp: https://codesandbox.io/s/ymspt However, when I implemented the code in my webapp, the result didn ...

Autofill Text Input with React Material-UI

For my current project, I am utilizing Material UI and React. Each TextField in the project has a button next to it, and when the button is clicked, I want it to retrieve the value of its corresponding TextField and set that value as the input for another ...

Instructions on adjusting the height of a flexbox container to utilize the available space

I am facing a challenge in grasping the interaction between flexbox containers and other elements on a webpage. When I have only a flexbox on my page, everything works smoothly. However, when I introduce other elements, things start acting strangely. It se ...

React and MaterialUI Chrome Extension - Data did not populate in the table

Currently, I am in the process of developing a browser extension. My main challenge lies in displaying data within a table that has been created using MaterialUI and React. Despite not encountering any errors, everything else renders perfectly. The console ...

What is the best way to trigger a refresh in Next.js React component?

Current Tech Stack Versions Next.js : 14.0.3 React : 18.0.2 // TestClientComponent.tsx "use client"; import { IResident } from "@interface/resident.types"; import { getResidents } from "@models/resident.service"; import { So ...

Placing a division inside another division element

I am currently having trouble containing a color wheel element within the boundaries of its parent div (speech_bubble_middle_bar). I understand that the correct approach is to use absolute positioning within a relative positioned div, so I attempted to cr ...

Choosing an Option Doesn't Show Any Value (ReactJS/Material UI)

I'm having trouble creating a multi-select form control where the selected items are not getting rendered. Even though the handleChange function successfully retrieves the event.target.value, it doesn't seem to update the roleIds state. Additiona ...

Adjustable dimensions and proportions for the following image

I'm currently working on a blog page using next.js and I've encountered an issue with displaying images. Each post has a main image of varying dimensions and aspect ratios, making it difficult to maintain consistency when scaling for smaller scre ...

Tips for adding React components to an array with the help of backticks

Currently, I am attempting to populate an array with icons by extracting the name from data and concatenating "<" and "/>" around it in order to convert it into an Mui Icon. Despite renaming the imported icons to match the names in the data, when I ...

Creating a Bootstrap 5 modal with a dynamic width percentage

Is there a way to adjust the width of a Bootstrap 5 modal to be a percentage of the full screen? This method seemed effective in previous versions of Bootstrap. <div class="modal-dialog modal-dialog-centered per80" role="document" &g ...

Implement a CSS class specifically for products with low stock within the single product page on WooCommerce

I need some assistance with adding a class to 'Low stock' in the code snippet below, as I am not very familiar with php. My goal is to style the Low Stock text in WooCommerce to appear orange using CSS. function change_stock_text( $availability ...

The specified subpath './lib/tokenize' does not match any defined "exports"

As a newcomer to React, I've been facing some challenges while trying to get started. Despite searching on Google and other platforms, I couldn't find a solution to my problem. I was attempting to run code from a YouTube channel called Lama Dev b ...

Enhancing Material-ui Select with custom components

I'm currently attempting to insert a <Button /> within the Material-ui <Select /> component, with the specific requirement of closing the drop-down dialog when the button is clicked. However, I am facing an issue where the button is behavi ...

Ensure that the div and table header remain in a fixed position when scrolling

I have a container with a dropdown at the top and a table at the bottom. I am implementing ngx-infinite-scrolling feature, and I want the container and the table header to remain fixed when scrolling down as more data is loaded. You can view the jsfiddle ...