The components of my website shift and adjust as the size of the page is changed

I've been experimenting with different properties, but my CSS just won't stay in place. I've tried using absolute and fixed positioning, but without success. I want the elements to remain in their respective positions - the greeting on the left and the picture on the right of the sign-out buttons. I need them to stay in place even when resizing the window or viewing on a different screen.

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

https://i.sstatic.net/52qAy.png

This is my index.css:

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
    "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

... (CSS code continues)

<p>This is my app.css:</p>
<pre><code>.App {
  text-align: center;
}

... (CSS code continues)

<p>index.html :</p>
<pre><code><!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>

  </body>

Answer №1

Let's take a look at the HTML code for your header. Here's a concise example:
The header utilizes display: flex and flex-direction: row to prevent wrapping.
The button-container within the header follows the same pattern.
The key is in justify-content: space-between - this aligns each child element within the <header> to the far left and right. This, combined with the flex-direction specified for the button-container, prevents the elements from wrapping.
Feel free to test out the code snippet below and adjust the window size to observe it in action.
header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

header .button-container {
  display: flex;
  flex-direction: row;
}
<header>
  <div class="greeting">Hello, name</div>
  <div class="button-container">
    <div class="button">Button 1</div>
    <div class="button">Button 1</div>
  </div>
</header>

Answer №2

To create separation between two elements, consider adding margins to either one or both of them. To maintain their alignment, using percentages for positioning could be helpful. For example, setting right: 98%; for both elements may achieve the desired result.

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

Manipulating visibility of an input tag using JQuery

Having a simple input tag: <input id="DAhour" type="number" style="width:50px; font-size: xx-small; visibility:hidden"> Initially, the input tag is set to be hidden. Upon changing a combobox to a specific index, it should become visible. Despite su ...

What are some methods to achieve consistent alignment of input fields across different mobile devices using CSS?

I have been working on a login page that looks good on desktop and laptop devices. However, I am facing an issue with its display on mobile devices. The design appears differently on various mobile devices - for instance, it looks acceptable on an iPhone 1 ...

What are the steps for creating personalized sliders with WordPress?

Seeking guidance on how to code WP Template files to enable control from the WP dashboard for adding or removing slider images. A sample code snippet is provided below. <div id="myCarousel" class="carousel slide" data-ride="carousel"> <!-- Indi ...

Utilize Bootstrap-Vue to ensure that an element expands to occupy the available space

In my Bootstrap-Vue application, I have set up the following hierarchy: <b-navbar.../> <b-container > <b-row align-v="center" style="min-height:100vh"> <b-col style="align-items:center"> <h1>404 Error&l ...

Wrap react hooks provided by react query within a TypeScript class

Is it a good idea to encapsulate react query hooks in a class component for easier organization and usage? const keys = { receipts: (filter?: ReceiptFilter) => ['receipts', filter], receipt: (id: string) => ['receipt', id] }; ...

How to modify the link to ensure that a particular tab is already selected when the page loads

How to Add Script for Linking to Rawdata Tab I need help loading specific page content with the Rawdata tab open instead of the Summary tab. What code should I add to the link to ensure that the page loads with "Rawdata"? https://i.stack.imgur.com/avETs. ...

When the specified condition is met in HTML along with a designated URL

Is there a way I can implement an if condition in my HTML file within Codeigniter that checks the URL? Depending on whether or not it contains part of the URL, it would perform different actions. For instance: The initial URL is localhost/index.php/ca ...

What is the process of displaying text within a link?

I have a basic webpage where I want the text to display from a link. Here is my website: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Encyclopedia</title> <link href="test.css" re ...

Changing the `stripe` prop on Elements is not supported; once it is set, it cannot be altered

My Stripe Payment functionality is working fine, but I encountered a warning message in the console that says "Unsupported prop change on Elements: You cannot change the stripe prop after setting it." Here is my code. I can't figure out why ...

Activate scroll event when image src changes in Angular using jQuery

Seeking assistance with utilizing jQuery to scroll to a specific thumbnail inside a modal when left/right arrows are pressed. The modal should appear when the user clicks on an image. I've managed to implement scrolling upon clicking a thumbnail, but ...

parent node of React event target

Can we access the parent node of the event target in the virtual DOM? Within my simple React component, I have a function that is activated by an onClick event. Is it possible to retrieve the properties of the parent virtual DOM node within this function? ...

Reactjs is currently not integrated with Firebase

I recently completed a project in reactjs and realized it requires authentication. My initial thought was to connect it with Firebase as it seemed like the most straightforward option. Following a tutorial on YouTube, however, I encountered some difficult ...

Working towards ensuring my website is responsive

Hello, I am a CSS beginner currently working as an intern. My task is to make a website's CSS compatible with Internet Explorer, and then make it responsive and scalable. Essentially, the design should retain its appearance when the window size change ...

Select multiple dates using Material UI Calendar widget

Currently, I am working on incorporating the Material UI Calendar component into my project with a requirement to allow multiple date selections. The selected dates should remain highlighted even after selecting another date. Below is the current state of ...

Using the state in React to disable a button

Within a React class-based component, I have defined a state property as follows: state = { controls: { email: { validation: { required: true, isEmail: true }, invalid: false, }, password ...

Choose the offspring of an element using jQuery

Currently, I have a function set up to open a modal dialog and populate it with information from the 'dblclicked' node: $(function(){ $(".delete").live('dblclick', function () { var id = $(this).attr('id'); ...

Tips for confining a float within a div with fluctuating space

I have a scenario where I have multiple divs ('group') containing text, and there is a floated div ('toggle') in the bottom corner. The current code works well if the text length in 'group' is consistent, but the position of t ...

Building a Next.js project encounters issues with the <img> HTML tag

Lately, I've dived into creating websites using Next.js and have been experimenting with a combination of Image and img for different purposes. I am aware that Image is an integral part of Next.js and the preferred option, but there are instances whe ...

Steps to trigger a JavaScript popup from an iframe window to the parent window

Currently I am developing a web application using JSP with JavaScript. Within the parent window, an iframe is being utilized. The issue arises when a button in the iframe page is clicked, as it opens a popup window within the iframe itself. However, my obj ...

Server-side rendering or updating of React elements

One issue I am facing in my React app is that while all components can update themselves through the browser, a specific module called jenkins-api only functions when called server side. To retrieve the data and pass it into my template, I have utilized th ...