What is causing the issue with CSS properties and media queries not being effective on specific elements?

I have been experimenting with React and SCSS to create a portfolio page. Although I am familiar with these tools, I recently encountered issues where certain style changes were not reflecting in the browser.

One specific problem involved styling the header element with a nested nav element. Despite adjusting the 'color' property for the h4 and li elements in my code, nothing seemed to change. There are no errors in my SCSS file, and I've tried different browsers and cleared caches to no avail. This roadblock is hindering my progress on this project.

//Here is the react code

export default function Header() {
    return (
        <>
        <header>
            <nav>
                <h4 className="color">
                    <NavLink to='/'>Chidera</NavLink>
                </h4>

                <ul class="nav-links">
                    <li><NavLink to='/'> Home</NavLink></li>
                    <li><NavLink to='portfolio'> Portfolio</NavLink></li>
                    
                </ul>

                <div class="burger">
                    <div class="line1"></div>
                    <div class="line2"></div>
                    <div class="line3"></div>
                </div>
            </nav>
        </header>
        <Outlet/>
        </>
    )
}

//SCSS code

 header {
        width: 100%;
        height: 10vh;
        display: grid;
        place-items: center;

        nav {
            width: 80%;
            height: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;

            h4 {
                font-size: 1.2rem;
                color: gold;
                font-style: italic;
                background-color: #48ff00;
            }

            ul {
                display: flex;
                justify-content: space-between;
                align-items: center;
                width: 40%;
                color: white;
                padding: 0 1rem;

                li {
                    color: white;
                    background-color: #1906cc;
                }
            }

            .burger {
                cursor: pointer;
                display: none;

                div {
                    width: 25px;
                    height: 3px;
                    background-color: rgb(221, 168, 90);
                    margin: 5px;
                    transition: all 0.3s ease;
                }
            }
        }
        
    }

I also noticed that some media queries weren't working as expected, such as displaying the burger element when the device width is below 800px. This issue persists even though there are no errors in my SCSS code. It seems strange that changing the background color of the main element worked, but toggling the visibility of the burger did not, despite following the same nesting structure as in the React component.

@media screen and (max-width: 768px) {
    main {
        background-color: pink; //works
    }
    .burger {
        display: block; //does nothing
    }
}

https://i.stack.imgur.com/gogVV.png

https://i.stack.imgur.com/aOzAw.png

The developer's tools indicate that the color property has been recognized in the second image, yet the changes do not appear on the actual page. Additionally, while the main background color successfully changed, the burger element remains invisible.

I hope this issue isn't related to SCSS, as I prefer not to revert back to plain CSS. Your assistance is greatly appreciated.

Thank you.

Answer №1

NavLink and Link from 'react-router-dom' come with their own default styles that cannot be overridden by the CSS properties of h4 and li. To directly modify the styling of NavLink and Link, you will need to target them specifically.

The first step is to remove the color property from h4 and li elements:

<h4 className="color">
     <NavLink to="/" className="colorGold">Chidera</NavLink>
 </h4>

CSS

.colorGold {
  color: gold;
}

Styling for the li element:

<ul className="nav-links">
      <li>
           <NavLink to="/" className="listColor">Home</NavLink>
      </li>
      <li>
           <NavLink to="/portfolio" className="listColor">Portfolio</NavLink>
      </li>
</ul>

CSS

.listColor {
  color: white;
}

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

Why is it beneficial to integrate a state management library in a server-rendered application?

While working with NextJS to serve a SSR application, I have come across recommendations to use a state management library. In my experience, I am accustomed to using a state management library for client-side rendered applications, but I am unsure of it ...

Comparing Yii's CHtml::link() function with the use of regular <a href=''></a> HTML tags

When using elements like yii CHtml::link, what are the recommended practices? I'm working on a button that needs to include an icon, text, respond to hover events, and be wide. Are there any benefits to using CHtml instead of a regular tag that can e ...

Utilizing InputProps in the TextField component for efficient <input /> element usage

