Home >Web Front-end >HTML Tutorial >Please help me see why ul and a in my code cannot be displayed on the same line. _html/css_WEB-ITnose
我现在用ul做了一个横向菜单,想把它放在“home”连接的右边,但我发现它运行时ul菜单始终会换行,是不是css出了问题,请大家给我指教一下,谢谢。建议用firefox浏览器运行。
383eb734b02b508089ba2d78eb4c6f6893f0f5c25f18dab9d176bd4f6de5d30e
8bf70fa7ec24bdc478280e25c2cad5aa
b2386ffb911b14667cb8f0f91ea547a76e916e0f7d1e588d4f442bf645aedb2f
c9ccee2e6ea535a969eb3f532ad9fe89
ul { padding:0; margin:0; list-style:none;}
li { float:left; width:140px; }
ul li a { text-decoration:none; color:#777;}
li:hover { background-color:#ddd;}
ul li ul li table{ background-color:#ddd;}
li ul { display:none;}
li:hover ul,.over ul { display:block;}
531ac245ce3e4fe3d50054a55f265927
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
0236d3d45055ab08c7e0e82231875578
Home
5db79b134e9f6b82c0b36e0489ee08ed
|
ff6d136ddc5fdfeffaf53ff6ee95f185
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5cmenu15db79b134e9f6b82c0b36e0489ee08ed
ff6d136ddc5fdfeffaf53ff6ee95f185
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest15db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest25db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest35db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest45db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
bed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c6bf105d2543d3b6e2565ed52cc2866d3dmenu25db79b134e9f6b82c0b36e0489ee08ed
ff6d136ddc5fdfeffaf53ff6ee95f185
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest55db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest65db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
bed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
在线等,请大家帮忙。
ul是块级元素,当然会换行,你可以强制它inline,其它自己调吧。
ul { display:inline}
把css的最后一行改成li:hover ul,.over ul { display:inline;}也不行,没办法只能用类似下面的坐标定位来调整了:
ul{
position:absolute;
top: 10px;
margin-left:100px;
}
<html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title></title> <style> ul { padding:0; margin:0; list-style:none;display: inline;} li { float:left; width:140px; } ul li a { text-decoration:none; color:#777;} li:hover { background-color:#ddd;} ul li ul li table{ background-color:#ddd;} li ul { display:none;} li:hover ul,.over ul { display:block;} </style></head><body><div style="float: left;"> <a style="color:red;" > Home </a> | </div><ul style="float: left;"> <li><a href="">menu1</a> <ul> <li><a href="">test1</a></li> <li><a href="">test2</a></li> <li><a href="">test3</a></li> <li><a href="">test4</a></li> </ul> </li> <li><a href="javascript:void(0);">menu2</a> <ul> <li><a href="">test5</a></li> <li><a href="">test6</a></li> </ul> </li></ul></body></html>
oNA_na:你的代码可以把位置调整,但也带来了新的问题,在929d1f5ca49e04fdcb27f9465b944689后面的代码在鼠标移到菜单上去就会向右浮动。代码如下:
383eb734b02b508089ba2d78eb4c6f68
93f0f5c25f18dab9d176bd4f6de5d30e
8bf70fa7ec24bdc478280e25c2cad5aa
b2386ffb911b14667cb8f0f91ea547a76e916e0f7d1e588d4f442bf645aedb2f
c9ccee2e6ea535a969eb3f532ad9fe89
ul { padding:0; margin:0; list-style:none;display: inline;}
li { float:left; width:140px; }
ul li a { text-decoration:none; color:#777;}
li:hover { background-color:#ddd;}
ul li ul li table{ background-color:#ddd;}
li ul { display:none;}
li:hover ul,.over ul { display:block;}
531ac245ce3e4fe3d50054a55f265927
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
3124346daf0e7fceea033c2e82c1b9a7
0236d3d45055ab08c7e0e82231875578
Home
5db79b134e9f6b82c0b36e0489ee08ed
|
16b28748ea4df4d9c2150843fecfba68
b7f11e7d963cfecc4acec26a07f198a5
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5cmenu15db79b134e9f6b82c0b36e0489ee08ed
ff6d136ddc5fdfeffaf53ff6ee95f185
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest15db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest25db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest35db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest45db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
bed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c6bf105d2543d3b6e2565ed52cc2866d3dmenu25db79b134e9f6b82c0b36e0489ee08ed
ff6d136ddc5fdfeffaf53ff6ee95f185
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest55db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
25edfb22a4f469ecb59f1190150159c66170e8999ab19ec641e0422470b16d5ctest65db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
bed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
dc6dce4a544fdca2df29d5ac0ea9906b
076402276aae5dbec7f672f8f4e5cc81
test data 1 test data 1 test data 1 test data 1 test data 1 test data 1
076402276aae5dbec7f672f8f4e5cc81
test data 2
16b28748ea4df4d9c2150843fecfba68
076402276aae5dbec7f672f8f4e5cc81
6170e8999ab19ec641e0422470b16d5ctest data 3 test data 3 test data 3test data 3 test data 3 test data 3test data 3 test data 3 test data 35db79b134e9f6b82c0b36e0489ee08ed
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
这个定位问题很棘手也很急,我实在想不出来好的办法了,请大家帮忙。
我重新修改了一下css代码,定位正常了,但鼠标移到菜单上后面的元素就会往后移。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <title>prod menus</title> <style> ul.hMenu,ul.hMenu li ul { padding:0; margin:0; list-style:none;} ul.hMenu li { float:left; width:140px; } ul.hMenu li:hover a { color:red;} ul.hMenu li:hover { background-color:yellow;} ul.hMenu li ul li table{ background-color:yellow;} ul.hMenu li ul { display:none;} ul.hMenu li:hover ul,.over ul { display:block;} .eth{text-align:left;} </style> </head> <body> <div style="float: left;"> <a style="color:red;" > Home </a> | </div> <ul class="hMenu"> <li><a href="">menu1</a> <ul> <li><a href="">test1</a></li> <li><a href="">test2</a></li> <li><a href="">test3</a></li> <li><a href="">test4</a></li> </ul> </li> <li><a>menu2 </a> <ul> <li><a href="">test5</a></li> <li><a href="">test6</a></li> </ul> </li> </ul> <br/> test data 1 test data 1 test data 1 test data 1 test data 1 test data 1 <br/> test data 2 <br/> <a href="">test data 3 test data 3 test data 3test data 3 test data 3 test data 3test data 3 test data 3 test data 3</a> </body></html>
通过relative定位应该可以达到要求吧。
在上面代码的基础上,我改了一下。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <title>prod menus</title> <style> /*ul.hMenu,ul.hMenu li ul { padding:0; margin:0; list-style:none;}*/ /*ul.hMenu li { float:left; width:140px; }*/ /*ul.hMenu li:hover a { color:red;} */ /*ul.hMenu li:hover { background-color:yellow;}*/ /*ul.hMenu li ul li table{ background-color:yellow;} */ /*ul.hMenu li ul { display:none;}*/ /*ul.hMenu li:hover ul,.over ul { display:block;}*/ /**/ /*.eth{text-align:left;}*/ .hMenu { margin: 0; padding: 0; z-index: 30; width: 480px; height:23px;} .hMenu li { margin: 0; padding: 0; list-style: none; float: left; width:140px; } .hMenu li a { display: block; width: 140px; color:red; text-align: left; text-decoration: none} .hMenu li a:hover { background: yellow} .hMenu div { position: absolute; visibility: hidden; margin: 0; padding: 0; } .hMenu div a { position: relative; display: block; width: 140px; white-space: nowrap; text-align: left; text-decoration: none; background: yellow; color:red } </style> <script type="text/javascript"> var timeout = 100; var delaytime = 0; var ddmenuitem = 0; function mopen(id) { mcancelclosetime(); if(ddmenuitem) ddmenuitem.style.visibility = 'hidden'; ddmenuitem = document.getElementById(id); ddmenuitem.style.visibility = 'visible'; } function mclosedelay() { delaytime = window.setTimeout(mclose, timeout); } function mclose() { if(ddmenuitem) ddmenuitem.style.visibility = 'hidden'; } function mcancelclosetime() { if(delaytime) { window.clearTimeout(delaytime); delaytime = null; } } </script> </head> <body> <div style="float: left;"> <a style="color:red;" > Home </a> | </div> <ul class="hMenu"> <li><a href="" onmouseover="mopen('m1')" onmouseout="mclosedelay()">menu1</a> <div id="m1" onmouseover="mcancelclosetime()" onmouseout="mclosedelay()"> <a href="">test1</a> <a href="">test2</a> <a href="">test3</a> <a href="">test4</a> </div> </li> <li><a href="" onmouseover="mopen('m2')" onmouseout="mclosedelay()">menu2 </a> <div id="m2" onmouseover="mcancelclosetime()" onmouseout="mclosedelay()"> <a href="">test5</a> <a href="">test6</a> </div> </li> </ul> <br/> test data 1 test data 1 test data 1 test data 1 test data 1 test data 1 <br/> test data 2 <br/> <a href="">test data 3 test data 3 test data 3test data 3 test data 3 test data 3test data 3 test data 3 test data 3</a> </body></html>
lichuny257,你的代码可用,可以用纯css实现此效果吗?