html - 删除小型设备(手机)上的边距空白

标签 html css

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

所以我通过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”-文本在 .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 唯一的屏幕,但不能让它工作。

最佳答案

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

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

首先,片段 首先是最终代码,稍后解释(如 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) 来确定特定浏览器视口(viewport)大小所需的大小,而不是使用 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 ,视口(viewport)大小为0(x=0)时的y值,固定值
  • x 轴 浏览器视口(viewport)大小
  • y轴 (响应式)尺寸


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

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

  • :更少的 CSS,更少的维护

    骗子 :编码时需要更多准备,并且仅适用于直线(不包括火箭科学)。 calc() 的异常(exception)情况结果仍然需要一些 MQ。

    代码片段通过使用一些示例的逐步方程式进行了大量注释:
  • 响应式基本字体大小,body { font-size: calc() }
  • 响应式页面填充,.padded { padding: calc(2.5vh + 24px) calc(19.5vw - 54.4px) }
  • 共振半顶部/底部填充,简单地划分 .padded calc(2.5vh + 24px)通过 2
  • 关于html - 删除小型设备(手机)上的边距空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61859289/

    相关文章:

    php - 基于 PHP 变量的 CSS 媒体查询

    javascript - 用另一个覆盖任意元素

    asp.net - 如何让一个div固定大小,让其他div占据所有剩余空间?

    javascript - Bootstrap 中的内容无法在不同的屏幕尺寸上工作

    html - 相对位置浏览器差异,绝对有效但没有流量

    javascript - 在 Chrome/Firefox 中全屏按下 escape 时不会触发 Keydown 事件

    javascript - 浏览器字体中每个字符的渲染像素宽度数据

    JQuery UI CSS 样式表不起作用

    jquery - 如何使用 jquery ajax 整个表单

    html - 字体问题: HTML field types "text" and "textarea" are displaying different fonts