html - Other divs moving when list appears as dropdown in navbar -
i'm bit new css please forgive me shortcomings side.
i working on website , have made navbar in have few divs.
of divs contain list kind of drops down when hover of them. problem makes div increase in width , makes other divs around move around.
have observed width increases width of lengthiest element in list.
have no idea causing , have tried playing position nothing seems work.
stop moving around of above divs doesn't weird.
html:
<body> <div class="header" id="banner"></div> <div class="header" id="navbar"> <div class="nb_item">home</div> <div class="nb_item">about <ul id="abtus_menu"> <li>la martiniere college, lucknow</li> <li>la martiniere model united nations</li> <li>secretariat</li> </ul> </div> <div class="nb_item">register <ul id="rgstr_menu"> <li>individual</li> <li>delegation</li> </ul> </div> <div class="nb_item">committees <ul id="comt_menu"> <li>committee 1</li> <li>committee 2</li> <li>committee 3</li> <li>committee 4</li> <li>committee 5</li> <li>committee 6</li> </ul> </div> <div class="nb_item">resources</div> <div class="nb_item">events <ul id="evnt_menu"> <li>keynote speakers</li> <li>socials</li> </ul> </div> <div class="nb_item">sponsors</div> <div class="nb_item">contact us</div>
css:
.html { background-color: #ffffff; } body { margin: 0; padding:0; } .header { width: 80%; margin:auto; padding:0; display:flex; } #banner { height: 200px; width: 80%; background-color: #eeeeee; } #navbar { background-color: #70a5da; height: 28px; } .menu { list-style-type:none; } .nb_item { display:inline-block; width:auto; padding:0px 10px; color:#ffffff; margin:auto; text-align:center; line-height:28px; } .nb_item:hover { color: #dddddd; cursor:pointer; } #abtus_menu li { list-style-type:none; display:none; } .nb_item:hover #abtus_menu li { display:block; color: #dddddd; } #comt_menu li { list-style-type:none; display:none; } .nb_item:hover #comt_menu li { display:block; } #rgstr_menu li { list-style-type:none; display:none; } .nb_item:hover #rgstr_menu li { display:block; } #evnt_menu li { list-style-type:none; display:none; } .nb_item:hover #evnt_menu li { display:block; } .menu li { display:inline; }
here's jsfiddle : jsfiddle (zoom out 50% if there problems viewing)
it's because divs part of document flow, hence when displayed, pushes out. need use position absolute on them this:
.nb_item {position: relative} /* allows it's child absolute elements adhere parents position (.nb_item) */
and then:
.nb_item ul {position: absolute; top: 100%; left: 0} /* absolutely positions child (drop down menu) element it's taken out of document flow, hence not affecting positions of other elements */
hope helps!
edit: absolutely positioned elements should have width defined, otherwise take on width of widest non-breaking word. try this:
.nb_item ul {width: 200px; border: 1px solid #000} .nb_item ul li {text-align: left} .nb_item ul li {padding: 10px}
Comments
Post a Comment