英文:
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 my 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><a class="social-header-twitter" href="#"><i class="fa-brands fa-twitter fa-xl"></i></a></li>
<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>
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('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%
}
<!-- language: lang-html -->
<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>
<!-- 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('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;
}
header ul li.social-header{
flex-grow:0.5;
}
<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha512-MV7K8+y+gLIBoVD59lQIYicR65iaqukzvf/nwasF0nqhPay5w/9lJmVM2hMDcnK1OnMGCdVK+iQrJ7lzPJQd1w==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<header>
<div>
<ul>
<li class="social-header"><a class="social-header-facebook" href="#"><i class="fa-brands fa-facebook fa-xl"></i></a></li>
<li class="social-header"><a class="social-header-instagram" href="#"><i class="fa-brands fa-instagram fa-xl"></i></a>
<li class="social-header"><a class="social-header-twitter" href="#"><i class="fa-brands fa-twitter fa-xl"></i></a></li> <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>
<!-- 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('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;
}
header ul li:nth-child(-n + 3){
flex-grow:0.5;
}
<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha512-MV7K8+y+gLIBoVD59lQIYicR65iaqukzvf/nwasF0nqhPay5w/9lJmVM2hMDcnK1OnMGCdVK+iQrJ7lzPJQd1w==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<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><a class="social-header-twitter" href="#"><i class="fa-brands fa-twitter fa-xl"></i></a></li> <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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论