【问题标题】:Delete margin white space on small devices (mobile phones)删除小型设备(手机)上的边距空白
【发布时间】:2020-07-07 20:58:31
【问题描述】:

我不希望小型设备的边缘有任何空白。当屏幕已经很小时,使用除屏幕全宽之外的任何东西都会适得其反。

所以我通过 wordpress 使用主题,但我想出了容器 div 并且能够修改它,我想让它更窄。 我还声明了一个比容器(宽度为 65%)更宽的 div(child1wide),希望marings 会消失。

问题是在小屏幕上的文本两侧有边距,也就是空白。 我怎样才能摆脱这个空白?我仍然希望在更大的屏幕上留有余量。

您可以看到它今天的样子: https://imgur.com/dcVIGBJ

未修改的 .container 有可接受的边距,但我想让它适用于 .child1wide 并且可能学到一些新东西。

CSS(注意,.container 可能也在我的 wordpress 主题中定义,这只是我附加的“自定义 CSS”):

.child1wide {
  background-color: yellow;
  display: flex;
  margin-left: calc(-37.5vw + 50%);
  width: 75vw;
}

.container {
width: 65%  ;
padding: 0px 0px 0px 0px;
}

HTML(第二个“Lorem ipsum”-text 在 .child1wide-div 之外,意味着它自动在 wordpress 主题设置的 .container-div 中):

<div class="child1wide">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore neque repellat ipsum natus magni soluta explicabo architecto, molestias laboriosam rerum. Tempore eos labore temporibus alias necessitatibus illum enim, est harum perspiciatis, sit, totam earum corrupti placeat architecto aut minus dignissimos mollitia asperiores sint ea.
</div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore neque repellat ipsum natus magni soluta explicabo architecto, molestias laboriosam rerum. Tempore eos labore temporibus alias necessitatibus illum enim, est harum perspiciatis, sit, totam earum corrupti placeat architecto aut minus dignissimos mollitia asperiores sint ea.

我知道@media 唯一的屏幕,但不能让它工作。

【问题讨论】:

  • 嗨 Mr_G,欢迎来到 SO!我需要从您那里知道的是小型设备的边距(移动设备。需要一些小的边距,这样用户就不会无意中点击按钮或用指尖混淆文本)和大型设备(PC?)的边距我会告诉你一点数学是如何做到的。我一直使用这个:.main-container { padding: calc( 2.5vh + 24px) calc(19.5vw - 54.4px) }。含义:T/B 32px 在 320px 屏幕上和 72px 在 1920px 屏幕上和 L/R:8px 在 320px 和 320px 在 1920px 屏幕上。 CSS 填充所有其他尺寸的其余部分。使用“线性方程:y=mx+b”。
  • 谢谢!这可以帮助我。你能解释一下这个公式,或者举个例子吗?我仍然想让@media 只在屏幕上工作。

标签: html css


【解决方案1】:

当您(像我一样)数学不是那么好时,乍一看可能会吸收很多东西。但我向您保证,一旦您开始使用这些方程式,您将学会欣赏它们的强大功能和易用性。

初步版本:根据您的评论,此答案可能需要一些更新。

首先,sn-p 带有最终代码,稍后解释(如 tl;dr 中)。最好先将其保存在一个新的 HTML 文档中,在浏览器中打开该文档并开始调整大小...

/********************************/
/* a few preferred global rules */
/********************************/
html,body {
    box-sizing: border-box;     /* use client+padding+border in calculations */
    height: 100%; width: 100%;  /* to fill full viewport */
    margin: 0;                  /* getting rid of HTML spacing */
}
body { min-height: 100vh }      /* to fill full viewport */

*::before,*::after,
 * { box-sizing: inherit }      /* take over parent setting */

