【问题标题】:How do I edit the background image height without stretching?如何在不拉伸的情况下编辑背景图像高度?
【发布时间】:2022-01-11 00:39:38
【问题描述】:

我正在尝试将背景图像设置为设备屏幕的整个高度,但是我一直遇到一个阻止程序,它只填充了文本上方的屏幕。我一直在努力注意背景图像也需要降低不透明度并将其添加到正文中会影响这一事实?

我该如何解决这个问题?

{编辑: 我删除了中心标签并移动了图像的类以摆脱最大宽度。我的高度仍然有问题。}

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NASCAR Fan Email</title>
    <style>
        body {
            Margin: 0;
            padding: 0;
        }
        
        table {
            border-spacing: 0;
        }
        
        td {
            padding: 0;
        }
        
        img {
            border: 0;
        }
        
        .webkit {
            /* max-width: 900px; */
        }
        
        .outer {
            Margin: 0 auto;
            width: 100%;
            /* max-width: 900px; */
            border-spacing: 0;
            font-family: 'sans-serif';
            color: black;
        }
        
        .fanCouncil {
            border-bottom: 8px solid;
            border-image: linear-gradient(to right, #ffd659 30%, #e4002b 30%, #e4002b 30%, #e4002b 60%, #007ac2 40%, #007ac2 75%) 5;
        }
        
        .fanPhoto {
            opacity: 0.4;
        }
        
        p {
            line-height: 2.2;
            max-width: 1200px;
            font-size: 40px;
        }
        
        a {
            font-size: 40px;
        }
        
        .photoText {
            position: absolute;
            top: 30%;
            left: 25%;
        }
        /* p.entryText {
            position: absolute;
            top: 30%;
            left: 25%;
        }
        
        .clickStart {
            font-weight: bold;
            text-transform: uppercase;
            text-decoration: none;
            position: absolute;
            top: 63%;
            left: 25%;
        }
        
        .unableText {
            position: absolute;
            top: 65%;
            left: 25%;
        }
        
        .thankYou {
            position: absolute;
            top: 87%;
            left: 25%;
        } */
        
        a.copyLink:visited {
            color: rgb(99, 150, 194);
        }
        
        @media screen and (max-width: 600px) {
            .fanHeader,
            .fanPhoto {}
        }
        
        @media screen and (max-width: 600px) {}
    </style>
</head>

<body>

    <div class="webkit">
        <table class="outer" align="center">
            <tr>
                <td>
                    <table width="100%" style="border-spacing: 0;">
                        <tr>
                            <td class="fanCouncil" style="background-color: white; padding: 60px; text-align: center;">
                                <img class="fanHeader" src="https://www.nascar.com/wp-content/uploads/sites/7/2022/01/07/NFC_Horiz_BlackRGB.png" alt="Fan Email" width="900"> </td>
                        </tr>

                    </table>
                </td>
            </tr>
            <tr>
                <td class="photoText">

                    <p class="entryText">Hello, <br> <br> We’d like to invite you to participate in a short survey about this past weekend’s races. This survey should take less than 5 minutes and will be open through Wednesday, August 25. We want to know what you think!</p>
                    <a href="http://www.nascarfancouncil.com/c/al/5YVqVGmxA5g98218UalK-
                        Bl/5bn1vdLmXO68WPcydpyM7J
                        " class="clickStart">Click here to Start</a>
                    <p style="line-height: 3;" class="unableText">If you are unable to click the link, please copy and past the full URL below into your browser: <br>
                        <a href="http://www.nascarfancouncil.com/c/al/5YVqVGmxA5g98218UalK-
                            Bl/5bn1vdLmXO68WPcydpyM7J" class="copyLink">http://www.nascarfancouncil.com/c/al/5YVqVGmxA5g98218UalK-
                                Bl/5bn1vdLmXO68WPcydpyM7J</a>
                    </p>
                    <p class="thankYou">Thank you! <br>Fan Council</p>

            </tr>
            <tr>
                <td>
                    <table width="100%" style="border-spacing: 0;">
                        <img class="fanPhoto" src="https://www.nascar.com/wp-content/uploads/sites/7/2022/01/07/NASCAR_FanCouncil_DaytonaFanPhoto_1-1.jpg">
                    </table>

                </td>
            </tr>
        </table>
    </div>

</body>

</html>

【问题讨论】:

  • 首先,&lt;center&gt; 不是 HTML5 支持的标签。 developer.mozilla.org/en-US/docs/Web/HTML/Element/center
  • 嘿@MattHamer5 我删除了中心标签并对代码进行了编辑,你认为你有解决高度问题的方法吗?
  • 您是否希望背景不移动(固定并居中),而是滚动其中的内容?所以我可以提供帮助(至少尝试提供帮助)
  • @LaaouatniAnas 是的,就是这样
  • @LaaouatniAnas 我相信 tr/td 没问题,这将是一个电子邮件模板

标签: html css height width background-image


【解决方案1】:

我知道这可能不是一个“修复”的答案,但它会是有用的信息,将导致真正的修复,了解问题所在有助于您从概念上理解事物并避免未来的错误

你有很多问题阻止你做你想做的事。首先,您使用table 作为您的架构元素。使用div。仅当您实际上打算以表格形式显示某些东西时才使用表格,例如实际的表格/网格。您正在尝试使用本质上不是响应式的元素来实现响应式行为。表格 HTML 元素没有响应,并且对单元格空间和边框有很多限制。

另外,center 不是有效的语义 HTML 元素。即使您尝试使用,浏览器也不会识别它。我看到你正在尝试做的 webkit 类。您基本上是在尝试重新创建 div。

最后,任何时候您进行响应式设计时,您的宽度都不应被限制为任何已定义的度量单位(px、em、rem 等)。它应该是弹性的(%、vh 或 vw)

重构您的 HTML 或至少将所有表格内容包装在 div 中,然后将背景图片作为 CSS 属性值赋予该 div

.wrapper {
    background-image: url("https://www.nascar.com/wp-content/uploads/sites/7/2022/01/07/NASCAR_FanCouncil_DaytonaFanPhoto_1-1.jpg");
    background-origin: center center;
    background-size: cover;
    background-repeat: no-repeat;
    display: flex;
    height: 100vh; // this ensures the flexbox stretches to 100% of the viewport height (vh)
  }

但实际上,重构您的 HTML。这真的很糟糕而且很糟糕。您正在使用 wordpress。他们有比这更好的模板

【讨论】:

  • 我不久前修改了我的代码并删除了中心标签,但我会考虑有关设备宽度和尺寸的建议,但是,这将用作电子邮件模板和我被指示尽可能少地使用 div。下次我会确保说明这一点,以免下一个提供帮助的人置若罔闻。
  • 嗯,应该添加一点信息。如果是这种情况,那么您不希望图像具有响应性。电子邮件没有响应。现在所有的努力都无关紧要了。将图像保留为图像而不是背景。为了便于阅读,您应该将背景保留为白色。许多人在手机中使用深色主题,其中包括他们的电子邮件应用程序。此图像会混淆文本
【解决方案2】:

这个问题很有挑战性,因为:

  • 我们不能使用GRIDFLEX
  • 我们需要使用table,因为对于电子邮件模板。

所以我们不能使用背景图像,所以我使用了普通的 &lt;img&gt; 并将其设置为使用所有空间,所以 100% 解决了这个错误
object-fit: cover; object-fit 在这里施展魔法!

object-fit 使图像具有响应性,如果高度太长,会自动调整所有东西,使图像看不到所有细节,但仍然看起来不错...

你不想滚动吗?使用position: fixed; 我们也解决了这个问题

我还看到 Image 会超过第一个 &lt;tr&gt;(导航栏)并使导航栏变成灰色,这不是我们想要的
所以...解决方案是把index:-1;

这里是我添加的代码

.fanPhoto {
    opacity: 0.4;
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
    object-fit: cover;
}

文档:

CSS 支持

电子邮件 CSS 支持:https://www.campaignmonitor.com/css/

object-fit 支持大多数电子邮件客户端,尤其是移动端:

  • Gmail 移动版
  • Gmail 桌面
  • IOS 11、10 邮件
  • android 4.4.4 邮件
  • Mac 版 Outlook(目前 Mac 版支持大部分 CSS) 但不是 Outlook 网页版。

支持position

z-index所有客户端都支持!

所有完整的固定代码

body {
  Margin: 0;
  padding: 0;
}

table {
  border-spacing: 0;
}

td {
  padding: 0;
}

img {
  border: 0;
}

.webkit {
  /* max-width: 900px; */
}

.outer {
  Margin: 0 auto;
  width: 100%;
  /* max-width: 900px; */
  border-spacing: 0;
  font-family: 'sans-serif';
  color: black;
}

.fanCouncil {
  border-bottom: 8px solid;
  border-image: linear-gradient(to right, #ffd659 30%, #e4002b 30%, #e4002b 30%, #e4002b 60%, #007ac2 40%, #007ac2 75%) 5;
}

.fanPhoto {
  opacity: 0.4;
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

p {
  line-height: 2.2;
  max-width: 1200px;
  font-size: 40px;
}

a {
  font-size: 40px;
}

.photoText {
  position: absolute;
  top: 30%;
  left: 25%;
}


/* p.entryText {
    position: absolute;
    top: 30%;
    left: 25%;
}

.clickStart {
    font-weight: bold;
    text-transform: uppercase;
    text-decoration: none;
    position: absolute;
    top: 63%;
    left: 25%;
}

.unableText {
    position: absolute;
    top: 65%;
    left: 25%;
}

.thankYou {
    position: absolute;
    top: 87%;
    left: 25%;
} */

a.copyLink:visited {
  color: rgb(99, 150, 194);
}

@media screen and (max-width: 600px) {
  .fanHeader,
  .fanPhoto {}
}

@media screen and (max-width: 600px) {}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>NASCAR Fan Email</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="webkit">
    <table class="outer" align="center">
      <!-- navbar -->
      <tr>
        <td>
          <table width="100%" style="border-spacing: 0;">
            <tr>
              <td class="fanCouncil" style="background-color: white; padding: 60px; text-align: center;">
                <!-- navbar image FAN COUNCIL -->
                <img class="fanHeader" src="https://www.nascar.com/wp-content/uploads/sites/7/2022/01/07/NFC_Horiz_BlackRGB.png" alt="Fan Email" width="900">
              </td>
            </tr>
          </table>
        </td>
      </tr>

      <!-- the text below the big navbar -->
      <tr>
        <!-- the text: hello,... -->
        <td class="photoText">
          <!-- hello ... think! -->
          <p class="entryText">Hello, <br> <br> We’d like to invite you to participate in a short survey about this past weekend’s races. This survey should take less than 5 minutes and will be open through Wednesday, August 25. We want to know what you think!</p>
          <!-- 1 link -->
          <a href="http://www.nascarfancouncil.com/c/al/5YVqVGmxA5g98218UalK-Bl/5bn1vdLmXO68WPcydpyM7J" class="clickStart">Click here to Start</a>
          <!-- if you ... browser: -->
          <p style="line-height: 3;" class="unableText">If you are unable to click the link, please copy and past the full URL below into your browser: <br></p>
          <!-- 2 link full-long-link -->
          <a href="http://www.nascarfancouncil.com/c/al/5YVqVGmxA5g98218UalK-Bl/5bn1vdLmXO68WPcydpyM7J" class="copyLink">http://www.nascarfancouncil.com/c/al/5YVqVGmxA5g98218UalK-Bl/5bn1vdLmXO68WPcydpyM7J</a>
          <!-- thank you -->
          <p class="thankYou">Thank you!<br>Fan Council</p>
        </td>
      </tr>

      <!-- here where there is the background -->
      <tr>
        <td>
          <table width="100%" style="border-spacing: 0;">
            <img class="fanPhoto" src="https://www.nascar.com/wp-content/uploads/sites/7/2022/01/07/NASCAR_FanCouncil_DaytonaFanPhoto_1-1.jpg">
          </table>
        </td>
      </tr>
    </table>
  </div>

</body>

</html>

【讨论】:

  • 很抱歉回复晚了,但非常感谢您的帮助和您引用的资源!祝你好运
猜你喜欢
  • 1970-01-01
  • 2015-08-07
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 2012-03-21
  • 2015-05-04
  • 1970-01-01
相关资源
最近更新 更多