How can you make divs adapt to screen resizing in a similar way to images?

In my REACT project, I am faced with the challenge of positioning three divs next to each other, with a small margin between them, and ensuring they are horizontally centered on the screen.

As the screen width decreases, I need the right-most div to move below the first two while still maintaining horizontal alignment.

Finally, as the screen size shrinks further, all three divs should transition into a column layout at the center of the screen.

While achieving this with images is straightforward:

HTML (using REACT's JSX syntax):

<div id='container'>
    <img src={image}/>
    <img src={image}/>
    <img src={image}/>
<div/>

It becomes more challenging for non-image elements. How can we achieve a similar layout using different objects?

Answer №1

Welcome to the world of coding! I believe what you're searching for is the magic of CSS flexbox. You can learn more about it here: https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox.

#container {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}

.childClass { // Don't forget to assign a className to your children
  width: // Specify the width of your divs here, this will help with wrapping
  flex-grow: 0;
  flex-shrink: 0;
}

I believe following these instructions should solve your problem, but I do recommend delving deeper into flexbox by checking out https://css-tricks.com/snippets/css/a-guide-to-flexbox/ - it's an invaluable resource for mastering flexbox layouts.

Answer №2

//implementing flexbox for organizing HTML elements
.container {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.first {
  background-color: red;
  height: 100px;
  width: 100px;
  flex-wrap: wrap;
}

.second {
  background-color: blue;
  height: 100px;
  width: 100px;
}

.third {
  background-color: yellow;
  height: 100px;
  width: 100px;
}

.flex-item {
  padding: 10px;
  margin: 1rem;
}
<div class="container">
  <div class="first flex-item">First div</div>
  <div class="second flex-item">Second div</div>
  <div class="third flex-item">Third div</div>
</div>

for a detailed guide on flexbox, check out: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

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

Guide on adding multiple entries into a PostreSQL database through NodeJS

In my quest to enhance my ReactJS skills, I've encountered a challenge of adding multiple rows to the database. Currently, I'm using the following code snippet: let request = new Request('http://localhost:3000/songs', { method: &ap ...

Why is it possible for an element to be dragged under a MUI dialog backdrop? Shouldn't the modal backdrop be preventing event propagation in

The issue can be replicated here: https://codesandbox.io/s/serene-blackburn-nrgj0b?file=/src/App.js Typically, when a MUI (Material-UI) Dialog is open, it prevents mouse clicks on components behind the backdrop. However, for unknown reasons, a react-grid- ...

After applying the withStyles and withTranslation higher order components to a React component, a Typescript error is displayed

Trying to create a React component using Typescript, incorporating withStyles from @material-ui/core and withTranslation from react-i18next, both of which are Higher Order Components (HOC). Encountering a typescript error when using them together. Specif ...

Modify the innerHTML to adjust font size when a button is clicked in Ionic 5, or eliminate any unnecessary spaces

I have been experimenting with changing the font size of a variable in .html when the variable contains whitespace. In my .ts page, I use the following code to remove the whitespace: this.contents = this.sanitizer.bypassSecurityTrustHtml(this.product[&apos ...

Encountering an issue with obtaining an undefined value for a URL query parameter when attempting to directly access a

Using the Next.js framework, I have a link in the index.js file (for the client lists page) like this: <Link href={{pathname: '/tickets/[id]', query: {id: rowData.id}}}> <Button icon='pi pi-eye' /> </Link> In the [i ...

The JSX element 'SubscribeCard' does not contain any construct or call signatures

I'm looking to implement the react-subscribe-card module for handling email subscriptions in my react.js project. Below is the code snippet from my popup.tsx file: import React from "react"; import SubscribeCard from "react-subscribe-c ...

SCSS: When hovering over one div, display a different div

My goal is to display the elements in another div when hovering over a specific div. However, my current code does not seem to be working as expected. At the moment, there is no response upon hover and it remains non-responsive. I would greatly appreciate ...

Leveraging Enum with sec:authorize="hasRole(...)" for user authentication in a Spring Boot and Thymeleaf application

Trying to make a change: <div sec:authorize="hasRole('ADMIN')"></div> to: <div sec:authorize="hasRole(${T(com.mypackage.Role).ADMIN.getText()})"></div> However, the above modification is not working. ...

Guide on transforming Div content to json format with the use of jquery

I am trying to figure out how to call the div id "con" when the export button is clicked in my HTML code. I want it to display JSON data in the console. If anyone has any suggestions or solutions, please help! <html> <div id ="con"> < ...

Angular renders HTML elements

Greetings! I am currently experimenting with using AngularJS for the frontend and Wordpress as the backend of my project. To load content from Wordpress into Angular, I am utilizing a JSON wordpress plugin along with making $http requests. The content th ...

Tips for preventing the line through effect on a span element when it is checked

I am working on a project that involves a list of items labeled as li. For example: <ul class="list"> <li class="checked">Groceries <span>&#215;</span></li> <li>Medicine <span>& ...

Parsing of the module failed due to an unexpected character appearing when trying to insert a TTF file into the stylesheet

As a newcomer to Angular, I recently completed the tutorial and am now working on my first app. While trying to add an OTF file, everything went smoothly. However, when I made a change to my app.component.css file and included this code snippet: @font-fa ...

Use .load() to set an image as background in the div

There is a block of code that is supposed to display images in a div with the class "img", but it isn't functioning correctly. Can you provide some guidance on how to fix this issue? <div class="other"><a href="/index1.html">Link1</a&g ...

What is the best way to create vertical separators between CSS grid elements?

I currently have a grid with 3 columns and would like to add two vertical lines to separate the elements. To achieve this, I have placed two span elements between the grid elements using css styling. .vertical_line { position: absolute; height: 100%; ...

Ways to switch visibility based on user's selection

Currently, I am working on a project that involves a select form with multiple options. However, I need to display a specific div only when a particular option is selected. Can anyone suggest the best solution for this scenario? Your guidance would be grea ...

React is inferring the type of the 'charts' property in the object literal as 'any[]'

ide: vscode typescript: 2.7.1 react: 16.3.0-alpha.1 interface IState { numbers: number[]; } class CustomCanvas1 extends React.Component<undefined, IState> { constructor(properties: undefined) { super(properties); this.state = { ...

Having trouble with playing the appended HTML5 Video?

Having trouble getting a video player to display after clicking a button. This is the code I use to add the video player in index.html: $("#videoContainer").append( "<video id=\"video-player\" width=\"100%\" height ...

Guide to adding a tooltip to a "custom layer" in the nivo bar chart

In my React application, I utilized @nivo/bar to create bar charts. I successfully added a line displaying the average value. The tooltip functions properly when hovering over the bars. Now, I am looking to enhance the user experience by adding a tooltip ...

Loading a large quantity of items into state in React Context using Typescript

I am currently working on a React context where I need to bulk load items into the state rather than loading one item at a time using a reducer. The issue lies in the Provider initialization section of the code, specifically in handling the api fetch call ...

Guide on eliminating flex in CSS

Having an issue with aligning two grids created in HTML using Bootstrap. Below is the code: <!DOCTYPE html> <html lang="en"> <head> <title>IRIS Flower Classification</title> <link rel="stylesheet" href="{{ url_for( ...