/*
    Responsive page padding using
    Linear Equation y=mx+b for points p1(x1,y1) p2(x2,y2)

    Reference
    MathIsFun: Equation of a Straight Line
    https://www.mathsisfun.com/equation_of_line.html

    y = resulting size we need

    m = (y2 - y1) / (x2 - x1),
        fixed result 1

    x = always one of 100vh/vw/vmin/vmax (VX in below CSS calc)
        variable part of our equation, which makes our y change on browser resize

    b = y1 - m * x1 and with m substituted: b = y1 - (y2 - y1) / (x2 - x1) * x1
        fixed result 2

    x1 - minimum viewport size
    y1 - needed size at minimum viewport     

    x2 - maximum viewport size 
    y2 - needed size at maximum viewport
    
    x1,y1,x2,y2 in pixel unit (can be any unit, provided you use the proper unit conversion)

    CSS calc: calc(m * 100VX + b) 
    Final   : calc(mVX + b) => multiply m with 100 to get rid of '* 100VX' 

    top/bottom padding: p1(320,32) p2(1920, 72) => y = 0.025x + 24   (vp height dependent)
    left/right padding: p3(320, 8) p4(1920,320) => y = 0.195x - 54.4 (vp width dependent)

    top/bottom padding:
        m = (72 - 32) / (1920 - 320) = 40 / 1600 = 0.025
        x = vp height dependent, so 100vh
        b = 32 - 0.025 * 320 = 32 - 8 = 24
        CSS calc = calc(0.025 * 100vh + 24px) => calc(2.5vh + 24px) 

    left/right padding:
        m = (320 - 8) / (1920 - 320) = 312 / 1600 = 0.195
        x = vp width dependent, so 100vw
        b = 8 - 0.195 * 320 = 8 - 62.4 = -54.4
        CSS calc = calc(0.195 * 100vw - 54.4px) => calc(19.5vw - 54.4px) 

*/
.padded { padding: calc(2.5vh + 24px)     calc(19.5vw - 54.4px) }
.halfTB { padding: calc((2.5vh + 24px)/2) calc(19.5vw - 54.4px) }
/* half height T/B padding, simply divide result of calc for T/B  by 2 */

/* uncomment to constraint padding below 320, above 1920 *//*
@media screen and (max-width: 320px) { .padded { padding: 32px   8px } }
@media screen and (min-width:1920px) { .padded { padding: 72px 320px } }
/* probably not really needed, just to be complete */

/* Extra: responsive base font size: y = 0.00625x + 12 */
/*        points p1(320,14) p2(1280,20) vp independent where 0.75rem = 12/16 */
body        { font-size: calc(0.625vmin + 0.75rem); line-height: 1.3333 } /* use root fontsize */
:root,html  { font-size: 100% }   /* use browser default fontsize (from browser user settings) */

.child1wide { width: 100% } /* width is restricted by L/R .padded, centered automatically */
.container  { width:  66.667%; margin: 0 auto } /* width restricted by percent%, centered by margin */
<h1 class="padded halfTB">calculated padding versus percentage<br>resize the browser to see the effect</h1>
<h3 class="padded halfTB">normally you would use ".padded" on some main container, now split to show difference</h3>

<div class="child1wide padded">
    <h2>padding with Linear Equation</h2>
    <p>Lorem ipsum dolor sit amet, exerci dolorem est ad. Sumo rebum prompta vim ad. Legendos expetendis id sed. Ex ius quem accusamus, pri et
        deleniti copiosae.</p>
    <p>Cu vel debet nobis, repudiare deseruisse reprehendunt usu ad. Ex elit idque nam. Omnis munere detraxit mei te, eu labore appareat verterem
        est. Mel ex oporteat consectetuer.</p>
    <p>Pro ea nonumy integre, mel at solum corpora. Id viris audiam repudiare cum, pri dolore appareat ex, per propriae detracto tacimates ex.
        Elitr sapientem quo et, usu suas porro tibique cu.</p>
</div>
<div class="container">
    <h2>width 66.667%, margin: 0 auto</h2>
    <p>Lorem ipsum dolor sit amet, exerci dolorem est ad. Sumo rebum prompta vim ad. Legendos expetendis id sed. Ex ius quem accusamus, pri et
        deleniti copiosae.</p>
    <p>Cu vel debet nobis, repudiare deseruisse reprehendunt usu ad. Ex elit idque nam. Omnis munere detraxit mei te, eu labore appareat verterem
        est. Mel ex oporteat consectetuer.</p>
    <p>Pro ea nonumy integre, mel at solum corpora. Id viris audiam repudiare cum, pri dolore appareat ex, per propriae detracto tacimates ex.
        Elitr sapientem quo et, usu suas porro tibique cu.</p>
</div>

正如您所说,您希望在较小的设备上响应(几乎)没有间距,在较大的设备上实现“正常”间距,使用@media 查询 (MQ) 将是显而易见的选择。它们在世界各地被许多开发人员普遍使用,就像我一样。

然而,在过去的几年里,我学会了使用单个方程 (Codepen: responsive typography) 来确定特定浏览器视口大小下所需的大小,而不是使用 MQ 列表来测试特定 vp 大小和设置特定断点处的大小。

例如:

.some-class: { font-size: calc(0.625vmin + 12px) }

可以做的完全一样

.some-class { font-size: 13px } @media (min-size: 320px) { .some-class { font-size: 14px } } @media (min-size: 480px) { .some-class { font-size: 15px } } @media (min-size: 640px) { .some-class { font-size: 16px } } @media (min-size: 800px) { .some-class { font-size: 17px } } @media (min-size: 960px) { .some-class { font-size: 18px } } @media (min-size: 1120px) { .some-class { font-size: 19px } } @media (min-size: 1280px) { .some-class { font-size: 20px } }