Can InputProps be utilized in the <input /> element rather than <TextField /> from Material UI? This is my Input.js component where I am implementing InputProps: const Input = ({name, handleChange, type, handleShowPassword}) => { retur ...

Material-UI: The call stack has exceeded the maximum range, causing an Uncaught RangeError

I am currently utilizing the Dialog and Select components offered by Material-UI in conjunction with React. Here is a quick example: import React from 'react'; import { Dialog, MenuItem, Select } from '@material-ui/core'; class Examp ...

What is the best method for capturing the error message: "Warning: [react-router] Location "/non-existent-route" does not correspond to any routes"?

Currently, I am developing a server-side rendering application and utilizing react-router's match({}, function callback(error, redirect, props){}). In situations where the given URL does not match any routes, I would like to allow the next middleware ...

CSS struggles after implementing conditional checks in return statement for an unordered list

I'm encountering a CSS issue with the header section. When I include the following code snippet in my code to display a tab based on a condition, the entire header list doesn't appear in a single horizontal line: <div> {isAdmin(user) ? ...

Automating the execution of `npm run dev` for a React Vite application running within a Docker environment

I currently have a setup where I am running a Vite React app with a Flask API inside Docker using WSL 2 and docker-compose. However, I find myself manually executing certain commands in order to access my React app in the browser: docker exec -it vite_dock ...

Tips for rendering the title based on the specific ID provided by the API in nextJS

There is an issue I am facing. I only want to show the "title" based on the ID, but currently all the "titles" are showing up at once. You can see it in the image below: https://i.stack.imgur.com/89D6L.png Here's my code: const getData = () => { ...

Incorrect Reactjs installation technique

When I try to run create-react-app on my Windows PC, only dependencies are being installed with no folders other than node_modules. Even when using Yarn, I haven't been able to progress further. Please assist and thank you in advance for any help. Thi ...

The webpage is missing a rendered React component even though it should be displayed

I am facing an issue where a React component is not appearing on the webpage despite being rendered. I have provided the code and screenshots of the components below for reference. Below is the snippet from the "App.jsx" file: function createCard ...

What is the best way to dynamically assign a value to a prop of type PropTypes.node.isRequired in a Jest Enzym

When writing Jest tests for a React component using Enzyme, passing values to props for snapshot testing is crucial. So how can we achieve this? Foo.propTypes = { children: PropTypes.node.isRequired, text: PropTypes.string.isRequired } In our Foo ...

How can the caption be aligned to the right of the image within a Bootstrap thumbnail in a precise manner, while also ensuring the button is positioned at the bottom of the caption?

Greetings! I am in the process of developing a website using bootstrap v3.3.2. My first query revolves around utilizing the grid system to correctly position the caption on the right side of the thumbnail, as illustrated below: <div class="container"& ...

Accessing and manipulating web elements using either XPath or CSS selectors

Having trouble selecting a specific piece of information using xpath or css selector. Error messages keep appearing. Can someone help identify the issue? This snippet is from my code: output = driver.find_element_by_xpath("//td[@class_= 'sku']" ...

Deactivate a input field depending on an autocomplete selection in React.js using Material-UI

Currently, I am facing an issue with two fields on my form. The first field is an autocomplete feature while the second field is a textfield. By default, my second field is disabled but I need it to become enabled every time a user types and selects an opt ...

Jest tests are failing to render React IonDateTime component

While executing Jest on an Ionic React component, I encountered a test failure consistently, regardless of whether the component had a time value or not. test('IonDateTime display', () => { render(<IonDatetime data-testid="foo" ...

The presence of concealed cells within an HTML table is leading to an increase in the

I am currently working on an HTML Table that serves as a summary table for displaying a list of items. The table has 4 visible columns out of approximately 20, with the remaining columns set to display: none;. However, the hidden cells are causing the rows ...

Is there a way to prevent a page from rendering until the necessary data is retrieved?

I am facing an issue where my page is attempting to render before the data is available. I have used async/await in my code, but I keep getting an error saying that the data is undefined. Interestingly, when I comment out the page elements and check the Re ...

Adjust the border color of a TextField in react/material ui based on whether props are required

If a value is required, the TextField border color will be red. Once a value is entered, the red border will disappear. Check out the images by clicking on the links below: Entered Value Without Value with Required field ...

Setting up TLS for NextJS based applications with @improbable-eng/grpc-web

Currently, in the process of developing my web application using NextJS, I have incorporated https://github.com/improbable-eng/grpc-web for facilitating communication between frontend and backend. The React element below exemplifies the utilization of grpc ...

What steps can be taken to modify the border color of a Material-UI Box/Tab component to display in black?

I'm currently working on a React app where I am using the Box and Tab component from Material-UI. My goal is to change the border color of the Box component to black while still maintaining its function as a divider. Despite trying to set the borderCo ...