How can I make a list-style-image scale with the list's font size, when we can't use glyph fonts?

前端 未结 5 621
野趣味
野趣味 2020-12-28 13:24

A webpage I\'m working on uses some fancy chevrons for bullet points in a list. I\'d like to define a list style that scales with the font size of the list items itself: doi

相关标签:
5条回答
  • 2020-12-28 13:34

    This uses your svg as the background image for the ::before content, sized using em's to keep it the same size as the font. The image will be just smaller than the same size as the text no matter how you size the text or if you zoom in or out.

    ul.foo,
    ul.foo li {
      list-style-type: none;
      padding: 0;
      margin: 0;
    }
    ul.foo li {
      height: 1em;
    }
    ul.foo li:before {
      content: "";
      background-image: url(http://imgh.us/chevron_1.svg);
      background-size: .5em .5em;
      background-repeat: no-repeat;
      display: inline-block;
      vertical-align: top;
      position: relative;
      top: 50%;
      margin-top: -.175em;
      width: .5em;
      height: .5em;
    }
    <ul class="foo">
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
    </ul>
    <ul class="foo" style="font-size: 2em">
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
      <li>List Item</li>
    </ul>

    0 讨论(0)
  • 2020-12-28 13:38

    In your SVG image XML you must remove the width and height attributes, and then the SVG will scale to be 100% or 1em of the font-size

    Here is the base64 version of your image with this done:

    list-style-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiIHZpZXdCb3g9IjAgMCA4IDE0IiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCA4IDE0IiB4bWw6c3BhY2U9InByZXNlcnZlIj48cGF0aCBmaWxsPSIjNjY2NjY2IiBkPSJNMC4zNywxMi42MzhsNS43MjYtNS41NjVMMC41MzEsMS4zNDdDMC4yNTIsMS4wNTksMC4yNjEsMC42MDEsMC41NDcsMC4zMjFjMC4yODktMC4yNzksMC43NDYtMC4yNzIsMS4wMjYsMC4wMTZsNi4wNjIsNi4yNGMwLDAuMDAyLDAuMDA2LDAuMDA0LDAuMDA4LDAuMDA2YzAuMDY4LDAuMDcsMC4xMTksMC4xNTYsMC4xNTYsMC4yNDRDNy45MDIsNy4wODgsNy44NDYsNy4zOTksNy42MzEsNy42MWMtMC4wMDIsMC4wMDQtMC4wMDYsMC4wMDQtMC4wMSwwLjAwNmwtNi4yMzgsNi4wNjNjLTAuMTQzLDAuMTQxLTAuMzMxLDAuMjA5LTAuNTE0LDAuMjA1Yy0wLjE4Ny0wLjAwNi0wLjM3Mi0wLjA3OC0wLjUxMS0wLjIyMUMwLjA3NiwxMy4zNzYsMC4wODMsMTIuOTE5LDAuMzcsMTIuNjM4Ii8+PC9zdmc+');
    

    Unfortunately you cannot explicitly set the size of a list-style-image, however there is one hack solution which doesn't require any further HTML;

    If your LI elements only contain a single line of text (which is quite often the case with lists) then you can use the css selector ::first-line to scale your font-size up or down without affecting your list-style-image.

    Giving this alternative solution:

    ul {
      list-style-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiIHZpZXdCb3g9IjAgMCA4IDE0IiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCA4IDE0IiB4bWw6c3BhY2U9InByZXNlcnZlIj48cGF0aCBmaWxsPSIjNjY2NjY2IiBkPSJNMC4zNywxMi42MzhsNS43MjYtNS41NjVMMC41MzEsMS4zNDdDMC4yNTIsMS4wNTksMC4yNjEsMC42MDEsMC41NDcsMC4zMjFjMC4yODktMC4yNzksMC43NDYtMC4yNzIsMS4wMjYsMC4wMTZsNi4wNjIsNi4yNGMwLDAuMDAyLDAuMDA2LDAuMDA0LDAuMDA4LDAuMDA2YzAuMDY4LDAuMDcsMC4xMTksMC4xNTYsMC4xNTYsMC4yNDRDNy45MDIsNy4wODgsNy44NDYsNy4zOTksNy42MzEsNy42MWMtMC4wMDIsMC4wMDQtMC4wMDYsMC4wMDQtMC4wMSwwLjAwNmwtNi4yMzgsNi4wNjNjLTAuMTQzLDAuMTQxLTAuMzMxLDAuMjA5LTAuNTE0LDAuMjA1Yy0wLjE4Ny0wLjAwNi0wLjM3Mi0wLjA3OC0wLjUxMS0wLjIyMUMwLjA3NiwxMy4zNzYsMC4wODMsMTIuOTE5LDAuMzcsMTIuNjM4Ii8+PC9zdmc+');
    }
    
    .small-list {
      font-size: 140%;
    }
    .large-list {
      font-size: 350%;
    }
    
    .small-list li::first-line,
    .large-list li::first-line{
     font-size: 70%;
    }
    
    0 讨论(0)
  • 2020-12-28 13:46

    You can use SVG as an embed image:

    <img src="chevron.svg" alt="chevron-icon" class="my-icon" />
    

    *CSS

    .my-icon{
        width:50px;
        height:auto;
    }
    

    A helpfull article:

    http://soqr.fr/testsvg/embed-svg-liquid-layout-responsive-web-design.php

    For a better browser compatibility, i recommend convert your SVG files into a font, you can do that here:

    https://icomoon.io/

    Or use a predesigned font like FontAwesome:

    http://fontawesome.io/

    Here you will get the icon that you need:

    http://fontawesome.io/icon/chevron-right/

    Implement FontAwesome with:

    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
    

    But i highly recommend that you download the source files instead of using man in middle files.

    And add this CSS:

    ul{
        list-style:none;
        color:#888;
        font-size:24px;
    }
    
    ul li:before{
        font-family: FontAwesome;
        content:"\f054";
    }
    

    HTML

    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
    </ul>
    

    You can get a preview here:

    http://jsfiddle.net/a1vkeg6c/1/

    Of this way you will have the same retina support of SVG that you want, and even in a more practical way.

    I hope been helpful!

    0 讨论(0)
  • 2020-12-28 13:48

    I would approach solving this problem using a pseudo element before each li

    Here is the markup

    ul {
        list-style: none;
    }
    
    li {
        position: relative;
    }
    
    li:before {
        /*
        The desired width gets defined in two places: The element width, and background size.
        The height only gets defined once, in background size.
        */
        position: absolute;
        display: block;
        content: '\2022'; /* bullet point, for screen readers */
        text-indent: -999999px; /* move the bullet point out of sight */
        left: -.75em;
        width: .4em; /* desired width of the image */
        height: 1em; /* unrelated to image height; this is so it gets snipped */
        background-repeat: no-repeat;
        background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiIHdpZHRoPSI4cHgiIGhlaWdodD0iMTRweCIgdmlld0JveD0iMCAwIDggMTQiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDggMTQiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxwYXRoIGZpbGw9IiM2NjY2NjYiIGQ9Ik0wLjM3LDEyLjYzOGw1LjcyNi01LjU2NUwwLjUzMSwxLjM0N0MwLjI1MiwxLjA1OSwwLjI2MSwwLjYwMSwwLjU0NywwLjMyMWMwLjI4OS0wLjI3OSwwLjc0Ni0wLjI3MiwxLjAyNiwwLjAxNmw2LjA2Miw2LjI0YzAsMC4wMDIsMC4wMDYsMC4wMDQsMC4wMDgsMC4wMDZjMC4wNjgsMC4wNywwLjExOSwwLjE1NiwwLjE1NiwwLjI0NEM3LjkwMiw3LjA4OCw3Ljg0Niw3LjM5OSw3LjYzMSw3LjYxYy0wLjAwMiwwLjAwNC0wLjAwNiwwLjAwNC0wLjAxLDAuMDA2bC02LjIzOCw2LjA2M2MtMC4xNDMsMC4xNDEtMC4zMzEsMC4yMDktMC41MTQsMC4yMDVjLTAuMTg3LTAuMDA2LTAuMzcyLTAuMDc4LTAuNTExLTAuMjIxQzAuMDc2LDEzLjM3NiwwLjA4MywxMi45MTksMC4zNywxMi42MzgiLz48L3N2Zz4=');
        background-size: .4em .7em;
        background-position: 0 .3em;
    }
    
    .small-list {
        font-size: 85%;
    }
    
    .large-list {
        font-size: 150%;
    }
    <ul class="small-list">
      <li>The goal is to make the chevron smaller for this list</li>
      <li>Specifically, just slightly smaller than capital letters, as stated.</li>
      <li>Nomas matas</li>
      <li>Roris dedit</li>
    </ul>
        
    <ul class="large-list">
      <li>And larger for this list</li>
      <li>Multiline list item<br>for testing</li>
      <li>Nomas matas</li>
      <li>Roris dedit</li>
    </ul>

    Explanation:

    • First we get rid of the default bullets on the ul
    • Then we create a pseudo element in front of each li using the :before selector and content: '\2022';
      • The content: '\2022'; adds the unicode bullet point, •, for screen readers to read out. The text indent moves it well out of sight.
    • We then apply a background (chevron) to the pseudo elements, and size it using a few properties. The key part here is to ensure that the dimensions maintain the same ratio as the svg. The dimensions on the pseudo element are defined using em so that they adjust proportionally when the font-size is changed. Finally, we also position the background where the bullet would have been.
      • background-size: .4em .7em; tells the browser to size the background the way the image should be sized, we need to maintain the correct aspect ratio here.
      • background-position: 0 .3em; moves the chevron image in line with the text.
      • width: .4em; makes the psuedo element just wide enough to fit the image, and height: 1em; makes it match line height, and be tall enough to fit the offset as well.

    Caveat: - IE 8 doesn't support background-size, but I presume that this will not be an issue as it also doesn't support rendering svg.

    0 讨论(0)
  • 2020-12-28 13:51

    You could define the svg in your css directly and change the height and width as needed, not very DRY, but it works:

    .small-list {
      font-size: 85%;
      list-style-image: url('data:image/svg+xml;utf8,<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 8 14" width="6" height="9"><path fill="%23666666" d="M0.37,12.638l5.726-5.565L0.531,1.347C0.252,1.059,0.261,0.601,0.547,0.321 c0.289-0.279,0.746-0.272,1.026,0.016l6.062,6.24c0,0.002,0.006,0.004,0.008,0.006c0.068,0.07,0.119,0.156,0.156,0.244 C7.902,7.088,7.846,7.399,7.631,7.61c-0.002,0.004-0.006,0.004-0.01,0.006l-6.238,6.063c-0.143,0.141-0.331,0.209-0.514,0.205 c-0.187-0.006-0.372-0.078-0.511-0.221C0.076,13.376,0.083,12.919,0.37,12.638" /></svg>');
    }
    .large-list {
      font-size: 150%;
      list-style-image: url('data:image/svg+xml;utf8,<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 8 14" width="8" height="14"><path fill="%23666666" d="M0.37,12.638l5.726-5.565L0.531,1.347C0.252,1.059,0.261,0.601,0.547,0.321 c0.289-0.279,0.746-0.272,1.026,0.016l6.062,6.24c0,0.002,0.006,0.004,0.008,0.006c0.068,0.07,0.119,0.156,0.156,0.244 C7.902,7.088,7.846,7.399,7.631,7.61c-0.002,0.004-0.006,0.004-0.01,0.006l-6.238,6.063c-0.143,0.141-0.331,0.209-0.514,0.205 c-0.187-0.006-0.372-0.078-0.511-0.221C0.076,13.376,0.083,12.919,0.37,12.638" /></svg>');
    }
    <ul class="small-list">
      <li>The goal is to make the chevron smaller for this list</li>
      <li>Specifically, just slightly smaller than capital letters, as stated.</li>
      <li>Nomas matas</li>
      <li>Roris dedit</li>
    </ul>
    
    <ul class="large-list">
      <li>And larger for this list</li>
      <li>Nomas matas</li>
      <li>Roris dedit</li>
    </ul>

    Or you could dry it out a little by using the svg as a background image rather than as a list style image. Note that placing the svg in the css isn't necessary here:

    ul {
      list-style: none;
      padding-left: 0;
    }
    li {
      padding-left: .65em;
      background: url('data:image/svg+xml;utf8,<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 8 14"><path fill="%23666666" d="M0.37,12.638l5.726-5.565L0.531,1.347C0.252,1.059,0.261,0.601,0.547,0.321 c0.289-0.279,0.746-0.272,1.026,0.016l6.062,6.24c0,0.002,0.006,0.004,0.008,0.006c0.068,0.07,0.119,0.156,0.156,0.244 C7.902,7.088,7.846,7.399,7.631,7.61c-0.002,0.004-0.006,0.004-0.01,0.006l-6.238,6.063c-0.143,0.141-0.331,0.209-0.514,0.205 c-0.187-0.006-0.372-0.078-0.511-0.221C0.076,13.376,0.083,12.919,0.37,12.638" /></svg>')no-repeat scroll 0% 50% /.65em .65em transparent;
    }
    .small-list li {
      font-size: 85%;
    }
    .large-list li {
      font-size: 150%;
    }
    <ul class="small-list">
      <li>The goal is to make the chevron smaller for this list</li>
      <li>Specifically, just slightly smaller than capital letters, as stated.</li>
      <li>Nomas matas</li>
      <li>Roris dedit</li>
    </ul>
    <ul class="large-list">
      <li>And larger for this list</li>
      <li>Nomas matas</li>
      <li>Roris dedit</li>
    </ul>

    0 讨论(0)
提交回复
热议问题