Tips on adjusting elements to fit every screen size without compromising their positions

I'm currently working on a front-end UI project using React, Next.js, and Tailwind CSS. I've set the design for mobile devices but encountering issues with element placement on smaller screens. Most elements have fixed sizes, and I've tried using percentage width and viewport width to make them responsive without success. My goal is to scale down each element to fit perfectly on small devices and scale up on larger ones. I also attempted applying scaling to the body tag with no luck. If anyone has come across a similar situation or can provide guidance, please help me out.

For reference, here's a link to an example website that demonstrates the behavior I'm trying to achieve: Example Website. Feel free to view it in mobile responsive mode and adjust the viewport for testing purposes.

This code snippet showcases one of the elements:

 <div className="grid grid-cols-3 gap-10 mt-10">
    {/* Characters */}
    <div className="flex flex-col justify-center items-center">
      <div className="rounded-full flex justify-center items-center bg-blue-100 w-[2.75rem] h-[2.75rem]">
        <img src={`/assets/menu/${gameName}/${menu.characters.image}`} alt='menu' className="w-[35px] h-[35px]" />
      </div>
      <p className="text-blue-100 font-semibold mt-1">{menu.characters.text}</p>
    </div>
    {/* Weapons */}
    <div className="flex flex-col justify-center items-center">
      <div className="rounded-full flex justify-center items-center bg-blue-100 w-[2.75rem] h-[2.75rem]">
        <img src={`/assets/menu/${gameName}/${menu.weapons.image}`} alt='menu' className="w-[35px] h-[35px]" />
      </div>
      <p className="text-blue-100 font-semibold mt-1">{menu.weapons.text}</p>
    </div>
    {/* Gears */}
    <div className="flex flex-col justify-center items-center">
      <div className="rounded-full flex justify-center items-center bg-blue-100 w-[2.75rem] h-[2.75rem]">
        <img src={`/assets/menu/${gameName}/${menu.gears.image}`} alt='menu' className="w-[35px] h-[35px]" />
      </div>
      <p className="text-blue-100 font-semibold mt-1">{menu.gears.text}</p>
    </div>
    {/* Map */}
    <div className="flex flex-col justify-center items-center">
      <div className="rounded-full flex justify-center items-center bg-blue-100 w-[2.75rem] h-[2.75rem]">
        <img src={`/assets/menu/${gameName}/${menu.map.image}`} alt='menu' className="w-[35px] h-[35px]" />
      </div>
      <p className="text-blue-100 font-semibold mt-1">{menu.map.text}</p>
    </div>
    {/* Books */}
    <div className="flex flex-col justify-center items-center">
      <div className="rounded-full flex justify-center items-center bg-blue-100 w-[2.75rem] h-[2.75rem]">
        <img src={`/assets/menu/${gameName}/${menu.readable.image}`} alt='menu' className="w-[35px] h-[35px]" />
      </div>
      <p className="text-blue-100 font-semibold mt-1">{menu.readable.text}</p>
    </div>
    {/* Beings */}
    <div className="flex flex-col justify-center items-center">
      <div className="rounded-full flex justify-center items-center bg-blue-100 w-[2.75rem] h-[2.75rem]">
        <img src={`/assets/menu/${gameName}/${menu.beings.image}`} alt='menu' className="w-[35px] h-[35px]" />
      </div>
      <p className="text-blue-100 font-semibold mt-1">{menu.beings.text}</p>
    </div>
  </div>

Answer №1

Success at last!

After some experimentation, I discovered a new approach.

<div className="rounded-full flex justify-center items-center bg-blue-100 w-[10vw] h-[10vh]">

I initially believed that width should be specified with vw and height with vh. However, this was not the optimal solution.

Instead, by adjusting both properties using vw, I achieved the desired result without the need for extra media queries.

<div className="rounded-full flex justify-center items-center bg-blue-100 w-[10vw] h-[10vw]">

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

Using Bootstrap classes within a specified class declaration

Can you nest style classes within a single class? For example: .my-upper-class{ .hidden-md, .hidden-sm, .hidden-lg} ...

Having trouble with CSS :before pseudo-element and sibling selector?

