Positioning elements at the center in React

Hey there, I need some help with my header component and search bar. Here is the code for my header:

import React from 'react';
import './style.css';
import SearchBar from './SearchBar';
import logo from '../../assets/logo.png';

export default function Header() {
  return (
    <>
      <div className="header-top"></div>
      <div className="header">
        <img className="header__logo" src={logo} />
        <SearchBar />
      </div>
    </>
  );
}

And here is the search bar component that is imported in the Header:

import React from 'react';

export default function SearchBar() {
  return (
    <div className="search-form-container">
      <form className="search-form">
        <input
          type="text"
          className="search-form__input"
          placeholder="Search for brands or products..."
        ></input>
      </form>
    </div>
  );
}

Here is the CSS used for styling:

.header-top {
  width: 100%;
  height: 30px;
  background: black;
  color: white;
  font-weight: bold;
}

.header {
  width: 100%;
  background: white;
  height: 60px;
  box-shadow: 0px 11px 13px -9px rgba(141, 141, 141, 0.75);
  display: flex;
  align-items: center;
}

.header__logo {
  height: 20px;
  padding-right: 100px;
  object-fit: contain;
}

.search-form__input {
  width: 700px;
  height: 45px;
  border: none;
  background: #f6f6f6;
  padding-left: 20px;
  color: black;
  border-radius: 5px;
}

The current layout looks like this :

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

I'm looking to center the searchbar inside the header while keeping the logo separate on the right side. When I add justify-content:center, it centers everything including the logo. How can I achieve the desired result? Thanks for your help!

Answer №1

To center content in a component, you can wrap it in a container and justify the content to the center.

<div className="header">
        <img className="header__logo" src={logo} />
        <SearchBar />
      </div>

This can be achieved by

<div className="header">
        <img className="header__logo" src={logo} />
        <div className="justify-center">
           <SearchBar />
        </div>
   </div>

In your CSS file, you can make changes like this:

.justify-center{ display:flex; justify-content:center;}

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

Extracting information from an express backend with React

Struggling to retrieve data from my backend through express, I'm also unsure about the setup for routing. Do I only use express routes when querying my database? My User component import React from 'react'; class User extends React.Comp ...

issue with map function not populating data

models/docs.js is used to fetch the API URL from the backend server const doc = { baseUrl: window.location.href.includes("localhost") ? "http://localhost:1337" : "", getallDocs: async function getallDocs() { ...

Display a unique and unconventional image as a dynamic full-screen background, ensuring the entire image is visible using React/RedwoodJS

Looking for a somewhat unusual solution here - I want to use an image as a background with all edges of the picture visible at all times. Typically, I would use the "cover" setting for this, but that doesn't achieve what I need (image included). After ...

How can you prevent an element from overflowing when employing window.pageYOffset and using a switch case to alter the background color at specified pixel thresholds?

I want the <body> element's background-color to change every 500px as I scroll down. I've scoured the web for answers, but still can't figure out what's going wrong. Please review the code. However, Firefox Browser indicates that ...

How can I assign a prop to a component within the root layout in Next.js version 13, especially when the root layout is required to be a server-side component at all times?

I need assistance with a project I'm working on. My goal is to create a SongPlayer component in Next.js version 13 that plays music and is positioned at the bottom of the window, similar to Spotify's player. However, my SongPlayer component requi ...

Iconic Navigation: Bootstrap 3 Navbar featuring Carousel

I'm currently in the process of developing a website using bootstrap3. Despite watching numerous tutorial videos, I am still facing challenges. One of my goals is to have the logo on the navbar aligning with the left side (which I've achieved ...

Using Bootstrap 4 to create a light color overlay on top of an image for enhanced text visibility and emphasis

Trying to apply a light color on top of an image or its parent div to ensure that the text remains visible regardless of the image's darkness. Using the Bootstrap card class for overlaying text over the image but facing issues with opacity dimming bot ...

Duplicate multiple "li" elements using jQuery and place them in a designated position within the ul element, rather than at the end

I am currently working on developing a dynamic pagination bar. This pagination bar will dynamically clone the "li" elements based on a number received from an external webservice. Here is the structure of my pagination element: <ul class="pagination"& ...

Modifying the status of an external entity

As a React beginner, I'm unsure if I'm on the right track. How can I update the state of a third-party object that has its own functions and variables? Check out the code snippet below: import React, { Component } from 'react'; import ...

Discover the secret to achieving the perfect onload screen effect

Can someone please help me achieve the initial onload effect on my website? I am currently using Bootstrap 4 and JavaScript, but I am new to JavaScript. You can check out the template I'm referring to here. ...

Common hurdles encountered when integrating IPFS with React components

I am facing issues importing IPFS due to some Webpack errors. I have tried to troubleshoot them without success, but upon researching, I suspect it may be related to Webpack 5. Here are the steps I followed: npx create-react-app test-app cd test-app npm in ...

center items after aligning floats

Currently, I am in the process of creating a website using a mobile-first approach with media queries. As I progressed to a larger device, specifically a tablet, I had to utilize the float property on my .textcontainer element to ensure it aligned properly ...

Getting user input with JQuery and dynamically updating CSS properties

<img src="purplemoon.png" alt="Purple moon" id="moon-photo" /> <table> <tr> <th colspan="4">Control panel</th> </tr> <tr> <td> <!-- attempting to retrieve value from the input field ...

selection menu and advancement gauge

While working on my code, I have a task where I need to make the progress bar move a specific amount when a name is clicked based on the option's value. <!DOCTYPE html> <html> <head> <title>testinggg</title> &l ...

I am experiencing an issue where the borders of a dropdown menu do not properly wrap around

I'm currently in the process of creating a dropdown menu that will display a list of articles along with the respective authors' images next to them. Initially, I intended to utilize Bootstrap 4's "card" system for this task. However, I enco ...

The challenge of incorporating multiple stylesheets into an express/node/ejs application: encountering the error "Undefined style."

I am working on a project using express, node, and ejs and I want to be able to use different stylesheets for different views. In order to render a specific view with its corresponding style, I have included the following in my app.js file: app.get('/ ...

Trying to access a specific index in an array in react js [0] but encountering an error message "cannot read properties

https://i.sstatic.net/WZKlc.png After running the code successfully, I encounter an issue when trying to access the first index which results in an error message stating "cannot read properties of undefined (reading '0')" https://i.sstatic.net/ ...

The object is identified as 'unknown' within a generic component (error code: 2571)

In my Table component, I have implemented generics and used a static component within the Table to create columns, also with generics. My issue lies in wanting the Column component to inherit the generic type passed to the Table during the execution of th ...

Can Child Component Changes in React Checkbox Cause Parent Node to Re-Renders?

Struggling with creating checkboxes for my project, I've spent an entire day on it without finding a solution. The issue involves a main checkbox that controls all child checkboxes. The desired functionality is to have the parent node's "allChec ...

Determine the value of every row in the fieldArray using Reactjs Hook

How can I dynamically calculate the subtotal for each row in a list using React Hook Form within a Functional Component? Here is my current code: const { fields: partsField, append: partsAppend, remove: partsRemove, } = useFieldArray({ control, ...