Center aligning elements in React using CSS with HTML can be achieved through Flexbox. However, sometimes images

Currently, I'm in the process of developing a website with react and I've encountered an issue with centering elements vertically on the left and right sides. It's been troubling me for a few days now and I haven't found a solution yet.

The layout consists of a div that contains two elements - one on the left and one on the right.

.whatbdh_content-bottom {
  background: #eee;
  display: flex;
  flex-direction: row;
  padding: 2rem;
}

.whatbdh_content_left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-left: 2rem;
  margin-right: 2rem;
}

.whatbdh_content-grid {
  max-width: 1140px;
  margin: auto;
  display: grid;
  grid-gap: 40px;
  grid-template-columns: repeat(2, 1fr);
}

.card {
  text-align: center;
  padding: 1rem;
  border: 1px solid #000;
  color: #000;
}

.whatbdh_content_right {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 5rem;
}

.whatbdh_content_right svg,
img {
  max-width: 100%;
  height: auto;
  max-height: 100%;
}

@media screen and (max-width: 640px) {
  .whatbdh_section {
    flex-direction: column;
    margin: 0 0 3rem;
  }
}

@media screen and (max-width: 980px) {
  .whatbdh_content-grid {
    max-width: 90%;
    margin: auto;
    grid-template-columns: 1fr;
  }
}
<div className='whatbdh_content-bottom'>
  <div className='whatbdh_content_left'>
    <h1 className='title_text'>Not the best title right now</h1>
    <div className='whatbdh_content-grid'>
      <div className='card'>
        <p>We will show up</p>
      </div>
      <div className='card'>
        <p>We will show up <br />okay</p>
      </div>
      <div className='card'>
        <p>We will show up <br />okay</p>
      </div>
      <div className='card'>
        <p>We will never show up, <br /> okay </p>
      </div>
    </div>
  </div>
  <div className='whatbdh_content_right'>
    <img src={arrow2}/>
  </div>
</div>

I am looking to horizontally align my left and right content, additionally, I am unsure why my image disappears on a small screen. Thanks in advance

Answer №1

There are two key sections in the CSS code that are contributing to this problem:

Firstly, this section:

.whatbdh_content_right{
flex:1;
display: flex;
justify-content: center;
align-items: center;
margin: 5rem; <<== Issue lies here
}

And secondly, this part:

.whatbdh_content_right svg, img{
max-width: 100%; <<== This is causing trouble
height: auto;
max-height: 100%;

@media screen and (max-width: 640px) {
    .whatbdh_section{
        flex-direction: column;
        margin: 0 0 3rem;
    }

}

@media screen and (max-width: 980px) {
    .whatbdh_content-grid {
        max-width: 90%;
        margin: auto;
        grid-template-columns: 1fr;
    }
}
}

The first part sets the image's horizontal (and vertical) margin to 5rem, restricting its width to that of the neighboring grid.

The second part further limits the image from exceeding 100% of its designated width, affecting its ability to resize responsively.

In essence, as the screen width decreases, the image disappears due to the constrained space allotted for it within the grid.

To address this issue, options include reducing the margins of .whatbdh_content_right (first CSS section), adjusting the size allocation of the grid and text on the left, or allowing the image to exceed the 100% constraint imposed by the second CSS segment.

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

Ways to resolve the issue of iOS Safari showcasing 'a' tag styling differently compared to other browsers

Here's a simple question for you. Take a look at the images below for reference. Check out the iOS Safari browser on my phone https://i.sstatic.net/o9jOe.jpg and compare it to my desktop browser, both screenshots were taken live from the server. ...

What steps should I follow to create a versatile table component?

