What is the best way to position the button directly in the header with styled components?

I've been struggling to align that button correctly, but I can't figure out how. Can anyone offer some guidance? I attempted using position:right on HomeButton, but it didn't work as expected. Below are my code snippets:

Header.styles.js

import styled from 'styled-components';

export const Wrapper = styled.div`
    display:flex;
    background-color: #a86332;
    padding: 1%;
`
export const Title = styled.h1`
    color: white;
    margin:0;
`
export const HomeButton = styled.button`
    border:2;
    margin:10px 10px 10px 0;
    height: 50px;
    width: 50px;
    background-color:#a86000;
    border-color:white;
    color:white;
    cursor:pointer;
    transition-duration:250ms;
    &:hover{
        height: 70px;
        width: 70px;
        margin:0 0 0 0;
        color:black;
        background-color:white;
        border-color: red;
    }
`

Header.js

import React from 'react'
import { HomeButton, Title, Wrapper } from './Header.styles'


const Header = () => {
    return (
        <Wrapper>
            <Title>McKing Burger</Title>
            <HomeButton >Home</HomeButton>
        </Wrapper>
    )
}

export default Header

Screen capturehttps://i.sstatic.net/kyKP3.png

Answer №1

export const Wrapper = styled.div`
display:flex;
justify-content: space-between;
background-color: #a86332;
padding: 1%;
justify-content: center;` // Added "justify-content" property

just only add "justify-content" property to Wrapper style. :)

Answer №2

To create space between each item, use justify-content: space-between

export const Wrapper = styled.div`
    display:flex;
    background-color: #a86332;
    padding: 1%;
    justify-content: space-between;
`

Alternatively, you can add align-self: flex-end to the button.

Answer №3

To accomplish this, implement the following CSS guidelines on the container:

display: flex;
flex-direction: row;
justify-content: space-between;

Answer №4

By utilizing the display flex property in this manner:

export const Container = styled.div`
    display:flex;
    background-color: #a86332;
    padding: 1%;
    display:flex;
    justify-content: space-between;
`

The Title and home button will be positioned separately, with the title on the left and the button on the right side :)

Answer №5

You have a few options to achieve this.

One way is by using justify-content: space-between;

export const Container = styled.div`
    display:flex;
    justify-content: space-between;
`

Another method is to use right:0:

 export const Container = styled.div`
  position: absolute;
  top: 20px;
  right:0;
 `

A third approach is using justify-content: flex-end;

export const Container = styled.div`
        display: flex;
        justify-content: flex-end;
     `
  

Answer №6

To resolve your problem, try the following solution:

export const Container = styled.div`
        display: flex;
        background-color: #b84a6e;
        padding: 2%;
        justify-content: space-around;
    `

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

Running a <script> tag with an external src attribute in a dynamic manner through the use of eval

Currently, I am utilizing the Genius API to fetch lyrics for a particular song and then embed them within an HTML <div> tag. My interaction with this API is through PHP, employing an AJAX GET request. Upon a successful AJAX request, the following HT ...

Implementing the linking of a URL to an HTML hidden button that is dynamically displayed through a Javascript program

I'm currently developing a basic webpage that has a feature allowing users to download the final result as an image file in the last step. To achieve this, I've added a hidden HTML button for downloading the result image and used CSS to set its ...

Attempting to conceal the API, however, the backend is throwing an error

view the error image I am currently in the process of developing an NFT search application that is capable of locating the NFTs associated with a specific wallet address. However, I am faced with the challenge of using Alchemy without exposing the API key ...

Dynamic routing in Next.js expands upon the current path

With my Next.js blog utilizing Strapi CMS, I am attempting to arrange the posts by their respective categories. I have set up a dynamic route for categories [id].js and have implemented getStaticPaths as shown below: export async function getStaticPaths() ...

Challenges with Aligning Grids

