如何减小我的 flex 元素之间的间距?

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

How can I decrease the space between my flex elements?

问题

Sure, here's the translated portion of your request:

我正在制作一个标题,在标题中,我希望社交媒体链接之间的间距很小,而主页、关于、政策和联系链接之间的间距正常。

这是我的HTML代码:

<header>
    <div>
        <ul>
            <li><a class="social-header-facebook" href="#"><i class="fa-brands fa-facebook fa-xl"></i></a></li>
            <li><a class="social-header-instagram" href="#"><i class="fa-brands fa-instagram fa-xl"></i></a></li>
            <li><a class="social-header-twitter" href="#"><i class="fa-brands fa-twitter fa-xl"></i></a></li>
            <li><a class="links-header" href="#">主页</a></li>
            <li><a class="links-header" href="#">关于</a></li>
            <li><a class="links-header" href="#">政策</a></li>
            <li><a class="links-header" href="#">联系</a></li>
        </ul>
    </div>
</header>

这是我的CSS代码:

header {
    position: sticky;
    top: 0;
}

header ul {
    display: flex;
    position: absolute;
    width: 100%;
}

header ul li {
    list-style: none;
    flex-grow: 1;
}

header .links-header {
    text-decoration: none;
    color: rgb(255, 255, 255);
    background-color: rgba(255, 255, 255, 0.537);
    border-radius: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 15px;
    padding-right: 15px;
    transition: .5s;
}

header .social-header-facebook,
.social-header-instagram,
.social-header-twitter {
    text-decoration: none;
    color: rgb(255, 255, 255);
    background-color: rgba(255, 255, 255, 0.537);
    border-radius: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 6px;
    padding-right: 6px;
    transition: .5s;
}

希望这可以帮助您。如果您有其他问题,请随时提问。

英文:

I'm making a header and in that header I want the margin between the social media links to be small while the margin between the home, about, policy, contact links to normal.

this is what it looks like

this is my html

 &lt;header&gt;
            &lt;div&gt;
                &lt;ul&gt;
                    &lt;li&gt;&lt;a class=&quot;social-header-facebook&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-facebook fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;social-header-instagram&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-instagram fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                    &lt;li&gt;&lt;a class=&quot;social-header-twitter&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-twitter fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;home&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;about&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;policy&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;contact&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/div&gt;
        &lt;/header&gt;

this is my css

header {
    position: sticky;
    top: 0;
    /* sticky with top 0 make the bar sticks to the top */

}

header ul {
    display: flex;
    position: absolute;
    width: 100%;
    /* when using flex and absolute you will need width % to make your items fill the screen */

}

header ul li {
    list-style: none;
    flex-grow: 1;

}

header .links-header {
    text-decoration: none;
    color: rgb(255, 255, 255);
    background-color: rgba(255, 255, 255, 0.537);
    border-radius: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 15px;
    padding-right: 15px;
    transition: .5s;

}

header .social-header-facebook,
.social-header-instagram,
.social-header-twitter {
    text-decoration: none;
    color: rgb(255, 255, 255);
    background-color: rgba(255, 255, 255, 0.537);
    border-radius: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 6px;
    padding-right: 6px;
    transition: .5s;
}

ps: I'm a begginer at css and this is my first time using flex anyway, so I'd be thankful if you point out any mistakes you find as well.

I tried setting a negative margin but that didn't work as inteneded.

答案1

得分: 1

以下是代码的翻译部分:

最简单的解决方案是将包含社交媒体链接的3个“li”放入一个div中,并为该div设置一个宽度。可以使用以下代码实现这一点。容器div的宽度越短,您的社交媒体图标就越靠近。

body{
  background:url('https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547__480.jpg') no-repeat ;
  background-size:cover;
}

header {
    position: sticky;
    top: 0;
    /* sticky with top 0 make the bar sticks to the top */
}

header ul {
    display: flex;
    position: absolute;
    width: 100%;
    /* when using flex and absolute you will need width % to make your items fill the screen */
}

header ul li {
    list-style: none;
    flex-grow: 1;
}

header .links-header {
    text-decoration: none;
    color: rgb(255, 255, 255);
    background-color: rgba(255, 255, 255, 0.537);
    border-radius: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 15px;
    padding-right: 15px;
    transition: .5s;
}

header .social-header-facebook,
.social-header-instagram,
.social-header-twitter {
    text-decoration: none;
    color: rgb(255, 255, 255);
    background-color: rgba(255, 255, 255, 0.537);
    border-radius: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 6px;
    padding-right: 6px;
    transition: .5s;
}

.social-icons{
  width:20%;
  display:flex;
  padding:0 10%;
}
<header>
    <div>
        <ul>
          <div class="social-icons">
             <li><a class="social-header-facebook" href="#"><i class="fa-brands fa-facebook fa-xl"></i></a></li>
            <li><a class="social-header-instagram" href="#"><i class="fa-brands fa-instagram fa-xl"></i></a>
            <li><a class="social-header-twitter" href="#"><i class="fa-brands fa-twitter fa-xl"></i></a></li>
          </div>
           
            <li><a class="links-header" href="#">home</a></li>
            <li><a class="links-header" href="#">about</a></li>
            <li><a class="links-header" href="#">policy</a></li>
            <li><a class="links-header" href="#">contact</a></li>
        </ul>
    </div>
</header>

请根据需要更改背景。我在演示中使用了一个背景图像。

英文:

