Home Web Front-end HTML Tutorial 移动端优先策略下的css如何编写_html/css_WEB-ITnose

移动端优先策略下的css如何编写_html/css_WEB-ITnose

Jun 21, 2016 am 08:55 AM

移动互联网时代,如何开发一个响应式站点,是每一个前端人员必备的技能。通常,我们能意识到良好的设计对移动端开发的重要性,但是在代码层面很少讨论。

接下来,让我们搞清楚:

1,什么是移动端优先策略(小屏优先策略)

2,这种策略的样式如何编写。


 移动端优先策略VS桌面端优先策略


移动端优先策略:以适配移动端设备为首要目标进行设计开发,尽可能的适配桌面端大屏。

桌面端优先策略:以桌面端设备作为首要考虑目标,移动端放到次要位置考虑

这里就是一个主次关系的考虑。

在移动端优先策略下样式的编写,我们可以借用媒体查询来完成对于桌面端大屏设备的适配(例如min-width)。

举个例子:

// This applies from 0px to 600px

body { 

  background: red; 
}

// This applies from 600px onwards

@media (min-width: 600px) {

  body { 
    background: green; 
  }

}

这里,body在600px一下的宽度时,背景色为红色,而超过600px,就变为绿色了。

在桌面端优先的策略下,我们要这么写css:

// This applies from 600px onwards

body { 

  background: green; 
}


// This applies from 0px to 600px

@media (max-width: 600px) {

  body { 
    background: red; 
  }

}


为什么要采用移动端优先的策略?


相对于小屏幕,为了适配大屏幕,我们的css代码通常要复杂一些,所以移动端优先的策略有利于精简代码。


考虑如下场景:

一个站点有个如下布局,.Content在移动端占据100%,在桌面端占据60%

此时,对于小屏幕,我们可以借助属性的默认值为content区域添加样式,比如一个div,默认情况下,作为块级元素默认宽度为100%。

小屏优先策略下的sass代码:

.content {
  // Properties for smaller screens. 
  // Nothing is required here because we can use the default styles 

  // Properties for larger screens
  @media (min-width: 800px) {
    float: left; 
    width: 60%; 
  }

}

大屏优先策略下:

.content {
  // Properties for larger screens.
  float: left; 
  width: 60%; 

  // Properties for smaller screens. 
  // Note that we have to write two default properties to make the layout work
  @media (max-width: 800px) {
    float: none; 
    width: 100%; 
  }

}

看看,我们节省了2行代码,减少了点脑力消耗,想想,如果实在大项目中,这会有更大的益处吧。

移动端优先策略下,我们如何使用Max-width

当你想要将样式的应用目标限定在某一特定的视口宽度时,max-width就派上用场了,结合min-width的使用,还可以创造一个限定区间。

考虑一个缩略图展示页的布局:800px以下的视口宽度时,显示为3列,否则为4列

当这些缩略图之间没有间隙时,很简单:

.gallery__item {
  float: left; 
  width: 33.33%; 
  @media (min-width: 800px) {
    width: 25%; 
  }

}

实际情况下,通常是有间距的,情况就变的复杂了:

很容易想到如下的css:

.gallery__item {

  float: left; 
  width: 30.333%;
  margin-right 5%;
  margin-bottom: 5%;
  &:nth-child(3n) {
    margin-right: 0; 
  }

  @media (min-width: 800px) {
    width: 21.25%; // (100% - 15%) / 4
    &:nth-child (4n) {
      margin-right: 0; 
    }
  }

}

但是因为,我们设定了在每个第三项,margin-right为0,而当一行有4项时,应该是每个第四项为0,我们可以考虑通过重写的方式解决:

.gallery__item {
  // ...
  @media (min-width: 800px) {
    // ...
    &:nth-child (3n) {//恢复第三项的间距
      margin-right: 5%; 
    }
    &:nth-child(4n) {
      margin-right: 0%;
    }
  }

}

这个方式并不是太好,因为当我们需要在更大的屏幕上显示更多的项时,我们会遇到同样的问题。

考虑如下方式,把随视口变化的东西放到相应的查询语句中,默认样式只保留公共的部分,能减少重写带来的麻烦:

.gallery__item {
  float: left; 
  margin-right: 5%;
  margin-bottom: 5%;
  @media (max-width: 800px) {
    width: 30.333%;
    &:nth-child(3n) {
      margin-right: 0; 
    } 
  }

  @media (min-width: 800px) {
    width: 21.25%; // (100% - 15%) / 4
    &:nth-child (4n) {
      margin-right: 0; 
    }
  }

}



参考:  http://zellwk.com/blog/how-to-write-mobile-first-css/


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles