Can I select only when there's no text around it?

前端 未结 4 995
执笔经年
执笔经年 2021-01-21 16:05

I would like to select anchor tags only when they\'re completely by themselves, that way I can make those look like buttons, without causing anchors within sentences to look lik

4条回答
  •  天命终不由人
    2021-01-21 16:18

    I don't think it's generally possible, but you can come close. Here are some helpful places to start:

    1. The Only Child Selector which would allow you to select all a elements which have no siblings like so a:only-child {/* css */}. See more here. (Also see edit)

    2. The Not Selector which would allow you to exclude some elements perhaps using something along the lines of :not(p) > a {/* css */} which should select all anchors not in a paragraph. See some helpful information here.

    3. Combining selectors to be as specific as possible. You might want all anchors not in an h1 and all anchors not in a p.

    Example:

    The final product might look like this:

    a:only-child, :not(p) > a {/* css */}
    

    This should select all anchors that are only children and anchors that are not in a paragraph.

    Final note:

    You may want to consider making the buttons actual button or input tags to make your life easier. Getting the HTML right first usually makes the CSS simpler.

    Edit: the only child ignores the text, so that's pretty much useless here. I guess it's less doable than I thought.

提交回复
热议问题