Using grid-template-areas in ReactJS function components does not function as expected

REMINDER: Check out and customize the code in CodeSandbox.

This is a parent component with 5 children elements. Among them, 3 are React components and the remaining 2 are regular HTML buttons:

import "./App.css";
import React from "react";
import FormField from "./FormField";

export default function App() {
  return (
    <form className="Form">
      <FormField className="name" title="Name" type="text" isRequired={true} />
      <FormField className="salary" title="Salary" isRequired={true} />
      <FormField className="age" title="Age" type="text" isRequired={true} />
      <button className="submit">Submit</button>
      <button className="update">Update</button>
    </form>
  );
}

The CSS styling for the parent (App.css) includes the grid-template-area property as shown below:

.Form {
  display: grid;    
  grid-template-areas:
    "name name"
    "salary salary"
    "age age"
    "submit update";
  gap: 20px;
}
.name {
  grid-area: name;
}
.salary {
  grid-area: salary;
}
.age {
  grid-area: age;
}
.submit {
  grid-area: submit;
}

.update {
  grid-area: update;
}

Expected layout based on grid areas:

https://i.sstatic.net/WJ9QR.png

Actual output differs:

https://i.sstatic.net/5253y.png

What could be causing the issue that doesn't align my grid with the specified areas?

Answer №1

To create a structured layout, enclose your FormFields within a div that has the grid area class applied to it. Remember that the FormField only adds the specified class to the input element, not to itself.

<form className="Form">
  <div className="name"><FormField title="Name" type="text" isRequired={true} /></div>
  <div className="salary"><FormField title="Salary" isRequired={true} /></div>
  <div className="age"><FormField title="Age" type="text" isRequired={true} /></div>
  <button className="submit">Submit</button>
  <button className="update">Update</button>
</form>

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

Changing the display of elements using Javascript in IE6 by modifying class

Currently, I am facing an issue at work that involves a piece of code. The code functions correctly in Firefox 3.6 where clicking on a row changes its class name and should also change the properties of the child elements. However, in IE6 and possibly othe ...

The Firebase storage percentChanges() method is throwing a NaN error

I want to create a Firebase storage service using an Angular client to handle user profile image uploads. This service should return two observables: uploadProgress$ and downloadUrl$. The uploadProgress$ observable will store the value from percentChanges ...

how to display ajax response in webpage using jQuery

I need to perform multiple ajax calls in a for loop, with each call returning a text/html response that needs to be printed. Here is the code I have implemented: function printBill(printBills, lastBillNo, type,taxType,outletId,date){ var printableObjects ...

Unable to send a function as props to a child component in TypeScript

Within my application, I have a parent component that holds a state and a setState function. This state and function are then passed down to a child component in order to retrieve values (such as input field data). When the function is triggered in the chi ...

The React useEffect hook runs whenever there is a change in the state

Within my React component, I have the following code snippet (excluding the return statement for relevance): const App = () => { let webSocket = new WebSocket(WS_URL); const [image, setImage] = useState({}); const [bannerName, setBannerName] = use ...

Retrieve data from HTML in order to apply styling to an element

I am attempting to set the width of my .bar-1 outer span element based on the value of my inner span element. <span class="bar bar-1"> <span name="PERCENTAGE" id="PERCENTAGE" disabled="" title="Total Percentage" maxlength="255" value="66" tabinde ...

CSS - changing images for the menu based on user interaction

I'm in the process of designing a website and I have a unique challenge - I need to figure out how to create a dynamic triangle below an item on the menu bar using CSS. The catch is that the horizontal position of this triangle will shift based on wh ...

The content on the right side is spilling over my footer and causing

I am struggling to understand why the content in the right column is overlapping my footer. I believe it has something to do with a float, but I can't seem to pinpoint the exact issue. Could you please take a look at this page for me and provide some ...

What is the best way to convert a locale code into a language name with i18next?

In the application I'm working on, there is a language selector feature that allows users to choose the display language for the app. Currently, we only have limited options available, but there are plans to expand the list in the future. My goal is t ...

Encountering a hiccup while trying to retrieve information from a JSON

I am currently working on a Jquery Drop Upload form and everything is functioning well. However, I am encountering an error when trying to retrieve data from the database using JSON. I'm not sure why this error is occurring, so please see below for mo ...

Troubleshooting the Discrepancy Between console.log Array and ReactJS State

I am currently working on a CompanyAddress component that deals with the addresses of companies. Each address is represented as an object with two fields: address and metro. class CompanyAddress extends React.Component<Props, State> { constructo ...

What is the best way to spin an element around its center?

I'm faced with a challenge where I have an element that needs to be rotated using JavaScript. The rotation is currently functional, but it's rotating around points other than its center. My goal is to rotate the element around its own center. ...

Creating a cohesive "notification" feature in React with TypeScript that is integrated with one specific component

I am currently working on designing my application to streamline all notifications through a single "snackbar" style component (utilizing the material UI snackbar component) that encompasses the entire app: For instance class App extends React.Component ...

Personalized bar graph description using Highcharts

Looking to create a unique stacked Highcharts bar chart with custom text within each bar, but currently only seeing the data number displayed by Highcharts. Check out the fiddle here. Here's the code snippet: $(function () { $('#container& ...

Tips for preventing unstyled content flash when updating CSS files dynamically

The following jQuery function is used to replace CSS files: function UpdateTheme(theme) { var links = $("link"); var _t = theme; $.each(links, function (i, link) { var _h = $(link).attr('href'); _updatedHr ...

Utilizing Vue.js: Dynamically Rendering Components Based on Routes

I needed to hide some components for specific Routes, and I was able to achieve this by using a watcher for route changes that I found in this StackOverflow question - Vuejs: Event on route change. My goal was to not display the header and sidebar on the c ...

Using jQuery Datepicker, showcase two distinct datepickers on a single page

Currently, I am utilizing the jQuery [Datepicker][1] on a website, but I am facing challenges when attempting to display two datepickers on the same page in different manners. The first one should only appear once you click the text box, and once a date i ...

Retrieving information within a loop through the utilization of a left join操作。

Presently, I am utilizing a while loop to fetch user comments from a MySQL table and applying a conditional class to the buttons within the comment div. Each comment contains two buttons: thumbsup button thumbsdown button I aim to assign the class name ...

Maintaining the style of `li` elements within a `ul` list while ensuring they all

Struggling to create a menu list item with 4 items that won't fit on one line? Nested divs didn't work when adding padding. Here is my latest HTML and CSS: HTML: <div id="header"> <ul id="menu"> <li><a href="#"& ...

Angular directive ceases to trigger

I am currently working on implementing an infinite scrolling directive. Initially, when the page loads and I start scrolling, I can see the console log. However, after the first scroll, it stops working. It seems like it only triggers once. Can anyone poi ...