Guide to aligning the searchbox beside the logo image seamlessly in ReactJS without any excessive spacing

I've been encountering some difficulties while attempting to position a search box next to the logo image at the center of the navbar styled section. Strangely, the search box keeps appearing in the right corner of the page instead.

Below is the React code snippet:

const Home = () => {
  return (
    <>
      <header>
            <div className='container-fluid'>
                <div className='row'>
                   <div className='sm2'>
                      <img src={logo} alt=''/>
                   </div>
                   <div className='search-box'>
                      <i><Fas.FaSearch/></i>
                      <input type='text' placeholder='Hit Enter'/>
                   </div>
                </div>
            </div>
      </header>
    </>
  )
}

export default Home

Here is the relevant CSS code that seems to be causing the issue:

header{
    width: 100%;
    height: 80px;
}

header .row {
    width: 100%;
    display:flex;
    align-items: center;
    justify-content: space-between;
    background-color: #E6E6FA;
    
}

img{
    width: 20%;
}

.search-box{
  width: 100%;
  text-align: center;
}

The current result can be seen in this screenshot Evidence big gap

If you have any suggestions on how to properly position the search box in the center of the webpage, I would greatly appreciate it. Thank you!

Answer №1

The issue stems from sm2 and searchbox. By default, width: 100% is applied to sm2, causing a similar problem with search-box.

To resolve this issue, take the following steps:

  1. Add a fixed width to sm2 such as width: 100px and also include width: 100% to img to cover all available space within sm2.
  2. The same width issue occurs with search-box, but can be fixed by adding display: flex to search-box.

Following these steps should rectify the problem. I have replicated the issue using only HTML and CSS, but it should work in React as well. Below is an example demonstrating the solution:

header {
  width: 100%;
  height: 80px;
}

header .row {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #E6E6FA;
}

img {
  width: 100%;
}

.sm2 {
  width: 100px;
}

.search-box {
  display: flex;
  width: 100%;
  text-align: center;
}
<header>
  <div class='container-fluid'>
    <div class='row'>
      <div class='sm2'>
        <img src="https://images.pexels.com/photos/27590775/pexels-photo-27590775/free-photo-of-a-bird-is-perched-on-a-tree-branch.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" alt='' />
      </div>
      <div class='search-box'>
        <i><Fas.FaSearch/></i>
        <input type='text' placeholder='Hit Enter' />
      </div>
    </div>
  </div>
</header>

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

Transform the data into put and choose the desired item

Here is the data I am working with "dates": { "contract": [ {"id":1,"name":"1 month","value":false}, {"id":2,"name":"2 months","value":true} ] } I want to display this data in a select dropdown on my HTML page. Here is what I have tried s ...

Show a persistent header once you scroll past a certain point using Bootstrap

Utilizing Bootstrap affix property to reveal a header after scrolling down by 100px has been successful for me. However, I encountered an issue when trying to set the opacity property to 0.0001, it works as expected. But when setting it to 0 or changing di ...

Switching between different CSS transition effects

Perhaps my approach is not ideal, so please feel free to provide feedback if this is not considered best practice. I have created DIVs that contain a checkbox and label. These styled DIVs resemble buttons, and I have implemented code to allow users to tog ...

Clicking on an item causes it to move to the following line

Whenever I click on the third (bottom) line in the pagination block, the number item 30 keeps jumping to the next line. You can see it happening here. This issue only occurs in the Chrome browser. It seems like there might be a problem with the CSS. Can ...

Issue encountered while initializing a simple React application built using Create-react-app

Hey there! I'm diving into the world of React and decided to embark on creating my first React webapp by following the "Getting Started" tutorial found here. npx create-react-app first_react_app cd first_react_app npm start But, I've hit a bum ...

What is the best way to reorganize components within Material UI?

On my react page, I used Material UI to create a stylish card design. Here is the code snippet: <Grid item xs={6} sm={6} md={4}> <Card className={classes.cardContainer1} elevation={0}> <CardHeader title={ <Typo ...

Zod: ensure at least one field meets the necessary criteria

Currently, I am in the process of developing a form that allows users to input either their email address or phone number. While they have the option to provide both, they are required to enter both before proceeding. For this project, I am utilizing Zod a ...

Guide to Configuring Loader When a Component API Request is Made or State Value is Updated

Recently, I developed a react application with multiple components, each having its own store. These stores are integrated into a global store. However, I encountered an issue where I needed to display a loader on the entire screen whenever there is pendin ...

Ways to display or hide particular div in a table

Coding Example:- '<td>' + item.Message + ' <input type="button" class="btn btn-info" id="' + item.LogID + '" onclick="Clicked(this);" value="View More" /> <p> ' + item.FormattedMessage + ' </p>& ...

A linear gradient effect originating from the center/middle of the background

Is it possible to create a linear-gradient background that starts from the center or middle of the screen? This is the CSS I am currently using: body { display: table; width: 100%; height: 100%; margin: 0 auto; background-repeat: no-r ...

Issue with CSS not getting applied to content placed within modal dialog on Internet Explorer

After writing some code, I encountered a situation where I have multiple jQuery UI modal dialogs. I decided to remove the default title bar provided by jQuery in these modal dialogs. The code snippet I used within the create function in the dialog code to ...

A guide to displaying numerous notifications using notistack in a React environment

I'm currently experimenting with React's 'notistack' module to showcase multiple notifications as a stack. However, it seems like I might be making an error since every time I encounter a warning: react_devtools_backend.js:3973 Warning ...

Encountering a ELILIFECYCLE error while attempting to execute npm run build on a Linux system is a common issue

I am attempting to compile the project using sudo npm run build on a Linux system, but I am encountering the following error: ubuntu@ip:/var/www/reactcamera$ sudo npm run build > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfema ...

The best approach for setting a select value and managing state in React using TypeScript

Currently, I am in the process of familiarizing myself with TypeScript within my React projects. I have defined a type for the expected data structure (consisting of name and url). type PokedexType = { name: string; url: string; } The API respon ...

Is there a way to customize the slot CSS for FormControlLabel?

mui5 "@mui/material": "^5.8.6", In order to achieve a lighter font weight for the label, I encountered an issue. https://i.sstatic.net/2JcpY.png Despite my efforts, the text still appears in bold: <FormControlLabel sx={{ label: ...

Tips for positioning two bootstrap cards alongside each other

I am trying to align two cards next to each other horizontally, but they are currently stacked vertically. I want Heading 1 card to be positioned beside the Heading 2 card, even though I have applied some margin it seems to only shift them horizontally a ...

Tips for creating a validation section in a CRUD application using React.js

I have been working on developing a CRUD app using ReactJS. However, I am facing some challenges with the validation process as it does not seem to be working correctly and I am not receiving any results. As a beginner in this field, I would greatly apprec ...

The amazingly efficient Chrome quick find feature, accessible by pressing the powerful combination of Ctrl + F, ingeniously

Currently using Google Chrome version 29.0.1547.62 m. I've employed the CSS attribute overflow set to hidden on the parent element, which renders some of my DIV elements hidden from view. Additionally, these concealed DIV elements are adjusted in pos ...

The function `map` cannot be applied to `this.state.[object]`

It seems like I might be overlooking a simple mistake, but I keep encountering this.state.iata.map is not a function , even though I have bound the context to all functions where I need to access that state. Additionally, the iata state is an array, s ...

The warning message from npm indicates a lack of permission to write to the specified directory, `/usr/local/lib

Important: For security reasons, please avoid using the suggested solution and opt for the top-voted answer instead! Query: I am attempting to execute the installation of Monaca with this command. npm install -g monaca However, immediately encountering ...