Comment aligner horizontalement ul au centre de div?

J’essaie de centrer un

    intérieur d’un

    . J’ai essayé ce qui suit

     text-align: center; 

    et

     left: 50%; 

    Cela ne fonctionne pas.

    CSS :

     .container { clear: both; width: 800px; height: 70px; margin-bottom: 10px; text-align: center; } .container ul { padding: 0 0 0 20px; margin: 0; list-style: none; } .container ul li { margin: 0; padding: 0; } 

    Je veux que l’ ul soit centré à l’intérieur du conteneur.

    Vous trouverez ci-dessous une liste de solutions pour centrer les choses en CSS horizontalement. L’extrait de code les inclut tous.

     html { font: 1.25em/1.5 Georgia, Times, serif; } pre { color: #fff; background-color: #333; padding: 10px; } blockquote { max-width: 400px; background-color: #e0f0d1; } blockquote > p { font-style: italic; } blockquote > p:first-of-type::before { content: open-quote; } blockquote > p:last-of-type::after { content: close-quote; } blockquote > footer::before { content: "\2014"; } .container, blockquote { position: relative; padding: 20px; } .container { background-color: tomato; } .container::after, blockquote::after { position: absolute; right: 0; bottom: 0; padding: 2px 10px; border: 1px dotted #000; background-color: #fff; } .container::after { content: ".container-" attr(data-num); z-index: 1; } blockquote::after { content: ".quote-" attr(data-num); z-index: 2; } .container-4 { margin-bottom: 200px; } /** * Solution 1 */ .quote-1 { max-width: 400px; margin-right: auto; margin-left: auto; } /** * Solution 2 */ .container-2 { text-align: center; } .quote-2 { display: inline-block; text-align: left; } /** * Solution 3 */ .quote-3 { display: table; margin-right: auto; margin-left: auto; } /** * Solution 4 */ .container-4 { position: relative; } .quote-4 { position: absolute; left: 50%; transform: translateX(-50%); } /** * Solution 5 */ .container-5 { display: flex; justify-content: center; } 
     

    CSS: Horizontal Centering

    Uncentered Example

    This is the scenario: We have a container with an element inside of it that we want to center. I just added a little padding and background colors so both elements are distinquishable.

    My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.

    Tasha Yar about Data

    Solution 1: Using max-width & margin (IE7)

    This method is widely used. The upside here is that only the element which one wants to center needs rules.

    .quote-1 { max-width: 400px; margin-right: auto; margin-left: auto; }

    My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.

    Tasha Yar about Data

    Solution 2: Using display: inline-block and text-align (IE8)

    This method utilizes that inline-block elements are treated as text and as such they are affected by the text-align property. This does not rely on a fixed width which is an upside. This is helpful for when you don't know the number of elements in a container for example.

    .container-2 { text-align: center; } .quote-2 { display: inline-block; text-align: left; }

    My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.

    Tasha Yar about Data

    Solution 3: Using display: table and margin (IE8)

    Very similar to the second solution but only requires to apply rules on the element that is to be centered.

    .quote-3 { display: table; margin-right: auto; margin-left: auto; }

    My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.

    Tasha Yar about Data

    Solution 4: Using translate() and position (IE9)

    Don't use as a general approach for horizontal centering elements. The downside here is that the centered element will be removed from the document flow. Notice the container shrinking to zero height with only the padding keeping it visible. This is what removing an element from the document flow means.

    There are however applications for this technique. For example, it works for vertically centering by using top or bottom together with translateY().

    .container-4 { position: relative; } .quote-4 { position: absolute; left: 50%; transform: translateX(-50%); }

    My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.

    Tasha Yar about Data

    Solution 5: Using Flexible Box Layout Module (IE10+ with vendor prefix)

    .container-5 { display: flex; justify-content: center; }

    My friend Data. You see things with the wonder of a child. And that makes you more human than any of us.

    Tasha Yar about Data

    Faites les marges gauche et droite de votre auto UL et atsortingbuez-lui une largeur:

     #headermenu ul { margin: 0 auto; width: 620px; } 

    Edit: Comme l’a suggéré kleinfreund, vous pouvez également centrer l’alignement du conteneur et lui donner un affichage en bloc intégré, mais vous devez également donner aux LI un affichage à gauche ou un affichage en ligne.

     #headermenu { text-align: center; } #headermenu ul { display: inline-block; } #headermenu ul li { float: left; /* or display: inline; */ } 
     ul { width: 90%; list-style-type:none; margin:auto; padding:0; position:relative; left:5%; } 

    Vous pouvez vérifier cela a résolu votre problème …

      #headermenu ul{ text-align: center; } #headermenu li { list-style-type: none; display: inline-block; } #headermenu ul li a{ float: left; } 

    http://jsfiddle.net/thirtydot/VCZgW/

    Vous pouvez rapidement et facilement centrer l’ ul avec CSS Flexbox .

     #headermenu { display: flex; justify-content: center; /* center ul horizontally */ align-items: center; /* center ul vertically (if necessary) */ width: 800px; height: 70px; margin-bottom: 10px; }