简体   繁体   English

如何在此 li 和 span 中对齐图像和文本

[英]How do I align the image and the text in this li and span

I'm trying to align an image and text in an li and span.我正在尝试在 li 和 span 中对齐图像和文本。 I've tried a lot of questions and answers similar to this, but no success.我已经尝试了很多与此类似的问题和答案,但都没有成功。 Using a label or table is not an option unfortunately.不幸的是,使用标签或表格不是一种选择。

Thanks谢谢

在此处输入图片说明

 .checkbox, .radio { width: 19px; height: 20px; padding: 0px; background: url(../../Content/Images/TriStateCheckboxes.jpg); display: block; clear: left; float: left; } .green { background-color: green; background: url(../../Content/Images/PermissionChecked.jpg); position: absolute; top: 11px; left: -20px; } .red { background-color: red; position: absolute; top: 11px; left: -20px; } li { cursor: pointer; }
 <ul class="nav nav-list" id="MM0" style="border: currentColor; border-image: none;"> <li> <span class="accordion-heading" aria-expanded="true" data-toggle="collapse" data-target="#MMID_0"> <span class="checkbox MMTree-checkbox styled red" id="sp_MM_0"> <input name="MM_0" class="MMTree-checkbox styled red" id="MM_0" style="display: none;" type="checkbox" value="false" iid="FkComponentId= ComponentId=2 Granted=-1 PermissionId=0|"> </span> <span> LookupTables </span> <span class="caret"></span> </span> <ul class="nav nav-list collapse show" id="MMID_0" style="padding-left: 60px;"> <li> <span class="accordion-heading" data-toggle="collapse" data-target="#MMID_00"> <span class="checkbox subtree-checkbox styled red" id="sp_SM_0_0"> <input name="SM_0_0" class="subtree-checkbox styled red" id="SM_0_0" style="display: none;" type="checkbox" value="false"> </span> <span> Countries </span> </span> </li> </ul>

Use display: flex;使用display: flex; in your container, and then align-items: center to center them.在您的容器中,然后align-items: center将它们居中。 Also, to remove the order image use list-style-type: none;此外,要删除订单图像,请使用list-style-type: none;

 .checkbox, .radio { width: 19px; height: 20px; padding: 0px; background: url(../../Content/Images/TriStateCheckboxes.jpg); display: block; clear: left; float: left; } .red { background-color: red; } ul { list-style-type: none; } li { cursor: pointer; } .accordion-heading { display: flex; align-items: center; }
 <ul class="nav nav-list" id="MM00"> <li> <span class="accordion-heading" aria-expanded="false" data-toggle="collapse" data-target="#MMID_0 "> <span class="checkbox MMTree-checkbox styled red" id="sp_MM_0 "> <input name="MM_0" class="MMTree-checkbox styled red" id="MM_0" style="display: none; " type="checkbox" value="false" > </span> <span> LookupTables </span> <span class="caret"></span> </span> </li> </ul>

Use position:absolute to .red and position with top/left as you want...使用position:absolute.red并根据需要在top/left定位...

Also use position:relative to wrap DOM in your case .accordion-heading还可以使用position:relative将 DOM 包装在您的案例中.accordion-heading

 .checkbox, .radio { width: 19px; height: 20px; padding: 0px; background: url(../../Content/Images/TriStateCheckboxes.jpg); display: block; clear: left; float: left; } .accordion-heading { position: relative; } .red { background-color: red; position: absolute; left: -28px; } li { cursor: pointer; list-style-type: none; }
 <ul class="nav nav-list" id="MM00" style="b order-image; none;"> <li> <span class="accordion-heading" aria-expanded="false " data-toggle="collapse " data-target="#MMID_0 "> <span class="checkbox MMTree-checkbox styled red " id="sp_MM_0 "> <input name="MM_0 " class="MMTree-checkbox styled red " id="MM_0 " style="display: none; " type="checkbox " value="false " ></span> <span> LookupTables </span> <span class="caret "></span> </span> </li> </ul>

If you don't like to use flex , you can use combo absolute + transform: translate :D.如果你不喜欢使用flex ,你可以使用组合absolute + transform: translate :D。 So in my code,所以在我的代码中,

position: absolute; top: 50%;

mean 50% of 1st parent with position: reative ( li>span )平均 50% 的第一个父级的position: reative ( li>span )

transform: translateY(-50%);

mean 50% of itself意味着自身的 50%

 .checkbox, .radio { width: 19px; height: 20px; padding: 0px; background: url(../../Content/Images/TriStateCheckboxes.jpg); display: block; } .red { background-color: red; } li{ list-style: none; cursor: pointer; } li>span{ display: inline-block; position: relative; cursor: pointer; } li>span span{ position: absolute; top: 50%; transform: translateY(-50%); } li>span span:last-child{ left: unset; right: 0px; } li>span span:nth-child(2){ right: unset; left: 20px; }
 <ul class="nav nav-list" id="MM0" style="border: currentColor; border-image: none;"> <li> <span class="accordion-heading" aria-expanded="true" data-toggle="collapse" data-target="#MMID_0"> <span class="checkbox MMTree-checkbox styled red" id="sp_MM_0"> <input name="MM_0" class="MMTree-checkbox styled red" id="MM_0" style="display: none;" type="checkbox" value="false" iid="FkComponentId= ComponentId=2 Granted=-1 PermissionId=0|"> </span> <span> LookupTables </span> <span class="caret"></span> </span> <br/> <ul class="nav nav-list collapse show" id="MMID_0" style="padding-left: 60px;"> <li> <span class="accordion-heading" data-toggle="collapse" data-target="#MMID_00"> <span class="checkbox subtree-checkbox styled red" id="sp_SM_0_0"> <input name="SM_0_0" class="subtree-checkbox styled red" id="SM_0_0" style="display: none;" type="checkbox" value="false"> </span> <span> Countries </span> </span> </li> <li> <span class="accordion-heading" data-toggle="collapse" data-target="#MMID_00"> <span class="checkbox subtree-checkbox styled red" id="sp_SM_0_0"> <input name="SM_0_0" class="subtree-checkbox styled red" id="SM_0_0" style="display: none;" type="checkbox" value="false"> </span> <span> Age </span> </span> </li> </ul>

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

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