css如何设置列表的,横排,或者竖排?

html-css016

css如何设置列表的,横排,或者竖排?,第1张

CCS如何设置列表的横排或者竖排

请看案例:

让二级菜单变成一行,只需要在竖排的效果上,让二级菜单都浮动起来,这样就在一行了。

下面是简单的代码实现,仅供参考:

<style>

* {margin:0pxpadding:0px}

li {list-style:nonewidth:100pxheight:30pxfont-size:14pxtext-align:leftline-height:30pxborder:1px solid #000position:relative}

.box>li {float:leftposition:relative}

.son {position:absolutetop:30pxleft:-1pxwidth:306px}

.son>li {float:left} /*浮动后,二级菜单就在一行了*/

.grason {position:absolutetop:-1pxleft:100px}

.son,.grason {display:none}

.active {display:blockbackground:#FF0}

</style>

<script>

window.onload=function(){

var aLi = document.getElementsByTagName('li')

for(var i=0i<aLi.lengthi++)

{

/*给一级菜单加鼠标移入,移出事件,让二级菜单显示,隐藏*/

aLi[i].onmouseover = function(){

this.className = 'active'

var oSon = this.getElementsByTagName('ul')[0]

if(oSon)

{

oSon.style.display='block'

}

}

aLi[i].onmouseout = function(){

this.className = ''

var oSon = this.getElementsByTagName('ul')[0]

if(oSon)

{

oSon.style.display='none'

}

}

}

}

</script>

</head>

<body>

<ul class="box">

<li>首页</li>

<li>公司简介

<!--创建的二级菜单-->

<ul class="son">

<li>大事件</li>

<li>领导致辞</li>

<li>企业文化</li>

</ul>

</li>

<li>联系我们</li>

<li>产品显示</li>

</ul>

</body>

1、新建一个html文件,命名为test.html,用于讲解。

2、在test.html文件内,创建一个div模块,并设置其class属性为mydiv。

3、在div模块内,使用img图片标签创建两张图片,src属性指向不同的图片路径。

4、在css标签内,使用“*”初始化页面所有元素的css样式,设置内边距为0,外边距为0。

5、在css标签内,设置div的样式,设置其宽度为700px,高度为400px,边框为1px,居中对齐。

6、在css标签内,设置图片的的大小,宽度为280px,高度为200px,为了使用图片水平排列,需要使用float属性设置图片浮动的统一方向,例如,这里设置统一浮动向左。

7、在浏览器打开test.html文件,查看图片水平排列的效果。

两种方法:

一种是左浮动:{float:left}

Float常跟属性值left、right、none

Float:none 不使用浮动

Float:left 靠左浮动

Float:right 靠右浮动

float语法:

float : none | left |right

参数值:

none :  对象不浮动

left :  对象浮在左边

right :  对象浮在右边

<div class="divcss5">

<div class="divcss5_left">布局靠左浮动</div>

<div class="divcss5_right">布局靠右浮动</div>

<div class="clear"></div><!-- html注释:清除float产生浮动 -->

</div>

二种内联样式:{display:inline}。

例子:

<html>

<head>

<style type="text/css">

p {display: inline}

div {display: none}

</style>

</head>

<body>

<p>本例中的样式表把段落元素设置为内联元素。</p>

<p>而 div 元素不会显示出来!</p>

<div>div 元素的内容不会显示出来!</div>

</body>

</html>