当前位置:主页 > 查看内容

css下div下同行多元素右对齐

发布时间:2021-04-26 00:00| 位朋友查看

简介:方法一: float:right 此外,浮动还会使得布局更紧凑(没空隙) div style=background-color: red;width: 100%;height: 60px;/* text-align: right; */div style=width: 30px;height: 100%;background-color: yellow;float: right;hello/divdiv style=width:……

方法一:

float:right
此外,浮动还会使得布局更紧凑(没空隙)

<div style="background-color: red;width: 100%;height: 60px;/* text-align: right; */">
	<div style="width: 30px;height: 100%;background-color: yellow;float: right;">hello</div>
	<div style="width: 60px;height: 100%;background-color: blue;float: right;">hi</div>
</div>

效果图如下:

方法二:

display:inline-block+text-align:right
text-align:right影响的是其下的行内元素或文字,所以inline-block使得div具有了行内元素的特性,可以右对齐

<div style="background-color: red;width: 100%;height: 60px;text-align: right;">
	<div style="width: 30px;height: 100%;background-color: yellow;display: inline-block;">hello</div>
	<div style="width: 60px;height: 100%;background-color: blue;display: inline-block;">hi</div>
</div>

效果图如下:

从上面两个方法来看:

float的布局更为紧凑一些;
float:right会改变顺序,而text-align:right不会;
text-align同时会影响其下元素的文字对齐方式;

所以,综合两个方法,可以得到如下组合:

<div style="background-color: red;width: 100%;height: 60px;text-align: right;">
	<div style="width: 30px;height: 100%;background-color: yellow;display: inline-block;">hello</div>
	<div style="width: 60px;height: 100%;background-color: blue;float: right;">hi</div>
</div>

效果如下:

进一步发现,综合之后:
布局更为紧凑;
float和inline-block同行时,并不会产生覆盖浮动的现象,只是会各自规矩地排列着。

 到此这篇关于css下div下同行多元素右对齐的文章就介绍到这了,更多相关div同行多元素右对齐内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!


原文链接:https://m.jb51.net/css/743775.html
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