Transform a Linear Gradient in CSS into a Stylish Bootstrap Button in HTML

I've crafted an American football field using only CSS. In my Codepen sandbox, you can find the code (I'm utilizing ReactJS).

What I'm aiming to achieve is to convert each -webkit-linear-gradient into a clickable button that logs its position on the console upon clicking. Each gradient represents a specific field position (there should be 101, including the home and away end zones). See the diagram below for reference.

While I can easily log the end zones using unique spans, I'm unsure how to proceed with the other field positions. Here's the React code:

function App() {
  const printEndzone = (isHome) => {
    isHome ? console.log("home endzone") : console.log("away endzone");
  };

  return (
    <div id="football">
      <span className="endzone" title="home endzone" onClick={() => printEndzone(true)}>
      </span>
      <span className="yard" data-yard="10"></span>
      <span className="yard" data-yard="20"></span>
      <span className="yard" data-yard="30"></span>
      <span className="yard" data-yard="40"></span>
      <span className="yard" data-yard="50"></span>
      <span className="yard" data-yard="40"></span>
      <span className="yard" data-yard="30"></span>
      <span className="yard" data-yard="20"></span>
      <span className="yard" data-yard="10"></span>
      <span className="yard"></span>
      <span className="endzone" title="away endzone" onClick={() => printEndzone(false)}>
      </span>
    </div>
  );
}

export default App;

CSS:

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: #d1d2d3;
}

body > div {
  margin: 20px auto;
}

.middle {
  top: 50%;
  width: 100%;
  height: 1px;
  position: absolute;
  left: 0;
  background: #120930;
  display: block;
}
#football {
  width: 70%;
  height: 420px;
  border: 3px solid #fff;
  position: relative;
  box-shadow: 0 0 55px #000;
  margin: auto;
}
...
...

Example of a field position:

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

Answer №1

To determine the x,y position of the mouse within a pitch range of 0 to 10, you can utilize the code snippet provided below. Once you have obtained this information, you can proceed to modify the onClick function to ascertain the current possession and their precise pitch position based on possession status.

import { MouseEvent } from "react";
import "./styles.css";

function App() {
  const printEndzone = (
    event: MouseEvent<HTMLSpanElement>,
    marker: string | number
  ) => {
    const rect = (event.target as HTMLSpanElement).getBoundingClientRect();
    const x =
      ((event.clientX - rect.left) /
        (event.target as HTMLSpanElement).clientWidth) *
      10;
    const y =
      (event.clientY -
        rect.top / (event.target as HTMLSpanElement).clientHeight) *
      10;
    console.log(x, y);
  };

  const markers = ["home", 10, 20, 30, 40, 50, 40, 30, 20, 10, 0, "away"];

  return (
    <div id="football">
      {markers.map((marker, index) => (
        <span
          key={index}
          className={
            marker === "home"
              ? "endzone"
              : marker === "away"
              ? "endzone"
              : "yard"
          }
          onClick={(event) => {
            printEndzone(event, marker);
          }}
          data-yard={marker !== "yard" ? `${marker}` : ""}
        ></span>
      ))}
    </div>
  );
}

export default App;

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

Blend a background image using rgba without incorporating a gradient effect

Can you merge a background image with an rgba color without resorting to an rgba gradient? My current CSS3 style appears as follows: html { background: linear-gradient(rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.75)), url("../icons/sombrero.jpg")no-repe ...

Tips for ensuring that flex-box children can scroll properly

I'm having trouble getting the content to scroll correctly while dealing with nested flex-box structures. Below is my CSS code: html,body,#root{ width:100%; height:100%; padding:0; margin:0; } .flex-fill{ height:100%; width:100%; ...

Failure of Angular to execute HTTP calls asynchronously

