css - Angular中的浮动位置
问题描述
我想将元素的位置设置在元素内的右侧div
。float:right
不起作用,所以我尝试margin-left:auto
,但如果我只有一个元素,它看起来很漂亮。我怎样才能为多个元素实现这一点?
我的 CSS:
.toolbar {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 60px;
display: flex;
align-items: center;
background-color: #1976d2;
color: white;
font-weight: 600;
}
.toolbar a {
/*float:right;*/
margin-left: auto;
}
解决方案
您不能使用display:flex
with ,float
因为它违反了规则 - 默认情况下,浮点数在flex containers
. 您应该在布局中使用浮动或 flex,我几乎不推荐flex
.
我建议进一步阅读:flex guid
要调整您的代码,请将您的样式替换为:
.toolbar {
position: absolute;
top: 100;
left: 0;
right: 0;
height: 60px;
display: flex;
align-items: center;
justify-content: flex-end;
background-color: #1976d2;
color: white;
font-weight: 600;
}
但是,如果您希望您的代码与floats
您一起使用,则必须删除align-items
which 是flex
property 和 remove display:flex
,这将使您的代码正常工作 - 项目将正确浮动。
推荐阅读
- python - 气流:超时后如何将 ExternalTaskSensor 运算符标记为成功
- matplotlib - 如何更改图例标签的 alpha?
- codenameone - 禁用后启用标签滑动(在代号一上)
- javascript - 在 ngFor 循环中使用 Object.keys 时如何设置默认值?
- colors - 使用 Imagemagick 将图像处理为“默认调色板”(例如 16 或 256 色)
- .net - 1.5 小时后 Azure 登录请求超时
- java - 如何使用 google-docs-api 和 Java 在新文档中设置页眉和页脚
- fortran - openmpi 和 gfortran 错误:没有通用“mpi_waitall”的特定子程序
- mysql - 选择用户没有任何其他活动订阅的所有订阅
- android - Android Management API 的委派范围似乎没有被 Companion 应用程序获取