Expanding Soccer Field to Maximize Viewing Experience- React/CSS

I'm looking to make a soccer pitch design in CSS that can adjust its size based on the device screen, making it responsive and reusable for both web and native app development (specifically with React).

Currently, I'm stuck trying to determine how to maximize the CSS width while maintaining the original dimensions of the soccer pitch on different devices. Any suggestions on how I should modify the width and height properties?

html {
  --green: #4c7;
  --light: #5d8;
  background: var(--green);
}

body {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 90vmin;
  height: 60vmin;
  border: 0.5vmin solid white;
  background-image:
    radial-gradient(circle at 0% 0%, transparent 1%, white 0 1.5%, transparent 0),
    radial-gradient(circle at 100% 0%, transparent 1%, white 0 1.5%, transparent 0),
    radial-gradient(circle at 0% 100%, transparent 1%, white 0 1.5%, transparent 0),
    radial-gradient(circle at 100% 100%, transparent 1%, white 0 1.5%, transparent 0),
    radial-gradient(circle at 50% 50%, white 1%, transparent 0),
    radial-gradient(circle at 50% 50%, transparent 14%, white 0 14.75%, transparent 0),
    linear-gradient(to right, transparent 49.75%, white 0 50.25%, transparent 0),
    repeating-linear-gradient(to right, var(--green) 0 10%, var(--light) 0 20%);
}

Answer №1

If you want to adjust the height and width of a pitch based on the aspect ratio of the viewport, consider using the CSS media query aspect-ratio feature.

By utilizing this method, you can set the height of the pitch to a maximum value (100vh) and calculate the width based on the aspect ratio of 9/6 times 100vh, or vice versa depending on the viewport's aspect ratio.

Included in this code snippet is a slight border adjustment using 95vh/vw to create some extra space around the edge for visibility. It introduces a 'pitch' class that positions the element within a div container instead of directly within the body, offering more flexibility for layout purposes.

html {
  --green: #4c7;
  --light: #5d8;
  background: var(--green);
}

.pitch {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 95vw;
  height: calc((6 / 9) * 95vw);
  border: 0.5vmin solid white;
  background-image: radial-gradient(circle at 0% 0%, transparent 1%, white 0 1.5%, transparent 0), radial-gradient(circle at 100% 0%, transparent 1%, white 0 1.5%, transparent 0), radial-gradient(circle at 0% 100%, transparent 1%, white 0 1.5%, transparent 0), radial-gradient(circle at 100% 100%, transparent 1%, white 0 1.5%, transparent 0), radial-gradient(circle at 50% 50%, white 1%, transparent 0), radial-gradient(circle at 50% 50%, transparent 14%, white 0 14.75%, transparent 0), linear-gradient(to right, transparent 49.75%, white 0 50.25%, transparent 0), repeating-linear-gradient(to right, var(--green) 0 10%, var(--light) 0 20%);
}

@media (min-aspect-ratio: 9/6) {
  /* this means that we have to fit the height to 100vh and the width must adjust accordingly */
  .pitch {
    padding-bottom: 0;
    height: 95vh;
    width: calc((9 / 6) * 95vh);
  }
}
<div class="pitch"></div>

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

What is preventing a nested flexbox from expanding to the entire width?

Here's an example of some HTML/CSS code: <div style="display: flex; background-color: yellow;"> <div style="display: flex;background-color: lightblue;">i am a NESTED flexbox</div> </div> This results in https://i.sstati ...

The calculator I designed using HTML, CSS, and JavaScript is experiencing difficulty adjusting to various screen sizes

I recently built a calculator using HTML, CSS, and JavaScript. It works perfectly on PC or big screens, but when viewed on smaller devices like phones or tablets, the display gets cut off and does not adjust properly. Here are some example pictures for ref ...

Should a checkbox be added prior to the hyperlink?

