2016-09-12 166 views
0

我需要水平居中嵌入塊的子元素。問題是子子菜單是可變寬度,可以比其父母寬。我最初的解決方案是將子元素設置爲左側&右側-50px,但這不是一個真正的可變寬度,而是靜態設置爲比其父元素寬100px。中心絕對定位的子元素

.main-menu { 
 
    text-align: left; 
 
    margin: 0; 
 
    padding: 0; 
 
    font-size: 0px; 
 
    font-size: 0rem; 
 
} 
 
.main-menu li { 
 
    font-size: 18px; 
 
    font-size: 1.8rem; 
 
    line-height: 1em; 
 
    list-style-type: none; 
 
    text-align: center; 
 
    display: inline-block; 
 
    zoom: 1; 
 
    padding: 0 25px; 
 
    position: relative; 
 
} 
 
li > a { 
 
    display: inline-block; 
 
    zoom: 1; 
 
    padding: 21px 0 19px; 
 
    font-family: "Open Sans", sans-serif; 
 
    font-weight: 400; 
 
    color: #282828; 
 
    font-size: 16px; 
 
    font-size: 1.6rem; 
 
    line-height: 1.2em; 
 
    text-decoration: none; 
 
    -webkit-transition: all .25s ease-in-out, background-image 0s, background-position 0s; 
 
    transition: all .25s ease-in-out, background-image 0s, background-position 0s; 
 
} 
 
.sub-menu { 
 
    position: absolute; 
 
    top: 100%; 
 
    left: -50px; 
 
    right: -50px; 
 
    max-width: 250px; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    margin-bottom: 0; 
 
    min-width: 150px; 
 
    background-color: white; 
 
    text-align: left; 
 
    padding: 17px 0; 
 
    box-shadow: 0 2px 5px #dcdcdc inset; 
 
    border-top: 18px solid #ffdd00; 
 
    }
<ul class="main-menu clearfix"><li id="menu-item-657" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children first menu-item-657"> 
 
<li id="menu-item-657" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children first menu-item-657"><a href="link">Solutions</a> 
 
<ul class="sub-menu"> 
 
\t <li id="menu-item-220469" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-220469"><a href="link">Solutions</a></li> 
 
\t <li id="menu-item-18009" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-18009"><a href="link">How It Works</a></li> 
 
\t <li id="menu-item-10344" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-10344"><a href="link">Why Use It</a></li> 
 
\t <li id="menu-item-3375" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3375"><a href="link">Products</a></li> 
 
</ul> 
 
</li> 
 
</ul>

+0

'的text-align:center',而不是'left'爲'子menu'爲你的作品? – kukkuz

回答

1

這就是我通常這種情況做:

.parent { 
 
    /*....your existing css...*/ 
 
    position: relative; 
 
} 
 
.sub-menu { 
 
    display: block; 
 
    left: 50%; 
 
    text-align: center; 
 
    position: absolute; 
 
    transform: translateX(-50%); 
 
}
<div class="parent"> 
 
    <div class="sub-menu"> 
 
    Centered 
 
    </div> 
 
</div>

+0

真棒,工作一個治療:)謝謝! –

0

我上週就遇到了這個問題,這樣的固定我的問題:

li > a { 
    ....your existing css... 
    position: relative; 
} 
.sub-menu { 
    display: block; 
    left: 0; 
    right: 0; 
    text-align: center; 
    position: absolute; 
    margin-left: -100%; 
    margin-right: -100%; 
} 

在我的情況,.sub-menuli > a更寬,但我想.sub-menu到集中在li > a之下。這是你遇到的問題嗎?

+0

嘿 - 感謝您花時間回答,是的,這是我遇到的問題,但不幸的是上述不會工作,這是因爲底部的菜單是不是真正的動態寬度,但仍然基於它的父母和一些較小的子菜單結束太寬:( –