Add target=“_blank” in CSS

前端 未结 6 1870
闹比i
闹比i 2020-11-28 07:57

I have external links in top menu of my website. I want to open these links in new tab. I could achieve it using \"target=_blank\" in HTML. Is there a similar CSS p

6条回答
  •  执笔经年
    2020-11-28 08:20

    There are a few ways CSS can 'target' navigation. This will style internal and external links using attribute styling, which could help signal visitors to what your links will do.

    a[href="#"] { color: forestgreen; font-weight: normal; }
    a[href="http"] { color: dodgerblue; font-weight: normal; }  
    

    You can also target the traditional inline HTML 'target=_blank'.

    a[target=_blank] { font-weight: bold; } 
    

    Also :target selector to style navigation block and element targets.

    nav { display: none; }
    nav:target { display: block; }  
    

    CSS :target pseudo-class selector is supported - caniuse, w3schools, MDN.

    a[href="http"] { target: new; target-name: new; target-new: tab; }
    

    CSS/CSS3 'target-new' property etc, not supported by any major browsers, 2017 August, though it is part of the W3 spec since 2004 February.

    W3Schools 'modal' construction, uses ':target' pseudo-class that could contain WP navigation. You can also add HTML rel="noreferrer and noopener beside target="_blank" to improve 'new tab' performance. CSS will not open links in tabs for now, but this page explains how to do that with jQuery (compatibility may depend for WP coders). MDN has a good review at Using the :target pseudo-class in selectors

提交回复
热议问题