html tags <ul class="navmore"> <li><a href="link-1">Link 1</a></li> <li><a href="link-2">Link 2</a></li> </ul> Jquery Implementation in the footer $(".navmore li a").each(function(){ v ...

Utilizing the CSS :not() selector within a nested table

I am having an issue with my AjaxControlToolkit where the main-page CSS is affecting the inner "calendar" popup. To illustrate what I need, what I've tried, and the temporary workaround, I have simplified everything into a one-page example. I am in s ...

Add the Load More feature to your Next JS project for enhanced user experience

Utilizing the SWR package along with Next JS, I have successfully retrieved data and displayed it in a table. Now, my goal is to implement a "load more" type of pagination for the listing page. Below is the code snippet: Locations component: import useSW ...

Tips for customizing video layout using HTML

I currently have a basic video displayed on my website <video width="100%" class="posted_vid"> <source src="uploaded_videos/<?php echo $Video; ?>"> </video> However, the video player appears as a default HTML video, simila ...

Exploring the gridview with JQuery to iterate through and verify if any checkboxes have been selected

I am currently working on a jQuery-based application. In this application, I have an ASP.net GridView that contains checkboxes in each row. My goal is to determine whether any of the checkboxes are checked or not. Below is the code snippet where I loop thr ...

Tips for refreshing a specific div element at set intervals using JQuery AJAX?

I need to make updates to a specific div element without refreshing the entire HTML page. The code below currently works, but it reloads the entire HTML page. Additionally, I am working with different layouts where I have separate files for the header, l ...

Utilizing Javascript to export modules and call multiple functions simultaneously

Is there a way to automate the calling of all functions within a module instead of individually selecting each one? For instance: bettermovies.js module.exports={ printAvatar: function(){ console.log("Avatar"); }, printLord: funct ...

Repeating background images in CSS

Hey there! I'm having a little trouble with my background image in HTML. It's showing up multiple times in a row and despite searching online, I can't seem to find the right solution. Here is the code snippet from my file: <!DOCTYPE html& ...

Error occurs when JSON.parse is used

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> var data = "{ 'name': 'John' }"; var result = JSON.parse(data); </script> ...

Using jQuery to select the third element from every group of six

I am attempting to select the 3rd .foo element out of every set of 6. To clarify, here is a brief example: 1 2 3 (this) 4 5 6 1 2 3 (this) 4 5 6 and so on... So far, I have only managed to target every 3rd element, which is not exactly what I need beca ...

Encountering issues while running the npm build command due to exporting async functions in React

In my React project, I am utilizing async functions and have created a file named apiRequest.js structured like this: const axios = require('axios'); const serverURL = "http://localhost:8080" getInfo = async function ({email}) { try { r ...

Enhance the information within the textextjs plugin by incorporating additional data

I am attempting to invoke a function within the textextjs method- //my function function GetAreaTags() { return "some text"; } //textext initialization $('#territory').textext({ plugins: 'tags prompt focus autocomplete ajax', ...

Creating a Mithril.js Single Page Application (SPA) using a JSON data source: A

I am currently working on creating a single page application (SPA) using Mithril.js. While I have come across some helpful tutorials like the one here and on the official Mithril homepage, I am struggling to combine the concepts from both sources effective ...

Developing a personalized Object3D class

Having a background in AS3/Away3D, I am new to THREE.js and I am trying to create a custom object class that extends THREE.Object3D for my scene. CustomObject will contain behavioral properties and methods, with each instance having its own data object det ...

Direct AngularJS to automatically reroute users from the login page to the welcome page

I am currently developing a web application where I need to redirect from the login page to the welcome page once the user id and password have been validated. <script> var app = angular.module('myApp', []); app.controller(&apo ...

Warning: The utilization of a domain property in MakeCallback is no longer supported and may result in

Encountering an error while making a request. (node:3993) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead. Here is the code for my ...

Warning: HTML input values are being cleared when added

I've been working on some code that adds an input field when a button is clicked. When the limit reaches 5 (counter), it displays a bootstrap warning. The issue I'm facing is that after hitting "add field" more than 5 times, the values in the fie ...

Transform this color matching game into an image matching game using JavaScript and jQuery

I have a color matching game that I would like to enhance by matching background-images instead of just background-colors. However, I am facing difficulties in making this change. For instance, instead of matching the color red with the text "red," I wan ...