Optimizing the arrangement of flex items for ideal spacing

Seeking advice on the best way to handle the following issue: I have designed a set of navigation links, but when the screen size is reduced, there are spacing inconsistencies between the flex items. Sometimes, the '|' symbols that should be placed between items appear at the end of a row before the item drops down to the next line. Additionally, it seems that the text inside the tags can be on separate lines when the browser is widened after being shrunk (though this could be due to my browser).

Is there an effective solution for these problems? I initially used display:flex with justify-content: space-between to evenly space out the items, which works well overall but has some minor flaws.

Thank you

.footer-links {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  background: pink;
}
.footer-links > a {
  flex-wrap: nowrap;
  background: orange;
}
.footer-links > a:nth-child(n+2) {
  padding: 0 1%;
  background: lightgreen;
}
.footer-links > a:last-child {
  padding-right: 0;
  background: red;
}
.footer-links > a:first-child {
  padding-right: 1;
  background: lightblue;
}
@media (min-width: 1024px) {
  .footer-links { justify-content: center }
}
<div class='footer-links'>
  <a>Home</a> | <a>Terms &amp; Conditions</a> | <a>Contact Us</a> | <a>Apps</a> | <a>Some Link</a> | <a>Contact Us</a> | <a>Apps</a>
</div>

Answer №1

It's impressive how the flexbox follows our commands like a robot!

Your media query clearly specifies that only justify content space-between should be used before 1024px width, as inherited from the parent class. If you want them to be closer together, simply remove the media query for 1240px+ so it applies to all widths, change the justify-content property in the footer-links class to center, and it will adjust accordingly.

If you wish to avoid the pipe symbol (|) breaking lines before its sibling, consider including it within the same element:

Instead of:

<a>Home</a> |

Try this:

<a>Home |</a>

Alternatively, utilize the :after or :before pseudo-classes as recommended in their intended use guidelines, maintaining an inline display by default. You may also restructure your code using <li> tags to enhance accessibility and logical hierarchy.

To directly address the question, here is an example with a pseudo-class and centered justified content:

Please refer to the linked documentation for details on background color inheritance for pseudo-elements.

Answer №2

  • Within the .footer-links section, I made sure to include flex-direction: row; and removed

  • I also noticed an error in .footer-links>a:first-child where it was missing the percentage sign after padding-right: 1;.

.footer-links {
  display: flex;
  flex-direction: row; /* included */
  /* justify-content: space-between; */ /* omitted */
  flex-wrap: nowrap; /* adjusted */
  background: pink;
}

.footer-links>a {
  flex-wrap: no-wrap;
  background: orange;
  white-space: nowrap; /* added */
}

.footer-links>a:nth-child(n+2) {
  padding: 0 1%;
  background: lightgreen;
}

.footer-links>a:last-child {
  padding-right: 0;
  background: red;
}

.footer-links>a:first-child {
  padding-right: 1%; /* added the % */
  background: lightblue;
}

@media (min-width: 1024px) {
  .footer-links {
    justify-content: center
  }
}
<div class='footer-links'>
  <a>Home</a> | <a>Terms &amp; Conditions</a> | <a>Contact Us</a> | <a>Apps</a> | <a>Some Link</a> | <a>Contact Us</a> | <a>Apps</a>
</div>


Furthermore, I implemented a few enhancements that can elevate the aesthetics of your design. Feel free to make use of these suggestions as you see fit :)

Check out the JSFiddle demo here

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

PHP/SQL Search: A Basic Solution

Hey everyone, I could really use some help with my PHP SQL HTML project. I've been struggling to get my code to run properly and I can't figure out why my html form won't display. The program is supposed to search for user input in a search ...

"The challenges faced while working with a PHP script on a mySQL table, including problems related to the FROM_UNIX

I am tackling my first PHP script for a MySQL table. My SQL table is structured as shown below: https://i.sstatic.net/Chjiw.png My goal is to create a dynamic HTML table from this data with the following criteria: The HTML table should be sorted in as ...

What is the best way to ensure a div automatically adjusts its height to fit the screen, and expands if the content goes

