HTML+CSS实现:中央正方形被四个长方形环绕的大正方形背景图
要使用HTML和CSS创建一个包含中央正方形和包围它的四个长方形的大正方形图,可以视图如下构建: ```html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>正方形与长方形</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; /* 颜色 */ } .outer-square { position: relative; width: 300px; /* 大正方形的边长 */ height: 300px; /* 大正方形的边长 */ background-color: #d3d3d3; /* 外部长方形的色 */ } .inner-square { position: absolute; left: calc(50% - 50px); /* 中心对齐 */ top: calc(50% - 50px); /* 中心对齐 */ width: 100px; /* 内部正方形的边长 */ height: 100px; /* 内部正方形的边长 */ background-color: #4CAF50; /* 内部正方形的色 */ } .rectangle { position: absolute; background-color: #2196F3; /* 外部长方形的色 */ } .top-rectangle { top: -50px; /* 位置调整 */ left: 0; width: 300px; /* 外部长方形的宽度 */ height: 50px; /* 外部长方形的高度 */ } .bottom-rectangle { bottom: -50px; /* 位置调整 */ left: 0; width: 300px; height: 50px; } .left-rectangle { top: 0; left: -50px; /* 位置调整 */ width: 50px; height: 300px; } .right-rectangle { top: 0; right: -50px; /* 位置调整 */ width: 50px; height: 300px; } </style> </head> <body> <div class="outer-square"> <div class="inner-square"></div> <div class="rectangle top-rectangle"></div> <div class="rectangle bottom-rectangle"></div> <div class="rectangle left-rectangle"></div> <div class="rectangle right-rectangle"></div> </div> </body> </html> ``` 说明 - 创建了一个大正方形 `.outer-square`。 - 在中间放置了一个较小的正方形 `.inner-square`。 - 用四个长方形(上、下、左、右)包围中间的正方形,每个长方形的位置和大小通过CSS进行调整。 - 可以通过修改CSS中的`width`和`height`属性来改变正方形和长方形的大小。 ######[AI写代码神器 | 1653点数解答 | 2024-11-04 23:18:55]
- 探秘这段 HTML 代码背后的网页奥秘(字节豆包 | 51点数解答 | 2024-12-09 20:31:23)368
- 棋盘方格中正方形与长方形数量的计算方法(字节豆包 | 460点数解答 | 2026-04-20 12:14:43)21
- 帝王尬笑博客:技术、项目与生活点滴的精彩汇聚(GPT | 86点数解答 | 2024-11-16 02:59:47)307
- HTML+jQuery 实现列表元素悬停动画效果:代码详解与注意事项(字节豆包 | 475点数解答 | 2024-12-19 14:58:05)393
- HTML+jQuery:打造动态导航栏动画效果及注意事项(字节豆包 | 495点数解答 | 2024-12-19 14:58:06)337
- HTML+jQuery:打造带动态交互效果的商品展示页面(讯飞星火 | 497点数解答 | 2024-12-19 14:58:18)342
- HTML与jQuery结合:实现商品列表交互动画的首页代码解析(讯飞星火 | 497点数解答 | 2024-12-19 14:58:20)363
- 锁屏页面代码优化:从内联样式到外部 CSS 的完美转变(字节豆包 | 2052点数解答 | 2025-05-31 12:45:02)240
- 锁屏页面代码优化:模块化、注释与兼容性全攻略(字节豆包 | 2110点数解答 | 2025-05-31 12:45:28)265
- 微信钱包前端演示代码:从问题修正到完善功能及安全提示(字节豆包 | 2097点数解答 | 2025-07-11 18:06:46)179
- 充值页面LayUI框架代码分析:问题、优化与逻辑要点(DeepSeek | 354点数解答 | 2025-05-05 02:54:04)209
- 奥特曼特摄站代码:从问题重重到完美优化全揭秘(字节豆包 | 1523点数解答 | 2025-06-04 13:19:47)635