Tips for positioning the avatar to the right along with other links?

I've encountered various methods to align text or an image to the right, but I'm having trouble aligning a basic avatar with some text. It used to work before I switched to material-ui-next, and now I can't seem to get them aligned properly. What I currently have is: https://i.stack.imgur.com/35QxX.png However, I want Text1 on the left (as it is) and Text2 and the Avatar next to each other, both aligned to the right in the same line. Below is the render code I've tried so far; it has gone through multiple variations, including using <li>, although I'm not sure if it's necessary in this case.

This is the render for the top container:

  render() {
      return (

        <div className="main-container">
            <div className="links-container">
                <div className="logo-container">Text1</div>
                <div className="right-links-container">
                    <ul className="right-list-container">
                        <li><a href="">Text2</a>
                        </li>
                        <li><AccountInfo />
                        </li>
                    </ul>


                </div >
            </div>
        </div>
      );
    }
  }

This is the render for the avatar:

render() {
        return (

            <div className="profile-container">
                <Avatar
                    src="https://www.shareicon.net/data/2016/08/05/806962_user_512x512.png"
                    size={30}
                    onClick={this.handleAvatarClick}
                        />

                <Popover
                    open={this.state.showMenu}
                    anchorEl={this.state.anchorEl}
                    anchorOrigin={{horizontal: 'left', vertical: 'bottom'}}
                    targetOrigin={{horizontal: 'left', vertical: 'top'}}
                    onRequestClose={this.handleCloseAccountInfoMenu}
                >

                <MenuList role="menu">
                    <MenuItem >My account</MenuItem>
                    <MenuItem >Logout</MenuItem>
                  </MenuList>
                </Popover>
            </div>
        );
      }

And here's the CSS:

.main-container {
    margin-top: 10px;
    margin-bottom: 10px;
    width: 100%
}
.links-container {
    margin: 15px;
    text-align: right;
}
.logo-container {
    float: left;
}

.right-list-container a {
    text-decoration:none;
}
.right-list-container li:nth-child(1) {
    text-decoration:none;
    height:30px;
    display:block;
    background-position:right;
    padding-right: 15px;
}
ul {
    list-style-type:none;
    padding:0px;
    margin:0px;
}

.accountinfo-menu {
    width: 200px;
}
.profile-container {
    clear: both;
    vertical-align: middle;
    display: inline;
}

I feel like I might be overcomplicating things and that it should be simpler. Any suggestions on how to achieve the desired layout? Thank you!

Answer №1

Here is a quick example of how you can use flexbox to layout your elements. This code snippet should help you achieve what you are looking for.

render() {
      return (

        <div className="main-container">
            <div className="links-container flexbox">

                <div className="logo-container">Text1</div>

                <div className="right-links-container">
                    <ul className="right-list-container flexbox">
                        <li><Avatar /></li>
                        <li><a href="">Text2</a></li> 
                    </ul>
                </div>

            </div>
        </div>
      );
    }
  }

CSS:

    .flexbox{
        display:-webkit-box;
        display:-moz-box;
        display:-ms-flexbox;
        display:flex;
    }



 .links-container{
    align-items:center;
    width:100%;
    }

    .logo-container{
    width:auto;
    }

    .right-list-container{
    margin-left:auto;
    width:auto;
    align-items:center;
    }

    .right-list-container li{
    margin-right:10px;
    }

Check out this demo for a visual representation of the code in action!

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

Adjust the placement of the material autocomplete popup icon

I am currently seeking a solution for adding a unique button to the end of a material autocomplete feature. The issue I am facing is that the popup icon is styled to appear at the end of the autocomplete input which complicates adding a button in that posi ...

How to position the play button in the center using Material UI and Flexbox

