简体   繁体   English

内联文本 - 与填充的背景颜色

[英]Inline text - background color with padding

I need to make a Headline with background color which is not a block(only the text-bg is colored). 我需要制作一个背景颜色的标题,这不是一个块(只有text-bg是彩色的)。

Here is an image of what I have to archieve: 这是我必须达到的目标的形象: 在此输入图像描述

And here is what I've got so far: http://jsbin.com/tiwuquze/1/edit 这是我到目前为止所做的: http//jsbin.com/tiwuquze/1/edit

You can see the padding is not right and I couldn't figure how to set it. 你可以看到填充不正确,我无法想象如何设置它。 When you play around with the padding, you will see that it will overlap and the second line is not going in like the first one. 当您使用填充时,您将看到它将重叠,第二行不会像第一行那样进入。

I am happy about every hint! 我很高兴每一个提示!

Solution: 解:

John Smith made a working example: http://jsbin.com/tiwuquze/21/edit He used box-shadow to emulate the left padding. 约翰史密斯做了一个工作的例子: http//jsbin.com/tiwuquze/21/edit他使用box-shadow来模拟左边的填充。

UPDATE got the solution, use box shadow :D UPDATE得到了解决方案,使用框阴影:D

h1 {
    background-color: #FE8020;
    color: #FFFFFF;
    display:inline;
    white-space:pre-wrap;
    line-height : 24px;
    font-size: 18px;
    font-family:arial, sans-serif;
    padding: 5px;
    padding-left:0px;
    box-shadow: -16px 0 0 #FE8020; /* box shadow is emulating padding liek a charme */

}

for me this looks good 对我来说这看起来不错

http://jsbin.com/tiwuquze/21/edit http://jsbin.com/tiwuquze/21/edit

Try setting a bigger padding and use line-height example : 尝试设置更大的填充并使用行高示例:

line-height:24px;
padding:4px; 

Nowadays (understanding years have passed since this question was posted), if you were willing to use experimental technology and concerned primarily with modern browsers (and could accept how it looked without it on unsupported browsers), you could use box-decoration-break . 如今(了解自问题发布以来已经过去多年),如果您愿意使用实验技术并主要关注现代浏览器(并且可以接受在不支持的浏览器上没有它的情况下看起来如何),您可以使用box-decoration-break

The box-decoration-break CSS property specifies how an element's fragments should be rendered when broken across multiple lines, columns, or pages. box-decoration-break CSS属性指定在跨多行,每列或多个页面分割时应如何呈现元素的片段。

h1 {
    background-color: #FE8020;
    color: #FFFFFF;
    display:inline;
    white-space:pre-wrap;
    line-height : 24px;
    font-size: 18px;
    font-family:arial, sans-serif;
    padding: 5px;padding-left:0px;box-shadow: -16px 0 0 #FE8020;
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
}

https://jsbin.com/hadedulici/1/edit https://jsbin.com/hadedulici/1/edit

Also, for what it's worth, box-shadow -based solutions don't seem to work for me in Firefox 55.0.3 (Windows 7) nor Firefox Quantum 62.0.3 (Windows 10) at time of writing. 此外,基于box-shadow的解决方案在编写本文时似乎不适用于Firefox 55.0.3(Windows 7)和Firefox Quantum 62.0.3(Windows 10)。

You can do as @ReKnawn suggested increase the padding and line-height just ensure that the line height is larger than the font-size + padding x 2 since in this case both the top and bottom are padded. 您可以这样做,因为@ReKnawn建议增加填充和行高,只需确保行高大于font-size + padding x 2,因为在这种情况下,顶部和底部都是填充的。 eg if the font-size is 16 and padding is 4px line-height must be greater than 24 to ensure reasonable space and avoid overlapping 例如,如果font-size为16且填充为4px,则line-height必须大于24,以确保合理的空间并避免重叠

here check this solution: 在这里查看此解决方案

http://jsbin.com/tiwuquze/17/edit http://jsbin.com/tiwuquze/17/edit

Add a span inside the h1 element. 在h1元素中添加span。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM