Flexbox divs positioned side by side with content wrapping, aligning along the baseline rather than a grid

I am currently working on developing a sudoku solver using ReactJS as a way to improve my skills in the framework. Each square in the grid can either have a single number (classified as 'filled') or up to 9 'notes' that indicate possible numbers for that square.

One issue I am facing is that when a filled square is next to a notes square, the notes squares seem to shift downwards, aligning the first line of their baseline with that of the filled square digit. Ideally, I would like these squares to remain fixed in relation to each other regardless of their content.

My layout is utilizing flexbox for positioning elements.

Despite my efforts to adjust the CSS properties, I cannot figure out why this shifting behavior is occurring. I have tried various modifications but none seem to make a difference.

Below is the structure of the HTML:

<div class='board'>
  <div class='board-row'>
    <div class='square'>
      <div class='number filled'>9</div>
    </div>
    <div class='square'>
      <div class='note'>1</div>
      <div class='note'>2</div>
      <div class='note'>3</div>
      <div class='note'>4</div>
      <div class='note'>5</div>
      <div class='note'>6</div>
      <div class='note'>7</div>
      <div class='note'>8</div>
    </div>
    <div class='square'>
      <div class='note'>1</div>
      ...
    </div>
    ...
  </div>
  ...
</div>

relevant CSS code:

body {
  font-family: "helvetica";
  font-size: 16px;
}

.board {
  display: flex;
  flex-direction: column;
  font-family: monospace;
}

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

.square {
  height: 3em;
  width: 3em;
  border: 1px solid black;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  display: inline-flex;
}

.square .filled {
  font-size: 2rem;
  background-color: #CC0;
}

.square .note {
  padding-left: 3px;
  padding-right: 3px;
}

Here is a snapshot of the issue (red borders highlight the notes - containing ' ')

For image reference, follow this link: https://i.sstatic.net/ohVyw.jpg

The objective is to maintain alignment of the black-bordered squares within a grid layout.

Answer №1

In my opinion, it seems like you should also apply display: flex to your .board-row, rather than just setting the flex-direction (which is redundant since row is the default flex-direction if you want to streamline your code).

Answer №2

The only thing you need to add is vertical-align: top to the .square class.

This solution is effective because display: inline-flex; behaves similarly to display: inline-block.

Answer №3

I came across a solution: try using columns instead of rows.

By updating .board-row to .board-column and eliminating the flex-direction: row from .board-row, I was able to arrange the notes in 3 rows of 3 without the need for 9 wrapping elements.

Although this may not be the optimal solution, I'm documenting it here for future reference.

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

CSS - Maximizing the effectiveness of position:absolute by optimizing for specific screen sizes

With a body width of 800px: body { width:800px; margin:15px auto;} A <div> area (100x200px) is intended to be positioned in the TOP RIGHT corner of the body, regardless of screen size or browser. The code attempted is as follows: #login-hover-cont ...

Consolidate code by implementing on selectmenu

