【问题标题】:How do I pin text to a particular part of an image on resize?如何在调整大小时将文本固定到图像的特定部分?
【发布时间】:2021-04-17 22:24:29
【问题描述】:

我有一个初始/标头/整页图像,其中心文本需要几乎总是与此相同的外观/格式,无论调整大小如何。 (见下面的imgur链接) [带文字的玻璃球背景图]1

  • 我尝试了一种解决方法,其中背景已经将文本作为图像的一部分,但我的客户抱怨丢失的白色缺乏“亮度”,而与白色的“亮度”相比,菜单。

  • 问题是背景图像的玻璃球稍微偏离中心,并把其他所有东西都扔掉了。

  • 我还有一个按钮,需要在桌面上位于中心文本的最右侧,在移动屏幕上位于其下方。

  • 我所有的代码解决方案都在更改填充或边距,但我必须添加无休止的媒体查询。

我正在使用 Bootstrap 4 ......必须有比我尝试过的更好的解决方案。

请任何帮助或建议都会有所帮助(请注意,我在 Photoshop 和其他解决方法方面很糟糕)。

这是我的标头 CSS:

.masthead {
  height: 100vh;
  background-image: url('../images/glassball2.jpeg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.btn {
  border-radius: 34.5px;
  border: none;
  color: white;
}

.btn.main-cta {
  font-family: $header-black;
  padding: 20px;
  font-size: 20px;
}

.splash-header h1 {
  text-transform: uppercase;
  font-family: $body-font;
  color: white;
  text-shadow: 0px 5px 5px rgba(0, 0, 0, 0.4);
  letter-spacing: .5px;
  line-height: 1.3em;
}

还有我的 HTML

<header class="masthead">
  <div class="container h-100">
    <div class="row h-100 align-items-center">
      <div class="col-md-2">
        <p></p>
      </div>
      <div class="splash-header col-md-7 text-center">
        <h1>your industry link to trusted global marketplaces and technologies</h1>
      </div>

      <div class="col-md-3 text-center">
        <a class="main-cta gradient btn" data-value="contact" href="#">Schedule your free <br> 30-min consultation</a>
      </div>

    </div>
  </div>
</header>

【问题讨论】:

  • 我不太清楚问题出在哪里,尤其是玻璃球将其他所有东西都扔掉是什么意思?也许您可以建立一个工作的 sn-p,其中包括实际图像,并描述您希望它在各种不同设备的纵横比上的样子。
  • 我无法直接添加图像,但它被 [1] 标签链接引用:i.stack.imgur.com/kcTtE.png 图像本身中的玻璃球(您将看到)并不完美居中,这意味着我尝试将文本和 div 居中是行不通的。
  • 请描述您希望它在具有不同纵横比的不同设备上的外观。您希望文本以视口为中心还是以玻璃球为中心?
  • 我希望它以玻璃球为中心,其中一半文本位于上半部分,另一半位于下半部分(与图像中的比例接近)。当我做 % padding 或 margin 时,随着屏幕的扩展,它看起来总是不同的......在小屏幕上更容易控制,但在大屏幕上却不是那么容易。
  • 谢谢,但我仍然不明白在不同的视口纵横比下会发生什么。我在答案中放了几个示例(实际上是扩展评论),因此您可以描述每个示例中会发生什么。

标签: html css image bootstrap-4 flexbox


【解决方案1】:

要求是将文本放置在地球上出现在视口中的任何位置 - 即无论图像与视口相比的纵横比如何。主要问题是它没有垂直或水平居中,因此普通的文本居中技术不起作用。

如果我们测量图像的高度和宽度以及地球的中心位置及其直径(以什么单位无关紧要)

然后我们可以让 CSS 计算各种百分比位置等,因此我们可以始终将 div 元素放在地球顶部。

这里是 sn-p,它使用 CSS 变量作为尺寸,因此您可以在图像发生变化时更改它们(或者如果您更准确地测量它们!)。灰色圆圈只是为了显示 div 的放置位置,当然可以删除。您将需要查看类命名以及最终生产版本的字体大小。

有更准确的方法可以使文本保持在一个圆圈内,但希望这个 sn-p 至少有助于确定基本位置。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  width: 100vw;
  height: 100vh;
  /* the image width and height (in any unit) */
  --w: 23.6;
  --h: 14.9;
  /* the globe center and diameter */
  --cx: 12.4;
  --cy: 11;/*10.6*/
  --doriginal: 6.5;
  
  /* we need the actual width of the text to be a little less for portrait phones */
  --d: calc(var(--doriginal) - 0.1);
}

