【发布时间】:2022-01-15 00:10:32
【问题描述】:
使用下面的代码,我试图点击屏幕右侧的“关注”按钮。为此,我在 .follow 类中使用了 margin: 0 0 0 auto。当我在父元素(这里是标题元素)中使用 display: flex 时它可以工作,但是当我从父元素中删除 display: flex 时,它就会出现我的名字。为什么会这样?
header {
display: flex;
font-family: Montserrat, monospace;
font-weight: 200;
}
.profile-pic {
width: 50px;
height: 50px;
border-radius: 50%;
border: 2px;
}
.name {
margin-left: 10px;
}
.username {
margin-left: 10px;
}
.follow {
margin: 0 0 0 auto;
border: 0;
border-radius: 10px;
}
<header>
<img src="pic1.jpg" alt="Azraf Khan Zarif" class="profile-pic">
<div class="name">Azraf Khan Zarif</div>
<div class="username">@Zarif</div>
<button class="follow">Follow</button>
</header>
【问题讨论】:
-
试试
float: right; -
因为
margin: 0 0 0 auto;用于右对齐流入块级元素,而您的按钮是内联级。