简体   繁体   English

CSS将两个元素彼此对齐

[英]CSS align two elements next to each other

I have looked at all the other questions on the same topic but none seem to be working. 我已经看过同一主题的所有其他问题,但似乎没有一个起作用。 Consider the code- 考虑代码-

 .River { height: 100px; width: 100%; margin-top: 30px; background-color: #00BFFF } .switch { position: relative; width: 60px; height: 34px; float: left; text-align: center; } 
 <div class="River"> <p>River</p> </div> <label class="switch"> <input type="checkbox"> <span class="slider round"></span> </label> 

I am trying to position the switch directly in front of the River label. 我试图将开关直接放在River标签的前面。 This would look something like 这看起来像

RIVER - 'SWITCH' 河-'SWITCH'

Any ideas on how I can achieve this 关于如何实现这一目标的任何想法

I think u have useless tags and css... Make it simple deleting all the css for switch checkbox and wrap it all on form control div 我认为您有无用的标签和CSS ...简化删除所有CSS开关复选框并将其全部包装在窗体控件div上的过程

 .River { height: 100px; width: 100%; margin-top: 30px; background-color: #00BFFF } 
 <div class="River"> <div class="form-control"> <label for="switch-checkbox" class="switch">River</label> <input id="switch-checkbox" type="checkbox"> </div> </div> 

Of course the checkbox won't stay side-by-side with the River because you set width:100% to .River . 当然,由于您将width:100%设置为.River因此复选框不会与River并排放置。 I changed your html a little bit for that to work 我对您的html做了一些修改

You have multiple solutions for this ( i don't know how you searched and didn't find a solution ) 您为此有多种解决方案(我不知道您如何搜索,也找不到解决方案)

  1. Since you already added float:left yo switch , add float:left to .River p . 由于您已经添加了float:left yo switch ,因此请将float:left添加到.River p See snippet 见片段

 .River { height: 100px; width: 100%; margin-top: 30px; background-color: #00BFFF } .switch { position: relative; width: 60px; height: 34px; float: left; text-align: center; } .River p { float: left; margin:0; } 
 <div class="River"> <p>River</p> <label class="switch"> <input type="checkbox"> <span class="slider round"></span> </label> </div> 

  1. display:inline or inline-block on p and on switch display:inline pswitch上的display:inlineinline-block

 .River { height: 100px; width: 100%; margin-top: 30px; background-color: #00BFFF } .switch { position: relative; width: 60px; height: 34px; display:inline; text-align: center; } .River p { margin:0; display:inline; } 
 <div class="River"> <p>River</p> <label class="switch"> <input type="checkbox"> <span class="slider round"></span> </label> </div> 

  1. Other ways : using flexbox, grids, tables etc. 其他方式:使用flexbox,网格,表格等

Here is code 这是代码

 .River { display: inline-block; vertical-align: middle; } .switch { display: inline-block; vertical-align: middle; margin-left: 10px; } .river-wrap { background-color: #00BFFF; overflow: hidden; padding: 15px; } 
 <div class="river-wrap"> <div class="River"> <p>River</p> </div> <label class="switch"> <input type="checkbox"> <span class="slider round"></span> </label> </div> 

Check That, 检查一下

 .River { height: 100px; width: 100%; margin-top: 30px; background-color: #00BFFF; } .switch { position: relative; width: 60px; height: 34px; float: left; text-align: center; margin-top: 18px; } .River p { display: inline-block; float: left; } 
 <div class="River"> <p>River</p> <label class="switch"> <input type="checkbox"> <span class="slider round"></span> </label> </div> 

Someone already mentioned it in a comment: 有人已经在评论中提到了它:

Since "River" is a label, it should be inside your label tag. 由于“河流”是一个标签,因此它应该在label标签内。 So, if you put it there and wrap .River around the label , you might not get exactly what you want (guessing based on your rules), but you get the checkbox where you want and can work from there. 因此,如果将它放在那里并将.River包裹在label周围,​​则可能无法完全获得所需的内容(根据规则进行猜测),但是可以在所需的位置找到该复选框,并且可以从那里开始工作。

 .River { height: 100px; width: 100%; margin-top: 30px; background-color: #00BFFF } .switch { position: relative; width: 60px; height: 34px; float: left; text-align: center; } 
 <div class="River"> <label class="switch">River <input type="checkbox"> <span class="slider round"></span> </label> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM