Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
How to use css gradient using a variable in Reactjs?

I want to use the following css property in the Navlink component. In CSS file , the property can be written as given below which uses gradient.

.ele {
       background-image:
       linear-gradient(45deg, #808080 25%, transparent 25%), 
       linear-gradient(-45deg, #808080 25%, transparent 25%),
       linear-gradient(45deg, transparent 75%, #808080 75%),
       linear-gradient(-45deg, transparent 75%, #808080 75%);

       background-size:20px 20px;    
       background-position:0 0, 10px 0, 10px -10px, 0px 10px;
}

While using in react taking them as a variable tabStyle as shown below, I know that:

  • background-size changes to backgroundSize
  • background-image changes to backgroundImage
  • background-position changes to backgroundPosition

but how can we use linear-gradient?

NavbarJs Code:

import React from "react";
import { NavLink, useLocation } from "react-router-dom";

const NavbarComponent = () => {

let location = useLocation();

let tabStyle = {
   backgroundImage:
   linearGradient('45deg', '#808080 25%', 'transparent 25%'), 
   linearGradient('-45deg', '#808080 25%', 'transparent 25%'),
   linearGradient('45deg', 'transparent 75%', '#808080 75%'),
   linearGradient('-45deg', 'transparent 75%', '#808080 75%');

  }

return (
  <>
    <nav variant="pills" className="navbar" style={{ borderBottom: "1px solid white" }}>
      <ul className="nav nav-pills">
        <li>
          <NavLink style={location.pathname === "/" ? tabStyle:{}} to="/">Home</NavLink>
        </li>
        <li>
          <NavLink style={location.pathname === "/about" ? tabStyle:{}} to="/about">About</NavLink>
      </li>
      <li>
        <NavLink style={location.pathname === "/articles-list" ? tabStyle:{}} to="/articles-list">Articles</NavLink>
      </li>
    </ul>
  </nav>
</>
 );
};

export default NavbarComponent;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

no you don't need to use Camelcase syntax for css functions like linear-gradient just use a simple string

let tabStyle = {
   backgroundImage:
   "linear-gradient(45deg, #808080 25%, transparent 25%)",
   "linear-gradient(-45deg, #808080 25%, transparent 25%)",
   "linear-gradient(45deg, transparent 75%, #808080 75%)",
   "linear-gradient(-45deg, transparent 75%, #808080 75%)";
}

about 4 years ago · Juan Pablo Isaza Relatório

0

It should be just "linear-gradient()" as a string value of backgroundImage:

about 4 years ago · Juan Pablo Isaza Relatório

0

Having a quick look, it looks like there is a syntax error with the styles object you are passing in. Try something like

export default function App() {
  const styles = {
    background: `
      linear-gradient(45deg, #808080 25%, transparent 25%), 
      linear-gradient(-45deg, #808080 25%, transparent 25%),
      linear-gradient(45deg, transparent 75%, #808080 75%),
      linear-gradient(-45deg, transparent 75%, #808080 75%)
    `
  };

  return (
    <div className="App" style={true ? styles : {}}>
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

Works in my codesandbox: https://codesandbox.io/s/optimistic-edison-nqynx?file=/src/App.js

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda