1px problem and responsive layout in mobile H5

In the H5 development of the mobile terminal, there are two common problems: 1px problem and responsive layout. Here is a brief summary of them:

1px question:

On mobile devices with high pixel density, since the device pixel ratio (Device Pixel Ratio, referred to as DPR) is greater than 1, the 1-pixel line or border defined in CSS will become blurred or displayed as multiple physical pixels when rendered, resulting in Borders appear thicker than expected. This creates challenges for designs that require high detail.

Common solutions to the 1px problem include:

  • Scale using pixel units and applying a transform: Realistic 1 can be achieved by defining a thin line pseudo-element and then scaling it using the scale property
  • Pixel lines effect. Use the meta tag to set the viewport: By setting it, you can avoid page scaling, so that 1-pixel lines can be displayed more accurately.

Specifically, there are several ways to solve the 1px problem in mobile H5 development:

  • Use pseudo-class elements: By using the ::after or ::before pseudo-class elements on the elements that need to display thin lines, set their height to 1px, and then use transform: scaleY(0.5) to scale, you can achieve real 1 pixel line effect.

  • Use viewport units: vw units can be used instead of px units to define the width or border size of an element. For example, setting an element's border to 1vw makes it adaptive on different devices in proportion to the viewport width.

  • Use zoom technology: use JavaScript or the transform attribute of CSS to zoom the page to a ratio of 0.5 times or less, so that the original 1-pixel line can be displayed as a real 1-pixel on a high-pixel-density device.

Responsive layout:

Responsive layout is a web page layout method designed to adapt to different screen sizes and devices. By using technologies such as CSS media queries and the flex box model, the layout of the page and the size of elements can be automatically adjusted according to the device used by the user to provide a better user experience.

Key points for implementing a responsive layout include:

  • Using Media Queries (Media Queries): By writing style rules in CSS for different screen sizes, different styles can be applied according to different device characteristics.

  • Flexible box model (Flexbox): Flexible layout can be achieved by using the flexible box model, so that the elements of the page can adapt to the screen size and be arranged reasonably according to the size of the content.

  • Image responsive design: In order to adapt to different screen sizes, you can use the max-width attribute of CSS or set the width of the image to 100% to ensure that the image is displayed normally on different devices and prevent overflow.

When it comes to the 1px problem and responsive layout in mobile H5 development, the following are two specific cases:

Case 1: Solve the 1px problem

Suppose we want to implement a bottom navigation bar on the mobile terminal, where there is a dividing line of 1 pixel between each icon. The 1px problem can be solved by the following code:

<style>
  .nav {
    
    
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    background-color: #f3f3f3;
  }
  
  .nav-item {
    
    
    position: relative;
    flex-grow: 1;
    height: 100%;
    text-align: center;
  }
  
  .nav-item::after {
    
    
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: #ccc;
    transform: scaleY(0.5);
  }
</style>

<div class="nav">
  <div class="nav-item">图标1</div>
  <div class="nav-item">图标2</div>
  <div class="nav-item">图标3</div>
</div>

In the above code, by setting the height of the ::after pseudo-class element of each navigation item to 1px, and using transform to scale, the real dividing line effect of 1 pixel is realized.

Case 2: Responsive layout

Suppose we want to create a responsive image display page with three columns on large screens and one column on small screens. This can be achieved with the following code:

<style>
  .image-container {
    
    
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  
  .image-item {
    
    
    width: 30%; /* 在大屏幕上显示3列 */
    margin-bottom: 20px;
  }
  
  @media only screen and (max-width: 768px) {
    
    
    .image-item {
    
    
      width: 100%; /* 在小屏幕上显示1列 */
    }
  }
</style>

<div class="image-container">
  <div class="image-item">图片1</div>
  <div class="image-item">图片2</div>
  <div class="image-item">图片3</div>
  <!-- 更多图片项... -->
</div>

In the above code, the media query @media is used to set the width of the picture item to 100% (displayed as a column) when the screen width is less than or equal to 768px; while on a screen larger than 768px, the width of each picture item is 30 %, to achieve a responsive layout effect.

By combining media query, flex box model and other related technologies, web pages can present good layout and user experience on different devices.

Hope the above summary is helpful to you! If you need more detailed information or have additional questions, please feel free to ask.

Guess you like

Origin blog.csdn.net/qq_37609787/article/details/131226167