将文本放置在amp-img内,布局为“ response”

时间:2019-01-31 17:49:59

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

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

有没有办法做到这一点?

我可以将图像放在背景中,但是我会失去amp-img提供的自适应大小。我尝试使用背景大小的Cover或Contain进行此操作,但是我总是总是在右侧或底部裁剪图像。

我还尝试将文本放置在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创建标签吗。

3 个答案:

答案 0 :(得分:1)

您的包含

缺少宽度。请参见下面的一个简单示例。

<!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>

答案 1 :(得分:0)

我发现使用带有单个幻灯片的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>

答案 2 :(得分:0)

<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>