html - 如何将元素对齐到 div 框的右侧?
问题描述
如何将元素对齐到 div 框的右侧?
我的 div
<div id="foo">
<div id="tree">Some Text here</div>
</div>
我的 CSS
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
}
我需要将树放在foo的右上角。
解决方案
有几种方法可以做到这一点。一种方法是向树添加自动左边距:
margin-left: auto;
另一种选择是应用于float: right;
树,这可能会或可能不会导致您需要的内容流。
最后,老实说,我的建议是只使用 flexbox。
保证金示例
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
margin-left: auto;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
浮动示例
#foo {
display: block;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
width: 100px;
height: 30px;
background: #000000;
float: right;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
弹性示例
#foo {
display: flex;
justify-content: flex-end;
width: 500px;
height: 500px;
background: #5e5e5e;
}
#tree {
display: flex;
width: 100px;
height: 30px;
background: #000000;
}
<div id="foo">
<div id="tree">Some Text here</div>
</div>
推荐阅读
- java - SceneBuilder中的JavaFX组件位置问题
- python - Python Pandas Groupby -- by = list 给我一个错误?
- c# - C# 8 中的范围和索引类型是什么?
- excel - 根据标准生成随机名称列表
- sql - 如何在没有聚合的情况下分组?
- java - 在Java中将两个“整数”值与“==”进行比较是否安全?
- django - ModuleNotFoundError:使用 apache 进行 django 部署时没有名为“new”的模块错误
- django - 如何路由我的 API 调用以收集具有连接到特定模型实例的 ForeignKey 的所有对象?
- postgresql - 仅针对某些客户端的 Postgres 连接失败。消息:`GSSAPI 身份验证不支持`
- java - 在数组中实例化一个对象,该对象是另一个对象的属性