Can you please advise me on how to create generic data in a table using Typescript? I encountered this error message while trying to useTable({ at line data The error states: Type 'T[]' is not assignable to type 'readonly object[]'. ...

ScriptManager is not accessible in the current ASP.Net Core Razor Page context

I'm facing an issue where I have a view (such as Index.cshtml) and a page model (like Index.cshtml.cs). In the view, there's a JavaScript function that I want to call from the OnPost() method in the page model. I tried using ScriptManager for thi ...

Real-time updates are seamlessly integrated using a combination of React, Express, Node, and either MongoDB or

Over the past few months, I've been working on several MERN projects and came across something that really bothered me. It wasn't because I didn't understand what it was, but rather because it was completely unfamiliar to me. What I mean is ...

Fixing issues with internal web page connections (#x) present in the HTML code (href="#x" id="x")

Can anyone come up with a brilliant solution as to why my internal links are not functioning properly on a webpage? I've coded the HTML with the usual <a href="#link"><button>Some Text</button></a> .... bunch of content .... & ...

Feed information into a Select element from the Material UI version 1 beta

Having trouble populating data from a < Select > component in Material UI version 1.0.0.0 beta. Snippet of my code: This section is located inside the render() method. <Select value={this.state.DivisionState} onChange={this.handleChang ...

Display a loading spinner at the bottom of a React-Native ListView

Trying to find a method to attach an activity indicator at the bottom of a ListView when the user has scrolled to the end and the app is fetching additional data from a server. I attempted adding the indicator after the ListView element, but it appears c ...

Ensure that all elements are consistently kept within a DIV container

In my experience with web development, one recurring challenge is ensuring that everything stays contained within a div element. I often encounter situations where multiple nested divs and content cause styling nightmares when elements bleed out of their d ...

"Step-by-step guide on using Proptypes in your code

Just starting out with React, so please be patient :) I'm having trouble getting prop types to validate in my code. Despite not finding any errors in the console, I can't seem to figure it out after spending quite some time on it. Any help or gu ...

Tips on ensuring that PRE elements expand to fit column size without growing when additional data is added

I am facing a design challenge where I have a main card (1) that contains two sub-cards (2 and 3) using Bootstrap columns. Sub-card number 2 adjusts its height dynamically based on the screen size, while sub-card number 3 consists of a code snippet element ...

Rendering nested JSON using React Native

After successfully loading a JSON object and storing it in this.state, I am encountering difficulty accessing nested levels beyond the initial level. Here is an example of the JSON file being used: { "timestamp": 1530703572, "trend": { "value": 0, ...

Leveraging ACE in conjunction with WT

UPDATE 3 Here is the finalized working code below. Remember to use ace.js from the src folder, as it will not work from the libs directory. You need the pre-packaged version from their site. WText *editor = new WText(root()); editor->setText("function( ...

Is there a way to update a single element within an array without triggering a refresh of the entire array?

Is there a way to prevent the component from rerendering every time new data is input? I attempted to memoize each cell but I am still facing the same issue. Any suggestions on how to accomplish this? import React, { useState, memo } from "react&quo ...

What is the best way to conceal text while retaining icons on a compact screen?

How can I hide the text links for Home, Reservations, My Reservations, About, and Settings on smaller screens while still keeping the icons visible? Currently, I am using the following resources: http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.1 ...

Toggle the div if the if statement is true; otherwise, hide the broken

click: function(event, data) { $('#clicked-state') .text('You clicked: '+data.name); if (data.name == "VA") { $('#va').toggle(); } else { $('#va').style.d ...

Tips for enabling a keypad to restrict input field character limit

Encountering an issue with an input field that has a maximum length of 6 characters. Typing normally seems to work fine, but when using a custom keypad, the limit is not enforced and it allows for more than 6 characters to be entered. HTML: <div cl ...

Ways to incorporate a reverse transition effect

I am encountering an issue with a sliding effect on a div. When it has a class hddn, the overlay will have right: -100% from the viewport. However, when the hddn class is removed, it should return to the viewport with right: 0. How can I animate this movem ...

Leveraging em units in jQuery Script

I'm currently in the process of building a website and I'm facing an issue with converting measurements to em's. The script that I have reused from a previous project only seems to work with pixels, despite my efforts to make it compatible w ...

Modifying the information depending on the option chosen from the dropdown menu using angularJS

I have a dropdown menu where I can choose between two options, and when selected, the corresponding data is displayed. However, I would like to display the data that is inside a div tag instead. Check out this Fiddle Demo HTML: <div ng-controller="Ct ...

Troubleshooting a Vue.js formatting problem in Visual Studio 2019

Encountering an issue with VS2019 while attempting to format this code section. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="milestone.ascx.cs" Inherits="uc.dms.milestone" %> <section class="content-header"> <h1> ...