Having trouble getting the text in a list item to align vertically with CSS?

When styling my li items with a list-style-image and single-line text, I noticed that the alignment didn't look quite right.

It seems that vertically aligning the text to the middle of the image would solve this issue.

I attempted to use vertical-align: middle, but unfortunately, it did not have the desired effect.

<html>
    <head>
        <title> Demo Example </title>
        <style>
            body {
                background-color: #464443;
                color: white;
            }
            ul {
                list-style-image: url('bg.png');
            }

            li {
                vertical-align: middle;
            }

        </style>
    </head>

    <body>
        <ul>
            <li>abc</li>
            <li>abc</li>
            <li>abc</li>
            <li>abc</li>
        </ul>
    </body>
</html>

Answer №1

An effective method to achieve this (in my opinion) is by avoiding the use of list-style-image and instead utilizing the background property to define the "bullet" image (I replaced it with a placeholder image for demonstration purposes). The padding and other dimensions may need adjustment based on the size of the "bullet" image.

Feel free to check out the fiddle: http://jsfiddle.net/huBpa/1/

body {
  background-color: #464443;
  color: white;
}

 ul {
   list-style: none;
   padding: 0;
   margin: 0;
 }

 li {
   background: url('http://lorempixel.com/output/technics-q-c-32-32-1.jpg') no-repeat;
   line-height: 32px;
   vertical-align: middle;
   padding-left: 40px;
 }​

Answer №2

My preferred method involves using padding to position text rather than relying on vertical align. This way, I have full control over where the text is placed.

    ul {
            list-style: none;
            margin-left: 0;
            padding-left: 0;
    }

    li {
            padding: 10px 10px 15px 20px;
            background-image: url(images/arrow.png);
            background-repeat: no-repeat;
            background-position: 0px;
    }

Answer №3

Adjust the line-height of li elements to match the height of the image

`

li {
            vertical-align: middle;
            line-height: 30px; 
        }

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

Having trouble locating the text box using Selenium in Java. Xpath and CSS selectors are not working

I'm currently attempting to locate the textbox in the HTML snippet below using chromedriver ...

Best practices for organizing fonts in SCSS 7-1 architecture?

My current project restructuring involves implementing the 7-1 architecture recommended by . As part of this process, I am incorporating flaticons into my project. I'm unsure of where to place the folder and scss file provided by flaticon within the ...

What strategies does Wired magazine use to create their unique thick underline link style?

Recently, I came across an interesting design choice on Wired magazine's website - they use a thick blue underline for their links that crosses over text descenders and is a different color from the text itself. You can see an example on this random p ...

The Kendo Dropdownlist mysteriously disappears behind the PDFViewer when using Safari

Currently, I am immersed in a project based on the MVC framework and utilizing the Kendo DropDownList. An problem has arisen, which is visualized in the image below. Specifically, the items of the Kendo DropDownList are becoming obscured by the PDFViewer ...

Unable to capture the text within the span element. Attempting to retrieve a response from a previously run javascript function

Currently, I am facing a challenging task in FileMaker using Webdirect. I have implemented a QR code generator and reader, and while I can successfully read the code, I am struggling to capture the result of the scan. The scanned data is displayed in a hid ...

What is the best way to line up two forms side by side?

Looking to create a login view for a webpage with two forms (one for registration and one for login) aligned side by side. Initially attempted using a table, then tried using bootstrap form groups (rows and cols) without success. Finally, aligned them in t ...

Is there a way to prevent the text box from expanding beyond the small breakpoint?

Help! I'm trying to control the size of a Bootstrap 4 input text box. I need it to stop growing after the small breakpoint, so it doesn't expand to the full width of the screen. ...

Designing a CSS dot leader on the top line against a intricate backdrop

I am looking to incorporate dot leaders after the first line of text using CSS so that it can be used over a texture or image background. Expected behavior: https://i.sstatic.net/6Jsrk.png I have come across a few implementations of dot leaders on the i ...

Create a line with elements that end on the next line without taking up the full width

In a row of three links, I want the third one to be on the next line, but using .new-line { display:block; } makes the link 100% width on the next line, causing the entire area to be clickable. I also have content in the :before that should appear inline w ...

The Mobile Side Menu Is Not Scrollable

I've encountered an issue with the side menu on my website. While it works perfectly fine on PC, it refuses to scroll on mobile devices. I've tested it on both my iPhone 5 and iPad, but no luck. Additionally, the toggle button isn't function ...

Expand the div to fit 100% width inside the Blueprint container

I am currently working on a website and considering using the Blueprint CSS framework. You can view the site I'm referencing here: http://jsfiddle.net/timkl/uaSe3/ The Blueprint framework allows for a nice 24 column grid layout. One challenge I&apos ...

What could be causing my div to not appear when using jquery's show function?

I'm dealing with HTML code that looks like this: <div id="answerTypeSection" style="visibility: hidden"> <div class="row"> <div class="col-md-2">adfadfafasdfasdfas</div> </div> <label class="control-label"&g ...

problem with the video pathway in the javascript document

I'm currently in the process of putting together a Video gallery playlist using HTML, CSS, and JavaScript. So far, I've set up the html and css files along with two js files. The first js file contains all the video information as shown here: ...

Show an HTML email message on a webpage without disrupting the existing page layout

Looking for a solution to display mail messages on a web page originating from an exchange server with various style elements that might interfere with the existing page layout. Attempting to load these mail messages within a div results in the style elem ...

Align boxes in the center within a DIV container

Here is the box design that I have created: https://i.sstatic.net/3rtcn.jpg The green color boxes are generated dynamically inside a "col-md-10" div. If there are less than 3 boxes in the second row, I would like to center align them. For example, in thi ...

Ways to modify information in MySQL without requiring an image update initially

I am looking to make updates to my data stored in a MySQL database. However, I have encountered an issue where if I update a specific field (such as the first name), the user's profile photo gets lost. <?php include 'function_page_user.p ...

Ways to add gaps between flexbox items within a single row?

I have a coding dilemma where I am trying to display two items in the same row with a 12px gap between them, but my current method is not working as desired. I want there to be space between the A's and B's without resorting to traditional techni ...

Having trouble centering my menu bar on CSS, can anyone help?

I'm having trouble getting the buttons inside the menu bar to align properly. Does anyone have any suggestions on how to fix this? I'm not very experienced with coding, so any assistance would be greatly appreciated! .main-navigation { clear ...

Kids sitting at the table-cell bootstrap columns not stretching to their full height

I currently have 3 account cards, each with different content, that I want to take up the full height of a column with col-md-4 class. After doing some research in [this][1] thread, it seems like using display: table is one way to achieve this. However, t ...

How to center text both horizontally and vertically in the heart of an HTML/CSS layout

I need to center the text vertically and horizontally in the nav div. Below are both the HTML and CSS code snippets. Here is the HTML snippet: <html> <head> </head> <title> Dom Chronicles </title> <body> <div id=" ...