简体   繁体   English

列表项中的Div(ul li)不断破坏样式

[英]Div inside a list item (ul li ) keeps breaking the styling

I'm trying to fit a div inside a list item where the tree structure looks like this: 我正在尝试将div放入树形结构如下所示的列表项中:

<ul>
<li>
<div class="myclass">
<a href="#">
<img />
</a>
</div>
<a href="#">
<img /> </a>
</li>
</ul>

this list becomes several lists inside eachother goes several levels down, so you have ul's inside ul's. 这个列表变成了彼此内部的几个列表,每个层次都下降了几层,所以您在ul里面有ul。 The problem is that the contents of the myclass div's link styling is not being picked up by css, but rather the "li a" styling. 问题是myclass div链接样式的内容不是由CSS而是由“ li a”样式获取的。 How do i force it to not lump the two links and images together? 我如何强制它不要将两个链接和图像放在一起? Putting them in a class seems not be working. 将它们放在课堂上似乎没有用。

Here's the css: 这是CSS:

.myclass > a#mylink {
    width: 20px;
    height: 20px;
    display: block;
    float: right;

}

.myclass > img{
    width: 10px;
    height: 10px;
    display: block;
    float: right;
}

Keeps getting overridden by: 不断被以下对象覆盖:

ul > li ul li a {
height: 20px;
display: block;
line-height: 20px;
vertical-align: middle;
float: left;
color: #444;
font-size: 1.1em;
}

ul > li ul li img {
width: 20px;
height: 20px;
display: block;
float: left;
padding-right: 10px;
}

To clarify the question is basically, how do i keep styling of div's content to work, and not pick up the styling of the li's its inside of. 要澄清的问题基本上是,我如何保持div内容的样式有效,而不使用li内部的样式。

It may not be the only source of issues you're having (I'm not sure of the final effect you're after), but both of your CSS rules are incorrect. 它可能不是您遇到问题的唯一来源(我不确定您所追求的最终效果),但是两个CSS规则都不正确。 The first: 首先:

.myclass > a#mylink {
    width: 20px;
    height: 20px;
    display: block;
    float: right;
}

The 'a#mylink' matches an 'a' tag with an id of 'mylink'. 'a#mylink'与ID为'mylink'的'a'标签匹配。 No such tag exists in your document. 您的文档中不存在此类标签。 You may just mean the 'a' immediately under <div class="myclass">: 您可能只是说<div class =“ myclass”>下的'a':

.myclass > a {
    width: 20px;
    height: 20px;
    display: block;
    float: right;
}

The second rule: 第二条规则:

.myclass > img{
    width: 10px;
    height: 10px;
    display: block;
    float: right;
}

The '>' character implies "an img that is immediately under an element with class 'myClass'". '>'字符表示“在类'myClass'的元素正下方的img”。 However, your HTML has an 'A' tag between the two. 但是,您的HTML在两者之间有一个'A'标签。 Try altering this to: 尝试将其更改为:

.myclass > a > img{
    width: 10px;
    height: 10px;
    display: block;
    float: right;
}

[Edit - the below answers the original question, not the edited version. [编辑-以下内容回答了原始问题,而不是编辑后的版本。 I've left it here as I feel it may still add some value to the topic generally] 我将其留在这里,因为我认为它通常仍可以为该主题增加一些价值]

Your CSS can be more specific than just 'li a' if required. 如果需要,您的CSS可以比“ li a”更具体。 For example, you could have the following structure: 例如,您可能具有以下结构:

<ul class="top">
    <li>
        <a> ... </a>
        <ul>
            <li>
                <a> ... </a>

The following CSS will, as you note, be applied to all 'a' tags: 如您所注意到的,以下CSS将应用于所有'a'标签:

li a { }

However, this one will only apply to those immediately under the top UL: 但是,这一条款仅适用于紧随UL之下的企业:

ul.top > li > a {  }

I'd recommend reading up on the selection patterns available within CSS; 我建议阅读CSS中可用的选择模式; there's a wide variety out there that a single StackOverflow answer can only touch upon. 有各种各样的内容,一个StackOverflow答案只能涉及。

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

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