【问题标题】:Place text inside an amp-img with layout="responsive"将文本放置在带有 layout="responsive" 的 amp-img 中
【发布时间】:2019-06-25 06:17:21
【问题描述】:

我有一个带有 layout="responsive" 的 amp-img,如果您喜欢这样说,我需要在其中放置一些文本,或者在其顶部放置一些文本。 amp-img 将填充屏幕的宽度,高度将由 amp-img 确定,以便整个图像可见并保持纵横比。

有没有办法做到这一点?

我可以将图像放在背景中,但我会失去 amp-img 提供的响应式大小。我尝试过使用背景大小的封面或包含,但我的图像总是被裁剪,无论是在右侧还是底部。

我也尝试使用 position:absolute 放置文本,但无法将文本放在图像顶部。这是一种尝试,最终得到了图片下方的文字:

<div style="position:relative">
<amp-img src="/images/@Model.ImageUrl" layout="responsive" width="1920" height="1080" alt=""></amp-img> @* 16 x 9 *@
<div class="clearfix" style="padding-top:25%; padding-bottom:10%; position:absolute; z-index:1">
    <div class="mx-auto md-col-9">
        <h2 class="tx-g2 tx-center ml-1 mr-1 shadow mb-0" style="{text-transform:uppercase;}">
            <amp-fit-text width="400" height="20" layout="responsive" max-font-size="75">
                @Html.Raw(Model.Title)
            </amp-fit-text>
        </h2>
    </div>
</div>
</div>

有没有办法让图片大小正确并在上面放置文字?

PS。有人会为 amp-fit-text 创建标签吗?

【问题讨论】:

    标签: background-image css-position amp-html amp-img


    【解决方案1】:
    <div style="position: relative;">
        <amp-img src="https://omoiwords.com/stories-poster.jpeg-2048.jpeg" 
            width="1228" height="819" layout="responsive"></amp-img>
        <div style="background-color: rgba(0,0,0,0.7); 
            color: white; width: 80%; position:absolute; top:10%; left: 10%;">
            <amp-fit-text 
                width="200" height="50" layout="responsive">
                Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
                Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
            </amp-fit-text>
        </div>
    </div>
    
    <script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>
    

    【讨论】:

    • 请不要忘记添加脚本。
    【解决方案2】:

    我找到了一种使用 amp-carousel 和单张幻灯片的解决方法:

    <amp-carousel layout="responsive" height="1080" width="1920" type="slides" style="position:relative;">
    <div  style="background:linear-gradient(rgba(0,0,0,0), rgba(0,0,0,0)),
                url(/images/@Model.ImageUrl);
            background-size:contain; background-repeat:no-repeat; width:100%; height:100%;">
        <div class="clearfix" style="padding-top:25%; padding-bottom:10%;">
            <div class="mx-auto md-col-9">
                <h2 class="tx-g2 tx-center ml-1 mr-1 shadow mb-0" style="{text-transform:uppercase;}">
                    <amp-fit-text width="400" height="20" layout="responsive" max-font-size="75">
                        @Html.Raw(Model.Title)
                    </amp-fit-text>
                </h2>
            </div>
        </div>
    </div>
    

    【讨论】:

      【解决方案3】:

      包含

      缺少宽度。请参阅下面的更简单示例。

      <!doctype html>
      <html ⚡>
      <head>
        <meta charset="utf-8">
        <title> Hello World</title>
        <script async src="https://cdn.ampproject.org/v0.js"></script>
      <script async custom-element="amp-fit-text" src="https://cdn.ampproject.org/v0/amp-fit-text-0.1.js"></script>
      
        <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
        <style amp-custom>
        </style>
        <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
      </head>
      <body>
          <div style="position: relative;">
              <amp-img src="https://omoiwords.com/stories-poster.jpeg-2048.jpeg" 
                  width="1228" height="819" layout="responsive"></amp-img>
              <div style="background-color: rgba(0,0,0,0.7); 
                  color: white; width: 80%; position:absolute; top:10%; left: 10%;">
                  <amp-fit-text 
                      width="200" height="50" layout="responsive">
                      Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
                      Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
                  </amp-fit-text>
              </div>
          </div>
      </body>
      </html>

      【讨论】:

      • 我错过了什么?我认为我的 amp-fit-text 的宽度为 400。在我的失败时,一定有其他东西可以让你工作。
      • 包含 amp-fit-text 而非 amp-fit-text 的元素。即,您的元素响应的容器。
      • 我想我明白了。我的 SEO 家伙想要那个 h2,但它是另一个元素,它将 amp-fit-text 与包含 div 上的宽度(类'md-col-9')分开。感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多