[英]How can I align text to the right side of a div?
我基本上是想把綠色圓圈里的東西移到紫色的盒子里。
.counter { margin-left: 20%; margin-right: 20%; width: 382px; }.counter-right { margin-left: 60%; text-align: right; }
<div class="counter"> <img class="counter.img" src="https://via.placeholder.com/100" alt="Counter"> <div class="counter-right"> <h1>Counter</h1> </div> </div>
大利潤並不是一個很好的布局方式。 您應該使用內聯塊顯示或flexbox 。
.counter { margin-left: 20%; margin-right: 20%; display: flex; /* defaults to "row" (horizontal orientation) */ background: #ddd; }.counter-left { flex: 1; /* stretch to use all available space */ }.counter-right { flex: none; /* use only the space needed by contents */ background: pink; }
<div class="counter"> <div class="counter-left"> <img class="counter.img" src="https://via.placeholder.com/100" alt="Counter"> </div> <div class="counter-right"> <h1>Counter</h1> </div> </div>
如果您對display: flex
不滿意,這是另一個簡單的解決方案:
根據需要使用margin
和padding
作為間距。
.counter { width: 100%; }.counter-left { float: left; }.counter-right { float: left; /* border: 1px solid red; */ line-height: 10px; padding-left: 10px; }
<body> <div class="counter"> <div class="counter-left"> <img class="counter.img" src="https://via.placeholder.com/300" alt="Counter"> </div> <div class="counter-right"> <h1>Counter</h1> </div> </div> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.