using css to pass arguments

I need help with a code that I have, here's the structure:

<div className="site-col-1">
   content 1
</div>
<div className="site-col-2">
  content 2
</div>

Here is how the corresponding CSS file looks...

.site-col-1 {
    grid-column: span 1;
}

.site-col-2 {
    grid-column: span 2;
}

.site-col-3 {
   grid-column: span 3;
}

Is there a way I can simplify this by passing the number as an argument from the div rather than creating individual CSS classes for each? Something like this:

.site-col-ARG {
  grid-column: span ARG
}

Answer №1

To accomplish the same result, you can utilize styled components in combination with React JS.

const StyledDiv = styled.div`
  /* Adjust the grid-column value based on the provided props */
  grid-column: ${props => props.value};
`;

// Within your .jsx file
render(
  <div>
    <StyledDiv value="1">
      content 1
    </StyledDiv>
    <StyledDiv value="2">
      content 2
    </StyledDiv>
  </div>
);

Answer №2

It is partially true that in an HTML file where code is written, you can make use of JS_Template libraries like Handlebars, Mustache, etc. to achieve this.

In React as well, it is possible but only in the file where templates are added with the help of JavaScript (or JSX in case of React).

CSS:

If you are attempting to do the same in a plain CSS file, the answer is a definite NO. It is not achievable in CSS.

SASS:

However, if you are using a CSS preprocessor like SASS, you could utilize loops. You can write something similar to the following example:

@for $i from 1 through 5 {
.site-col-#{$i} {
  grid-column: span #{$i}
}
}

For conversion, refer to: (select to convert SCSS and NOT SASS).

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

Is there a way to align these side by side?

Is it a silly question? Perhaps. But I can't seem to figure out how to align my text and colorpicker on the same line instead of two. Take a look at my fiddle. I've tried removing display:block and clear:both, but that didn't do the trick. H ...

Using Selenium's findElement method along with By.cssSelector to locate elements with multiple classes

I'm a beginner with selenium. Can someone explain how to locate an element that has multiple classes and values? <div class="md-tile-text--primary md-text">Getting Started</div> using the findElement method By .css Selector. ...

Limiting Ant Design Date range Picker to display just a single month

insert image description here According to the documentation, the date range picker is supposed to display two months on the calendar, but it's only showing one month. I carefully reviewed the documentation and made a change from storing a single va ...

Using HowlerJS with React

Has anyone tried using Howler in React before? I'm facing an issue with a simple function (handleRave) that is supposed to toggle music on and off using useState. It seems to work fine for starting the music, but I can't seem to stop the "Rave" a ...

Avoid displaying "undefined" on HTML page when Firebase database value is empty

I am trying my best to explain my issue, although I struggle with scripts. Please bear with me and help me improve. ˆˆ Below is the script I am working on. The goal is to display all records with a date set for 'today or in the future'. Progr ...

The getInitialProps function is never triggered

I am currently working on creating a custom Twitter component that can load tweets from the Twitter API and display them as HTML using a <blockquote> and a <script> tag. An advantage of this approach, especially if implemented on the server si ...

"I can't figure out why my footer keeps showing up at the top of the page unexpectedly

Currently, my footer is showing up at the top of the page but I really want it to be at the bottom. I'm new to HTML so apologies if this is a simple fix. Here is my file with internal CSS: .middle { position: fixed; top: 50%; left: 50%; tr ...

Customizing the footer on various pages using Footer.php

For the past week, I've been working on updating our mobile site and have encountered an issue with the footer. It loads perfectly fine on the home page but crashes when viewing a regular page. Strangely enough, both pages are using the same footer.ph ...

Adding and adjusting the size of different DIV elements within a shared area

Looking to create a dynamic bar with the ability to add multiple child DIVs (similar to this: https://i.stack.imgur.com/tdWsq.jpg). Utilizing jQuery, jQuery UI, Bootstrap, and various plugins. The structure for the div (or span) system could be structured ...

Tips for quietly printing a PDF document in reactjs?

const pdfURL = "anotherurl.com/document.pdf"; const handleDirectPrint = (e: React.FormEvent) => { e.preventDefault(); const newWin: Window | null = window.open(pdfURL); if (newWin) { newWin.onload = () => ...

"Enjoy a unique browsing experience with a two-panel layout featuring a fixed right panel that appears after scrolling

I am facing difficulty in designing a layout with two panels where the left panel has relative positioning and the right panel becomes fixed only after a specific scroll point. Additionally, I need the height of the right panel to adjust when the page scro ...

Embed my React component within a personalized hook

Utilizing React 18, Material UI version 5, and nanostores version 0.7.4. I have developed a custom hook called useApi.tsx which sets up a new axios instance every time the hook is used. Within this hook, an interceptor has been implemented to capture error ...

Issue with the left margin of the background image

I am currently facing an issue with a series of background images that are supposed to fade in and out behind my content. These images are centered, wider than the content itself, and should not affect the width of the page. However, there is an unexpected ...

Using Typescript with React functional components: the proper way to invoke a child method from a parent function

My current setup is quite simple: <Page> <Modal> <Form /> </Modal> </Page> All components mentioned are functional components. Within <Modal />, there is a close function defined like this: const close = () => ...

What steps should I take to resolve the NextRouter "not mounted" error when deploying my Next JS 13 app on Vercel

I am encountering an issue while deploying my Next.js 13 app. The error message states: Module not found: Can't resolve 'encoding' in '/vercel/path0/node_modules/node-fetch/lib' Additionally, I am facing a "Next Router not mounte ...

tool for showcasing data on a webpage with a specific layout

Which chart library is best suited for presenting data in the formats shown in the images below? Can HighCharts handle these formats? Does Google Charts allow for a combination of bubbles and lines? ...

Utilizing Shopify API to seamlessly add items to cart without any redirection for a smoother shopping experience

Looking for a solution to submit an add to cart POST request without any redirection at all? I have tried changing return_to to "back" but that still reloads the page, which is not ideal. My goal is to smoothly add the item to the cart and notify the cli ...

Is there a way to verify the presence of multiple array indices in React with TypeScript?

const checkInstruction = (index) => { if(inputData.info[index].instruction){ return ( <Text ref={instructionContainerRef} dangerouslySetInnerHTML={{ __html: replaceTextLinks(inputData.info[index].instruction) ...

detect mouse click coordinates within an iframe that spans multiple domains

I am currently encountering an issue with tracking click position over a cross-domain iframe. Here is my current code: <div class="poin"> <iframe width="640" height="360" src="http://cross_domain" frameborder="0" allowfullscreen id="video">< ...

Node timers exhibiting unexpected behavior contrary to documented specifications

Feeling a bit lost with this one. I'm running Ubuntu and using nvm for node. I made sure to uninstall the version of node installed via apt just to be safe. node --version > v10.10.0 npm --version > 6.4.1 So, I go ahead and create-react-app a ...