学习flex布局,并用flex模仿php中文网首页,听老师讲的时候感觉是会了,当自己动手的时候还真的费劲了,真是看花容易绣花难啊。先看看示例:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>php中文网手机端-首页</title> <style> body { margin: 0 auto; background: #edeff0; overflow-y: initial; position: relative; /*不要出现水平滚动条*/ overflow-x: hidden; /*设置点击链接跳转时出现高亮,设置为透明: ios / ipad*/ -webkit-tap-highlight-color: transparent; } .top { position: fixed; top: 0; width: 100%; height: 42px; background-color: #444444; left: 50%; -webkit-transform: translateX(-50%); transform: translateX(-50%); } .top { display: flex; } .top img:first-of-type, .top img:last-of-type { width: 25px; height: 25px; margin-top: 8px; } .top img:first-of-type { border-radius: 50%; margin-left: 5px; } .top .logo { text-align: center; flex: 1; } .top .logo img { width: 94px; height: 45px; margin-top: -2px; } /*轮播图*/ .banner { display: flex; margin-top: 40px; } .banner img { width: 100%; height: 160px; } /*导航区*/ .nav { height: 170px; background-color: white; box-sizing: border-box; } .nav ul { margin: 0; padding: 6px; } .nav ul li { list-style-type: none; text-align: center; height: 75px; } .nav ul li img { width: 45px; height: 45px; } .nav ul li a { text-align: center; color: gray; text-decoration: none; } .nav ul { display: flex; } .nav ul li { flex: 1; } /*推荐课程区*/ h3 { color: gray; padding-left: 10px; } .courses { height: 326px; color: gray; } .courses ul { margin: 0; padding: 0; list-style: none; /*flex布局*/ display: flex; } .courses ul li { padding: 5px; flex: 1; } .courses ul img { width: 100%; height: 90px; } .courses div { background-color: white; padding: 10px; margin-top: 10px; height: 90px; /*flex布局*/ display: flex; justify-content: flex-start; } .courses div img { width: 100%; height: 90px; } .courses div a { text-decoration: none; color: gray; flex: 0.45; } .courses div p { flex: .55; margin-left: 15px; } .courses div p span:first-of-type { font-size: 0.8rem; background-color: black; color: white; border-radius: 30%; padding: 0 2px; } .courses div p span:last-of-type { font-size: 0.7rem; } /* 最新更新 **************/ .news-container { height: 226px; color: gray; } .news-container ul { margin: 0; padding: 0; list-style: none; /*flex布局*/ display: flex; } .nenews-containerws ul li { padding: 5px; flex: 1; } .news-container ul img { width: 100%; height: 90px; } .news-container div { background-color: white; padding: 10px; margin-top: 10px; height: 90px; /*flex布局*/ display: flex; justify-content: flex-start; } .news-container div img { width: 100%; height: 90px; } .news-container div a { text-decoration: none; color: gray; flex: 0.45; } .news-container div p { flex: .55; margin-left: 15px; } .news-container div p span:nth-of-type(2) { font-size: 0.8rem; background-color: black; color: white; border-radius: 30%; padding: 0 2px; } .news-container div p span:nth-of-type(1) { display: block; /*内联对象需加*/ overflow: hidden; white-space: nowrap; text-overflow: ellipsis; width: 300px; } .news-container div p span:last-of-type { font-size: 0.7rem; } h3.news { margin-top: 49px; padding-left: 10px; } .news_span { float: right; padding: 12px; } /* **********************最新文章 **************/ .wenz-container { height: 326px; color: gray; margin-bottom: 20em; } .wenz-container ul { margin: 0; padding: 0; list-style: none; /*flex布局*/ display: flex; } .wenz-containerws ul li { padding: 5px; flex: 1; } .wenz-container ul img { width: 100%; height: 90px; } .wenz-container div { background-color: white; padding: 10px; margin-top: 10px; height: 90px; /*flex布局*/ display: flex; flex-direction: row-reverse; justify-content: flex-start; } .wenz-container div img { width: 100%; height: 90px; } .wenz-container div a { text-decoration: none; color: gray; flex: 0.45; } .wenz-container div p { flex: .55; margin-left: 15px; } .wenz-container div p span:nth-of-type(2) { font-size: 0.8rem; background-color: black; color: white; border-radius: 30%; padding: 0 2px; } .wenz-container div p span:nth-of-type(1) { padding: 9px; } .wenz-container div p span:last-of-type { font-size: 0.7rem; } h3.wenz { margin-top: 23px; padding-left: 10px; } .wenz_span { float: right; padding: 12px; } /*底部样式*/ .footer { position: fixed; bottom: 0; width: 100%; height: 42px; background: #eee; } .footer ul { display: flex; margin: 0; padding: 0; list-style-type: none; text-align: center; } .footer ul li img { width: 20px; height: 20px; } .footer ul li a { color: gray; text-decoration: none; } .footer ul li { flex: 1; } </style> </head> <body> <!--布局原则: 宽宽自适应, 高度是固定--> <!--固定定位: 顶部--> <div class="top"> <img src="http://hzone.4008691117.com/static/images/user-pic.jpeg" alt=""> <div class="logo"> <img src="http://hzone.4008691117.com/static/images/logo.png" alt=""> </div> <img src="http://hzone.4008691117.com/static/images/user-nav.jpg" alt=""> </div> <!--banner轮播图,这里用图片暂时代替--> <div class="banner"> <img src="http://hzone.4008691117.com/static/images/banner.jpg" alt=""> </div> <!--导航区--> <div class="nav"> <ul> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/html.png" alt=""><br>HTML/CSS</a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/JavaScript.png" alt=""><br>JavaScript</a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/code.png" alt=""><br>服务端</a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/sql.png" alt=""><br>数据库</a> </li> </ul> <ul> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/app.png" alt=""><br>移动端</a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/manual.png" alt=""><br>手册</a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/tool2.png" alt=""><br>工具</a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/live.png" alt=""><br>直播</a> </li> </ul> </div> <!--课程区--> <h3>推荐课程</h3> <div class="courses"> <ul> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc1.jpg" alt=""></a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc2.jpg" alt=""></a> </li> </ul> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc3.jpg" alt=""></a> <p> <a href="">CI框架30分钟极速入门</a> <br> <span>中级</span><span>49748次播放</span> </p> </div> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc4.jpg" alt=""></a> <p> <a href="">2018前端入门_HTML5</a> <br> <span>初级</span><span>210066次播放</span> </p> </div> </div> <!-- 最新更新 --> <h3 class="news">最新更新</h3> <div class="news-container"> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc3.jpg" alt="图片区"></a> <p> <a href="">Laravel 5.8 中文文档手册</a> <br> <span>《Laravel 5.1/5.8速查表:https://www.php.cn/phpkj/laravel/cheatsheet5</span> <span>中级</span><span class="news_span">49748次播放</span> </p> </div> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc4.jpg" alt="图片区"></a> <p> <a href="">2018前端入门_HTML5</a> <br> <span>《Laravel 5.1/5.8速查表:https://www.php.cn/phpkj/laravel/cheatsheet5</span> <span>初级</span><span class="news_span">210066次播放</span> </p> </div> </div> <!-- 最新文章 --> <h3 class="wenz">最新文章</h3> <div class="wenz-container"> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc3.jpg" alt=""></a> <p> <a href="">百度站长主动推送php怎么弄</a> <br> <span>发布时间:2019.9.11</span> </p> </div> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc4.jpg" alt=""></a> <p> <a href="">宝塔phpmyadmin无法打开</a> <br> <span>发布时间:2019.9.11</span> </p> </div> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc4.jpg" alt=""></a> <p> <a href="">宝塔phpmyadmin无法打开</a> <br> <span>发布时间:2019.9.11</span> </p> </div> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc4.jpg" alt=""></a> <p> <a href="">宝塔phpmyadmin无法打开</a> <br> <span>发布时间:2019.9.11</span> </p> </div> <div> <a href=""><img src="http://hzone.4008691117.com/static/images/tjkc4.jpg" alt=""></a> <p> <a href="">宝塔phpmyadmin无法打开</a> <br> <span>发布时间:2019.9.11</span> </p> </div> </div> <!--底部--> <div class="footer"> <ul> <li> <a href=""><img src="http://hzone.4008691117.com/static/font-icon/zhuye.png" alt=""><br><span>主页</span></a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/font-icon/video.png" alt=""><br><span>视频</span></a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/font-icon/luntan.png" alt=""><br><span>社区</span></a> </li> <li> <a href=""><img src="http://hzone.4008691117.com/static/font-icon/geren.png" alt=""><br><span>我的</span></a> </li> </ul> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
【问题】
老师您好,在实际处理中,有以下几点问题请教您:
1、 课堂上讲的页面采用 min-width: 320px max-width:768px ,我在处理时候,尝试了去掉,直接用 width: 100%
发现也能够用,这两者的区别在哪里呢?
2、当用底部或者头部固定元素的时候,会将其他元素遮挡显示不全,为了让挡住的元素显示,我采用给靠近底部元素增加了margin-bottom 属性, 当我采用 像素(px)的时候,发现不怎么起作用,后来改用 em 的时候效果明显。老师,在移动端 页面布局元素大小是不是最好用 em ?
3、老师,并不是所以的地方都需要采用 flex 布局对吧?要根据实际场景来。
老师,您辛苦了~~