简体   繁体   English

使用CSS将图标放置在标签的右侧

[英]Place an icon at the right of a label with CSS

 #accordion input { display: none; } #accordion label { background: #88C2E6; color: white; cursor: pointer; display: block; margin-bottom: .125em; padding: .25em 1em; z-index: 20; min-height: 40px; line-height: 40px; } #accordion label:hover { opacity: 0.6; } #accordion label:after{ content:url(http://image.noelshack.com/fichiers/2016/48/1480523700-arrow-dl.png); background-position: right; } #accordion input:checked + label { background: #88C2E6; color: white; margin-bottom: 0; } #accordion article { background: white; height:0px; overflow:hidden; z-index:10; } #accordion article p { padding: 1em; } #accordion input:checked article { } #accordion input:checked ~ article { height: auto; margin-bottom: .125em; border-left: solid 1px #88C2E6; border-right: solid 1px #88C2E6; } 
 <div id="accordion"> <div> <input type="checkbox" id="check-1" /> <label for="check-1">Some label</label> <article> <p>Some text</p> </article> </div> </div> 

I have an accordion which consists of an input with a label and an article, when I click the label, the article expands or retracts. 我有一个手风琴,它由带有标签的输入和文章组成,当我单击标签时,文章会展开或缩回。 I'd like to add an icon at the right end of the label. 我想在标签的右端添加一个图标。

I tried using :after, like this : 我尝试使用:after,像这样:

#accordion label:after{
content:url(/img.png);
}

But then, the icon is placed right after the text, like this 但是然后,图标就放置在文本之后,就像这样

Text ICON 文字图示

And I'd like it to be placed at the right, like this 我希望它像这样放在右边

Text----------------------------------------------------------------------ICON 文本 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -图标

When I use the property background-position, it remains at the same place. 当我使用属性背景位置时,它会保留在同一位置。

How should I do this ? 我该怎么办?

One option you could go with is setting #accordion label 's position to relative and the :after pseudo element to absolute . 您可以使用的一种选择是将#accordion label的位置设置为relative,将:after伪元素设置为absolute Then assign it with a right value of 0 (or whatever value you want). 然后为其分配一个right0 (或您想要的任何值)。

 #accordion input { display: none; } #accordion label { background: #88C2E6; color: white; cursor: pointer; display: block; position: relative; margin-bottom: .125em; padding: .25em 1em; z-index: 20; min-height: 40px; line-height: 40px; } #accordion label:hover { opacity: 0.6; } #accordion label:after{ content:url(http://image.noelshack.com/fichiers/2016/48/1480523700-arrow-dl.png); position: absolute; right: 0; } #accordion input:checked + label { background: #88C2E6; color: white; margin-bottom: 0; } #accordion article { background: white; height:0px; overflow:hidden; z-index:10; } #accordion article p { padding: 1em; } #accordion input:checked article { } #accordion input:checked ~ article { height: auto; margin-bottom: .125em; border-left: solid 1px #88C2E6; border-right: solid 1px #88C2E6; } 
 <div id="accordion"> <div> <input type="checkbox" id="check-1" /> <label for="check-1">Some label</label> <article> <p>Some text</p> </article> </div> </div> 

You have to use position: absolute; 您必须使用position: absolute; on your icon : 在您的图标上:

 #accordion input { display: none; } #accordion label { background: #88C2E6; color: white; cursor: pointer; display: block; margin-bottom: .125em; padding: .25em 1em; z-index: 20; min-height: 40px; line-height: 40px; position: relative; } #accordion label:hover { opacity: 0.6; } #accordion label:after{ content:url(http://image.noelshack.com/fichiers/2016/48/1480523700-arrow-dl.png); position: absolute; right: 15px; } #accordion input:checked + label { background: #88C2E6; color: white; margin-bottom: 0; } #accordion article { background: white; height:0px; overflow:hidden; z-index:10; } #accordion article p { padding: 1em; } #accordion input:checked article { } #accordion input:checked ~ article { height: auto; margin-bottom: .125em; border-left: solid 1px #88C2E6; border-right: solid 1px #88C2E6; } 
 <div id="accordion"> <div> <input type="checkbox" id="check-1" /> <label for="check-1">Some label</label> <article> <p>Some text</p> </article> </div> </div> 

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

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