I need assistance with handling multiple select menus on a View page Below is a snippet of the code: $(function() { var selectSpeed = $('#speed'), selectTest = $('#test'); selectSpeed.selectmenu(); selectTest.selectmenu() ...

How can I dynamically remove the sticky class when scrolling down in an Angular 6 application?

My Angular 6 app has a simple navbar with ng-sticky for a sticky effect on desktop, but I don't want the navbar to be sticky on mobile devices. Here is the HTML code: <div class="main-header"> <nav class="main-nav" ng-sticky [offSet]="0" ...

tips for placing an input field and button side by side

Is there a way to align the input and button on the same line using Bootstrap 5? I've tried various methods but nothing seems to be working. Any suggestions on how to style it or utilize specific Bootstrap 5 markup? <link href="https://cdn.jsd ...

Monitoring the parent's CSS attribute in Angular 2

One of my directives dynamically adds the number of pixels corresponding to its parent element's right CSS attribute. import { Directive, ElementRef, AfterViewInit } from "angular2/core" @Directive({ selector: "[d]" }) export class Positioni ...

What is the best way to split an input field into distinct fields for display on the screen?

Take a look at this image: https://i.stack.imgur.com/LoVqe.png I am interested in creating a design similar to the one shown in the image, where a 4 digit one-time password (OTP) is entered by the user. Currently, I have achieved this by using 4 separate ...

Odd behavior from CSS

Running into a bit of a snag with my website lately, specifically with the CSS. As I'm relatively new to web design, these issues tend to crop up more frequently. Usually, I can solve them on my own, but this one has me completely puzzled. What' ...

Preventing Divs from Wrapping in a Floating Layout (Thumbnail)

I have my own website which you can check out by clicking here. I am facing an issue where the text wraps around the image when it is a small image and there is a large amount of text. <div class="page-list-ext-item"> <div class="page-list-ext-i ...

Limit the maximum height of a list within a fluid-width content container

I came across this link The layout works fine when the elements are limited in the #commentWrapper, but adding more items pushes everything downwards. Copying additional <li>test</li> items clearly demonstrates the issue. I want the input box ...

What is the method for updating the value of the Sass variable in Angular 4?

Within the file app.component.scss, there is a variable named $color that has been set to 'red'. What steps can be taken within the file app.component.ts in order to access this variable and change its value to 'green'? ...

Ways to create an oval background for my button

Can someone help me create a button with a unique shape similar to the Products button on stack overflow? Check out this image for reference I attempted using border radius, but it didn't give me the desired effect. I searched for other options but c ...

Expand the width of the image gradually until it reaches its initial size within a flexible DIV container

I am working with a <div> container that occupies 80% of the screen on a responsive page. Within this <div>, I am attempting to display an image that is sized at 400 x 400 pixels. My objective: As the screen width increases: The <div> ...

Position the image before the unordered list item to align with the text

I'm looking to align the image and text horizontally using CSS. Check out my code below: <div class="latest-tweets"> <ul> <li> <p class="tweet-text">This is tweet Text</p> </li> </ul> </ ...

In Vue, when you want to display text after reaching a height of 50px, any additional text will automatically be replaced by five full

https://i.stack.imgur.com/mp0YJ.png >>>>>>>>>Explore Sandbox Example <div style="height:50px"> ...

Unable to get table borders to display correctly using CSS styling

Can someone lend me a hand with a CSS issue I'm facing? I want to create some CSS "classes" for tables in my form, but I can only get the borders to display correctly on one of the tables. Here is the code I've been using: table.paddedTable tabl ...

sidebar that appears upon the initial page load

I'm currently working on implementing a sidebar navigation panel for my website using JavaScript, HTML, and CSS. However, I am facing an issue where the sidebar automatically opens when the page is first loaded, even before clicking on the icon to ope ...

Eliminate the hovering effect from images that are hyperlinked

I am feeling incredibly desperate as I have spent hours searching the internet for a solution with no success. When it comes to text links, I have included the following CSS code: a:hover img { border-bottom: none !important; } However, this code is als ...

Achieving effective targeting of the second segment within a string enclosed in a span tag through increased specificity

When it comes to styling elements, the easiest way is to use classes and assign the properties needed. However, 1. I'm not a huge fan of creating classes all over the place. That's what specificity and CSS mapping are for. 2. Going thro ...

The CSS classes for the dropzonejs are not being properly updated for editing purposes

I'm currently facing an issue where I am attempting to include an editable area inside a dropzone, but for some reason, the editable area is not visible within the dropzone and the CSS classes are not being applied. <a href="#" editable-text="us ...

Height Miscalculation: Chrome and FF encounter window dimension

There is a large application with numerous pages. When I use the console to execute console.log($(window).height()) on any page within the application, it returns the expected result: the height of the window, not the document. For instance: $(window).he ...