How to apply a style to a single special HTML character across the page

让人想犯罪 __ 提交于 2019-11-29 08:46:27
Dave Haigh

I dont believe it can be done with CSS alone.

I would use jQuery to find and replace the &reg; content with <sup>&reg;</sup>

There is a similar question below with a correct answer on how to do this (saves me the trouble):

Altering registered symbols (R)

Unfortunately, I don't think you can just select certain characters with only CSS.

However...

  1. There is a CSS rule for superscript, and it is:

    vertical-align: super;
    
  2. This jQuery works:

    $(document).ready(function() {
       $('*').each(function() {
        var n = $(this).html().replace(
            new RegExp('®','g'),
            '<sup>®</sup>'
            ).replace(
            new RegExp('&reg;','g'),
            '<sup>&reg;</sup>'
            );
        $(this).html(n);
      });
    });​
    

I hope this helps!

  • I don't think you can do this will css alone
  • Using jquery/javascript/css you could create a class containing the style information for the character then have jquery parse the file section and wrap the character inside <span></span> tags with that css class applied
  • I know nothing about silverstripe so I cant comment on its usage

Strictly speaking you cannot create "super-script" with any single CSS property (there is veritcal-align "super", but it doesn't change the size or style of the characters, only it's position relative to the text baseline). However, there are ways to accomplish this successfully that I have tried before.

First of all, as the two other posters have mentioned, you'll need to target the symbol somehow. If you can wrap all &reg; characters with markup, then the rest can be easily done with CSS. For example:

When you have <sup>&reg;</sup> you can simply add these styles to your stylesheet to get a desired effect:

<div>
<p>This is some crazy text brought to you by my Company<sup>&reg;</sup><p>
<div>​

div{
 background:#000;
 width:80%;
 height:80%;
 padding:10% 10%;    
}
p{
 background:#4d4d4d;
 padding:10% 20%;    
 color:#fff;
 font-family:Arial-black,Arial,sans-serif;
 font-size:2em;
}
sup{
 position:relative;
 font-size:.75em;
}

​ Here's an example: http://jsfiddle.net/jglovier/vqHuu/

Of course, if you can't access the markup like this, you'll need to use jQuery to look for the characters and wrap them in a <sup> tag as other's have suggested.

In the PHP code, you can put this into your Page class to do what you want on the server side:

function Content() {
  return str_replace('®', '<sup class="copyright">®</sup>', $this->Content);
}

$Content in the template engine will first look for $page->Content() (the method) and if that doesn't exist will look for $page->Content. By contrast, the CMS editor only looks for $page->Content. This means that by defining a Content() method that processes the output, you can include code that is only executed on template display and doesn't muck with the CMS.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!