My website features a div that expands in height to match the screen size. Below is the CSS I am using: .round-cornerapp { -webkit-border-radius: 5px 5px 5px 5px; -moz-border-radius: 5px 5px 5px 5px; -o-border-radius: 5px 5px 5px 5px; ...

Modifying icon color upon button click in Vue 3: A quick guide

I'm currently implementing Vue 3 into my project. Within the database, there are a total of 10 individuals, each of which should have their own card displayed. Users have the ability to add any person to their list of favorites. Below is the code snip ...

Switching PHP include on an HTML page using JavaScript

I've been attempting to modify the content of the div with the ID "panel_alumno" using a JavaScript function that triggers when a button is clicked. My goal is to display a different table each time the button is pressed, but so far, I haven't be ...

Utilizing jQuery to dynamically adjust the padding of a CSS class

My website, Lemendu, features two CSS classes called "redback" and "greenback" which correspond to the top-menu sections "ESPAÑOL" and "FRANÇAIS". I want these classes to be displayed on the right-hand-side, but I am facing an issue with media queries. W ...

Whenever I implement JavaScript validation on my HTML page, it causes the page to become un

Whenever I try to enter more than 30 to 40 characters in the password input field on my HTML page, all web browsers become unresponsive. This issue persists even if I modify the minimum length and other requirements to 50. I am new to JavaScript. <!D ...

Is there a way to use sed to modify font-size and font-family properties in a CSS file?

I'm attempting to modify the CSS font-size and font-family properties using the 'sed' command in Ubuntu. Here is a snippet of my CSS code: stage { font-family: "Comic Sans MS", cursive, sans-serif; font-size: 10pt; } Currently, I a ...

Vertical spacing on elements utilizing a position of absolute

Looking to create vertical space for elements with position:absolute and changing heights using just CSS. Any clever techniques involving containers or display properties that could be helpful? ...

Is there a way to incorporate a website into a pop-up window for a Chrome extension?

Hey there! I'm currently working on developing a Google Chrome extension that will feature a button in the browser. When this button is clicked, it should open up a bubble popup displaying content from this site: . However, as a newcomer to this proce ...

What is the best way to display a list of URLs from a dataframe column on a leaflet map?

I am looking to create a map with interactive leaflet points that display popup notes containing clickable links leading to internet pages. The URLs for these popups are stored in a column of my data frame, which contains thousands of rows. Here is some sa ...

I'm about to lose my mind because of this JavaScript code - is there a syntax

My code is showing an unexpected token < error, even though all the < signs are correctly placed. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Document</title> </head> <body& ...

What are some creative ways to enhance closePath() in canvas styling?

Currently, I am faced with the challenge of styling the closePath() function on my canvas. Specifically, I would like to apply different stroke styles to each line segment. For now, let's focus on changing colors for each line. In the example below, y ...

Creating a new webpage with a custom URL in a React application

After receiving an HTML file from the server, the application displays the data (responseToPost) on the page using the following code: <div dangerouslySetInnerHTML={{ __html: this.state.responseToPost }} /> I am interested in creating a new URL wher ...

Using jQuery and JSON: selecting classes based on associated JSON strings

If you want to see the complete codepen, I will share it here: http://codepen.io/JTBennett/pen/NdLWJy Let me try to simplify the problem. I have a click event that selects from a JSON object based on one of its string values. Text from various strings is ...

Guide to inserting a custom image into an Icon in next js

For my Next.js 13 and Tailwind project, I am looking to customize a chat/messenger icon by filling it with an image of my choice. While attempting to use the Lucide React Messenger icon, I found that it only allows hex color values for filling, but I want ...

IE having issues with border display

I am looking to add borders to specific elements based on certain criteria, such as: Elements with the .xforms-required class that are not span elements OR input elements that are descendants of elements with the .xforms-required class To achieve this, ...

Is there a way to implement a scrolling feature that specifically identifies an h1 element, and subsequently alters the color of said element upon reaching a distinct section?

Currently, my function changes the color to black when the scroll number reaches 100. However, I am looking to target the JavaScript so that it changes the color to black when the scroll reaches a specific section designated by a p tag. My code is almost t ...

How to activate the menu in AngularJS

Within my application, I have a header that contains various menu items. These menu items are fetched from a service and displayed in the header. When hovering over the main list, the submenus appear. My goal is to highlight the parent item as active when ...

Delay the fading in of an element using jQuery

Is there a way to remove the pause between the images in my JavaScript/jQuery slideshow when fading in and out? I attempted using a small delay, but it still didn't eliminate the blank space. Any suggestions? Check out the code on jsfiddle: https://j ...