Tips for positioning two `<div>` elements side by side, each with equal width

I am working on a section of my website and I want to align the contents inside a specific div horizontally with equal width. However, for some reason, my CSS code is not achieving the desired result.

const Reasons = ()=>{
    return (
        <div className="reasons" id='reasons'>
            <div className="left-r">
                <img src={image1} alt="" />
                <img src={image2} alt="" />
                <img src={image3} alt="" />
                <img src={image4} alt="" />

            </div>
            <div className="right-r">
                Right Side
            </div>
        </div>
    )
}

The CSS code:

.Reasons{
    padding: 0 2rem;
    display: flex;
}

.left-r{
    flex: 1 1;
    flex-direction: column;
}
.right-r{
    flex: 1 1;
    flex-direction: column;
    background-color: var(--orange);
}

Although I expected the content to be displayed flexibly in a horizontal manner, it seems to be showing up vertically instead.

Here is a screenshot of the webpage

Answer №1

I made corrections to the class name (changed it to .reasons instead of .Reasons), added a width property to the images for horizontal alignment, and included code to adjust the height if the images have varying aspect ratios.

*{
    margin: 0;
    padding: 0;
}

.reasons{
    padding: 0 2rem;
    display: flex;
    align-items: center;
}

.left-r{
    flex: 1 1;

    /* Add these codes if the images have different heights. Remove them if the heights are identical */
    height: 100px;
    overflow-y: hidden;
    display: flex;
    align-items: flex-start;
}

/* Set image width for horizontal layout */
.left-r img{
    width: 25%;
    height: auto;
}

.right-r{
    flex: 1 1;
    background-color: var(--orange);            
}
<div class="reasons" id='reasons'>
    <div class="left-r">
        <img src="https://picsum.photos/200/400?random=1" alt="" />
        <img src="https://picsum.photos/100/300?random=2" alt="" />
        <img src="https://picsum.photos/250/350?random=3" alt="" />
        <img src="https://picsum.photos/220/380?random=4" alt="" />
    </div>

    <div class="right-r">
        Right Side
    </div>
</div>

Answer №2

Consider implementing this solution in the CSS stylesheet, as I believe the issue stems from an error in the class name syntax. Avoid including any flex-related styles for the two inner divs within the primary div:

.reasons{
    padding: 0 2rem;
    display: flex;
    justify-content: center;
}
.right-r{
    background-color: var(--orange);
}

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

How to center a div both vertically and horizontally when height is not specified?

Is it possible to center a div on a webpage without specifying a fixed height, so that the height adjusts dynamically based on the content? I am willing to consider JS/jQuery solutions with fallback options but would prefer a pure CSS approach. Currently, ...

The CSS content spills beyond the edge of the screen

I am facing an issue with aligning my content in the center as it is currently shifting to the right side and going off the screen. Below is the code snippet I am working with: <?php /* Template Name: About Us */ $aboutheading = get_field('abou ...

Styling elements in CSS to position the form button outside of the table enclosed within the form

I'm attempting to position a form button outside the table shown below, using float or another method: <form action="./deleteentry.php" method="post"> <?php foreach ($dbr as $var) { ?> <tr> ...

Container that can be scrolled under varying heights

Trying to structure some CSS in order to create a website that resembles the layout of a typical desktop application: Almost there, but struggling to make the scrollable panel stick to the bottom of the viewport and show a scrollbar instead of overflowing ...

Efficiently incorporating styles and CSS, as well as Bootstrap CDN, into window.print() while utilizing Vue.js

I am trying to print from my Vuejs component and apply CSS and Bootstrap styles to the printed page. Currently, I am using window.print() inside the mounted lifecycle hook as shown below: export default { mounted() { ...

Using CSS to position elements absolutely while also adjusting the width of the div

In one section of my website, I have a specific div structure. This structure consists of two divs stacked on top of each other. The first div is divided into two parts: one part with a width of 63% and another part with a button. Beneath the first div, t ...

Creating a progress bar that includes hyperlinks: A step-by-step guide

Currently, I am in the process of developing an application form that consists of 9 pages of questions. Initially, I had implemented a progress bar which indicated completion by filling up 1/9 for each page completed. However, I have been informed that thi ...

Failure to append an object to the array occurs when submitting an HTML form

I am facing an issue with a form that is supposed to add input values to an array as objects when submitted. However, every time I submit the form, the console briefly shows that there is an array with one object, only for it to disappear. Below is the f ...

Can we adjust the width of columns in a bar chart generated with AmChart?

Having trouble with the AmChart column width as the column names are overlapping. Check out the image below: https://i.sstatic.net/YTDZE.png Can someone suggest a way to adjust the column width or move extra content to the next line? ...

I am attempting to adjust the CSS code so that it does not shrink when clicked on. How can I prevent this from happening?

Here is the element I am attempting to modify. I attempted &:active { flex:7;} but upon releasing the left click button, it reverted back to its original size. I was aiming for something similar to this <h1>Gallery</h1> < ...

Delay occurs unexpectedly when adding a class with jQuery to multiple elements

I am in the process of creating a flipping counter that is designed to change colors once it reaches a certain target number (in this case, 1000). However, I am noticing that the different sections of the counter do not change color simultaneously, there s ...

What is the expected output format for htmlspecialchars?

When I try the following: echo (htmlspecialchars("andreá")); I expect to see So, assuming that if I do echo (htmlspecialchars_decode("andreá")); I would get andreá. However, instead of the expected result, I see Additionally, when I run echo ...

Inconsistent heights of inline lists compared to wrapped text

Hey there! I'm having an issue with my inline list when the text in the anchor wraps onto a new line. I tried using line-height: 0;, but it's causing some problems with the text display. Any suggestions on how to keep everything on the same line? ...

The PHP code to include the head section with a link to the CSS is not functioning properly

I've recently dabbled in PHP and encountered my first issue that I can't seem to resolve. My goal is to have my index.php file, with the following code in the head section: <head> <?php require 'php/main/head.html'; ?> & ...

"Encountered an issue with submitting the nodejs form

edit 2 After attempting to implement the following code snippet in my nodejs application, I encountered an issue where the form data was not being successfully posted to the database: router.get('/dashboard/users/forms/competition-form/:id', en ...

What are the steps for submitting a form with AJAX?

I am having trouble submitting a form via ajax. I am not receiving any error messages even though I have set up error handling throughout the website. Here is my code: <script type="text/javascript" src="jquery.js"></script> <div id="shower ...

A pair of parallel lines cutting through a rectangular shape

I'm attempting to create two horizontal lines spanning a box. I tried using a plugin to generate the necessary code from a design, but the final output isn't optimized. <h1 id="H1_1"> <span id="SPAN_2">Feedback</span> </ ...

Determine the overall cost based on varying percentage increases for each step

I am currently working on developing a price estimation calculator. Here is the breakdown: For 1000 MTU, the price will be 0.035, resulting in a total of 35. For 2000 MTU, the price will be 0.034, making the total 67. For 3000 MTU, the price will be 0.03 ...

What could be causing the failure in Selenium's findElement(By.css("span:contains(string)") method?

I have been attempting to use selenium-webdriver to click on a specific element within a website, which happens to be plain text. However, when I use the following javascript code const spanElement = driver.findElement(By.css("span:contains('TEXT&apo ...

How can I pass an array from HTML to Node.js and subsequently store it in MongoDB?

Looking to combine the values of longitude and latitude into one array for input, then store it in the database. While there are plenty of examples on handling arrays, most of them are based on PHP. Check out the following code snippet: HTML <html> ...