Is there a method to place two items in a flex container while only centering one of them?

I managed to achieve the desired effect using absolute positioning, but I know this is not the most elegant solution and it lacks re-usability. Is there a way for me to group these two elements in a flex container and center only the text? Also, I'm facing an issue with an MUI icon having an invisible square around it that's causing alignment problems.

https://i.sstatic.net/Vj0Xp.png

Although I am working with React and MUI, this question primarily pertains to CSS so the specific frameworks shouldn't be a major concern:

export default function CreateGuest() {
  const defaultText = 'Enter a display name'
  const [guestDisplayName, setGuestDisplayName] = useState(defaultText)
  return (
    <Box
      display="flex"
      justifyContent="center"
      margin="auto"
      sx={{
        width: 366,
        height: 386,
        backgroundColor: 'primary.contrastText',
        boxShadow: '0 2px 3px #00000053',
      }}
    >
      <Stack justifyContent="space-evenly">
        <Typography
          variant="h3"
          component="pre"
          textAlign="center"
          sx={{ position: 'relative' }}
        >
          <ChevronLeftIcon
            sx={{
              fontSize: 48,
              color: 'secondary.main',
              position: 'absolute',
              bottom: '43%',
              right: '87%',
              '&:hover': {
                cursor: 'pointer',
              },
            }}
          />
          {`Create a\nGuest User`}
        </Typography>
        <TextField
          label={guestDisplayName === '' ? defaultText : guestDisplayName}
          onChange={(e) => setGuestDisplayName(e.target.value)}
          variant="outlined"
          color="info"
        ></TextField>
        <Button variant="contained">Create Guest</Button>
      </Stack>
    </Box>
  )
}

Answer №1

The primary function of the CSS property flex-box is to define the positioning rules for all elements and text nodes contained within it in relation to each other. This means that if you wish to keep the "Create a guest user" text node unaffected, you will need to use position absolute to remove it from the normal document flow.

The good news is that by setting CSS rules for spacing, you can prevent any major issues with overlapping between the text node and ChevronLeftIcon. My suggestion is to determine the starting point of the text and specify the top and left properties in pixels rather than using percentages for bottom and right positions. To avoid overlap, consider adding padding to the parent element (which is a better option) or apply a max-width to the text node after wrapping it in an element.

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 do I create individual tables for each JSON array within my object using React and MaterialUI?

I have a unique challenge with a component that creates multiple tables, all within one <TableContainer>. The issue lies in the fact that every table is confined within the same container and I am unable to customize each table separately. Each tabl ...

Tips for showcasing information from the tmdb api by leveraging the value or data obtained from a separate api

I am currently working on a project that involves displaying movie data using the tmdb api. I receive the response from my own api which only includes the id of the tmdb movie. Here is an example of the response: [ { "id": 0, "tit ...

React JS functionality does not support Bootstrap tooltips

I'm attempting to implement a tooltip in my React app, but I'm experiencing issues with it not displaying properly. I am utilizing vanilla Bootstrap for this purpose. I've included the following script tag in my index.html file to import the ...

Operating on Javascript Objects with Randomized Keys

Once I retrieve my data from firebase, the result is an object containing multiple child objects. myObj = { "J251525" : { "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c3823212 ...

Using Node/Express to split the request headers with the .split() method

I am currently working on a way to determine if a specific item exists in the req.headers in order to make a decision on what to send back to the user. Here is my code snippet: function serveAppData(req, res) { console.log("CHECKME", req.headers); //var h ...

Initiate a fresh session when prompted by the frontend application

I have encountered an issue while developing a login page where Express creates a new session each time a request is sent from the frontend application. Despite trying various solutions found online and on Stack Overflow, the problem persists. What baffles ...

EJS include function not working properly

In most cases, my EJS pages include the code below: <%- include('elements/fbviewpagepixel.ejs') %> Everything runs smoothly except for one particular page where I encountered an error message stating include is not a function. I managed t ...

"Embrace the power of ReactJS with the versatile

I'm working on a ReactJS application and encountering an issue. The problem lies in my Router setup: <Router history={browserHistory}> <Route name="Home" path="/" component={App}> <IndexRoute component={Home}></Index ...

Unable to execute PHP alongside a JavaScript event listener

Using PHP, I am creating a canvas for writing and the text output will appear in a textarea (handled by other functions). There are additional input tags like a title to gather user input. The values from these input tags (title and textarea) will be submi ...

Getting rid of padding in Material UI SwipeableViews in React

Seeking assistance with the React Tabs/SwipeableViews component. I used the MaterialUI Tabs as a reference and it mostly works fine. However, I am facing a minor issue with padding in the content section. Current outcome: Current Outcome The button ...

Every time the page is refreshed, ExpressJS and NodeJS are working together to duplicate the array

var express = require("express"); var router = express.Router(); const fs = require("fs"); const path = require("path"); const readline = require('readline'); const directoryPath = path.resolve(__dirname,"../log ...

Uploading base64 arrays from AngularJS to a Node.js server: Allowing Access-Control-Origin

My current challenge involves sending an array of base64 strings from my Client Side (AngularJs) to my NodeJs Server. However, I've encountered a peculiar situation. When I attempt to send an object containing the base64 data along with other properti ...

In Node.js, the module.exports function does not return anything

After creating a custom function called module.exports, const mongoose = require("mongoose"); const Loan = require("../models/loan_model"); function unpaidList() { Loan.aggregate([ { $group: { _id: "$ePaidunpaid", data: { $pus ...

Step-by-step guide on incorporating CSS box-shadow with the .style JavaScript property

I have a JavaScript code snippet that looks like this: document.getElementById("imgA").style.box-shadow = "0 0 5px #999999"; The hyphen in box-shadow is causing an invalid assignment exception to be thrown by the JavaScript engine (specifically in Firefo ...

Screen JSON data by applying filters

In my current project, I am working on extracting data from a JSON file and presenting it to the user in a way that allows them to input values that match the expected data. My goal is to show different sections of the screen at different times. The initi ...

How can I customize the appearance of the file input button in Angular Material?

Despite browsing numerous similar questions, I have yet to find a solution to my issue. My setup includes Angular 11, Angular Material 8, and a file input structured like this: <div class="form-group"> <input #myInput type="fil ...

"Enhancing User Interfaces with Reactjs and Typescript: Exploring the Border

I am working on a component that takes borderStyle as a prop and then passes it down to a child div. I am trying to specify a type for this prop but I'm struggling to find the right one. Below is a snippet of my code that is relevant to this: inter ...

Establishing Firebase configuration parameters

In my experience, I have utilized Heroku and Netlify to store Firebase environment variables. The process was quite simple as I could easily enter them manually on the respective websites. However, when exploring Firebase, I found it to be a bit more com ...

The clash between jQuery UI and Bootstrap themes

I am encountering an issue where the text on my Bootstrap-styled buttons is not appearing on my webpage. After some investigation, I discovered that certain lines in the jQuery UI theme file are causing this problem: .ui-widget input, .ui-widget select, . ...