Seeking advice on achieving a centered play button with borders in this layout. Struggling with flexbox to position the play button within the code provided. function Tasks(props) { const { classes } = props; return ( <div className={classe ...

The information displayed in Next.js appears in the form of an array, which is distinct from the JSON object provided by the API

I am retrieving data from a Laravel API using getServerSideProps(): This is the method I use to fetch the data: function Product({ data }) { console.log(data); return ( <div>Hello</div> ) } export async function getServerSidePr ...

Creating an HTML button to reveal additional text on the same page

Currently, I am working on a project involving html and javascript. My issue lies in displaying multiple options on the same webpage without switching pages. Essentially, I have a plot and a few buttons on one page, and when a user clicks on any of these b ...

Why is the Bootstrap template working on index.html but not on any other React components?

Greetings to all who stumble upon this post! Hello, I have been diligently working on a React application and am eager to integrate a Bootstrap template. However, everything seems to be in order except for the responsiveness of the template. It lacks anim ...

Preserve HTML client control values during page reloads

Is there a more efficient way to retain client-side HTML controls on postback? I've attempted using enableviewstate="true", but it didn't work. My current workaround involves creating a server-side function that resets all posted values using Cli ...

React - Dynamically import file path for PDF document

Initially, the page will send an http request to an API The API will then respond with an object containing a path: ./assets/sample.pdf I am trying to extract the urlPdf from import urlPdf from response.path Any suggestions on how I can achieve this? ...

Should each navigation item be enclosed within its own div element?

Currently, I am studying HTML + CSS and developing a website that requires a vertical navigation bar on the left side with four interactive elements. I'm wondering if it is customary to enclose each of these four elements in a div, or if there is a mo ...

A guide on verifying a phone number using just one character

I am looking to validate a phone number with only one character being allowed. For example, the format should be XXX-XXXXXXX where "-" is the only allowed character. Below is my validation function: function validatePhoneNumber() { if(addform.staff_m ...

Is polymer routing the best choice for large-scale websites?

I am currently working on a large project that consists of various layouts and structures, totaling around 100 different pages. Despite the variations in content, the core elements such as headers and menus remain consistent throughout. To streamline my ...

The property 'x' cannot be found on the data type 'true | Point'

I am dealing with a variable named ctx which can be either of type boolean or Point. Here is how Point is defined: type Point = { x: number y: number } In my React component, I have the following setup: const App = () => { const [ctx, toggleC ...

Determine whether to wrap a component with a <span> or <div> tag based on

How can I effectively wrap the component using logical operations? I want to add another span to enclose the Child component if showSpan is true. I've tried something like the following but it doesn't seem to be working as expected. const Child ...

Deactivate Pagination with a Button-Based Approach

After conducting thorough research online, I have experimented with various approaches but unfortunately, none have proven successful. view image description here I am seeking guidance on how to disable pagination when clicking on the replace button. Due ...

What steps do I need to take in order to ensure that this React/Redux application renders accurately?

I have been struggling for the past hour and a half to understand why my locally hosted React and Redux app is not displaying. I have checked a few other React apps to confirm that they are functioning correctly, which they are. However, this is my first t ...

Is there a way to utilize the map function for iterating over data in Immutable.js?

How to Fix Unexpected Token Input Error: let type = ["categories", "people", "organization"]; let tab = filterCache.map((name) => type.forEach((i) => <Tab>{name[i]} (1)</Tab> ) ); The code is returning an error: Unexpec ...

Is there a way to dynamically choose the name of a state property object in React in order to modify one of its child properties?

In my class, I have defined the following state properties: state = { step: 1, clientName: '', icecreamFlavors: { vanilla: false, chocolate: false, mintChocolateChip: false }, toppings: { sprinkles: fal ...

Ensure that the div remains within the viewport

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Tit ...

Limiting click event to only Image component in Next.js

Is there a way to trigger a click event only on the image itself, rather than the entire parent div? When setting width and height for the parent div, the click event seems to encompass the entire area. For instance, if the image is 600 pixels wide by 300 ...

Styling various versions of a <button> using Styled Components

In my app, I have a basic colored button that needs to change its color depending on the UI state: const StyledButton = styled.button` & { border: 0; color: white; cursor: pointer; } &:hover { background-color: ${(props) => ...

Footer void of color

It's puzzling to me where the extra space in my footer is coming from. It seems to be around 20-30px as you can see on the following url: Despite the code in my footer being minimal, with no apparent cause for this additional space below my social ic ...