Adjust the size of the list items to fit the window as it

My full-width and height div is overlaid by li elements. The li's have a width of 10% and a height of 20vh to create square shapes within the entire div. However, resizing is causing alignment issues for the div.

Despite my attempts using % and vh units, the resizing problem persists. I aim to replicate the design seen on this website:

jQuery(function($) {
  $('.squares li').mouseenter(function() {
    $(this).animate({
      opacity: '0.4'
    });
  }).mouseout(function() {
    $(this).animate({
      opacity: '0'
    });
  });
});
.squares {
  position: absolute;
  top: 0;
  z-index: 99;
  left: 0;
  padding: 0;
  margin: 0;
  width: 100%;
}

.squares li {
  width: 10%;
  height: 20vh;
  list-style-type: none;
  background: none;
  float: left;
  background: #ccc;
  transition: 0.4s;
  opacity: 0;
  cursor: pointer;
}

main {
  width: 100%;
  height: 100vh;
  background-image: url("/images/bg.png");
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
  display: table;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<main>
  <ul class="squares">
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
  </ul>
</main>

Answer №1

This issue may be unclear and the implemented solution is definitely ambiguous. Due to the resizable nature of browsers, it's impossible to create boxes that are both square and a fixed percentage of the browser width and height. The 20vh indicates a goal of achieving 5 rows, while the 10% suggests a desire for 10 columns. Given that users can resize their windows at will, compromises need to be considered. If maintaining a square shape is crucial, then the width and height must be identical. This leaves you with the choice of using either the width, the height, or a combination of both as your unit of measurement. A few tweaks to the CSS can achieve this square effect by utilizing 10vh (10% of the viewport width) for both the width and height.

jQuery(function($) {
  $('.squares li').mouseenter(function() {
    $(this).animate({
      opacity: '0.4'
    });
  }).mouseout(function() {
    $(this).animate({
      opacity: '0'
    });
  });
});
  .body, html {
  padding: 0;
  margin: 0;
}

main {
  width: 100vw;
  left: 0;
  height: 100vh;
  background: red;
  position: absolute;
  display: table;
}

.squares {
  position: absolute;
  top: 0;
  z-index: 99;
  left: 0;
  padding: 0;
  margin: 0;
  width: 100%;
  background: blue;
}

.squares li {
  width: 10%;
  height: 10vw;
  list-style-type: none;
  float: left;
  background: none;
  background: #ccc;
  transition: 0.4s;
  opacity: 0;
  cursor: pointer;
  background: orange;
}

.squares li:nth-child(10n+0) {
  clear: left;
}
.squares li:nth-child(10n+10) {  
  right: 0;
  position: absolute;  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<main>
  <ul class="squares">
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
  </ul>
</main>

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

Switching between javascript objects within the redux store and the application

I am working with a set of JavaScript objects that are retrieved from an external API library. My goal is to save these objects in my React application using Redux. These objects are ES2015 classes that include two useful methods called fromJSON and toJSO ...

What is the method for specifying input type in an HTML document?

I need assistance with controlling an input field labeled "Size" in HTML. The input should only allow users to enter Numbers and specific characters like X, ', and ". Other alphabets and characters should be restricted. I am working with AngularJS as ...

Is there a way to retrieve the value of a dropped file in a file input using jQuery?

Can the data from a dropped file be set in a file upload input field? Or does a dropped file need to be instantly uploaded to the server? Here is an example code snippet: <input type="file" id="drop-box" /> $("#drop-box").on('drop', fun ...

Is there a way to eliminate get variables and filename from a URL using JavaScript or jQuery?

I've been researching this issue, but unfortunately, I haven't been able to find a definitive solution for my specific needs. Let's say I have a URL like... How can I extract this URL and remove the "index.php?search=my+search" part so that ...

The await function does not pause execution before proceeding to the next line of code

Here is the code I've been working on: async function main(collection, token, symbol) { await collection.deleteMany({}); const priceHistory = await makePriceHistoryRequest(token, symbol, slow*2); await insertPriceHistory(collection,priceHistory) ...

Alter the text color once the halfway point is reached at an angle of 175 degrees

I want the text color to turn white when the background color changes to blue after reaching 50% height with a 175 degree angle. Check out this Fiddle .bg { background: linear-gradient(175deg, #e2e2e2 50%, #435289 50%); color:#000; } <div ...

execute a JavaScript script across numerous HTML documents

My situation involves having a directory filled with 1000 HTML files. I need to delete specific nodes from each HTML file using XPath. To streamline the process, I have created a JavaScript script. However, manually opening and running the script through ...

Is there a method to display the Freshservice app on portal pages specifically for Requesters' view?

Here is what I need: 1. The user will input a subject. 2. Based on the subject, I need to make a call to a third-party REST API (Unfortunately, it is currently blocked by CORS and even JSONP requests are not working). 3. After receiving the response, I w ...

Showcasing information retrieved from MongoLab on my site

I am struggling to find a straightforward method to retrieve data from MongoLab and present it using HTML, JavaScript, or JQuery. Despite reviewing their REST API extensively, I still can't grasp how it functions. Can someone offer assistance? This in ...

Tips on handling missing static files when using ExpressJS to host a single page application

When a query from the browser with "/_/*" is made in my application, it is directed to the static folder using the following code: app.use('/_', express.static(path.join(__dirname, '_'))); After that, all GET requests to / are routed ...

What advantages could learning ReactJS first give me before diving into NextJS?

Just mastered TS and now faced with the decision of choosing a framework. I'm curious why it's recommended to learn ReactJS before NextJS. I've read countless articles advising this, but no one seems to delve into the reasons behind it. Ca ...

Create a customized MUI select component with a label, all without the need for assigning an

One issue I am facing is with the Material UI React select component being used multiple times on a page. In the examples, all labeled selects use InputLabel with htmlFor that must match the id of the select. The challenge is that I cannot assign an id t ...

What is the best way to fill in fields on my webpage using a dropdown menu choice?

I'm exploring the world of ASP.NET MVC, AJAX, and jQuery for the first time. I'm attempting to populate some text boxes on my website based on a dropdown selection but it seems like the data isn't getting through. I suspect that the 'ch ...

Finding the Mobile Number value in my CakePHP controller

I created input fields on the View Page as follows: <div class="input text"> <label for="2">First Name (required) </label> <input type="text" value="" style="width: 300px;" id="2" class="required" name="First Name"/> ...

How can I maintain the default function for links that have been clicked using window.on('click') event listener?

I am currently working on a project to visualize the spatial positions of 4673 of the closest galaxies. To enhance the user experience, I have implemented customized click events that allow users to focus on individual galaxies and even change their colors ...

What is the best way to retrieve JSON data in a React application?

useEffect(async () => { const fetchPostData = async () => { const response = await axios("") setPosts(response.data) } fetchPostData(); }, []) Rendering : posts.map(post => <li>{post.name} ...

Trouble with mouseout listener in Wordpress causing Google Map to malfunction

Having trouble integrating a map into my WordPress site using the Google Maps API v3. The issue I am facing is that the listener assigned to the mouseout event is not functioning properly. I have copied and pasted the code from another website where it was ...

Unable to locate a custom definition for TypeScript v3

When I am running my webpack dev server, Typescript is generating this error: ERROR in ./src/components/allowcated-resources/AllowcatedResources.tsx Module not found: Error: Can't resolve 'my-scheduler' in 'mypath\allowcated-resou ...

Updating CSS property names with jQuery: A step-by-step guide

My issue involves a div with inline style "left: Random Number". I have attempted to use various CSS change functions, but none have been successful. The problem lies in the fact that the property of left is random, and I am trying to change it to right. ...

What are some solutions for troubleshooting a laptop freeze when running JavaScript yarn tests?

Running the yarn test command results in all 20 CPU cores being fully occupied by Node.js, causing my laptop to freeze up. This issue is particularly troubling as many NodeJS/Electron apps such as Skype, MS Teams, and Slack are killed by the operating syst ...