Looking for a CSS solution to change the appearance of a triangle when the .hide class is activated within my markup. I attempted using a pseudo-element like so: summary:before { content: '\25BC'; } summary:before + .hide { con ...

Adjusting the heights of all elements with identical ids simultaneously

I found the following code: <div xmlns="http://www.w3.org/1999/xhtml" style="z-index: 1; position: absolute; top: 90px; left: 90px; background: none repeat scroll 0% 0% black; width: 800px;" id="mainDiv"> <div style="width: 405px;" class= ...

Retrieve the parent element using XPath only if the child element is present

<element_1> <element_2>Text</element_2> <element_3> <element_4> <element_5>Test Text</element_5> </element_4> <element_4> </element_4> ...

The table will remain empty for eternity as the page is being loaded using ngRoute

Currently, I am utilising Angular routers. app.config(function($routeProvider) { $routeProvider .when('/comment/list', { templateUrl: 'commentList.htm', controller: 'mainController' }) }); ...

Is there a way to dynamically adjust a node's rotation using both jQuery and vanilla JavaScript, incorporating the transform property rotate(30deg)?

1. When aiming to support as many browsers as possible, which of the following is the optimal choice?: -webkit-transform: rotate(30deg); -moz-transform: rotate(30deg); -ms-transform: rotate(30deg); -o-transform: rotate(30deg); transform: rotate(30deg); ...

Select items displayed next to each other with varying number of columns in HTML

Users can select a medicine for each day of their plan, with the option to choose up to 7 days. I want to dynamically show a corresponding number of Select dropdowns based on the user's selection. body { width: 500px; margin: 0 auto; ...

Showing information from a SQL database in an HTML format

The PHP code below is used to retrieve data from a MySQL table, but I need assistance in displaying it in an HTML form. <?php $servername = "localhost"; $username = "root"; $password = "icsk"; $dbname = "yusuf"; // Create connection $conn = new mysqli ...

Adjust the height of the parent container using an unordered list

I have a menu in an unordered list and I am struggling to make each <li> the height of its parent. Despite knowing that this is a basic task, I can't seem to figure out where I am going wrong. <div class="menu"> <ul> <li cl ...

Assistance from HtmlHelper and htmlAttributes are valuable in coding

I'm new to MVC 3 and using the Razor view engine. I want to add a custom attribute with a dynamic value to hidden input elements using the Html.Hidden extension method. In HTML5, we can use 'data-' prefixed attributes for this purpose like b ...

Add the information from the tag to the retrieval query path

How can I extract the information from this paragraph: <p id1="p-1-1"> Samsung Galaxy S8 Dual </ p> and insert it into this parameter: <a href="/about?param=HERE" class="btn2"> Learn more </a> The text within the p tag may chan ...

Creating an HTML form that triggers a PHP script to send email upon submission via a Google Cloud Server

While running a php script to send an email upon form submission, I encountered an error message. Here are the details of the error: This XML file does not appear to have any style information associated with it. The document tree is shown below. &l ...

I'm struggling to make the navigation occupy the full space of my flexbox. Currently, it only occupies a small section within the container

I've been struggling with getting the flexbox to fill as I want it to when the code is executed. I'm also looking to make it more responsive to size changes. Despite searching for answers in various resources, I still can't figure out why it ...

How to asynchronously load an image in the background using Javascript and HTML?

While searching for solutions on how to load images in JavaScript, I found numerous topics but none that exactly fit my requirements. Currently, I am loading images in HTML using the typical method: <img src="images/big-image.jpg"> This results in ...

Creating a Bootstrap dropdown: step-by-step guide

I encountered an issue while creating a dropdown in the 'Menubar' using Bootstrap 5.1 in my 'ASP.NET Core 6 MVC' project. The dropdown is implemented as a partial view and the code snippet I used is: <li class="nav-item dropdown ...

css code for styling html elements

HTML: <link rel="stylesheet" type="text/css" href="styles.css"><table border="0" cellpadding="0" cellspacing="0" class="month"> <tr><th colspan="7" class="month">January 2017</th></tr> <tr><th class="mon">Mo ...

Creating an element that remains in a fixed position in relation to its parent container, rather than the entire screen - here's how

Encountering an issue where, upon user scrolling down, a portion of the sidebar becomes fixed in position. However, when attempting to apply CSS for fixing the sidebar element, it ends up fixed to the entire screen instead of just within its parent contain ...

Modify the button's text with jQuery after it has been clicked

I've been struggling to update the text of a button after it's clicked, and I'm encountering an issue where it works in one part of my code but not in another. Could someone please guide me on what might be missing in my code? The first bl ...

Ways to organize two columns of flexible items on a responsive display

Can someone assist me with CSS flexbox for responsive screens? Below is the layout of the flex container and items on a desktop screen: https://i.sstatic.net/AXBHw.png The container has flex and flex-wrap properties applied, with each column having flex: ...

Text hidden within the image, each line concealing a separate message

I am having an issue where my image is overlaying the text. I would like the text to wrap around the image and for any hidden text to appear on a different line. How can this be achieved? Here is the CSS code I am using: div.relative { position: relati ...