Auto-resizing skewed background in CSS (+ images?)

后端 未结 7 597
一向
一向 2021-02-08 01:36

I\'m going to convert this PSD image to CSS. I\'ve multiple h2s in multiple pages, so the inner text lenght and background-color may vary. Therefore the background should automa

7条回答
  •  孤独总比滥情好
    2021-02-08 01:58

    For anyone who's interested, here's my temporary solution: live demo.

    I used this sprite image:

    skewed bg sprite

    html:

    Short title

    A bit longer title

    A damn long title is here!

    (the .container div is only needed for variations)

    css:

    .sub-heading, .sub-heading:after, .sub-heading:before {
        background:url(tha-sprite-url.png) 0 0 no-repeat;
    }
    
    .sub-heading {
        position:relative;
        display:inline-block; clear:both;
        margin-left:31px; padding:0 18px 10px 0;
        font-size:25px; font-weight:normal; color:#FFF; line-height:47px;
        background-position:0 -75px;
        background-size:100% 282px; /* the sprite's height */
    }
    
    .sub-heading:before, .sub-heading:after { content:" "; position:absolute; top:0; }
    .sub-heading:before { left:-31px; width:31px; height:57px; }
    .sub-heading:after { right:-12px; width:12px; height:42px; background-position:-150px 0; }
    
    .sub-heading-txt {
        display:block;
        -webkit-transform:rotate(-2deg); -ms-transform:rotate(-2deg); transform:rotate(-2deg);
    }
    
    /* variations */
    .container { margin-bottom:10px; }
    .container:nth-child(3n+2) .sub-heading { background-position:0 -150px; }
    .container:nth-child(3n+2) .sub-heading:before { background-position:-50px 0; }
    .container:nth-child(3n+2) .sub-heading:after { background-position:-175px 0; }
    .container:nth-child(3n+3) .sub-heading { background-position:0 -225px; }
    .container:nth-child(3n+3) .sub-heading:before { background-position:-100px 0; }
    .container:nth-child(3n+3) .sub-heading:after { background-position:-200px 0; }
    

    Works on IE9+

    Unfortunately, background-size property doesn't support an "inherit" value, so the actual sprite's height must be set in the css :-( I'm still looking for a more efficient solution.

提交回复
热议问题