Having additional space preceding list elements within a flexbox setup

Check out the code snippet below:

HTML Code

<ul class="menu">
  <li><a href="#">Item 1</a></li>
  <li>
    <a href="#">Item 2</a>
  </li>
  <li>
    <a href="#">Item 3</a>
  </li>
  <li><a href="#">Item 4</a></li>
</ul>

CSS Style

ul.menu
{
  display: flex;
  list-style: none;
}
ul.menu li::before
{
  content: "\2022";
}

Currently, Items 1 and 4 are displaying properly but there is an extra space before items 2 and 3 (right after the bullet). The issue seems to be related to the new line in the HTML for these items. Interestingly, removing the ul.menu li::before rule resolves this spacing problem.

I believe there might be a simple solution that can eliminate this extra space while preserving the formatting of new lines in HTML. Any suggestions or ideas on how to achieve this would be greatly appreciated? I'm looking for a neat solution rather than simply using ul.menu li a::before which unfortunately makes the bullets clickable as well.

Check out JSfiddle for reference

P.S.: Even without the display: flex; property, the same issue persists proving that it's not directly related to flexbox.

Answer №1

The empty space you observe is a common feature of all inline elements - simply include display: flex in the li as well.

Check out the presentation below and my revised Fiddle:

body {
  font-family: sans-serif;
}
ul.menu {
  display: flex;
  list-style: none;
}
ul.menu li::before {
  content: "\2022";
}
ul.menu li {
  display: flex;
}
<ul class="menu">
  <li><a href="#">Item 1</a>
  </li>
  <li>
    <a href="#">Item 2</a>
  </li>
  <li>
    <a href="#">Item 3</a>
  </li>
  <li><a href="#">Item 4</a>
  </li>
</ul>

Answer №2

To remove the space between item2 and item3, write <li> for both items on the same line.

Give this code a try, it should work:

JSFiddle

HTML

<ul class="menu">
    <li><a href="#">Item 1</a></li><li><a href="#">Item 2</a></li><li><a href="#">Item 3</a></li><li><a href="#">Item 4</a></li>
</ul>

CSS

body
{
    font-family: sans-serif;
}
ul.menu
{
    display: flex;
    list-style: none;
}
ul.menu li::before
{
    content: "\2022";
}

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

What could be causing my HTML elements to shift position based on varying screen sizes or zoom levels?

Does anyone else experience their HTML and CSS elements shifting around based on the screen size or zoom level? I have screenshots illustrating this issue. Here is how it currently appears And here is how it's supposed to look ...

Creating responsive CSS layouts for various device sizes

My webpage features a form with a prompt for the input box, followed by the input box itself. I want to implement two different layouts based on the device accessing the page. For cell phones, I want everything stacked vertically. However, for computers, ...

Incorporating fresh data from a node.js backend to refresh a webpage section

I'm currently working on creating an app that can retrieve the song a user is currently listening to using a web scraper. While I've managed to direct users to the page and display the current song title, they must manually refresh the entire pag ...

Error in Angular 2 component when loading background images using relative URLs from an external CSS skin

In my angular2 component, I am utilizing a third-party JavaScript library. The skin CSS of the component attempts to load images using relative URL paths. Since I am following a component-based architecture, I prefer to have all component dependencies enca ...

ensure that html tags display properly when retrieved from json in an angular directive template

Trying to embed ruby tags in a directive template but struggling with escaping them. Despite reading about $sce and filters, the confusion deepens as browser tabs multiply. The directive code is as follows: angular.module("ngFlashCard", ["ngSanitize"]).d ...

"What is the best way to eliminate duplicate data from an ng-repeat loop in AngularJS

I have a query regarding appending data from the second table into $scope.notiData in AngularJS. Additionally, I need to figure out how to remove ng-repeat data when the user clicks on the remove symbol X. I have attempted some code but it is not functioni ...

What is the best way to add a textfield within an image?

Does anyone have a code snippet that can generate a "search box" like the one on www.dx.com? I was shocked to find that searching for "textfield inside image" only yielded results for "image inside textfield". ...

Changing the CSS background in React when updates occur

Currently working on toggling the CSS background image within this React application. The images have been successfully imported and a variable called "image" is set to the imported image as the iteration increases/decreases with each click. The issue I&a ...

What exactly is HTML cloud storage all about?

As I work on developing an app through phonegap, one question that comes to mind is the possibility of storing information online. For instance, if there's a number variable that increases when a button is pressed, can this value be saved somewhere an ...

Center JQuery within the current screen

My implementation of Jquery dialog is as follows: <body> <div id="comments_dialog"> Insert a comment <form> <input type="text" id="search" name="q"> </form> </div> .... </body> dialog = $( "#com ...

In jQuery, how to create a single event that applies to two different elements simultaneously, rather than individually for each element

How can I create a toggle event for 2 different TDs within my table row that will show/hide the next table row when clicked? <table> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> & ...

Using JavaScript to ensure that a div is not hidden on page load if a checkbox is unchecked

Upon inspecting a page, I am implementing a script to check if a checkbox is selected. If not selected, the goal is to hide a specific div element. While troubleshooting this issue, I suspect the problem may be due to the lack of an inline element within t ...

Interactive phone photo gallery

I am looking to create a mobile webpage that allows me to swipe my finger left or right in the middle of the page to scroll between images. Here is an example photo: I want to be able to swipe through the images with my finger. Any suggestions on how I ...

Arrange two div elements side by side so they remain adjacent even when you adjust the browser size

After years of success with my floated left and right divs, I am now facing a challenge with responsive design. No longer able to rely on fixed widths, I find that when the screen size decreases, the right div moves below the left one. I know that using f ...

Is there a way to showcase whitespacing in HTML?

I have a database full of content that includes whitespace formatting for display on a webpage. However, when viewed on Stackoverflow using the code tag, the formatting changes. The second way shows how it is stored in the database and how I want it to app ...

Data cannot be found when jQuery form submission is attempted

I am facing a challenge with my webpage that generates multiple dynamic forms through a database, each intended to submit data to a webhook for an API call. Despite searching various forums extensively, I have not yet found a solution to my problem. It ha ...

Tracking a user's path while redirecting them through various pages

Recently, I created a website with a login page and a home page using nodejs, javascript, and html. The client side sends the entered username and password to the server, which then replies based on the validation result. During navigation between pages, h ...

What is the process for eliminating the message "Hello Member" on the Woocommerce Checkout page?

I've been struggling to remove a certain area on the checkout page without success. I attempted using this CSS code: .avada-myaccount-user-column .username { display:none; } https://i.stack.imgur.com/okFg9.png https://i.stack.imgur.com/GisRQ.png Cu ...

Generate dynamic maps that are exportable to HTML format

I've utilized Leaflet to produce engaging interactive maps in R. However, I'm encountering an issue when trying to export the maps - the background map appears grey after exporting. library(leaflet) library(htmlwidgets) m <- leaflet(data.fra ...

Exploring nested list iteration in Python

I have a total of 43 items, each consisting of 75 data points. These 75 points represent different times of the day, and I am looking to calculate the standard deviation of each specific time across all 43 items. I attempted to use a nested for loop, but i ...