Tips for removing gaps between divs on separate lines

Take a look at this awesome code snippet. Can anyone help me figure out how to eliminate the space between lines? I've already attempted removing <br />, but it hasn't made any difference. I also don't want to resort to using negative margin-bottom values. Not sure if this is related to React or CSS.

HTML

<div id="app"></div>

CSS

div.square {
  display: inline-block;
  width: 20px;
  height: 20px;
}

React

class Square extends React.Component {
  constructor(props) {
      super(props);
      this.style = {
          backgroundColor: 'yellow',
          borderStyle: 'solid',
          borderWidth: 1
      }
  }
  render () {
      return (<div class = "square" style = {this.style}></div>);
  }
}

class App extends React.Component {
constructor(props) {
  super(props);
}

render() {
var ret = [];
for (var i = 0; i < this.props.m; i++) {
    for (var j = 0; j < this.props.n; j++) {
        ret.push(<Square></Square>);
    }
    //ret.push(<br />); Commented out line causing spacing issue
}
return ret;
}
}

ReactDOM.render(<App m = "10" n = "10" />,document.querySelector("#app"));

Answer №1

It's important to remember that the class attribute is reserved for creating components in React. When adding CSS class names to JSX elements, make sure to use the className prop instead of class

Update

 render () {
    return (<div className= "square" style = {this.style}></div>);
}

Also update your CSS to

 div.square {
     display:inline-block; 
     margin:0;
     vertical-align:top;
     width: 20px;
     height: 20px;
  }

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

When CSS width:auto expands beyond its intended length

I'm experiencing an issue when I set the width to auto, the div expands to full length rather than adjusting to the width of the inner divs. Here is the HTML code: <div class="vidswraper"> <div> <div class="videoimage"> <img src ...

Error with Typescript types when using Styled Components

After successfully setting up styled-components in react-native, I encountered an issue while trying to use it in a simple example with react-native-web: import * as React from 'react'; import styled from 'styled-components'; export d ...

What is the best way to trigger a download of an external image (AWS signed URL) when a button is clicked?

Currently, I am facing a challenge attempting to download an image from an s3 Presigned URL upon clicking a button in Nextjs (client-side). To provide some context: On the backend, I'm utilizing Golang and on the front end, it's the Nextjs Reac ...

There seems to be an issue with FastAPI not sending back cookies to the React

Why isn't FastAPI sending the cookie to my React frontend app? Take a look at my code snippet: @router.post("/login") def user_login(response: Response, username :str = Form(), password :str = Form(), db: Session = Depends(get_db)): use ...

Inconsistency in naming of jQuery CSS properties

Can you explain the reason behind the difference in property names: $(elem).css('padding-top') compared to $(elem).css('marginTop') ? Wouldn't it make more sense if we use: $(elem).css('margin-top') instead? ...

Bootstrap's hidden navigation bar feature

I noticed that my navbar changes from transparent to opaque when scrolling. I am trying to achieve a similar effect like the one in this example: https://i.sstatic.net/00uPM.png Initially, my navbar looks like this: https://i.sstatic.net/iMyYL.png As y ...

Importing the .css file within a React component for dynamic styling

In my component, I am trying to dynamically load a .css file based on the result of an AJAX call in componentDidMount(). I have attempted adding a <link> element in the render return (which did not work), and also tried injecting the tag directly int ...

Discovering the method to extract dates within a specified range using d3-scale time

I'm struggling to retrieve dates between two specific days. The issue I'm facing is that when using the d3 scaleTime function, it automatically rounds the hours, minutes, and seconds to zero. Is there a way to override this behavior? Currently, ...

Updating subdata in an array using Reactjs handleChange

I am facing an issue with my handleChange function. While I can easily change data in the same directory without any problem, I'm struggling to update sub-data. I would like to find a clean solution for this without having to add extra functions withi ...

Sending texture URL as a prop in react-three-fibre

Recently, I delved into using react-three-fibre and stumbled upon this intriguing example. It showcased loading an SVG image through the component instead of directly in the function. The idea resonated with me as I aimed to reuse the same component while ...

"Declare the Status, Refresh the Page, and Specify the Web

After refreshing the page, I am facing an issue where the text corresponding to the current URL is not being displayed. My application consists of a Main component that renders a MiniDrawer component containing a NavLink, followed by routing with the Route ...

"Is there a way to alter the background color of the second span within a Bootstrap row

I currently have 4 WordPress services in a loop, and I am trying to change the background color of the second service. However, when I try to apply the CSS background property, it ends up affecting all of the services. Here is my template code: <div ...

Media queries do not trigger the execution of CSS code

I am currently working on making a desktop-sized website responsive. However, I have encountered an issue where the CSS rule is being read by the browser but not executed, regardless of whether the media query rule is true or not. I have already included & ...

Styling based on a custom data attribute in CSS

Imagine having a snippet of code in your HTML file that looks like this: <div data-v="20px"> <p>this text repeats itself over and over again</p> </div> The width of the <div> should match the value of data-v, as seen bel ...

The ellipsis menu pops up next to the final video in the series of HTML videos

Currently, I am working on rendering HTML video tags for playing videos. However, I am facing an issue where the three dots menu is not appearing near the button that triggered it, instead, it displays near the last video's three dots. To illustrate t ...

Tips for resolving the 'component uncontrolled' alert in Material UI's TextField:

While attempting to implement TextField from Material UI as a controlled component using React, I followed the documentation's guidance: function SomeFunction (){ const [name, setName] = useState('some value') const handleChange = (e) =&g ...

Ways to personalize an image within a table cell using HTML

I need some advice on adjusting the size of an image that I have inserted into a cell of a table on my html page. Can someone please guide me on how to do this? Thank you! ...

Selecting multiple options with a default value in Material UI Autocomplete

I am facing an issue with my Material UI Autocomplete component that allows for multiple selection and requires default values. The problem lies with the checkbox selection. Even though the names appear in the text field, they are not selected as values. ...

What is the best way to ensure Swiper slides are always autoplaying on React and NextJs without needing to manually hit the pause

I have successfully implemented Swiper sliders on my React component in NextJs. The sliders autoplay but pause after each slide. I am looking to make them autoplay continuously without pausing, similar to this example (https://codesandbox.io/s/swiper-aut ...

Tips on effortlessly updating the URL of your website

Despite seeing the question asked multiple times, I still find myself struggling to understand how to modify a URL or create a new HTML page that seamlessly integrates into a website without redirecting users. I am curious about achieving the functionalit ...