Home > Web Front-end > CSS Tutorial > How to Center an Unordered List of Unknown Width Horizontally?

How to Center an Unordered List of Unknown Width Horizontally?

Mary-Kate Olsen
Release: 2024-10-29 02:46:02
Original
450 people have browsed it

How to Center an Unordered List of Unknown Width Horizontally?

Centering an Unordered List of Unknown Width Horizontally

When creating a footer with a set of links represented as an unordered list, it is often desirable to center the list horizontally within the footer div. While it's straightforward to center text elements or elements with a known width, centering an unordered list of unknown width presents a challenge.

Inline List Items

If the list items can be displayed inline, the solution is simple:

<code class="css">#footer { text-align: center; }
#footer ul { list-style: none; }
#footer ul li { display: inline; }</code>
Copy after login

Block List Items

However, in certain cases, it may be necessary to use display: block; on the list items. In this case, a slightly more complex solution is required:

<code class="css">#footer { width: 100%; overflow: hidden; }
#footer ul { list-style: none; position: relative; float: left; display: block; left: 50%; }
#footer ul li { position: relative; float: left; display: block; right: 50%; }</code>
Copy after login

This CSS positions the unordered list at the center of the footer div and offsets the list items so that they align horizontally within the list.

The above is the detailed content of How to Center an Unordered List of Unknown Width Horizontally?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template