Achieving Left and Right Alignment of Navigation Elements using Flex Basis in Chakra UI with Next JS

For my project, I am working on creating a straightforward Navigation bar using Chakra UI and Next JS. The goal is to have an svg logo positioned at the top-left, while the menu and UI buttons are aligned to the top-right. However, I'm facing challenges with the justify property not behaving as expected, and even including a <Spacer /> component doesn't seem to make a difference despite being present in the DOM.

I have been following tutorials on YouTube, adjusting the display values between none and flex based on breakpoints, but I can't seem to get the positioning of the nav bar elements right.

Currently, all my logo and menu items are aligned to the left, and no matter what I try, their positioning remains unchanged.

UPDATE: I found that specifying a pixel value for margin left works, but using margin-left: auto does not have any effect.

import { useColorMode, IconButton, Flex, Button, Spacer } from "@chakra-ui/react";
import { SunIcon, MoonIcon, HamburgerIcon, CloseIcon } from "@chakra-ui/icons"
import NextLink from 'next/link'
import { useState } from "react"
import styled from "@emotion/styled";
import SiteLogo from "./Logo"


const DarkModeSwitch = () => {
    const { colorMode, toggleColorMode} = useColorMode()
    const [display, changeDisplay] = useState("none")
    const iconColor = {
        light: 'black',
        dark: 'white'
    }

    const navHoverBg = {
        light: 'gray.600',
        dark: 'gray.300'
    }
    
    const bgColor = {
        light: 'white',
        dark: '#171717'
    }

    const color = {
        light: 'black',
        dark: 'white'
    }
    const StickyNav = styled(Flex)`
    position: sticky;
    z-index: 10;
    top: 0;
    backdrop-filter: saturate(180%) blur(20px);
    transition: height .5s, line-height .5s;
    `

    return(
    <StickyNav 
    flexDir="row"
    justifyContent="space-between"
    alignItems="center"
    maxWidth="1000px"
    midWidth="max-content"
    width="100%"
    bg={bgColor[colorMode]}
    color={color[colorMode]}
    as="nav"
    px={[2, 6, 6]}
    py={2}
    mt={8}
    mb={[0, 0, 8]}
    mx="auto"
    >    
        <Flex
        align="center"
        >
        <SiteLogo />
            <Flex
            display={['none', 'none', 'flex', 'flex']}
            justify="flex-end"
            >
                <NextLink href="/" passHref>
                    <Button as="a" variant="ghost" p={[1, 2, 4]} _hover={{backgroundColor: navHoverBg[colorMode]}}>
                        Home
                    </Button>
                </NextLink>
                <NextLink href="/map" passHref>
                <Button as="a" variant="ghost" p={[1, 2, 4]} _hover={{ba...

Answer №1

After discovering that the container holding the items I wanted to align was missing a specified width, everything fell into place once it was set. My investigation into what disrupts margin:auto; led me to the solution.

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

"Utilizing CSS exclusively in conjunction with a PHP API for seamless integration

My CSS is working only when inline, and I'm struggling to track down the reason for this issue. As a beginner in web design, I am unsure of what steps to take to troubleshoot the problem. Since I cannot open the console to check for errors, what facto ...

Using useState as props in typescript

Let's imagine a situation where I have a main component with two smaller components: const MainComponent = () => { const [myValue, setMyValue] = useState(false) return ( <> <ChildComponent1 value={myValue} setValue={set ...

How can I compare the current page URL with the navigation bar?

Looking to compare the URL of a current page to an element in the navigation bar using jQuery. Started by assigning the current URL to a variable: var currentPage = window.location.href; Then utilized an .each function to loop through each item in the n ...

Enhanced auto-zoom feature with orientation change for iOS 7

My web page was functioning perfectly until the release of iOS 7 by Apple today, causing the layout to break when the screen orientation is changed. When focusing on a text area and rotating the screen to landscape view, the entire page zooms in. However, ...

Header with a stable background image that adjusts responsively

While attempting to convert a PSD to HTML, I encountered a small issue. Please refer to the image below: https://i.sstatic.net/5KoIV.jpg As indicated by the red arrow, there is a blue background image in the header of the template! How can I add this bac ...

The SPLoaderError has encountered an issue while loading the component: Unable to load the specified component

While developing a SharePoint app in SPFX Framework, I encountered an issue. When compiling it with gulp build, everything works fine. However, when running gulp serve and adding the app to the workbench, the following error is displayed: [SPLoaderError ...

What is the process of running PHP in a .ctp file within the CakePHP framework?

I have recently started working with CakePHP and I have a question about how PHP code is executed in a file with the .ctp extension. Can you explain how PHP is processed within a .ctp file in CakePHP? Additionally, I'm also curious about executing PHP ...

Having difficulty locating audio files within a Next.js project

I am facing challenges when trying to import my audio files into a redux slice. Currently, I am attempting to retrieve my audio files from the app > public > audio directory. I have made efforts to access my audio files through 2 different director ...

What is the best way to navigate away from the main page of a nested route using react-router?

Can't seem to find any solutions online for this issue. I am looking to have my application automatically redirect from the root route (/) to 'posts'. For example, www.app.com/user/1 should turn into www.app.com/user/1/posts I attempted to ...

Optimizing Safari for the Best Screen Size Experience

I seem to be encountering issues with optimizing my website. Everything appears to be working correctly in terms of widths, heights, margins, etc., on my laptop. However, when I view the site on an iMac or any other computer with a larger screen size, the ...

Would it be possible for me to adjust the CSS to center the sections?

I'm facing an issue with the layout of my website at . At the bottom, there is a navigation menu and three boxes with images. However, they are currently left-aligned instead of centered. Can anyone suggest CSS code that I can use to center them? Her ...

What is the best way to conceal text while retaining icons on a compact screen?

How can I hide the text links for Home, Reservations, My Reservations, About, and Settings on smaller screens while still keeping the icons visible? Currently, I am using the following resources: http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.1 ...

Animate the toggling of classes in jQuery

Here is a code snippet that I came across: $('li input:checked').click(function() { $(this).parent().parent().toggleClass("uncheckedBoxBGColor", 1000); }); This code is functioning correctly when the element is clicked for the first time. I ...

``How can one validate a radio button within a reducer by utilizing the event object that is passed into the reducer process

In my React app, I am using the following reducer: const initialState = { genderRadio : false, ageRadio : false } const reducer = (state = initialState, action) => { switch(action.type) { case "VALI_RADIO_INP": console. ...

Ways to access the new value of a cell in a Material UI Datagrid through the use of GridCellParams

When trying to access the newly modified value in a cell using the GridCellParams interface, I am faced with the issue that the 'value' property only provides me with the previous value of the cell. This asynchronous behavior is not ideal for my ...

Tips for aligning a div below another div nested within a separate container

Can I position div#d2 directly beneath the div#d1 container according to the code below, instead of positioning it outside the height of the wrapper? #wrapper { height: 100%; display: flex; background-color: steelblue; align-items: center; min ...

Learn how to handle internationalization routing in Next.js by utilizing a slash instead of a dash

I manage a website with multiple countries, each country having more than one locale. Below is the configuration from my next-i18next.config.js: module.exports = { i18n: { defaultLocale: 'en-eg', locales: ['en-br', 'en-eg ...

What is the best way to generate a fresh set of data from a search parameter?

I have received data from an API call that needs to be filtered. I am utilizing Redux instead of local state for this task. Below are the actions I have defined: import ActionTypes from '../constants/ActionTypes'; export const passengersDataAc ...

What is the process for incorporating dynamic templates in AngularJS?

I have created 3 unique templates (DetailView, CardView, Column) for displaying content on a single page. Users can easily switch between these views. My goal is to display only one view at a time on the page. When the user switches views, I want to remov ...

Ways to integrate debounce functionality in a React text area field

Implementing debounce in the handleInputChange function of my React component was my latest challenge. I wanted to prevent constant rerenders when a user types in the textarea (I even checked this by logging commentBody), but it seems like things didn&apos ...