首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Flex中丢失带有链接的图像的页边距

在Flex中丢失带有链接的图像的页边距
EN

Stack Overflow用户
提问于 2021-08-04 23:32:32
回答 2查看 30关注 0票数 0

我想在flexbox中添加带有链接的图片的边距,但以下代码不起作用。.icon img下的margin-top: 2.5%;未生效。有人能告诉我我哪里做错了吗?谢谢。

代码语言:javascript
复制
.name {
  margin-top: 6%;
  color:darkblue;
  margin-bottom: 0;
  margin-left: 47.3%;
  margin-right: 2%;
}

.nameAndContacts {
  display: flex;
  justify-content: space-between;
  margin-left:26.8%;
  margin-right: 30%;
}

.icons img {
  margin-top: 2.5%;
  max-height: 20px;
  max-width: auto;
}
代码语言:javascript
复制
<div class="nameAndContacts">
        <p class="name"><b>Tianyi Ma</b></p>
        <div class="icons">
          <a href="https://github.com/tianyi-m">
          <img src={githubIcon} alt="Github Icon"></img></a></div>
        <div class="seperator"></div>
        <div class="icons">
          <a href="https://medium.com/@tma8">
            <img src={mediumIcon} alt="Medium Icon"></img></a></div>
        <div class="icons">
          <a href="https://www.douban.com/people/243284156/">
            <img src={doubanIcon} alt="Douban Icon"></img></a></div>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-05 12:45:19

下面的代码片段会对你有很大的帮助。您可以使用具有良好html结构的display:flex;属性。

代码语言:javascript
复制
*,*::before,*::after{
    box-sizing: border-box;
}
.nameAndContacts {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 auto;
  width: 100%;
  max-width: calc(100% - 20%);
  background-color: #f5f5f5;
  padding: 10px;
}
.name {
  color:darkblue;
  margin: 0;
}
.icons-box{
    display: flex;
    align-items: center;
}
.seperator{
    height: 20px;
    min-height: 20px;
    width: 1px;
    min-width: 1px;
    background: #ccc;
    margin: 0 15px;
}
.icons a{
    display: block;
}
.icons img {
    min-height: 20px;
    min-width: 20px;
    max-height: 20px;
    max-width: 20px;
    display: block;
    margin: 0;
}
代码语言:javascript
复制
<div class="nameAndContacts">
    <p class="name"><b>Tianyi Ma</b></p>
    <div class="icons-box">
        <div class="icons">
            <a href="https://github.com/tianyi-m">
                <img src="https://avatars.githubusercontent.com/apple?v=4" alt="Icon"/>
            </a>
        </div>
        <div class="seperator"></div>
        <div class="icons">
            <a href="https://medium.com/@tma8">
                <img src="https://avatars.githubusercontent.com/ansible?v=4" alt="Icon"/>
            </a>
        </div>
        <div class="seperator"></div>
        <div class="icons">
            <a href="https://www.douban.com/people/243284156/">
                <img src="https://avatars.githubusercontent.com/gatsbyjs?v=4" alt="Icon"/>
            </a>
        </div>
    </div>
</div>

票数 0
EN

Stack Overflow用户

发布于 2021-08-05 12:19:30

这取决于其他样式以及在这些样式之前或之后添加的元素。您可以尝试将display: block;属性添加到<a></a>标记中,这可能会有所帮助。

代码语言:javascript
复制
<a href="https://github.com/tianyi-m" style="display: block;">
   <img src={githubIcon} alt="Github Icon"></img>
</a>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68659019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档