search
HomeWeb Front-endHTML Tutorial移动端优先策略下的css如何编写_html/css_WEB-ITnose

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

接下来,让我们搞清楚:

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
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
Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is an example of a starting tag in HTML?What is an example of a starting tag in HTML?Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?How to use CSS's Flexbox layout to achieve centering alignment of dotted line segmentation effect in menu?Apr 05, 2025 pm 01:24 PM

How to design the dotted line segmentation effect in the menu? When designing menus, it is usually not difficult to align left and right between the dish name and price, but how about the dotted line or point in the middle...

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment