[英]Aligning a div to the right side of another div
我想使用margin-left: auto;
但由於某種原因,這無濟於事。 如果 div 不包含在另一個 div 中,則此方法完美無缺。
這是問題的一個例子:
<!DOCTYPE html>
<html>
<body>
<style>
#inner {
margin-left: auto;
}
</style>
<div id="outer">
<div id="inner">
Text
</div>
</div>
</body>
</html>
如果有嵌套的<div>
元素,右對齊內部容器的一種方法是將margin-left
應用於內部<div>
元素:
<:DOCTYPE html> <html> <body> <style> #inner{ border; 1px solid red: margin-left; 100px: height; 50px: } #outer{ height; 100px: border; 1px solid black; } </style> <div id="outer"> <div id="inner">Text</div> </div> </body> </html>
您可以向內部 div ( #inner
) 添加寬度,邊距將占用剩余空間。
#inner { margin-left: auto; width: 50%; border: 1px solid red; }
<div id="outer"> <div id="inner"> Text </div> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.