Blogger Information
Blog 13
fans 0
comment 0
visits 9683
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
box-sizing、伪类选择器参数及媒体查询的应用
樱风醉
Original
520 people have browsed it

1. box-sizing的作用

box-sizing 属性定义如何计算一个元素的总宽度和总高度,主要设置是否需要加上内边距和边框等。

①默认情况下box-sizing的属性为content-box,并且任何边框和内边距的宽度都会被增加到最后绘制出来的元素宽度中。元素的宽度(width) 和高度(height)计算方式如下:

  1. width(宽度) + padding(内边距) + border(边框) = 元素实际宽度
  2. height(高度) + padding(内边距) + border(边框) = 元素实际高度

②当设置为border-box时,实际高度和宽度包含设置的边框和内边距的值,内容区的实际宽度/高度是 width/height 减 去(border + padding) 的值。

2.伪类选择器的参数an+b的经典应用

:nth-of-type(an+b) a,n,b = [0,1,2,3,4,….] a: 系数, n:计数器, b : 偏移量
n和b都从0开始,参数的有效值从计算结果为1时开始。
①当a=0时,获取指定的某一个 (b)

  1. .list:nth-of-type(4) {
  2. background-color: red;
  3. }

②当a=1时,获取指定位置后的全部元素 (n+b)

  1. .list :nth-of-type(n + 6) {
  2. background-color: violet;
  3. }

①②效果如下:

③获取正数前几个或倒数后几个(-n+b)

  1. .list > :nth-of-type(-n + 3) {
  2. background-color: red;
  3. }
  4. .list > :nth-last-of-type(-n + 3) {
  5. background-color: blue;
  6. }

效果如下:

④获取全部偶数(2n/even)或奇数(2n+1/odd)元素

  1. .list :nth-of-type(2n) {
  2. background-color: red;
  3. }
  4. .list :nth-of-type(2n + 1) {
  5. background-color: violet;
  6. }

效果如下:

3.媒体查询@media

@media可以针对不同的屏幕尺寸设置不同的样式,特别是如果你需要设置设计响应式的页面,当你重置浏览器大小的过程中,页面也会根据浏览器的宽度和高度重新渲染页面

①移动端优先,从最小的屏幕开始进行媒体查询

  1. @media (min-width: 480px) {
  2. html {
  3. font-size: 12px;
  4. }
  5. }
  6. @media (min-width: 640px) {
  7. html {
  8. font-size: 14px;
  9. }
  10. }
  11. @media (min-width: 720px) {
  12. html {
  13. font-size: 16px;
  14. }
  15. }

②桌面端/PC优先, 由大屏到小屏逐步进行媒体查询

  1. @media (max-width: 720px) {
  2. html {
  3. font-size: 16px;
  4. }
  5. }
  6. @media (max-width: 640px) {
  7. html {
  8. font-size: 14px;
  9. }
  10. }
  11. @media (max-width: 480px) {
  12. html {
  13. font-size: 12px;
  14. }
  15. }
  16. /*保留边界值 最大边界问题*/
  17. @media (min-width: 720px) {
  18. html {
  19. font-size: 16px;
  20. }
  21. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!