What is the best way to ensure a consistent spacing between two flex items?

I am working on a project to enhance my knowledge by rebuilding Facebook. One of the new concepts I am learning is Flexbox.

When you resize the login page of Facebook, you'll notice that the Navigation shrinks but the logo and login inputs remain static, maintaining the same space.

Many have suggested abandoning floats in favor of using Flexbox. I have implemented it with space-between to evenly distribute them to the edges of the container.

The challenge now is figuring out how to prevent them from moving closer together when the browser width decreases, similar to Facebook. How can I maintain the "fixed" space between them?

Check out the code here

header {
  position: fixed 0 0;
  background-color: #3b5998;
  background-image: linear-gradient(#4e69a2, #3b5998 50%);
  border-bottom: 1px solid #133783;
  height: 83px;
  width: 100vw;
}

header .container {
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  justify-content: space-between;
}

/*///// Header Logo ///////*/
header #logo {
  height: 40%;
  margin-top: 17px;
}

header img {
  height: 100%;
}

/*///// Header Login Bar ///////*/

#login {
  margin-top: 6px;
  font-size: .8rem;
}

#login thead {
  color: #ffffff;
  font-size: .8rem;
}

#login thead td {
  padding-left: 3px;
}

#header-input {
  margin-bottom: 5px;
}

#header-input input{
  margin-right: 14px;
  margin-top: 6px;
  width: 150px;
  height: 23px;
}

#login input{
  border: solid 1px #1d2a5b;
  padding: 2px;
}

#login input:focus{
  outline-color: rgb(240,119,70);
}

#login button {
  background-color: #4267b2;
  border: 1px solid #29487d;
  border-radius: 2px;
  color: #ffffff;
  font-weight: 600;
  padding: 3px 6px;
}

#login button:hover {
  background-color: #365899;
}

#forgot-account {
  padding-top: 6px;
}

#forgot-account a{
  color: #9cb4d8;
  text-decoration: none;
}

#forgot-account a:hover {
  text-decoration: underline;
}

Answer №1

If you're seeking a consistent distance between two elements, consider using fixed units such as pixels.

The use of justify-content may not be suitable for your layout because it simply redistributes available space in the container. Since the amount of space fluctuates when the screen size changes, the distribution of space will also change.

In other words, most flex properties, including justify-content, are intended to create adaptable – not rigid – layouts.

Instead, try implementing something like this:

header .container {
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  /* justify-content: space-between; */
}

header #logo {
  height: 40%;
  margin-top: 17px;
  margin-right: 450px;  /* new */
}

improved codepen

Alternatively, you can add a third, invisible flex item between the logo and login. This "spacer" item can be adjusted with something like: flex: 0 0 450px.

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

Issue with Bootstrap 4 navbar dropdown on mobile: unable to click on menu item

My website is currently in development with Bootstrap 4. I'm facing an issue where, on mobile devices, when the menu items collapse into the three bars, they are not clickable. Despite following the recommendations in the Bootstrap documentation, the ...

Are you looking to test your website's performance under limited bandwidth conditions

Similar Query: How can I test a website for low bandwidth? Would it be possible to mimic a slow internet connection to test my website's performance? I need to simulate lower speed conditions in order to observe how the site behaves. ...

I'm puzzled as to why a scroll bar materializes

I recently encountered an interesting issue while working on my responsive web design project. Upon resizing the browser to a width of 615px or less, a horizontal scroll bar unexpectedly appeared. It's puzzling as to which element is causing this prob ...

Variety of color palettes within a single style sheet

My theme features various styles, including different color schemes. Currently, I load the default CSS first and then an additional stylesheet with specific colors based on the chosen color scheme. However, this process is causing a delay in my site' ...

When I try to upload an image onto my website, the layout of my text gets disrupted

There seems to be quite a gap between the welcoming message and the main body text. Significant spacing noticed between Title and body ...

Setting the height of a div to 100% is not effective

I've designed a two-column layout. You can view the code on jsfiddle.net. My problem is that I want the center line with a width of 10px to have a height of 100%. I have a container div with the ID #obal (set to height: auto). When I set the height of ...

Browsing the web with Internet Explorer and uploading images

I am facing an issue with a large form that contains over 100 submit buttons. The code I am currently using is: <input type='image' src='../images/add.png' name='FormDuplicate' value='" . $resInvoices['iProjectID ...

Creating a process to automatically generate an input field upon the selection of checkboxes

Is there a way to automatically generate a text field for each checked box in a dynamically changing checkbox list? Below is my code snippet: <div> <label> Products </label> <li ng-repeat="item in INDproducttypes"> ...

Arrange three images both vertically and horizontally at the center of the page

I am facing an issue with aligning a button and 2 images in a row on my website. Currently, this is how it looks: https://i.stack.imgur.com/yrQB6.png Here is the HTML code I am using: <div class="row text-center"> <div class="col-md-12"> ...

Tips for inserting manual line breaks in justified text

My task involves taking a 5-page story from a Word document and implementing it on a website. #reading { hyphens: auto; text-align: justify } <div style="font-size: 20px; width: 750px; margin-bottom: 4em;" class="reading" id="reading"> TextT ...

Is it possible to utilize LESS to dynamically read the width?

My LESS code looks like this: &.settings-prompt .panel { margin-left: -190px; width: 380px; height: 200px; } &.create-playlist-prompt .panel { margin-left: -165px; width: 330px; height: 180px; } I want the .panel to have ...

Accordion-style elements arranged in a grid format, smoothly displacing surrounding content

I am facing an issue with a grid of accordions, as demonstrated in the codesandbox linked below. The problem arises when I open one accordion, causing all the accordions in the row below to shift down. Ideally, only the accordion directly below should be p ...

What is the best way to trigger the download of an image file created by PHP to a user's computer?

My PHP code (upload.php) allows users to upload an image from index.html, resize it, add a watermark, and display it on the same page. Users can download the watermarked image by using the 'Save image as...' option. The resized image is saved in ...

output a string from the write_html function in plotly

I am currently attempting to utilize plotly express to generate a string representation of a div object in HTML for integration with other packages. Although I have been following the documentation (Plotly v5.17.0), it seems that my attempts have not been ...

Having trouble connecting the HTML file with the JavaScript file

This is my unique HTML file <!DOCTYPE html> <html> <head> <script src="ll.js"></script> </head> <body> <link rel="stylesheet" href="home.css"> ...

Read through the text, determine the length of each word, and keep track of how

Objective: Create a JavaScript program that takes input from users in the form of several lines of text and generates a table displaying the count of one-letter words, two-letter words, three-letter words, etc. present in the text. Below is an example ou ...

iTextSharp rendering HTML to PDF in a unique way compared to browsers

I'm currently working on converting an HTML invoice to a PDF within my application, but I'm running into issues where the resulting PDF appears different from how the page displays in various browsers like IE, Chrome, and Firefox. Does anyone ha ...

Rotate the front view image to the left view with a three-dimensional twist

I've been attempting to rotate the image using the code below, but I can only manage to go from a front view to a bottom view. My goal is to rotate the image from the front view to the left view. Is there a way for me to achieve this? body { heig ...

Creating an array of multiple divs based on numerical input

I am working on a project to show multiple divs based on the user's input number. For example, if the user selects 3, then 3 divs should be displayed. While I have successfully implemented this functionality, I need to dynamically assign IDs to each ...

Using a variable with the :contains selector

function checkAndUpdateStatus(newName) { $('#myTable').children('tr').remove(":contains('" + newName + "')"); }; The function above is attempting to remove table rows in 'myTable' containing a string matching the ...