Search code examples
cssmenucenter

How to center this menu with CSS?


This is the menu I'm talking about

I'm having this problem that I can't solve... why it doesn't center?

I've already tried a lot of stuff here on this website, but I can't understand it all, can someone help me with the problem and try to explain to me how to center it?

.menu {
	font: 16px 'Dosis', sans-serif; font-weight: 600;
	list-style: none;
	margin: 0;
	padding: 0;
	float: left;
	border: none;
}

.menu li { position: relative; float: left; }

.menu li a {
	color: #FFF;
	text-decoration: none;
	list-style: none;
	display: block;
	margin: 0;
	padding: 14px 11px 0 11px;
	height: 36px;
}

.menu li a:hover { color: #FC0; background: #F60 url(../imgs/bg-menu2.png) repeat-x; }

.menu li ul {
	position: absolute;
	margin: 0;
	padding: 0;
	top: 50px;
	left: 0px;
	background: #F60;
	display: none;
	float: left;
	z-index: 999;
}

.menu li:hover ul ul { display: none; }

.menu li:hover ul, .menu li ul li:hover ul, .menu li.over ul, .menu li ul li.over ul { display: block; }

.menu li ul li { display: block; width: 200px; }

li.border0 { border: none; }

.menu li ul li ul {	z-index: 998; top: 0px; left: 200px; }

body { behavior: url(csshover.htc); }
<ul class="menu">
    <li><a href="../index.html" title="INÍCIO">INÍCIO</a></li>
    <li><a href="../apresentacao.html" title="PROGRAMAÇÃO">PROGRAMAÇÃO</a></li>
    <li><a href="../comissao.html" title="HOSPEDAGEM">HOSPEDAGEM</a></li>
    <li><a href="../local.html" title="SUBMISSÃO">SUBMISSÃO</a></li>
    <li><a href="../inscricao.html" title="LOCAL">LOCAL</a></li>
    <li><a href="../trabalhos.html" title="INSCRIÇÃO">INSCRIÇÃO</a></li>
    <li><a href="../programacao.html" title="CONTATO">CONTATO</a></li>    
	</ul>


Solution

  • You need to set text-align: center for .menu and unfloat a menu items:

    (I've added background: teal; for visibility of white words)

    .menu {
    	font: 16px 'Dosis', sans-serif; font-weight: 600;
        text-align: center;
    	list-style: none;
    	margin: 0 auto;
    	padding: 0;
    	background: teal;
    }
    
    .menu li { 
      display: inline-block;
      position: relative;
     }
    
    .menu li a {
    	color: #FFF;
    	text-decoration: none;
    	list-style: none;
    	display: block;
    	margin: 0;
    	padding: 14px 11px 0 11px;
    	height: 36px;
    }
    
    .menu li a:hover { color: #FC0; background: #F60 url(../imgs/bg-menu2.png) repeat-x; }
    
    .menu li ul {
    	position: absolute;
    	margin: 0;
    	padding: 0;
    	top: 50px;
    	left: 0px;
    	background: #F60;
    	display: none;
    	float: left;
    	z-index: 999;
    }
    
    .menu li:hover ul ul { display: none; }
    
    .menu li:hover ul, .menu li ul li:hover ul, .menu li.over ul, .menu li ul li.over ul { display: block; }
    
    .menu li ul li { display: block; width: 200px; }
    
    li.border0 { border: none; }
    
    .menu li ul li ul {	z-index: 998; top: 0px; left: 200px; }
    
    body { behavior: url(csshover.htc); }
    <ul class="menu">
        <li><a href="../index.html" title="INÍCIO">INÍCIO</a></li>
        <li><a href="../apresentacao.html" title="PROGRAMAÇÃO">PROGRAMAÇÃO</a></li>
        <li><a href="../comissao.html" title="HOSPEDAGEM">HOSPEDAGEM</a></li>
        <li><a href="../local.html" title="SUBMISSÃO">SUBMISSÃO</a></li>
        <li><a href="../inscricao.html" title="LOCAL">LOCAL</a></li>
        <li><a href="../trabalhos.html" title="INSCRIÇÃO">INSCRIÇÃO</a></li>
        <li><a href="../programacao.html" title="CONTATO">CONTATO</a></li>    
    	</ul>