使用HTML+CSS实现如图布局,border-width:5px,格子大小是50px*50px,hover时
边框变成红色,需要考虑语义化。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * { margin: 0; padding: 0; } .container { display: flex; width: 180px; height: 180px; flex-wrap: wrap; } .box { width: 50px; height: 50px; border: 5px solid blue; background-color:aqua; text-align: center; line-height: 50px; margin-right: -5px; } .box:hover { border: 5px solid red; z-index:9999; } </style> </head> <body> <div class="container"> <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> <div class="box">4</div> <div class="box">5</div> <div class="box">6</div> <div class="box">7</div> <div class="box">8</div> <div class="box">9</div> </div> </body> </html>
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>nine box</title> <style type="text/css"> table{border-collapse:collapse; margin:50px;text-align:center; } table tr {border:none;} table.tb2 td{width:50px;height:50px;border:5px inset blue;} table.tb2 td:hover{border:5px solid red;cursor: pointer;} </style> </head> <body> <table class="tb2"> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><td>4</td><td>5</td><td>6</td></tr> <tr><td>7</td><td>8</td><td>9</td></tr> </table> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> *{ margin: 0; padding: 0; } .container{ width: 180px; height: 180px; background-color: blue; } .box{ width: 50px; height: 50px; background-color: white; opacity: 0.5; float: left; border: 5px solid blue; text-align: center; line-height: 50px; } .box:hover{ border-color: red; cursor: pointer; } </style> </head> <body> <div class="container"> <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> <div class="box">4</div> <div class="box">5</div> <div class="box">6</div> <div class="box">7</div> <div class="box">8</div> <div class="box">9</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=\, initial-scale=1.0"> <title>Document</title> <style type ="text/css"> *{ margin:0; padding:0; list-style:none; } #cale{ /*position:relative;*/ width:170px; height:170px; background:pink; } ul{ /*position:absolute; margin-left:0px;*/ } ul li{ float:left; width:50px; height:50px; border:5px solid blue; margin-left:-5px; margin-bottom:-5px; text-align:center; line-height:50px; background-color:lawngreen; } .fir{ margin-left:0px; } ul li:hover{ position:relative; border-color:red; cursor:pointer; } </style> </head> <body> <div id="cale"> <ul> <li class="fir">1</li> <li>2</li> <li>3</li> <li class="fir">4</li> <li>5</li> <li>6</li> <li class="fir">7</li> <li>8</li> <li>9</li> </ul> </div> </body> </html>
<style type="text/css"> ul, li { padding: 0; margin: 0; } .warper { width: 170px; height: 170px; overflow: hidden; background-color: blue; } .item { box-sizing: boder-box; width: 50px; height: 50px; background-color: gray; float: left; list-style: none; line-height: 50px; text-align: center; border: 5px solid blue; margin: 0 -5px -5px 0; cursor: pointer; position: relative; } li:hover { border-color: yellow; z-index: 1; } </style> </head> <body> <ul class="warper"> <li class="item">1</li> <li class="item">2</li> <li class="item">3</li> <li class="item">4</li> <li class="item">5</li> <li class="item">6</li> <li class="item">7</li> <li class="item">8</li> <Li class="item">9</li> </ul> </body>
<div class="number-wrap"> <div class="number-item">1</div> ... <div class="number-item">9</div> </div>
.number-wrap { display: flex; justify-content: space-around; align-items: center; flex-wrap: wrap; width: 470px; } .number-item { margin-top: 5px; width: 150px; height: 150px; } .number-item:hover { border: 5px solid red; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>九宫格</title> <style> .contain{ width: 170px; height: 170px; background-color: blue; } .contain > div { width: 50px; height: 50px; background-color: white; float: left; margin-left: 5px; margin-top: 5px; /* */ text-align: center; line-height: 50px; } .contain > div:hover { box-shadow:0 0 0 5px yellow; } </style> </head> <body> <div class="contain"> <div> 1 </div> <div> 2 </div> <div> 3 </div> <div> 4 </div> <div> 5 </div> <div> 6 </div> <div> 7 </div> <div> 8 </div> <div> 9 </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <style> body{ background-color: antiquewhite; } table{ border-color: blue; border-width: 5px; } table td{ width: 50px; height: 50px; color:green; border-width: 5px; text-align: center; } table td:hover{ border-color: red; cursor: pointer; } </style> <body> <div> <table border="1" cellpadding='0' cellspacing="0"> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> </table> </div> </body> </script> </html>
.wrapper { width: 150px; display: flex; align-items: center; justify-content: center; flex-wrap: wrap; } .wrapper li { list-style: none; width: 50px; height: 50px; display: flex; align-items: center; justify-content: center; box-sizing: border-box; border: 5px solid blue; margin-left: -5px; margin-top: -5px; } .wrapper li:hover { border: 5px solid red; z-index: 1; }
使用flex布局和左margin和上margin为 -5px;并给外面的大盒子一个上padding <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } .container { display: flex; padding-top: 5px; flex-wrap: wrap; width: 170px; height: 170px; } .box { width: 60px; height: 60px; border: 5px solid blue; background-color: aqua; text-align: center; line-height: 50px; margin-right: -5px; margin-top: -5px; } .box:hover { border: 5px solid red; z-index: 9999; } </style> </head> <body> <div class="container"> <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> <div class="box">4</div> <div class="box">5</div> <div class="box">6</div> <div class="box">7</div> <div class="box">8</div> <div class="box">9</div> </div> </body> </html>