/* for when the width can be accommodated but we have to crop the top and bottom */
#headercontainer {
  width: 100vw;
  height: auto;
  margin-left: 0;
  margin-top: calc(calc(100vh - calc(100vw * calc(var(--h) / var(--w)))) / 2);
  position: fixed;
  overflow:hidden;
}

#headerimg {
  position: relative;
  width: 100vw;
  height: auto;
  overflow: hidden;
}

.center {
/* these 3 lines are here just to demonstrate where the text is going. Remove when have a real image */
  background-color: gray;
  border-style: solid;
  border-radius: 50%;
  
  position: absolute;
  width: calc(calc(var(--d) / var(--w)) * 100%);
  line-height: calc(calc(var(--d) / var(--w)) * 100vw);
  height: calc(calc(var(--d) / var(--h)) * 100%);
  text-align: center;
  left: calc(calc(calc(var(--cx) - calc(var(--d) / 2)) /var(--w)) * 100%);
  top: calc(calc(calc(calc(var(--cy) - calc(var(--d) / 1)) /var(--h)) * 100%));
}

.center h1 {
  margin: auto;
  /*font-size: 1.5em;*/
  font-size: 4vmin;
  font-weight: normal;
  text-transform: uppercase;
  /* font-family: $body-font; */
  font-family: Arial, Helvetica, 'sans serif';
  color: white;
  text-shadow: 0px 5px 5px rgba(0, 0, 0, 0.4);
  letter-spacing: .5px;

/* for vertical centering */
  line-height: 1.5;
  display: inline-block;
  vertical-align: middle;
}

/* for when the height can be accommodated but we have to crop the sides */
@media screen and (max-aspect-ratio: 236/149) {/* rumour has it that you cant use variables in aspect ratios - is that true? */
  #headercontainer, #headerimg {
    height: 100vh;
    width: auto;
    top: 0;
  }
  #headercontainer {
    margin-left: calc(calc(100vw - calc(100vh * calc(var(--w) / var(--h)))) / 2);
    margin-top: 0;
  }
  .center {
    padding: 0 20px 0 20px;/* to stop text flowing slightly out on the right on narrow portrait devices */
    line-height: calc(calc(var(--d) / var(--h)) * 100vh);
  }  
}
<div style="overflow:hidden;">
  <div id="headercontainer">
    <img id="headerimg" src="https://i.stack.imgur.com/kcTtE.png" />
    <div class="center"><h1>your industry link to trusted global market places and technologies</h1></div>
  </div>
  </div>

这是设置场景的原始答案:

这不是一个完整的答案,但对于明智的评论来说太长了。

我们需要确切地确定在不同的设备纵横比上会发生什么。

我浏览过 Chrome 开发工具中提供的所有设备,所有设备都可以包含整个地球(在某些情况下只是),这是一个好的开始,但文本必须以不同的方式流出它.您能否看看这两张图片,并说出您希望文本在每种情况下的样子。

需要决定的各种事情 - 可接受的最小字体大小是多少顶部和底部,如果地球位于左右视口内,那么文本框的最大宽度是多少?

【讨论】:

  • 感谢您的宝贵时间和回复!我知道这是一种荒谬的情况。因此,本质上,文本应该在地球的上部和底部之间“分裂”。我想我应该为 bg 图像本身设置一个最大尺寸,或者如果屏幕达到一定尺寸而只有白色边距而不是它总是全尺寸......我已经附上了理想情况下它应该如何在桌面上始终按比例显示和移动。 link
  • 谢谢,我现在清楚多了。我认为你可以一直使用封面,因为地球确实适合所有已知的设备。我们总是可以测试它什么时候不是极端的。我会回来讨论这个,但不会在几个小时内。
  • 没问题,我真诚地感谢它。我一直在尝试用 bg 本身做一个解决方法,但否则我完全干杯......如果你需要我提供任何其他信息,我不知道在 StackOverflow 上是否可以使用 PM,但很高兴......再次感谢。
猜你喜欢
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多