I am feeling a bit perplexed about how and when to use the .then and/or .success functions. Here is a function example: $scope.handleData = function(option){ if(option == 1){ link = firstLink/; }else{ link = secondLink/; } ...

What does [] represent in the context of the [].forEach.call() method?

I'm a bit confused about the meaning of [] in the code snippet below. Is it just indicating the most recently declared array? Can someone provide clarification? In this particular example, we have two arrays named racersList and volunteersList stored ...

Having trouble loading the .php file with my Ajax request

Attempting to retrieve data from the postcode.php file and display it in a #postcodeList div, but encountering issues as nothing happens. Upon inspecting the postcode.php file, it seems to be outputting all the correct information. var loadicecream = do ...

The combination of Next.js and Next-Auth's getSession() function does not function properly within getServerSideProps when used with

I'm experiencing an issue where I can't use getSession() in getServerSideProps with HTTPS. Is this expected behavior? I've tried multiple times to no avail. If I switch to using HTTPS, I encounter the problem of not being able to use getSe ...

Is it possible to display the content below the row of the clicked element when it is selected?

I am currently working on building a team page similar to the layout at My goal is to create a row of elements that float or display inline, with hidden content revealed beneath each selected element, pushing subsequent rows further down. Unfortunately, m ...

Tips for retrieving the return value from a function with an error handling callback

I am having an issue with my function that is supposed to return data or throw an error from a JSON web token custom function. The problem I am facing is that the data returned from the signer part of the function is not being assigned to the token const a ...

In order to log in using ReactJS, it is essential to click the login button

Currently, I have developed a Login page component successfully. Here is the code for my Login component: import React, { useState, useEffect } from "react"; import Axios from "axios"; import useForm from "../components/LoginFo ...

Ways to incorporate fixed CSS into a Dojo 7 application such as FontAwesome

Currently, I have a Dojo 7 (Dojo 2) application that was built using the dojo-cli tool. I am now looking to enhance it by incorporating FontAwesome icons. Luckily, I have a Pro subscription that provides me with a zip file containing various folders of CSS ...

It is important for the CSS :active state to transition to an "inactive" state after a certain period of

For mobile, I utilized the :hover and :active pseudo-classes to create a transition effect when tapping on a tab that enlarges the elements for more information. However, I am seeking a way for the activated element to return to its original state after a ...

Add two columns for mobile devices in the Woocommerce platform

How can I display 2 columns of products in my Woocommerce shop using a child theme based on 'Shopisle'? Is a CSS-only solution the best approach for this task, and will it work smoothly without any bugs? I suspect that the theme is built on Boot ...

There seems to be an issue with the VueJs + ElementUi Change method as it is

Just starting out with Vue and Element UI. I'm attempting to create a custom component using the ElementUI autocomplete/select feature. The problem I am facing is that the @change method does not contain a event.target.value value. When I try to acc ...

Filtering an Array in VueJS Based on User Input

Currently, I am working on a Vue.js application where my goal is to filter an array based on user input from a form. The issue I am facing is that the array named autocomplete is not being populated with visitors that match the query of the first name. T ...

Rearrange elements in an array

Currently, I have an array of sphere meshes and I am looking for a way to position them in a level. The meshes are currently set in a specific position (in the shape of a skeleton), but I would like to move the entire array and place it in a different loca ...

Ways to choose an element when all classes are identical but the text content varies?

I'm looking to automate the website, specifically by selecting the Widgets section and clicking on it. The issue is that all the cards have the same class name, only the text inside them differs. I tried using this code snippet: get getWidgetButton() ...

Staggered Drop-Down Menus

I've created a custom CSS drop-down menu that works well, but I've realized that updating this menu across all the HTML pages on my website would be a tedious task. If I ever need to add a new section and tier to the menu, I'd have to update ...

The absence of the function crypto.createPrivateKey is causing issues in a next.js application

For my next.js application, I am utilizing the createPrivateKey function from the crypto module in node.js. However, I encountered an issue as discussed in this thread: TypeError: crypto.createPrivateKey is not a function. It seems that this function was a ...

Passing conditional empty variables through `res.render`

I am currently facing a challenge with passing multiple variables into a res.render. One of the variables will have an object to send through, while the other may not have anything to pass. As a result, I am encountering an undefined error. Below is the ...

Issue with filtering of values returned by functions

I've been working with Angular's currency filter and I've run into an issue. It doesn't seem to be filtering the data that is returned from a function. I have a feeling that I might be missing something, perhaps it has to do with how th ...