The easiest solution would be to put the 3 "li" containing the social media links inside a div and give that div a width. This can be achieved using the below code. The shorter the width of the container div, the closer your social media icons.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body{
  background:url(&#39;https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547__480.jpg&#39;) no-repeat ;
  background-size:cover;
}


header {
    position: sticky;
    top: 0;
    /* sticky with top 0 make the bar sticks to the top */

}

header ul {
    display: flex;
    position: absolute;
    width: 100%;
    /* when using flex and absolute you will need width % to make your items fill the screen */

}

header ul li {
    list-style: none;
    flex-grow: 1;

}

header .links-header {
    text-decoration: none;
    color: rgb(255, 255, 255);
    background-color: rgba(255, 255, 255, 0.537);
    border-radius: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 15px;
    padding-right: 15px;
    transition: .5s;

}

header .social-header-facebook,
.social-header-instagram,
.social-header-twitter {
    text-decoration: none;
    color: rgb(255, 255, 255);
    background-color: rgba(255, 255, 255, 0.537);
    border-radius: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 6px;
    padding-right: 6px;
    transition: .5s;
}

.social-icons{
  width:20%;
  display:flex;
  padding:0 10% 
}

<!-- language: lang-html -->

&lt;header&gt;
            &lt;div&gt;
                &lt;ul&gt;
                  &lt;div class=&quot;social-icons&quot;&gt;
                     &lt;li&gt;&lt;a class=&quot;social-header-facebook&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-facebook fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;social-header-instagram&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-instagram fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                    &lt;li&gt;&lt;a class=&quot;social-header-twitter&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-twitter fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                    
                  &lt;/div&gt;
                   
                    &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;home&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;about&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;policy&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;contact&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/div&gt;
        &lt;/header&gt;

<!-- end snippet -->

PS: Please change your background accordingly. I have used one for demo purposes.

答案2

得分: 1

Sure, here are the translated parts:

你可以保持HTML有效,并利用flex-grow。在您想要调整的列表项上添加一个类,然后使用flex-grow:0.5;或适合您的值。

您还可以跳过添加类,并使用nth-child伪类,但显式类更好。

I've provided translations for the relevant code portions.

英文:

You can keep the HTML valid and leverage flex-grow. Add a class to the list items you want to adjust then use flex-grow:0.5; or a value that works for you.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  background: url(&#39;https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547__480.jpg&#39;) no-repeat;
  background-size: cover;
}

header {
  position: sticky;
  top: 0;
  /* sticky with top 0 make the bar sticks to the top */
}

header ul {
  display: flex;
  position: absolute;
  width: 100%;
  /* when using flex and absolute you will need width % to make your items fill the screen */
}

header ul li {
  list-style: none;
  flex-grow: 1;
}

header .links-header {
  text-decoration: none;
  color: rgb(255, 255, 255);
  background-color: rgba(255, 255, 255, 0.537);
  border-radius: 10px;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 15px;
  padding-right: 15px;
  transition: .5s;
}

header .social-header-facebook,
.social-header-instagram,
.social-header-twitter {
  text-decoration: none;
  color: rgb(255, 255, 255);
  background-color: rgba(255, 255, 255, 0.537);
  border-radius: 10px;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 6px;
  padding-right: 6px;
  transition: .5s;
}

header ul li.social-header{
  flex-grow:0.5;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css&quot; integrity=&quot;sha512-MV7K8+y+gLIBoVD59lQIYicR65iaqukzvf/nwasF0nqhPay5w/9lJmVM2hMDcnK1OnMGCdVK+iQrJ7lzPJQd1w==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;
/&gt;
&lt;header&gt;
  &lt;div&gt;
    &lt;ul&gt;
      &lt;li class=&quot;social-header&quot;&gt;&lt;a class=&quot;social-header-facebook&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-facebook fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li  class=&quot;social-header&quot;&gt;&lt;a class=&quot;social-header-instagram&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-instagram fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;
      &lt;li  class=&quot;social-header&quot;&gt;&lt;a class=&quot;social-header-twitter&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-twitter fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;         &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;about&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;policy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;contact&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/header&gt;

<!-- end snippet -->

You could also skip adding a class and use the nth-child pseudo class, but an explicit class is better .

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  background: url(&#39;https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547__480.jpg&#39;) no-repeat;
  background-size: cover;
}

header {
  position: sticky;
  top: 0;
  /* sticky with top 0 make the bar sticks to the top */
}

header ul {
  display: flex;
  position: absolute;
  width: 100%;
  /* when using flex and absolute you will need width % to make your items fill the screen */
}

header ul li {
  list-style: none;
  flex-grow: 1;
}

header .links-header {
  text-decoration: none;
  color: rgb(255, 255, 255);
  background-color: rgba(255, 255, 255, 0.537);
  border-radius: 10px;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 15px;
  padding-right: 15px;
  transition: .5s;
}

header .social-header-facebook,
.social-header-instagram,
.social-header-twitter {
  text-decoration: none;
  color: rgb(255, 255, 255);
  background-color: rgba(255, 255, 255, 0.537);
  border-radius: 10px;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 6px;
  padding-right: 6px;
  transition: .5s;
}

header ul li:nth-child(-n + 3){
  flex-grow:0.5;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css&quot; integrity=&quot;sha512-MV7K8+y+gLIBoVD59lQIYicR65iaqukzvf/nwasF0nqhPay5w/9lJmVM2hMDcnK1OnMGCdVK+iQrJ7lzPJQd1w==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;
/&gt;
&lt;header&gt;
  &lt;div&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a class=&quot;social-header-facebook&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-facebook fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a class=&quot;social-header-instagram&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-instagram fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;
      &lt;li&gt;&lt;a class=&quot;social-header-twitter&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa-brands fa-twitter fa-xl&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;         &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;about&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;policy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a class=&quot;links-header&quot; href=&quot;#&quot;&gt;contact&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/header&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定