Encountering a problem with React components displaying incorrect sizes while utilizing flexbox styling

I am creating a basic test application using NextJS/React.

Take a look at the code snippet below:

The content of Board.tsx file:

import './Board.css';

export default function Board() {
    return <div className="board">
    <Row/>
    <Row/>
    <Row/>
  </div>
} /* End of Board */

function Row() {
    return <div className="row">
    <Cell/>
    <Cell/>
    <Cell/>
  </div>
} /* End of Row */

function Cell() {
    return <div className="cell"/>
} /* End of Cell */

And here is what's inside the Board.css file:

.cell {
  width: 3rem;
  height: 3rem;
  background-color: rgb(255, 255, 0);
  border-width: 2px;
  border-color: rgb(55, 55, 55);
  margin: 11%;
}

.row {
  display: flex;
  flex-direction: row;
}

.board {
  display: flex;
  flex-direction: column;
}

At the top is the app/page.tsx file:

import Image from 'next/image'
import Board from './components/Board'

export default function Home() {
  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      X4C APP
      <Board/>
    </main>
  )
}

This is how the app displays in the web browser when it's running:

https://i.stack.imgur.com/9HoPv.png

Lastly, I have a question. Why do we not see perfect yellow squares even though the css specifies a fixed width and height for the cell component?

After some experimentation, I noticed that removing the following two blocks

.row {...}
.board {...}

from the Board.css file results in perfect squares being displayed.

So, what causes this issue in those two blocks?

Answer №1

The reason for the imperfect squares is due to using a percentage margin (margin: 11%;) for the Cell. Changing it to an absolute value like 10px or 10rem will result in perfect squares.

By default, the flex-shrink value for your cells is set to 1, causing them to shrink if there isn't enough space. Using a percentage margin will consume the available space of the Cell and cause it to shrink.

If you still want to use a percentage margin, you can set flex-shrink: 0; for your Cell to prevent them from shrinking.

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

Resolving the CORS preflight issue with Loopback API in Redux

My current setup involves a client application running on React-Redux and an API app on Loopback. During local testing, I have the client app on port 8080 and the server app on port 3000. While attempting to test Google OAuth (using the loopback-passport ...

Is it possible for JavaScript/React to observe the movement of elements on the webpage?

I'm searching for a solution to detect if an element moves on the screen. Currently, I have an absolute positioned div (acting as a download overlay) that appears when a document is clicked on my website. However, I want it to disappear whenever the d ...

Ways to determine the number of duplicate items in an Array

I have an array of objects that contain part numbers, brand names, and supplier names. I need to find a concise and efficient way to determine the count of duplicate objects in the array. [ { partNum: 'ACDC1007', brandName: 'Electric&apo ...

Having trouble with the onLoadingComplete props in the Next.js Image component?

Is there a way to properly retrieve the naturalWidth and naturalHeight using the onLoadingComplete props? I tried following the documentation on https://nextjs.org/docs/api-reference/next/image#onloadingcomplete but it doesn't seem to be working. Am I ...

Using the React Hook useEffect, we can fetch data by making API calls using axios and utilizing async/await. The API calling

When retrieving data from an API and storing the response in an array using useEffect, it calls the API continuously. let [products, setProducts] = useState([]); async function fetchData() { let response = await axios( `api` ); let it ...

The combination of jQuery, using .load method in javascript to prevent scrolling up, making XMLHttpRequest requests, updating .innerHTML elements, and troubleshooting CSS/JS

While utilizing this code, CSS and Javascript are disabled (only HTML loads): function loadContent(limit) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status ...

Incorporate concealed image into HTML

When I perform actions with a couple of images, I encounter delays because the browser needs to load the images. For example, if I use $('body').append(''); everything works smoothly without any delays. However, when I try using style= ...

Switching the active className in React and Next.js based on selection status

My goal is to dynamically change the className of each Card component when clicked on. When one Card is selected, the others should revert back to their default className. This is my UserBookingData component: const UserBookingData = ({ bookings }: any) = ...

The problem I am facing is with the React Ant d Modal Component, where I am unable to render the

Hey there! I'm currently working with React Js and the 'Ant design Modal Component'. When I click on a button, the modal should open and display the content of each user with their unique ID. However, I'm facing an issue where all modal ...

Using CSS syntax to target a class within an element distinguished by its unique id

Consider the following code snippet: <div id="dogs" class="content">hello</div> <div id="frogs" class="content">hello</div> <div id="hogs" class="content">hello</div> <div id="logs" class="content">hello</div&g ...

What is the best way to achieve full width for text on large screens in Bootstrap 3 while eliminating right margins?

I've been attempting to create a full-width text display on large screens using Bootstrap, but despite utilizing container-fluid and trying solutions from StackOverflow that should enable full width, there still remains whitespace at the right side of ...

Problem with React Native Camera: Camera display is not functioning correctly - React-Native-Vision-Camera Error

Hey there! I could really use some help with a tricky situation I'm facing in my React Native app, specifically regarding camera integration. Here's the scoop: The Issue: I'm working on a video recording application using React Native that ...

Styling Lists: How to Create a Second Line Indent with Counters

There seems to be an issue with the second line indent when using a custom counter. When attempting to use 'list-style-type: decimal', the numbering does not display as desired, for example: ol { counter-reset: ci-counter; list-style- ...

Disable Autocomplete Chip functionality when only one can be selected

When multiple is set to true, I prefer the Chip option. Is there a way to enable the Chip functionality even when multiple is set to false? <Autocomplete className={classes.search} options={top100Films} ge ...

Leveraging getStaticProps in Next.js

I am currently embarking on my inaugural Nextjs project, focused on developing a basic blog utilizing the JSON placeholder API. Strangely, I am encountering an issue where the prop "posts" is being perceived as undefined. Can anyone provide assistance with ...

Z-index problem occurring with rotateY on Safari and Chrome Mobile

https://jsfiddle.net/nxbg7rq3/ I've been struggling to get the .mask completely on top of the .screen in this example. It works fine in most browsers, but Safari and Chrome Mobile are giving me a hard time. I've experimented with various solutio ...

When attempting to launch my application on Heroku, I consistently encountered the following error code: 'ERR_DLOPEN_FAILED'

As a new user on Heroku, I am currently working on building an app with react (Node.js). Even though Heroku confirms that my app has been successfully deployed, the app itself does not seem to be deployed despite showing a deploy success message: "Deploy ...

How to Ensure an Absolutely Positioned Element Occupies the Full Width of its Container

My main content area has a sidebar positioned absolutely to the right side due to various reasons. However, I am facing an issue where if the main content area is shorter, the sidebar extends beyond the content. Do you know of any CSS technique that can h ...

How can I convert base64 data to a specific file type using React Cropper JS?

I have successfully implemented the crop functionality using react cropper js. Now, I am faced with a challenge of sending the cropped image as a file type. Currently, I can obtain the base64 string for the cropped image. However, when I submit the cropped ...

"Encountering issues with React, Webpack, and Material UI styling when deploying to

Attempting to construct a reusable React component with Material UI and then link it using npm to another application. Both the component and application are bundled through webpack. While the application successfully renders the component during developme ...