I'm looking to create a layout with 3 grid items on the left and 2 grid items on the right. I attempted to use float on the second grid container, but it didn't give me the desired result. Additionally, the grids contain elements such as Checkbox ...

Having trouble executing or constructing the project in Next.js?

Recently, I've been following tutorials from JSMastery on figma and locofy. However, I encountered an issue when trying to run the project in vscode. When attempting npm start, the following problem occurred: Error: Could not find a production build ...

Encountering a "is not a function" error in NextJS while attempting to import a custom hook

Exploring the world of NextJS, I am embarked on creating a responsive website starting with a navigation bar component. The goal is to ensure that it adapts seamlessly to different viewport dimensions. Let me walk you through my current folder structure: h ...

Double trouble: Symfony2 asset_version duplication issue

Here is a snippet from my config.yml: framework: templating: engines: ['twig'] assets_version: 2 In my twig template, I have the following code: {% block stylesheets %} {% stylesheets output='css/compiled/main.css ...

How big is the array size in the WebAudio API data?

Exploring the visualization of waveform and FFT generated by the audio stream from the microphone through the WebAudio API. Curiosity strikes - what is the size of each data array available at a given moment? Delving into the getByteTimeDomainData, it men ...

Concealing a form after submission using JavaScript

After submitting the form, I am attempting to hide it and display a loading GIF. I've experimented with various methods, including changing ClassName to Id, but haven't had success. This is for a school project, and I've spent a significant ...

Struggling to concentrate using jQuery?

When the search icon is clicked, I want the focus to be on the input so the user can start typing right away without having to manually click on it. Although I tried using focus(), it doesn't seem to work for this particular input. $('.header__ic ...

Bypassing the CORS restrictions to communicate with your backend API

I've recently begun teaching myself web development and have started sending XMLHttpRequests from a client-facing page to an Express.js API backend. The main purpose of this is to transmit form data and store it in a Mongo Database. However, I'm ...

Attempting to retrieve the key of an object nested within a JSON structure

I'm trying to extract all the key names from a JSON object, but I seem to be missing some when using the Object.keys method: var myJSON = [ { "Employees_Name": "Bill Sanders", "Work_plan_during_my_absence": "Work from home", ...

Jquery doesn't support adding click events to dynamically generated list items

Jquery Code Here is my jQuery code that retrieves a dynamically generated list of categories from a database. I have added a click event to each list item, but it is not triggering the alert as expected. <script type="text/javascript" src="<?= base ...

Can someone please provide me with a Javascript code snippet that accomplishes the same function

<script> document.addEventListener('DOMContentLoaded', function () { const menuItems = document.querySelectorAll('.headmenu li'); menuItems.forEach(function (menuItem) { menuItem.addEventL ...

Is there a way to stop the initial state in React.js from being regenerated randomly every time I handle an event?

I'm currently developing a game where players take turns attacking each other. Initially, I manually set the player's name and job, then randomly generate their life, damage, and magic attributes in componentWillMount(). https://i.sstatic.net/f0 ...

What is the process of invoking Link manually in React-router?

I am working on a component that is passed a <Link/> object from react-router as a prop. When the user clicks on a 'next' button within this component, I need to manually trigger the <Link/> object. Currently, I am using refs to acce ...

Formatting an HTML table for mobile devices

Hi everyone, I'm struggling to understand why my table is displaying incorrectly when switching to the mobile version. I've attempted various CSS adjustments to correct it, but the issue persists when viewed on mobile devices. Mobile view of the ...

Is there a way to position the textfield so that it is next to my button?

I'm currently customizing a newsletter form using a WordPress plugin and CSS. I'm also trying to apply the same style to a search form located above the newsletter signup section. Here is the HTML output and corresponding CSS: #sidebar { fo ...

After initializing the controller and postLink, ensure to reset the scope variable in the directive

I have created a basic directive: angular.module('app').directive('field', function () { return { restrict: 'E', template: '<div ng-click="clickElement()"><input id="{{inputId}}"></div& ...