居中子菜单(子菜单,悬停菜单)

时间:2016-11-27 14:23:34

标签: wordpress

请参阅我的项目:http://www.lightertaste.com/

如果您查看主导航(浏览食谱之后),每个菜单项都有一个子菜单,但我似乎无法将它们完全置于其父菜单的中心位置。

任何帮助将不胜感激!



/*
Site Navigation
----------------------------------------------------------------------------- */

.genesis-nav-menu:before {
	margin-right: 10px;
	content: "BROWSE THE RECIPES:";

	font-family: 'arquitecta-black';
	color: #f15c58;
	font-size: 13px;
	letter-spacing: 1px;
}

.genesis-nav-menu {
	width: 100%;
	clear: both;
	border-top: 1px solid #dcdcdc;
	border-bottom: 1px solid #dcdcdc;
	padding: 10px 0px;
	text-align: center;
}

nav.nav-primary div.wrap {
	border-top: 1px solid #dcdcdc;
	border-bottom: 1px solid #dcdcdc;
	padding: 2px 0px;
}

.genesis-nav-menu .menu-item {
	display: inline-block;
}

.genesis-nav-menu a {
	display: inline-block;
	position: relative;
	padding-left: 9px;
	padding-right: 12px;

	font-family: 'arquitecta-medium';
	color: #4a4947;
	font-size: 13px;
	letter-spacing: 1px;
	text-decoration: none;
	text-transform: uppercase;
}

.menu-item:last-child a {
	padding-right: 0px;
}

.genesis-nav-menu a:hover {
	color: #f15c58;
}

li#menu-item-281, li#menu-item-119, li#menu-item-120, li#menu-item-121, li#menu-item-124 {
	padding-right: 4px;
}

li#menu-item-281:after, li#menu-item-119:after, li#menu-item-120:after, li#menu-item-121:after, li#menu-item-124:after {
	display: inline-block;
	content: "/";
	color: #b1b1b1;
}

/* Sub Navigation
--------------------------------------------- */

.menu-primary li {
	position: relative;
}

.genesis-nav-menu .menu-item:hover > .sub-menu,
.genesis-nav-menu .menu-item:focus > .sub-menu,
.genesis-nav-menu .menu-item.menu-item-hover > .sub-menu  {
	left: 50%;
	transform: translateX(-50%);
}

.genesis-nav-menu .sub-menu {
	position: absolute;
	left: -9999px;
	z-index: 2000;
	width: 200px;
}

.genesis-nav-menu .sub-menu:before {
	content: '';
	display: block;
	margin: auto;
	width: 0;
	height: 0;
	border-left: 10px solid transparent;
	border-right: 10px solid transparent;
	border-bottom: 10px solid #4a4947;
}

.genesis-nav-menu .sub-menu a {
	position: relative;
	width: 200px;
	padding: 10px;
	background-color: #4a4947;

	color: #ffffff;
	font-size: 11px;
	letter-spacing: 1px;
}

.genesis-nav-menu .sub-menu a:hover {
	background-color: #f15c58;
}

<h2 class="screen-reader-text">Main navigation</h2><nav class="nav-primary" itemscope itemtype="http://schema.org/SiteNavigationElement" id="genesis-nav-primary" aria-label="Main navigation"><div class="wrap"><ul id="menu-main" class="menu genesis-nav-menu menu-primary"><li id="menu-item-281" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-281"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Appetizers &#038; Snacks</span></a>
<ul class="sub-menu">
	<li id="menu-item-297" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-297"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Dips &#038; Spreads</span></a></li>
	<li id="menu-item-389" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-389"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Meat &#038; Fish</span></a></li>
	<li id="menu-item-390" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-390"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Fruits &#038; Vegetables</span></a></li>
</ul>
</li>
<li id="menu-item-119" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-119"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Breakfast</span></a>
<ul class="sub-menu">
	<li id="menu-item-299" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-299"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Eggs &#038; Omelettes</span></a></li>
	<li id="menu-item-298" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-298"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Pancakes &#038; Waffles</span></a></li>
	<li id="menu-item-260" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-260"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Oatmeal &#038; Granola</span></a></li>
	<li id="menu-item-259" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-259"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Breads, Muffins, &#038; Pastries</span></a></li>
	<li id="menu-item-261" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-261"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Shakes &#038; Smoothies</span></a></li>
</ul>
</li>
<li id="menu-item-120" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-120"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Lunch</span></a>
<ul class="sub-menu">
	<li id="menu-item-258" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-258"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Sandwiches &#038; Wraps</span></a></li>
	<li id="menu-item-262" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-262"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Salads &#038; Power Bowls</span></a></li>
	<li id="menu-item-263" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-263"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Soups &#038; Stews</span></a></li>
</ul>
</li>
<li id="menu-item-121" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-121"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Dinner</span></a>
<ul class="sub-menu">
	<li id="menu-item-279" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-279"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Quick &#038; Easy</span></a></li>
	<li id="menu-item-278" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-278"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Beef &#038; Pork</span></a></li>
	<li id="menu-item-276" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-276"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Chicken &#038; Poultry</span></a></li>
	<li id="menu-item-277" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-277"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Seafood &#038; Shellfish</span></a></li>
	<li id="menu-item-273" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-273"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Casseroles &#038; One-Pans</span></a></li>
	<li id="menu-item-274" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-274"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Pastas &#038; Risottos</span></a></li>
	<li id="menu-item-275" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-275"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Pizzas &#038; Flatbreads</span></a></li>
</ul>
</li>
<li id="menu-item-124" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-124"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Desserts</span></a>
<ul class="sub-menu">
	<li id="menu-item-264" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-264"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Cakes &#038; Pies</span></a></li>
	<li id="menu-item-265" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-265"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Cookies &#038; Brownies</span></a></li>
	<li id="menu-item-288" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-288"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Ice Creams &#038; Sorbets</span></a></li>
</ul>
</li>
<li id="menu-item-123" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-123"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Sides</span></a>
<ul class="sub-menu">
	<li id="menu-item-392" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-392"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Vegetables &#038; Potatoes</span></a></li>
	<li id="menu-item-391" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-391"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Rice &#038; Noodles</span></a></li>
	<li id="menu-item-122" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-122"><a href="http://www.livingthelighterlife.com/sample-page/" itemprop="url"><span itemprop="name">Breads &#038; Rolls</span></a></li>
</ul>
</li>
</ul></div></nav>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的子菜单实际上是正确居中的,但由于分隔符,它看起来似乎不是:/ menu / 如果您考虑菜单周围的填充,并且所有菜单都是200px宽,您可以使用:

 .genesis-nav-menu .menu-item:hover > .sub-menu,
.genesis-nav-menu .menu-item:focus > .sub-menu,
.genesis-nav-menu .menu-item.menu-item-hover > .sub-menu  {
    left: 50%;
    margin-left: -110px;
    /* instead of
    transform: translateX(-50%); */
}

但是douse margin-left:第一个和最后一个元素是100px:

为此,请使用以下选择器添加新规则:

 .genesis-nav-menu .menu-item:hover > .sub-menu:first-child,
    .genesis-nav-menu .menu-item:hover > .sub-menu:last-child,
    .genesis-nav-menu .menu-item:focus > .sub-menu:first-child,
    .genesis-nav-menu .menu-item:focus > .sub-menu:last-child,
    .genesis-nav-menu .menu-item.menu-item-hover > .sub-menu:first-childfor,
    .genesis-nav-menu .menu-item.menu-item-hover > .sub-menu:last-child {
         left: 50%;
         margin-left: -100px;
 }