What happens in the React tutorial when the clear fix is commented out and the appearance does not change?

Currently, I am working through the React tutorial and have encountered a question regarding their starter code:

class Square extends React.Component {
  render() {
    return (
      <button className="square">
        {/* TODO */}
      </button>
    );
  }
}

class Board extends React.Component {
  renderSquare(i) {
    return <Square />;
  }

  render() {
    const status = 'Next player: X';

    return (
      <div>
        <div className="status">{status}</div>
        <div className="board-row">
          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}
        </div>
        <div className="board-row">
          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}
        </div>
        <div className="board-row">
          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}
        </div>
      </div>
    );
  }
}

class Game extends React.Component {
  render() {
    return (
      <div className="game">
        <div className="game-board">
          <Board />
        </div>
        <div className="game-info">
          <div>{/* status */}</div>
          <ol>{/* TODO */}</ol>
        </div>
      </div>
    );
  }
}

// ========================================

ReactDOM.render(
  <Game />,
  document.getElementById('root')
);

In conjunction with this CSS specification, where I have commented out the .board-row:after directives:

body {
  font: 14px "Century Gothic", Futura, sans-serif;
  margin: 20px;
}

ol, ul {
  padding-left: 30px;
}

/* .board-row:after {
  clear: both;
  content: "";
  display: table;
} */

.status {
  margin-bottom: 10px;
}

.square {
  background: #fff;
  border: 1px solid #999;
  float: left;
  font-size: 24px;
  font-weight: bold;
  line-height: 34px;
  height: 34px;
  margin-right: -1px;
  margin-top: -1px;
  padding: 0;
  text-align: center;
  width: 34px;
}

.square:focus {
  outline: none;
}

.kbd-navigation .square:focus {
  background: #ddd;
}

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

.game-info {
  margin-left: 20px;
}

My confusion lies in the fact that even after removing these directives,

.board-row:after {
  clear: both;
  content: "";
  display: table;
}

which seem to be implementing the "Easy Clearing Method" detailed in All About Floats, the squares remain organized as they were initially, not all in a row. Given that the default display property is inline, my expectation would be for all 9 squares to appear in a single row without utilizing the 'easy clearing method'. Why does the visual layout not change as expected?

Answer №1

In short, utilize Flexbox for your layout design.


Your understanding seems to be off in various areas.

  1. The issue is not exclusive to React, but rather stems from the HTML and CSS implementation you are using. The reference to .board-row:after as a "directive" is actually just a straightforward CSS rule.

  2. Each HTML element has default CSS properties that differ. By utilizing div elements with a default display value of block, they will drop to the next line instead of appearing inline with their siblings.

  3. A few years back, achieving what you are attempting with elements set to display: block involved using floats. However, floats can be cumbersome to work with; one challenge being the need to clear floats by adding an element at the end with clear: both. This led to the popular clearfix trick. Nowadays, Flexbox is the preferred method to achieve your desired layout.

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

Manually initializing Angular bootstrap with async in the Angular script tag

I am encountering an issue when trying to asynchronously download the Angular script in my application and manually bootstrap the application upon loading. The error message states: Failed to instantiate module wt due to: Error: [$injector:modulerr] htt ...

Update the .erb code with Ajax after successfully creating the object

What is the best way to use Ajax in Rails 4 to automatically refresh the code in ".erb" files after successfully creating a new "Box"? The relevant file is located in: views/modifications/show.html.erb <tbody id="boxes_count"> <% @modificat ...

Error: The specified module '/node_modules/.vite/vue.js?v=31b05063' does not export the required 'default' element

