如何在不使用Bootstrap的情况下将图标和内容并排显示?

huangapple go评论54阅读模式
英文:

How to display icon and content next to each other without using bootstrap?

问题

这是HTML代码的翻译部分:

我有这个HTML,左边显示图标,右边显示摘要。我使用Bootstrap类`col-sm-3 col-md-3`包装了图标和摘要。我这样做是因为我想每行只显示四个图标。如果图标超过四个,那么图标需要移到下一行。我将摘要放在`col-sm-3 col-md-3`类中,因为我希望文本位于图标旁边。

然而,我不再想使用Bootstrap,所以我正在寻找只使用CSS来实现我想要的效果的方法。

这是HTML代码:








全球变暖可能导致环境发生许多严重的变化,最终影响人类健康。它还可能导致海平面上升,导致沿海土地的丧失,降水模式的变化,增加了干旱和洪水的风险,并对生物多样性构成威胁。

```
```

这是CSS代码的翻译部分:

.theIcons{
    display: grid;
    grid-template-columns: repeat(4, 1fr);
}

.theSummary{
    width: 25%;
}

请注意,HTML和CSS代码已经被翻译,只包括您要求的翻译部分,没有额外的内容。

英文:

I have this HTML that displays icons on the left, and summary on the right. I wrapped the icons and the summary using a bootstrap class col-sm-3 col-md-3. I did this because I wanted to display only four icons per row.If there are more than four icons, then the icons need to move the next row. I put the summary in col-sm-3 col-md-3 class because I want the text to be next to the icons.

However, I no longer want to use bootstrap, so I am looking at ways to achieve what I want with just CSS.

Here is the HTML:

<div class="coreStuff"> 
    <div class="col-sm-3 col-md-3">
        <i class="fa-solid fa-globe"></i>
        <i class="fa-solid fa-globe"></i>
        <i class="fa-solid fa-globe"></i>
        <i class="fa-solid fa-globe"></i>
        <i class="fa-solid fa-globe"></i>
        <i class="fa-solid fa-globe"></i>
        <i class="fa-solid fa-globe"></i>
        <i class="fa-solid fa-globe"></i>
        <i class="fa-solid fa-globe"></i>
    </div>  
    <div class="col-sm-3 col-md-3">
        <p>Global warming can result in many serious alterations to the environment, eventually impacting human health. It can also cause a rise in sea level, leading to the loss of coastal land, a change in precipitation patterns, increased risks of droughts and floods, and threats to biodiversity.</p>
    </div>
</div>

I think I got it to partially work with CSS grid, but let's say there is only one icon, or two icon, in that case this CSS won't work because it creating a wide gap between the icon(s) and the summary. Is there a way to make the summary responsive and dynamically adjust with respective to the icons?

CSS:

.theIcons{
    display: grid;
    grid-template-columns: repeat(4, 1fr);
}

.theSummary{
    width: 25%;
}

答案1

得分: 0

希望对您有帮助。

<div class="coreStuff">
    <div class="icons">
        <i class="fa-solid fa-globe">1</i>
        <i class="fa-solid fa-globe">2</i>
        <i class="fa-solid fa-globe">3</i>
        <i class="fa-solid fa-globe">4</i>
        <i class="fa-solid fa-globe">5</i>
        <i class="fa-solid fa-globe">6</i>
        <i class="fa-solid fa-globe">7</i>
        <i class="fa-solid fa-globe">8</i>
        <i class="fa-solid fa-globe">9</i>
    </div>
    <div class="summary">
        <p>全球变暖可能导致环境发生许多严重变化,最终影响人类健康。 这也可能导致海平面上升,导致沿海土地的丧失,降水模式的改变,干旱和洪水风险增加,以及对生物多样性的威胁。</p>
    </div>
</div>

CSS:
(边框仅用于提供视觉反馈,您可以根据需要进行样式设置。另外,您可能需要根据自己的设计和布局调整解决方案)

.icons, .summary {
  border: 1px solid red;
}

.coreStuff {
  display: flex;
  width: 30em;
}

.icons i {
  border: 1px solid red;
}

.icons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  flex-basis: 30%;
}

.summary {
  flex-basis: 70%;
}
英文:

Hope this helps.

&lt;div class=&quot;coreStuff&quot;&gt; 
    &lt;div class=&quot;icons&quot;&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;1&lt;/i&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;2&lt;/i&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;3&lt;/i&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;4&lt;/i&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;5&lt;/i&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;6&lt;/i&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;7&lt;/i&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;8&lt;/i&gt;
        &lt;i class=&quot;fa-solid fa-globe&quot;&gt;9&lt;/i&gt;
    &lt;/div&gt;  
    &lt;div class=&quot;summary&quot;&gt;
        &lt;p&gt;Global warming can result in many serious alterations to the environment, eventually impacting human health. It can also cause a rise in sea level, leading to the loss of coastal land, a change in precipitation patterns, increased risks of droughts and floods, and threats to biodiversity.&lt;/p&gt;
    &lt;/div&gt;
&lt;/div

CSS :
(the borders are just to give visual feedback, you can style as per your needs. Also, you might have to tweak the solution based on your own design and layout)

.icons, .summary{
  border: 1px solid red;
}

.coreStuff { 
  display: flex;
/*  you&#39;ll probably want to give some fix width  */
  width: 30em;
}

.icons i {
  border: 1px solid red;
/*   height: 30px; */
/*   width: 30px; */
}

.icons{
  display: grid;
  grid-template-columns : repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  flex-basis: 30%;
/*   gap: 0.5em; */
}
.summary{
  flex-basis: 70%;
}

答案2

得分: 0

你的意思是每个摘要都有一个图标吗?如果是这样,你需要将你的HTML代码更改为以下内容:

<div class="coreStuff">
  <div class="box">
    <i class="fa-solid fa-globe"></i>
    <p>摘要内容</p>
  </div>
</div>

你的CSS可以像这样:

.coreStuff {
  display: flex;
  flex-wrap: wrap;
  gap: 1%;
}

.box {
  width: 24%;
  display: flex;
  gap: 10px;
}
英文:

Do you mean there is an icon for each summary? If that is true you have to change your HTML to be like this

&lt;div class=&quot;coreStuff&quot;&gt; 
&lt;div class=&quot;box&quot;&gt;
&lt;i class=&quot;fa-solid fa-globe&quot;&gt;&lt;/i&gt;
&lt;p&gt;your summary &lt;/p&gt;
&lt;/div&gt;

and your css It could be like this :

.coreStuff{
display:flex;
flex-wrap: wrap;
gap:1%
}
.box{
width:24%
display:flex;
gap : 10px ;
}

</details>



huangapple
  • 本文由 发表于 2023年6月15日 13:32:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定