Css a:hover:after:nth-child 1,2,3,4,5 content in dropdown navbar not displaying

huangapple go评论84阅读模式
英文:

Css a:hover:after:nth-child 1,2,3,4,5 content in dropdown navbar not displaying

问题

I created a simple navbar and I would love to add some css-nth-child:before to the menu depending on the main category

The dropdown navbar has 5 different categories:

News, Politics, Economy, Health and Education

Each category has its own subcategories with region names like this:

Europe, Asia, Africa, Oceania, North America and South America

What I want is:
when people put their mouse over (hover) lets say category: Politics, and the mouse is over the Politics sub-categories, on hover I want to display like this:

Politics news from Europe
Politics news from Asia
Politics news from Africa
etc

But if the mouse is over (hover) the category Economy and the mouse is over the Economy sub-categories, on hover I want to display like this:

Economy news from Europe
Economy news from Asia
Economy news from Africa
etc

What I tried is the following CSS

sub-categories a:hover:after:nth-child(2)  
{content: 'Politics news from • ';
sub-categories a:hover:after:nth-child(3)  
{content: 'Economy news from • ';
sub-categories a:hover:after:nth-child(4)  
{content: 'Health news from • ';

But it is not working

Here is the HTML

<main-categories><a href="#void"><strong>▪</strong>News</a>
  <sub-categories>
    <a href="#">Sweden</a>
    <a href="#">Europe</a>
    <a href="#">Asia</a>
    <a href="#">Africa</a>
    <a href="#">Oceania</a>
    <a href="#">North America</a>
    <a href="#">South America</a>
  </sub-categories>
</main-categories>

<main-categories><a href="#void"><strong>▪</strong>Politics</a>
  <sub-categories>
    <a href="#">Sweden</a>
    <a href="#">Europe</a>
    <a href="#">Asia</a>
    <a href="#">Africa</a>
    <a href="#">Oceania</a>
    <a href="#">North America</a>
    <a href="#">South America</a>
  </sub-categories>
</main-categories>

<main-categories><a href="#void"><strong>▪</strong>Economy</a>
  <sub-categories>
    <a href="#">Sweden</a>
    <a href="#">Europe</a>
    <a href="#">Asia</a>
    <a href="#">Africa</a>
    <a href="#">Oceania</a>
    <a href="#">North America</a>
    <a href="#">South America</a>
  </sub-categories>
</main-categories>

<main-categories><a href="#void"><strong>▪</strong>Health</a>
  <sub-categories>
    <a href="#">Sweden</a>
    <a href="#">Europe</a>
    <a href="#">Asia</a>
    <a href="#">Africa</a>
    <a href="#">Oceania</a>
    <a href="#">North America</a>
    <a href="#">South America</a>
  </sub-categories>
</main-categories>

and the CSS is

main-categories 
{position:relative;display:block;
text-align:left;width:92%;margin:0 auto;overflow:hidden;}
main-categories a 
{position:relative;margin:0 auto;display:block;
font-size:15px;text-transform:uppercase;
text-align:left;padding:12px 0 12px 0;width:100%;
border-bottom:1px dotted #121212;overflow:hidden}
main-categories a:last-child 
{border:0}	
main-categories a:hover 
{text-decoration:none;
color:#FFFFFF;background-color:#121212;padding-left:13px;}
main-categories:last-child 
{border:0}
sub-categories {display:none;position:relative;width:90%;
left:50%;transform:translatex(-50%);
text-align:left;overflow:hidden}
sub-categories a 
{display:block;font-size:15px;color:#121212;
text-transform:capitalize;padding:10px 0 10px 0;text-align:left;
border-bottom:1px dotted #121212}
sub-categories a:hover 
{color:#FFFFFF;background-color:#121212;padding-left:20px;}
sub-categories a:last-child
 {border-bottom:1px dotted #121212}	
main-categories:hover sub-categories 
{display:block}
sub-categories a:before{content: '• ';}
sub-categories a:hover:after:nth-child(1) 
 {content: 'News from • ';
sub-categories a:hover:after:nth-child(2) 
 {content: 'Politics from • ';
sub-categories a:hover:after:nth-child(3) 
 {content: 'Economy from • ';

I also created a Codepen to see it live

https://codepen.io/familias/pen/XWygWzY

英文:

I created a simple navbar and I would love to add some css-nth-child:before to the menu depending on the main category

The dropdown navbar has 5 different categories:

News, Politics, Economy, Health and Education

Each category has its own subcategories with region names like this:

Europe, Asia, Africa, Oceania, North America and South America

What I want is:
when people put their mouse over (hover) lets say category: Politics, and the mouse is over the Politics sub-categories, on hover I want to display like this:

Politics news from Europe
Politics news from Asia
Politics news from Africa
etc

But if the mouse is over (hover) the category Economy and the mouse is over the Economy sub-categories, on hover I want to display like this:

Economy news from Europe
Economy news from Asia
Economy news from Africa
etc

What I tried is the following CSS

    sub-categories a:hover:after:nth-child(2)  
{content: &#39;Politics news from • &#39;;
sub-categories a:hover:after:nth-child(3)  
{content: &#39;Economy news from • &#39;;
sub-categories a:hover:after:nth-child(4)  
{content: &#39;Health news from • &#39;;

But it is not working

Here is the HTML

&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;&lt;strong&gt;▪&lt;/strong&gt;News&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Sweden&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;
&lt;/main-categories&gt;
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;&lt;strong&gt;▪&lt;/strong&gt;Politics&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Sweden&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;
&lt;/main-categories&gt;
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;&lt;strong&gt;▪&lt;/strong&gt;Economy&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Sweden&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;
&lt;/main-categories&gt;
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;&lt;strong&gt;▪&lt;/strong&gt;Health&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Sweden&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;
&lt;/main-categories&gt;

and the CSS is

main-categories 
{position:relative;display:block;
text-align:left;width:92%;margin:0 auto;overflow:hidden;}
main-categories a 
{position:relative;margin:0 auto;display:block;
font-size:15px;text-transform:uppercase;
text-align:left;padding:12px 0 12px 0;width:100%;
border-bottom:1px dotted #121212;overflow:hidden}
main-categories a:last-child 
{border:0}	
main-categories a:hover 
{text-decoration:none;
color:#FFFFFF;background-color:#121212;padding-left:13px;}
main-categories:last-child 
{border:0}
sub-categories {display:none;position:relative;width:90%;
left:50%;transform:translatex(-50%);
text-align:left;overflow:hidden}
sub-categories a 
{display:block;font-size:15px;color:#121212;
text-transform:capitalize;padding:10px 0 10px 0;text-align:left;
border-bottom:1px dotted #121212}
sub-categories a:hover 
{color:#FFFFFF;background-color:#121212;padding-left:20px;}
sub-categories a:last-child
{border-bottom:1px dotted #121212}	
main-categories:hover sub-categories 
{display:block}
sub-categories a:before{content: &#39;• &#39;;}
sub-categories a:hover:after:nth-child(1) 
{content: &#39;News from • &#39;;
sub-categories a:hover:after:nth-child(1) 
{content: &#39;Politics from • &#39;;
sub-categories a:hover:after:nth-child(1) 
{content: &#39;Economy from • &#39;;

I also created a Codepen to see it live

https://codepen.io/familias/pen/XWygWzY

答案1

得分: 1

你在使用集成开发环境吗?因为你的最后一段CSS代码有问题,缺少了闭合的大括号。在集成开发环境中工作会对你有很大帮助。

你可以通过在 main-categories 上使用 nth-of-type() 操作符来实现这个效果。示例代码如下:

main-categories:nth-of-type(1) sub-categories a:hover:before {
    content: '• News from ';
}

main-categories:nth-of-type(2) sub-categories a:hover:before {
    content: '• Political news from ';
}

以下是你提供的CSS和HTML代码的翻译:

main-categories {
    position: relative;
    display: block;
    text-align: left;
    width: 92%;
    margin: 0 auto;
    overflow: hidden;
}

main-categories a {
    position: relative;
    margin: 0 auto;
    display: block;
    font-size: 15px;
    text-transform: uppercase;
    text-align: left;
    padding: 12px 0 12px 0;
    width: 100%;
    border-bottom: 1px dotted #121212;
    overflow: hidden;
}

main-categories a:last-child {
    border: 0;
}

main-categories a:hover {
    text-decoration: none;
    color: #FFFFFF;
    background-color: #121212;
    padding-left: 13px;
}

main-categories:last-child {
    border: 0;
}

sub-categories {
    display: none;
    position: relative;
    width: 90%;
    left: 50%;
    transform: translateX(-50%);
    text-align: left;
    overflow: hidden;
}

sub-categories a {
    display: block;
    font-size: 15px;
    color: #121212;
    text-transform: capitalize;
    padding: 10px 0 10px 0;
    text-align: left;
    border-bottom: 1px dotted #121212;
}

sub-categories a:hover {
    color: #FFFFFF;
    background-color: #121212;
    padding-left: 20px;
}

sub-categories a:last-child {
    border-bottom: 1px dotted #121212;
}

main-categories:hover sub-categories {
    display: block;
}

sub-categories a:before {
    content: '• ';
}

main-categories:nth-of-type(1) sub-categories a:hover:before {
    content: '• News from ';
}

main-categories:nth-of-type(2) sub-categories a:hover:before {
    content: '• Political news from ';
}

HTML部分不需要翻译。

英文:

Are you working in an IDE? Because your last CSS is broken and missing closing brackets. Working in an IDE would help you out a lot.

You can achieve this effect by using the nth-of-type() operater on main-categories. Example:

main-categories:nth-of-type(1) sub-categories a:hover:before {
content: &#39;• News from &#39;;
}
main-categories:nth-of-type(2) sub-categories a:hover:before {
content: &#39;• Political news from &#39;;
}

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

main-categories {
position:relative;
display:block;
text-align:left;
width:92%;
margin:0 auto;
overflow:hidden;
}
main-categories a {
position:relative;
margin:0 auto;
display:block;
font-size:15px;
text-transform:uppercase;
text-align:left;
padding:12px 0 12px 0;
width:100%;
border-bottom:1px dotted #121212;
overflow:hidden
}
main-categories a:last-child {
border:0
}
main-categories a:hover {
text-decoration:none;
color:#FFFFFF;
background-color:#121212;
padding-left:13px;
}
main-categories:last-child {
border:0
}
sub-categories {
display:none;
position:relative;
width:90%;
left:50%;
transform:translatex(-50%);
text-align:left;
overflow:hidden
}
sub-categories a {
display:block;
font-size:15px;
color:#121212;
text-transform:capitalize;
padding:10px 0 10px 0;
text-align:left;
border-bottom:1px dotted #121212
}
sub-categories a:hover {
color:#FFFFFF;
background-color:#121212;
padding-left:20px;
}
sub-categories a:last-child {
border-bottom:1px dotted #121212
}
main-categories:hover sub-categories {
display:block
}
sub-categories a:before{
content: &#39;• &#39;;
}
main-categories:nth-of-type(1) sub-categories a:hover:before {
content: &#39;• News from &#39;;
}
main-categories:nth-of-type(2) sub-categories a:hover:before {
content: &#39;• Political news from &#39;;
}

<!-- language: lang-html -->

&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;&lt;strong&gt;▪&lt;/strong&gt;News&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Sweden&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;
&lt;/main-categories&gt;
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;&lt;strong&gt;▪&lt;/strong&gt;Politics&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Sweden&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;
&lt;/main-categories&gt;
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;&lt;strong&gt;▪&lt;/strong&gt;Economy&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Sweden&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;
&lt;/main-categories&gt;
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;&lt;strong&gt;▪&lt;/strong&gt;Health&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Sweden&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;
&lt;/main-categories&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 02:39:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定