Centering Text in CSS ul menu w/background image

Posted on 2011-05-08
Medium Priority
Last Modified: 2012-05-11


I would like to center the text links up/down in this css menu ul that has a background image. I am using text-indent in the li class to move the text to the right to hit the background, but I can't figure out how to center the text top to bottom properly. I've tried padding and margin variations but the result moves the background, not the text.

Thank you.
Question by:SlickCatSeven
LVL 13

Accepted Solution

jonahzona earned 2000 total points
ID: 35716837
I accomplished this by doing the following:

On your .menu a selector I gave it a padding of 8px 0 0 0, so it came out as

.menu a {
    background: url("button.png") no-repeat scroll 0 0 transparent;
    color: #555555;
    display: block;
    height: 32px;
    padding: 8px 0 0 0;
    text-decoration: none;
    width: 300px;

Open in new window

This got the text centered. But because your a element is block, it spaces it all out. You can solve this by giving your .menu li a negative margin. This came out as

.menu li {
    list-style: none outside none;
    margin: -8px 0 0;
    text-indent: 80px;

Open in new window

Here is a screenshot.


Author Closing Comment

ID: 35716848
Well done!


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question