在溢出文本之前隐藏图标

发布于 2025-02-11 20:52:01 字数 3599 浏览 0 评论 0 原文

在我的网站上,我有一些瓷砖可以显示一些内容。根据屏幕截图,每行瓷砖的数量正在改变。每个瓷砖都有一个带有图标和文本的按钮。

我的问题是,在某些屏幕上,该按钮变为小,因此文本显示在新行中。

当按钮较小以在一行中显示两个时,我想做的是将图标隐藏在文本的前面。有什么方法可以实现吗?

示例代码:

.container {
  display: flex;
  flex-wrap: wrap;
}

.tile {
  width: 50%;
}

@media screen and (min-width: 350px) {
  .tile {
    width: 33.3333%;
  }
}

@media screen and (min-width: 500px) {
  .tile {
    width: 25%;
  }
}

.tile_content {
  margin: 10px;
  background: grey;
}

button {
  width: 100%;
}
<div class="container">

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

</div>

https://jsfiddle.net/qafc1wd9/

On my Website I have some tiles to show some content. Depending on screensize the number of tiles per line is changing. Each tile has a button with an icon and text inside.

My problem is that on some screensizes the button gets to small and therefore the text is displayed in a new line.
enter image description here

What I would like to do is to hide the icon in front of the text when the button gets to small to display both in one line. Is there any way to achieve that?

Example Code:

.container {
  display: flex;
  flex-wrap: wrap;
}

.tile {
  width: 50%;
}

@media screen and (min-width: 350px) {
  .tile {
    width: 33.3333%;
  }
}

@media screen and (min-width: 500px) {
  .tile {
    width: 25%;
  }
}

.tile_content {
  margin: 10px;
  background: grey;
}

button {
  width: 100%;
}
<div class="container">

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

</div>

https://jsfiddle.net/qafc1wd9/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2025-02-18 20:52:02

我发现了这篇文章解释了如何做:

.container {
  display: flex;
  flex-wrap: wrap;
}

.tile {
  width: 50%;
}

@media screen and (min-width: 350px) {
  .tile {
    width: 33.3333%;
  }
}

@media screen and (min-width: 500px) {
  .tile {
    width: 25%;
  }
}

.tile_content {
  margin: 10px;
  background: grey;
}

button {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

svg {
  min-width: 0;
  max-width: 30px;
  width: calc((100% - 124px)*124);
}
<div class="container">

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

</div>

https://jsfiddle.net/0uehno5l/

I found this article explaining how to do it:
https://www.freecodecamp.org/news/the-fab-four-technique-to-create-responsive-emails-without-media-queries-baf11fdfa848/

.container {
  display: flex;
  flex-wrap: wrap;
}

.tile {
  width: 50%;
}

@media screen and (min-width: 350px) {
  .tile {
    width: 33.3333%;
  }
}

@media screen and (min-width: 500px) {
  .tile {
    width: 25%;
  }
}

.tile_content {
  margin: 10px;
  background: grey;
}

button {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

svg {
  min-width: 0;
  max-width: 30px;
  width: calc((100% - 124px)*124);
}
<div class="container">

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

  <div class="tile">
    <div class="tile_content">
      <div>
        some content...
      </div>
      <div>
        <button>
          <svg fill="#000000" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30px" height="30px">
            <path d="M15,3C8.373,3,3,8.373,3,15c0,6.627,5.373,12,12,12s12-5.373,12-12C27,8.373,21.627,3,15,3z M16,21h-2v-7h2V21z M15,11.5 c-0.828,0-1.5-0.672-1.5-1.5s0.672-1.5,1.5-1.5s1.5,0.672,1.5,1.5S15.828,11.5,15,11.5z" /></svg>
          <span>Informations</span>
        </button>
      </div>
    </div>
  </div>

</div>

https://jsfiddle.net/0uehno5L/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文