或任何font-size 在任何给定时刻您需要的断点。

如您所见,一个计算而不是八个 CSS 规则。为此,我们需要使用一个

'线性方程:y = mx + b'MathIsFun: Equation of a Straight Line,通俗易懂的中学解释,值得一读)。

在哪里

  • y = mx + b,我们需要的响应式结果
  • m = (y2 - y1) / (x2 - x1),线的陡度,固定值
  • x = 始终为 100vmin/vh/vw/vmax,可变值
  • b = y1 - m * x1,视口大小为0(x=0)时的y值,固定值
  • x 轴 浏览器视口大小
  • y 轴(响应式)尺寸

  • 点 1 (x1,y1),一条线上的最低点,最小值。浏览器视口大小,最小值所需尺寸
  • 点 2 (x2,y2),一条线上的最高点,最大值。浏览器视口大小,最大值所需尺寸

我们实际上在做的是

  • 在 XY 图上选择一个低点和一个高点,这是我们需要的最小和最大响应尺寸
  • 在两点之间画一条假想线
  • 并让 CSS calc() 计算同一行上的所有其他点,即我们在任何给定时间需要的响应大小(字体、边距、填充、宽度、高度等)。

专业版:更少的 CSS,更少的维护

缺点:在编码时需要做更多准备,并且只适用于直线(不包括火箭科学)。 calc() 结果的异常仍需要一些 MQ。

代码 sn-p 使用一些示例通过逐步方程式进行了大量注释:

  • 响应式基本字体大小,body { font-size: calc() }
  • 响应式页面填充,.padded { padding: calc(2.5vh + 24px) calc(19.5vw - 54.4px) }
  • 共振半顶部/底部填充,只需将.padded calc(2.5vh + 24px) 除以2

【讨论】:

  • 谢谢!我很感激。这似乎真的很有帮助。我会尝试一下,但我首先得把它分成小块。
  • 我想知道我们是否要在正文上执行此操作并相应地缩放整个页面和其中的元素,我的 x1、x2 和 y1、y2 将是什么。我有点困惑,因为我总是达到斜率常数 1。
  • @Vikhyath Maiya,显然您可以使用“y=mx+b”作为缩放工具,但在这里并不是这样。取上面的.some-class { font-size: calc(0.625vmin + 12px) }。这实际上意味着:在视口 宽度为 320px 我想要 fontsize 为 14px 而在视口 width 为 1280px 我想要 fontsize 20px,现在“y=mx+b”计算所有显示宽度的所有其他字体大小。但是你为什么不把你的问题变成一个 SO 问题,“分享/链接”上面的答案,在你做错的时候在这里输入一个问题的 sn-p 并发表评论(因为我不能整天跟着你检查)。我帮你...
  • 另外,@Vikhyath Maiya,请在SO62961971: set fixed spacing... 上查看我的答案。它显示了“y = mx + b”的各种用法,以使“登录屏幕”在没有媒体查询的情况下响应。在各种设备上的各种浏览器中进行了测试......(不是公认的答案,但是嘿,谁在数......?)
【解决方案2】:

我不明白 calc 函数。 所以

.padded { padding: calc(2.5vh + 24px) calc(19.5vw - 54.4px) } 会产生顶部和底部填充,还是左右?

如果视口高度为 1000px,calc(2.5vh + 24px) 将给出一个填充

390.4px 到 390px * 2.5vh + 24px = 1000px??

【讨论】:

  • .padded 将产生顶部/底部左/右填充。 T/B 32px,L/R 8px,视口大小(浏览器窗口,vmin)为 320px,T/B 72px,L/R 320px,视口大小(也是浏览器窗口,vmin)为 1920px。将计算所有其他视口大小的所有其他填充值 (T/B/L/R) 并将按比例缩放。只需在新文档中打开代码并调整浏览器大小。观察页面上的主要 L/R 空间,你会看到它发生。 VMIN 表示当前最小值(浏览器宽度,浏览器高度),100vmin 是当前最小值(w,h)的 100%。
  • 如果视口高度为 1000px... 那么 calc(2.5vh + 24px) 将产生 1000px + 24px = 25px + 24px = 49px T/B 填充的 2.5%。查看MDN: CSS values and units
  • 谢谢,不胜感激!
【解决方案3】:

我现在通过将我的主题容器设置为:

@media screen and (max-width: 600px) {
  div.container {
    width:95%;
  }
}

【讨论】:

    【解决方案4】:

    溢出的原因:

    • 元素“child1wide”的宽度 =75vw - 将宽度更改为自动
    • “child1wide”的左边距为负 - 最好使用 position: relative 和移动内容(上、左、右、下)

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多