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

前端 未结 5 628
野趣味
野趣味 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: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%;
    }
    • The goal is to make the chevron smaller for this list
    • Specifically, just slightly smaller than capital letters, as stated.
    • Nomas matas
    • Roris dedit
    • And larger for this list
    • Multiline list item
      for testing
    • Nomas matas
    • Roris dedit

    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.

提交回复
热议问题