博主信息
博文 62
粉丝 2
评论 1
访问量 58215
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
15 前端的简单总结(20200630)
老黑
原创
780人浏览过

主要内容

  1. 自定义属性:以data-开始
  2. 尽量少用id,原因有可能污染全局执行环境
  3. 一组items时选择某个item时首选type,而非child
  4. 多个css文件的嵌套引用@import
  5. input中的各属性(字段位置)
  6. grid适合页面的整体布局, flex适合细节处理
  7. img处理中,宽与高只需要写一个,另一个会等比缩放
  8. img中的alt已经要加上,哪怕为空

1. 自定义属性:以data-开始

示例

  1. <body>
  2. <!-- 自定义属性必须以data-开始 -->
  3. <p class="phpcn-intro" data-intro="php.cn">
  4. php中文网是php与前端开发者的乐园
  5. </p>
  6. <h3 class="phpcn-intro">Hello PHP.CN</h3>
  7. <!-- 任何一个html元素都有二种属性: 内置属性, 自定义属性 -->
  8. <label for="login">用户名:</label>
  9. <input type="text" name="username" data-index="自定义属性" id="login" />
  10. <script>
  11. const intro = document.getElementsByClassName("phpcn-intro").item(0);
  12. intro.style.backgroundColor = "red";
  13. </script>
  14. </body>

2. 尽量少用id,原因有可能污染全局执行环境

  • 尽可能用class,不要用id
  • class或者其它属性的字符串值,如果涉及多个单词的时候,使用连接线进行连接,而不要用驼峰方式
  • 全局环境是当前js所有代码所共享,如果给一个元素添加了id属性,将会自动创建一个同名的全局变量
  • js最怕的就是全局执行环境被全局变量给污染了
  • id主要用在二个地方: 表单元素,锚点

3. 一组items时选择某个item时首选type,而非child

  • 分组选择的伪类: 首选。例如:

:first-of-type,
:last-of-type,
:nth-of-type(),
:nth-last-of-type(),
:only-of-type

  • 不分组的方式进行选择子元素。例如:

:first-child,
:last-child,
:nth-child(),
:nth-last-child(),
:only-child()

4. 多个css文件的嵌套引用

在一个css文件中直接用@import来进行嵌套引用。这样在html中就只要link这个css一个就够了,其他css会直接被引用进来。

  1. @import url("font/iconfont.css");
  2. /* @import必须写在css文档的前面 */
  3. /* 头部样式的模块 */
  4. @import url(header.css);
  5. @import url(footer.css);
  6. .icon-gouwuchekong:before {
  7. font-family: "iconfont";
  8. content: "\e602";
  9. font-size: 5rem;
  10. color: green;
  11. }

最后这块是通过伪类元素添加,同时改变iconfont的属性。“\e602”是对应的iconfont的位置或id。

5. input中的各属性(字段位置)

  1. <label for="email">邮箱:</label>
  2. <input
  3. id="email"
  4. class="my-email"
  5. type="email"
  6. name="email"
  7. value="admin@php.cn"
  8. placeholder="demo@email.com"
  9. autofocus
  10. required
  11. />
  • id,class通常在最前面
  • 中间写上当前元素的内置属性
  • 内置属性中的布尔属性,总是写在最后面

之所以因为这样,是为了长期快速编程做准备。例如如果格式等固定了。在js中就可以非常方便地进行调用和替换等。

  1. <script>
  2. const email = document.querySelector("input");
  3. const attrs = email.attributes;
  4. console.log(attrs);
  5. console.log(attrs[0]);
  6. console.log(attrs[1]);
  7. </script>

6. grid适合页面的整体布局, flex适合细节处理

7. img处理中,宽与高只需要写一个,另一个会等比缩放

  1. .box img {
  2. width: 100%;
  3. }

8. img中的alt已经要加上,哪怕为空

  • <img src="" alt="">
批改老师:GuanhuiGuanhui

批改状态:合格

老师批语:css 中 @import 尽量不要使用!因为@import引用的CSS会等到页面全部被下载完再被加载,用户体验不好!
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学