CSS樣式寫在JSP代碼中的幾種方法


1、行內樣式。

可以直接把css代碼寫在現有的HTML標簽元素的開始標簽里面,並且css樣式代碼要寫在style=" "雙引號中才可以,

如:

<p style="color:red">這里文字是紅色。</p>

2、內部樣式。

如果想讓HTML標簽和css樣式區分開來,可以采取將css樣式代碼寫在<style type="text/css"> </style>標簽之間。

這里的<style></style>可以寫在<head></head>標簽之間或者<body></body>標簽之間。

<head>
<style type="text/css">  
    body {background-color: red}  
    p {margin-left: 20px}
</style>  
</head>

3、外部樣式

將css單獨放置於一個文件中,在jsp中通過link引入。

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Login</title>
  <link rel="stylesheet" type="text/css" href="style.css"/>   <!--引入css樣式-->
</head>
<body>

<%
  Object message = request.getAttribute("message");
  if(message!=null && !"".equals(message)){
%>
<script type="text/javascript">
  alert("<%=request.getAttribute("message")%>");
</script>
<%  }  %>

<div id="login">
  <h1>Login</h1>
  <form action="Servlet?method=login" method="post" onsubmit="return check()">
    <input type="text" required="required"  placeholder="請輸入賬號" name="id"></input>
    <input type="password" required="required"  placeholder="請輸入密碼" name="password"></input>
    <button class="but" type="submit">登錄</button>
  </form>
</div>
</body>
</html>

style.css

html{
    width: 100%;
    height: 100%;
    overflow: hidden;
}
body{
    width: 100%;
    height: 100%;
    font-family: 'Open Sans',sans-serif;
    margin: 0;
    background-color: #4A374A;
}
#login{
    position: absolute;
    top: 50%;
    left:50%;
    margin: -150px 0 0 -150px;
    width: 300px;
    height: 300px;
}
#login h1{
    color: #fff;
    text-shadow:0 0 10px;
    letter-spacing: 1px;
    text-align: center;
}
h1{
    font-size: 2em;
    margin: 0.67em 0;
}
input{
    width: 278px;
    height: 18px;
    margin-bottom: 10px;
    outline: none;
    padding: 10px;
    font-size: 13px;
    color: #fff;
    text-shadow:1px 1px 1px;
    border-top: 1px solid #312E3D;
    border-left: 1px solid #312E3D;
    border-right: 1px solid #312E3D;
    border-bottom: 1px solid #56536A;
    border-radius: 4px;
    background-color: #2D2D3F;
}
.but{
    width: 300px;
    min-height: 20px;
    display: block;
    background-color: #4a77d4;
    border: 1px solid #3762bc;
    color: #fff;
    padding: 9px 14px;
    font-size: 15px;
    line-height: normal;
    border-radius: 5px;
    margin: 0;
}

 

css文件位置與對應jsp文件位置一樣即可

idea新建css文件方法:https://jingyan.baidu.com/article/636f38bb4ad6fed6b9461061.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM