25

How do you add custom font sizes to the toolbar with QuillJS? I've tried two approaches:

// Initiate the editor
        let toolbarOptions = [
            ['bold', 'italic', 'underline', 'strike'],
            [{ 'align': [] }],
            [{ 'size': ['10px', '20px', '80px'] }],
            [{ 'color': ['#FFF'] }]
        ];
        this.editor = new Quill('#executive-control-editor', {
            modules: {
                toolbar: toolbarOptions
            },
            theme: 'snow'
        });

and

<div id="toolbar">
        <span class="ql-formats">
            <button class="ql-bold"></button>
            <button class="ql-italic"></button>
            <button class="ql-underline"></button>
            <button class="ql-strike"></button>
        </span>
        <span class="ql-formats">
            <select class="ql-align"></select>
        </span>
        <span class="ql-format-group">
          <select title="Size" class="ql-size">
            <option value="10px">Small</option>
            <option value="13px">Normal</option>
            <option value="18px">Large</option>
            <option value="32px">Huge</option>
          </select>
        </span>
        <span class="ql-formats">
            <button class="ql-image"></button>
        </span>
    </div>

However neither of them work. Is there something I'm missing here? I've tried removing the "px" from the value as well; still nothing.

Brandon
  • 3,074
  • 3
  • 27
  • 44
  • See [this issue](http://stackoverflow.com/questions/33018395/quill-js-font-size-issue). – Jack Guy Jul 27 '16 at 22:55
  • @Harangue He had an issue of not being able to surpass 18px. I can't even get any custom size to work. Also, that was pre-Beta1.0. They've released 1.0 now. – Brandon Jul 28 '16 at 14:24
  • 1
    Remember to take a look [here](https://github.com/loagit/Quill-Examples-and-FAQ) too. – Loa Dec 30 '19 at 16:12

3 Answers3

53

The accepted answer above didn't work for me but put me on the right track.

Here is what I had to do to have the text editor set custom font sizes (and also set inline styles for the font-size in the underlying value instead of a CSS class):

var Size = Quill.import('attributors/style/size');
Size.whitelist = ['14px', '16px', '18px'];
Quill.register(Size, true);

var toolbarOptions = [
    [{ 'size': ['14px', '16px', '18px'] }],
];

var quill = new Quill("#quillElementSelector", {
    theme: 'snow',
    modules: {
        toolbar: toolbarOptions
    }
});

I also had to modify my CSS to override the labels on the toolbar dropdown. Note that the "data-value" values in the CSS selectors must match the values specified above.

.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="14px"]::before {
    content: 'Normal';
    font-size: 14px !important;
}

.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="16px"]::before {
    content: 'Large';
    font-size: 16px !important;
}

.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="18px"]::before {
    content: 'Huge';
    font-size: 18px !important;
}
mfranchi
  • 601
  • 5
  • 7
  • Thanks! This worked for me too. It is failing when changing `Size` to any other variable name. Strange!!! – Sahal Sajjad Aug 26 '17 at 19:09
  • 9
    It's absolutely ridiculous that you have to resort to overriding the styles via their data values instead of using some config like: `{'size': {'Small': '14px', 'Normal': false, 'Large': '16px', 'Huge': '18px'}}` Then again, this leaves the styling of the menu items up in the air... – pmarreck Dec 06 '17 at 16:03
  • 9
    Note that, additionally, in order to get the menu title to reflect the style of wherever the insertion point was (as it does for the stock/default menus), I had to also add this sort of CSS: `.ql-picker-label[data-value="10px"]::before { content: 'Small' !important; }` – pmarreck Dec 06 '17 at 16:25
  • 1
    This is a much better answer to the the problem i am having. my application requires the editor to have ~200 font sizes using a slider as a picker. do i have to white-list each value from my min to my max font size? – Lonergan6275 Apr 25 '19 at 15:03
  • @Lonergan6275 Did you find any solution? My requirement is same.. – Neha Apr 29 '20 at 09:48
13

Piggybacking off @mfranchi and @pmarreck, I got a standard "MS Word" font size list with the following:

const fontSizeArr = ['8px','9px','10px','12px','14px','16px','20px','24px','32px','42px','54px','68px','84px','98px'];

var Size = Quill.import('attributors/style/size');
Size.whitelist = fontSizeArr;
Quill.register(Size, true);

var toolbarOptions = [
    [{ 'size': fontSizeArr }],
];

const quill = new Quill("#quillElementSelector", {
    modules: {
        toolbar: toolbarOptions,
    },
    theme: 'snow',
});

Here's a pure CSS solution that I found as this Quill issue:

.ql-snow{
.ql-picker{
    &.ql-size{
        .ql-picker-label,
        .ql-picker-item{
            &::before{
                content: attr(data-value) !important;
            }
        }
    }
}
}
Andrew
  • 2,368
  • 1
  • 23
  • 30
  • 2
    It seems like you used SASS instead of pure CSS, based on the nested CSS class syntax. Here is the pure CSS solution I came up with today: .ql-snow .ql-picker.ql-size .ql-picker-label[data-value]::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value]::before { content: attr(data-value) !important; } – Pi Da Mar 02 '21 at 22:21
  • It seems that Chrome will auto adjust font-size to 12px when it's less than 12px. Although `transform: scale(0.83);` will work in normal dom, is there anyway in quilljs-editor? – kiritoXF Aug 11 '21 at 01:49
6

It's a bit weird right now so I may add this into a Quill configuration. But for now, the reason it's not working is Quill uses classes by default for sizing and what you want is inline styles. You can change this with:

var Size = Quill.import('attributors/style/size');
Quill.register(Size, true);

// Rest is the same
var editor = new Quill('#editor');
jhchen
  • 14,355
  • 14
  • 63
  • 91
  • Now, I'm using the CDN library. Will this still work? – Brandon Jul 28 '16 at 17:35
  • I couldn't get this solution to work. It kept throwing an error when trying to import that. However I did find a work around. I made my own copy of the snow css file and changed the font sizes on the file. Thanks man! – Brandon Jul 28 '16 at 17:52
  • 1
    My mistake I updated the answer with the correct import – jhchen Jul 28 '16 at 19:12