【问题标题】:Bootstrap header will not change the image size引导标头不会更改图像大小
【发布时间】:2021-08-17 02:50:00
【问题描述】:

我拼命想要完成这项工作,但我不确定自己在做什么。由于某种原因,小视口中的图像比它们在大视口中的外观要小,所以我很难过。

    <div class="container-fluid p-2">
        <div class="flex-row">
            <div class="d-flex justify-content-center justify-content-xl-between align-items-center flex-wrap">
                <div class="d-flex  p-2">
                    <img id="logo" class="img-fluid" src="img/croppedlogo.svg" style="min-width: 300px; max-width: 100%;">
                </div>
                <div class="w-100 d-none d-sm-flex d-xl-none"></div>
                <div class="d-none d-sm-flex p-2">
                    <div>
                        <a class="headerLink" href="index.aspx">Home</a>
                        <a class="headerLink" href="index.aspx">Products</a>
                        <a class="headerLink" href="index.aspx">About</a>
                        <a class="headerLink" href="index.aspx">Blog</a>
                        <a class="headerLink" href="index.aspx">Contact</a>
                    </div>
                </div>
                <div class="d-flex d-sm-none p-2">
                    <img src="img/menu.png">
                </div>
                <div class="w-100 d-xl-none"></div>
                <div class="p-2">
                    <a href="index.aspx">
                        <img src="img/btnLogin.png" />
                    </a>
                    <a href="index.aspx">
                        <img src="img/btnRequestDemo.png" />
                    </a>
                    <a href="index.aspx">
                        <img src="img/btnResources.png" />
                    </a>
                </div>
            </div>
        </div>
    </div>

这是我拥有的少量额外 CSS:

body {
    margin: 0px;
    font-family: Arial;
    font-size: 1em;
    background-color: #FBFBFD;
}

.headerLink + .headerLink {
    margin-left: 10px;
}

a.headerLink {
    color: black;
    text-decoration: none;
    font-weight: bold;
    padding: 0px;
}
a.headerLink:hover {
    text-decoration: underline;
}

按钮 png 图像在 100px x 50px 到 200px x 50px 之间

标志的标题如下:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="logo" x="0px" y="0px" viewBox="49.199989318847656 82.18773651123047 697.5000610351562 155.11227416992188" style="enable-background:new 0 0 792 330;" xml:space="preserve">
     viewBox="0 0 792 330" style="enable-background:new 0 0 792 330;" xml:space="preserve"&gt;>

我试图让图像在较小的视口中跨越屏幕的宽度,但就目前而言,手机中的布局比完整桌面设置中的布局还要小。

任何帮助将不胜感激

【问题讨论】:

  • 没有图像(或至少它们的尺寸),您的问题在技术上是无法回答的。如果它无法回答,它就无法帮助任何人,包括你自己,因此这里是题外话。请创建一个minimal reproducible example。并详细描述预期的结果。
  • 您添加了svg 标签,但它不包含任何图形(例如,&lt;rect&gt;&lt;path&gt; 等)- 它们在哪里? (另外,有些东西悬在空中,没有封闭标签,例如viewBox)。此外,您还没有添加 png 图像 - 如果不需要它们来介绍您面临的问题,最好从问题中删除该代码。此外,如果您可以添加预期与实际错误行为的屏幕截图,那就太好了。
  • 你能设置一个codepen吗?为了能够修补这个例子。我怀疑徽标内联样式的最大宽度,但我需要确认它。
  • 所以我很抱歉没有早点回来,但碰巧的是,我们的一个承包商为我们做了这项工作,所以我不再需要答案了,因为我不是一个图形的家伙,我不管它。感谢大家的回复。

标签: css bootstrap-4 flexbox responsive-design image-size


【解决方案1】:

根据您编写代码的方式,我了解到您的问题出在较小的屏幕上,因此根据您的情况,我编写了以下代码:

body {
  margin: 0px;
  font-family: Arial;
  font-size: 1em;
  background-color: #fbfbfd;
}

.headerLink+.headerLink {
  margin-left: 10px;
}

a.headerLink {
  color: black;
  text-decoration: none;
  font-weight: bold;
  padding: 0px;
}

a.headerLink:hover {
  text-decoration: underline;
}

@media all and (max-width: 768px) {
  figure img {
    width: 150px;
    height: 50px;
    object-fit: cover;
  }
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">

<div class="container-fluid p-2">
  <div class="flex-row">
    <div class="d-flex justify-content-center justify-content-xl-between align-items-center flex-wrap">
      <div class="d-flex  p-2">
        <img id="logo" class="img-fluid" src="img/croppedlogo.svg" style="min-width: 300px; max-width: 100%;">
      </div>
      <div class="w-100 d-none d-sm-flex d-xl-none">
        <div class="d-none d-sm-flex p-2">
          <div>
            <a class="headerLink" href="index.aspx">Home</a>
            <a class="headerLink" href="index.aspx">Products</a>
            <a class="headerLink" href="index.aspx">About</a>
            <a class="headerLink" href="index.aspx">Blog</a>
            <a class="headerLink" href="index.aspx">Contact</a>
          </div>
        </div>
      </div>
      <div class="d-flex d-sm-none p-2">
        <img src="img/menu.png">
      </div>
      <div class="w-100 d-xl-none"></div>
      <figure class="d-flex p-2">
        <a href="index.aspx">
          <img src="img/btnLogin.png" />
        </a>
        <a href="index.aspx">
          <img src="img/btnRequestDemo.png" />
        </a>
        <a href="index.aspx">
          <img src="img/btnResources.png" />
        </a>
      </figure>
    </div>
  </div>
</div>

请注意,我将负责图像的 div 重命名为 figure 只是为了使 CSS 工作更容易,但您可以随意命名。

您希望图像在较小的视口中占据整个屏幕宽度,但您说图像的宽度在 100 像素到 200 像素之间,因此不清楚是否要调整它们的大小,但作为示例,我使用了 150 像素CSS 以能够说明示例。 请注意,我为图像使用了object-fit: cover,以保持其比例并填充提供的尺寸。但是在这里可以裁剪图像以适合。但如果你想不惜一切代价扩展它们,你可以使用width: 100vw; object-fit: fill;

关于这个特定的图片问题,我建议您查看 W3 Schools 的此链接:CSS The object-fit Property

至于您提供的 .svg,我无法将其插入到您的代码中,因为我不知道它的确切位置。

如果您有任何问题,请在此处评论,根据您的需要我会更新我的答案。

【讨论】:

  • 感谢您抽出宝贵时间查看此内容,感谢您的详尽回答。
猜你喜欢
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多