简体   繁体   English

CSS,在导航中居中元素

[英]CSS, centering elements in the nav

Simple question, what's wrong?简单的问题,怎么了? I've got menu and logo and I can't center this.我有菜单和标志,但不能居中。 I don't know why.我不知道为什么。

My CSS code:我的 CSS 代码:

 #header { width: 100%; background-color: #515963; } #header #logo { width: 200px; margin-left: 80px; padding: 20px; float: left; } #header nav { float: right; margin-right: 80px; line-height: 104px; } #header nav .menuItem { float: right; color: #fff; text-transform: uppercase; font-size: 22px; letter-spacing: 0.8px; font-weight: 500; padding: 10px 30px 10px 10px; } #header nav .menuItem .item { cursor: pointer; #header #logo { display: block; width: 200px; margin: 0 auto; clear: both; } #header nav { clear: both; width: 369px; margin: 0 auto; line-height: 104px; } #header nav .menuItem { padding: 0; }
 <div id="header"> <a href="#"><img id="logo" src="img/logo.png" alt="AFP"/></a> <nav> <div class="menuItem item1"><span class="item">kontakt</span></div> <div class="menuItem item2"><span class="item">produkty</span></div> <div class="menuItem item3"><span class="item">o nas</span></div> <div style="clear:both;"></div> </nav> <div style="clear:both;"></div> </div>

and what I've got:以及我所拥有的:

在此处输入图片说明

I'm using div s in nav because I like do it this way.我在导航中使用div是因为我喜欢这样做。 I want the logo and the menu to be centered.我希望徽标和菜单居中。 I know the width and I don't understand why margin: 0 auto;我知道宽度,但我不明白为什么margin: 0 auto; doesn't work.不起作用。

Edit:编辑:

I want to reach such thing:我想达到这样的事情:

在此处输入图片说明

Simple error.简单的错误。 You need id not tag http://joxi.ru/eAOYEqBuxvRD9m你需要身份而不是标签http://joxi.ru/eAOYEqBuxvRD9m

The best way for this situations is flexbox.这种情况的最佳方法是 flexbox。

 #header{ display:flex; justify-content:center; width: 100%; flex-wrap:wrap; -webkit-flex-wrap:wrap; } #header #logo { width:100px; } nav{ display:flex; justify-content:center; width: 100%; } nav div { margin: 10px; }
 <div id="header"> <a href="#"><img id="logo" src="https://www.gravatar.com/avatar/087039a00851e75ff483470e3aba89c9?s=48&d=identicon&r=PG" alt="AFP"/></a> <nav> <div class="menuItem item1"><span class="item">kontakt</span></div> <div class="menuItem item2"><span class="item">produkty</span></div> <div class="menuItem item3"><span class="item">o nas</span></div> </nav> <div style="clear:both;"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM