像新浪微博 最上面一行导航, 是固定div 其实就是div的css的position属性。
position定位属性含义:
static
使用fixed属性
html代码如下(导航并不好看,后面再改~~):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'head.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" type="text/css" href="public/css/head.css" ></link>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">长颈鹿首页</a>
</li>
<li><a href="#">我的空间</a>
</li>
<li><a href="#">博客管理</a>
</li>
<li><a href="#">文档</a>
</li>
<li><a href="#">视频</a>
</li>
<li><a href="#">搜索</a>
</li>
<li><a href="#">其他</a>
</li>
</ul>
<div id="userstate" >
<s:if test="#session.user" >
${ sessionScope.user.username }--${ sessionScope.lastlogin }
</s:if>
<s:else>
请登录
</s:else>
</div>
</div>
</body>
</html>
css代码:
#nav {
position:fixed;
top:0px;
text-align: center;
font-size: 13px;
font-family: Arial, Helvetica, sans-serif;
height: 30px;
width: 100%;
background-color:#00688B;
opacity:0.7;
border: solid 1px red;
}
#nav ul {
margin-top:0px;
margin-left:auto;
margin-right:auto;
padding: 0px;
list-style-type: none;
width:700px;
}
#nav li {
float: left;
}
#nav li a {
text-decoration: none;
color: #EEE;
width: 100px;
line-height: 26px;
display: block;
}
#nav li a:hover {
color: #CC0000;
}
#userstate{
top:-3px;
position:relative;
left:50px;
margin:0px;
padding:0px;
color:red;
float:left;
border:solid 1px green;
}