[英]why does <li> element under <div> renders different from <li> element under <ul>?
i want to temporarily place a li element under a div but i have noticed that the element renders differently even when the div parent is styled exactly as the original ul parent. 我想将li元素临时放置在div下,但我注意到,即使div父项的样式与原始ul父项的样式完全一样,该元素的呈现方式也不同。 any reason for this behaviour?
这种行为有什么原因吗? any way to emulate the ul tag with a div tag?
有什么办法用div标签模拟ul标签?
function copy(){ var ul = document.getElementById ('real-ul'); var div = document.getElementById('emulate-ul'); var cStyle = (window.getComputedStyle) ? window.getComputedStyle(ul, null) : ul.currentStyle; for(var property in cStyle){ div.style[property] = cStyle[property]; } }
<div> <div id="emulate-ul"> <li>ITEM li under a div</li> </div> <ul id="real-ul"> <li>ITEM li under a ul</li> </ul> </div> <button onclick="copy()"> copy all computed styles </button>
The ul
element has a few default css props in most browsers, I think this is an accuarate list: ul
元素在大多数浏览器中都有一些默认的CSS道具,我认为这是一个准确的列表:
ul {
display: block;
list-style-type: disc;
margin-top: 1em;
margin-bottom: 1em;
margin-left: 0;
margin-right: 0;
padding-left: 40px;
}
You can see that the margin
before and after and the padding-start
are causing the difference here. 您可以看到前后的
margin
和padding-start
是造成差异的原因。 I suppose you could add them to your bounding div
to recreate the styling of a ul
. 我想您可以将它们添加到边界
div
以重新创建ul
的样式。
You don't put <li>
elements inside <div>
without <ul>
This is how you could put it: 在没有
<ul>
情况下,请勿将<li>
元素放入<div>
,这是您可以采用的方式:
<div>
<div id="emulate-ul">
<ul>
<li>ITEM li under a div</li>
</ul>
</div>
<div id="some-id">
<ul id="real-ul">
<li>ITEM li under a ul</li>
</ul>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.