I am struggling to find a solution to this problem in my vue3.js project. The console is showing an error related to the import statement in pinia store. I attempted changing the first line of the import to "import Vue from 'vue';" (without curly ...

Is there a way for me to configure parameters from the Rails server for the Transfer List?

I'm currently implementing material ui components in my project. I am trying to populate data from Rails into a Transfer List component. Below are the parameters I am working with: 0: {id: 1, name: 'Rank A', deleted_at: null, created_at: ...

Differences in behavior across operating systems when pasting content copied from Excel into Next.js

Exploring the Issue I am currently working on a project using Next.js 14 where users can paste data copied from an Excel file into a spreadsheet-like component called react-data-grid. However, I have encountered some inconsistencies when copy-pasting on M ...

Passing a parameter from a redirect function to an onClick in React using TypeScript

I am facing a challenge in passing a parameter to my redirectSingleLocker function. This function is intended to take me to the detailed page of a specific locker, identified by a guid. The lockerData.map method is used to display all the JSON data in a ta ...

Discover how to extract the value from the server side during document.ready using jQuery without the need for any specific event

In my project, I'm facing a requirement where I need to fetch a value from server-side code using any event and utilize it to create a dialog box. Initially, I attempted to retrieve the value in window.onload but discovered that window.onload is trigg ...

Can you guide me on how to activate the pickadate.js calendar above the input field?

Encountering an issue with the Pickadate calendar. There seems to be a problem at the bottom of the page, please refer to the attachment for more details. My challenge is that I need to display the Pickadate calendar above the input field when the field is ...

What is the best way to dynamically link an Angular Material table with information pulled from the backend server

I am attempting to connect a mat-table with data from the backend API following this Angular Material Table Dynamic Columns without model. Below is the relevant content from the .ts file: technologyList = []; listTechnology = function () { ...

Trouble locating the ID selector within a nested Backbone view

Within this code snippet, I am utilizing the element controller pattern to display a collection of products. The main view template is rendering properly, showing all elements and the div selector "#cart-wrapper". However, when the main view calls the nest ...

Exploring the Depths of the Internet: Uncovering Data with BeautifulSoup

I am currently working on extracting the rain chance and temperature/wind speed data for each baseball game from rotowire.com. My goal is to organize this data into three columns - rain, temperature, and wind. I have made some progress with the help of ano ...

Tips for uncovering a concealed div within an iframe?

It's as if there's a mystery surrounding how to reach this elusive div... div#someId iframe html>head>body div.wrapper div.footer Naturally, my strategy: #someId iframe body .footer is proving to be ineffective. ...

Utilizing the Filter Feature in a React Bootstrap Table

Just starting out with ReactJS and attempting to implement a filter in a react-bootstrap table. The goal is to display all records in the table when the search text box is empty, but currently nothing is being displayed even when the search box is empty. ...

What is the best method to retrieve the window object in XUL?

I'm attempting to assign an onLoad event to the current webpage through a Firefox extension. My approach involves utilizing the gBrowser object, although I am uncertain if this is the most optimal method. The goal is to establish an onLoad event for t ...

Introducing the new and improved SweetAlert 2.0, the ultimate solution for

I am currently utilizing SweetAlert2.0, known as "This One" <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script> It is functioning properly; however, I would like to display a table, div, or HTML element within this swe ...

Assistance needed in keeping an image with absolute positioning CSS fixed to the top left corner of the browser

Looking for some CSS help as a beginner here! I've been trying to position a transparent PNG image over a centered table, but it seems stuck in the upper left corner of the browser. Absolute positioning should give me freedom to move it around, right? ...

Issue in semantic-ui-react where Menu.Item click causes component to render twice

Exploring react and utilizing semantic-ui-react Menu to construct a SideBarMenu , presenting some intriguing Questions How is the parent component (SBMenu) render function triggered when clicking on a Menu.Item? Normally, the render function in a compo ...

What could be the reason for Angular showing the raw HTML code instead of

I'm currently diving into Angular and encountering an issue where my HTML isn't properly displaying the values I've set. It appears to only show the raw HTML. Here's a snippet from my HTML page: <p>to-do-items works!</p> < ...

Experiencing difficulty in successfully updating a React child component when the prop passed from the parent component is

In the backend implementation, I have successfully set up a socket.io, Node, Express configuration that has been tested to work correctly. It is emitting the right number of broadcasts to the appropriate client using socket.emit("team_set", gameI ...

The installation of AngularJS libraries via npm was unsuccessful

After installing mean.io and running sudo npm install, I followed the commands in sequence: sudo npm install -g meanio mean init yourNewApp cd yourNewApp sudo npm install -g bower sudo npm install The process was supposed to download and install AngularJ ...