【问题标题】::nth-child doesn't work:nth-child 不起作用
【发布时间】:2017-02-01 23:26:28
【问题描述】:

这是一个用于 ajax 加载帖子的 wordpress 插件的 html

<div class="row newsrow">
  <div>
    <div id="ajax-load-more" class="ajax-load-more-wrap alm-0" data-alm-id="0" data-canonical-url="http://bayron.nl/" data-slug="home">
      <ul class="alm-listing alm-ajax  " data-repeater="default" data-post-type="post" data-post-format="" data-category="" data-category-not-in="" data-tag="" data-tag-not-in="" data-taxonomy="" data-taxonomy-terms="" data-taxonomy-operator="" data-taxonomy-relation=""
        data-meta-key="" data-meta-value="" data-meta-compare="" data-meta-relation="" data-meta-type="" data-year="" data-month="" data-day="" data-author="" data-post-in="" data-post-not-in="" data-exclude="" data-search="" data-custom-args="" data-post-status=""
        data-order="DESC" data-orderby="date" data-offset="0" data-posts-per-page="3" data-lang="" data-scroll="false" data-scroll-distance="150" data-max-pages="0" data-pause-override="false" data-pause="false" data-button-label="Older Posts" data-button-class=""
        data-destroy-after="" data-transition="fade" data-images-loaded="true">
        <div class="alm-reveal">
          <li>
            <div class="list-content">
              <img width="360" height="240" src="http://bayron.nl/wp-content/uploads/2017/01/helsinki.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://bayron.nl/wp-content/uploads/2017/01/helsinki.jpg 360w, http://bayron.nl/wp-content/uploads/2017/01/helsinki-300x200.jpg 300w"
                sizes="(max-width: 360px) 100vw, 360px">
              <h3><a href="http://bayron.nl/2017/01/27/future-digital-finance-forum/" title="FUTURE DIGITAL FINANCE FORUM">FUTURE DIGITAL FINANCE FORUM</a></h3>
              <p>April 2017 | Helsinki | Keynote Speaker</p>
              <p>Future Digital Finance Forum gathers the pioneers and decision makers to discuss the opportunities and challenges caused by digitalization and disruption in the industry.</p>
            </div>
          </li>
          <li>
            <div class="list-content">
              <img width="384" height="240" src="http://bayron.nl/wp-content/uploads/2017/01/wroclaw.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://bayron.nl/wp-content/uploads/2017/01/wroclaw.jpg 384w, http://bayron.nl/wp-content/uploads/2017/01/wroclaw-300x188.jpg 300w"
                sizes="(max-width: 384px) 100vw, 384px">
              <h3><a href="http://bayron.nl/2017/01/27/impactcee-2016-fintechinsurtech/" title="IMPACTCEE 2016  FINTECH/INSURTECH">IMPACTCEE 2016  FINTECH/INSURTECH</a></h3>
              <p>December 2016 | Wroclaw | Keynote Speaker</p>
              <p>This is the most content driven fintech congress in the Central Eastern Europe gathering the top industry innovators.</p>
            </div>
          </li>
          <li>
            <div class="list-content">
              <img width="360" height="191" src="http://bayron.nl/wp-content/uploads/2017/01/classof17.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://bayron.nl/wp-content/uploads/2017/01/classof17.jpg 360w, http://bayron.nl/wp-content/uploads/2017/01/classof17-300x159.jpg 300w"
                sizes="(max-width: 360px) 100vw, 360px">
              <h3><a href="http://bayron.nl/2017/01/27/meet-the-11-startup-teams-from-our-fintech-cybersecurity-2017-class/" title="MEET THE 11 STARTUP TEAMS FROM OUR FINTECH &amp; CYBERSECURITY 2017 CLASS!">MEET THE 11 STARTUP TEAMS FROM OUR FINTECH &amp; CYBERSECURITY 2017 CLASS!</a></h3>
              <p>November 2016 | Startup Bootcamp</p>
              <p>On the 22-23 of November, Selection Days for our FinTech &amp; CyberSecurity program took place. The event was very special: not only it was selecting the first cohort for our recently announced FinTech &amp; CyberSecurity program but also
                celebrating the 100th startup selected for the programs in Amsterdam.<br> The event truly was a celebration of entrepreneurship, empowerment, ecosystem and innovation. <a href="https://youtu.be/uPsCgRjL6bc" target="_blank">Watch the aftermovie to feel the energy.</a></p>
              <p>&nbsp;</p>
            </div>
          </li>
        </div>
      </ul>
      <div class="alm-btn-wrap"><button id="load-more" class="alm-load-more-btn more">Older Posts</button></div>
    </div>
  </div>
</div>

很多,但我想展示它,因为我不知道问题出在哪里。我将尝试解释我使用的类在 HTML 中的位置

这些是我在项目中使用的唯一 2 个 nth-child ,它们不起作用。

.alm-listing > li:nth-child(4) {
    padding: 0 10px;
    margin-top: 5%
}

.alm-listing > li:nth-child(3) {
    position: absolute;
    top: 20%;
    left: 0;
    right: 0;
    color: #fff
}

.alm-listing 类从第 4 个 HTML 元素开始;无序列表 那么 css 应该选择该 UL 中的列表元素。在这些列表元素中,第 n 个孩子应该选择第 3 和第 4 个元素(两个不同的段落)。不用担心带有 list-contents 类的 div,我已经尝试将其取出,但 css 仍然无法正常工作。

如果有人可以帮助我,我将不胜感激!

【问题讨论】:

  • > 表示 li 不是的直接子元素,中间有一个 div .alm-reveal。你试过删除那个div吗?或者将 CSS 更改为 .alm-listing > div > li:nth-child() ?
  • 是的,我试过了。我什至尝试在 css 中将 .alm-listing 更改为 .alm-reveal 但这也不起作用

标签: html css css-selectors


【解决方案1】:

我认为你的问题是你试图直接在 &lt;ul&gt; 内设置 &lt;li&gt; 的样式,就像 alm-listing &gt; li 所做的那样,但是你在另一个 div 内有 &lt;li&gt; 标签。

试试这个

.alm-listing > div > li:nth-child(4) {
    padding: 0 10px;
    margin-top: 5%
}

.alm-listing > div > li:nth-child(3) {
    position: absolute;
    top: 20%;
    left: 0;
    right: 0;
    color: #fff
}

【讨论】:

  • 感谢您的快速回复。我已经尝试过你的建议,但它不起作用。如果您检查 www.bayron.nl,您可以看到列表元素位于页面顶部,因为位置:绝对。检查标题中列表项的 css,它在自身上使用 :nth-child(3) 代码.....WUT?!
  • 我觉得这个网站不错
【解决方案2】:

是的,我想我找到了问题所在。这不是 CSS。我精简了我的 CSS,以便我可以更轻松地安排一些媒体查询。在 unminify 之后是问题开始的时候。我从 2 天前找到了我的 CSS(缩小)的备份。当我将缩小版本上传到我的网站时,nth-childs 会像他们应该的那样工作。我取消了缩小的备份并将其上传到我的网站,瞧,我的代码停止工作......我不敢相信这甚至是一件事。对于想知道我曾经取消缩小哪些网站的人,他们在这里; unminify.com 和 codepen.com 的编辑器(整洁的 CSS)。

好吧,我想我必须重新开始;/

【讨论】:

  • 开始使用源代码控制的好时机,比如 git。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
  • 2017-01-22
  • 2011-07-06
  • 2021-12-10
  • 2014-06-07
  • 2013-05-28
  • 2023-03-09
相关资源
最近更新 更多