CSS - Heading background color with padding

Hi all

I have a jsfiddle here - https://jsfiddle.net/z9ptvz87/

I’d need the headings to have a background color and same padding on all sides.

I sort of have ot working here but it’s with line-height to get it working. Without line-height it looks like this.

Is there a better way to do this and have padding on the left and right of the text.

*{
	font-family: sans-serif;
}

.block{
	margin: 50px;
	width: 400px;
}

.block span{
    display: block;    
}

h1, h2{
	background: red;
	display: inline;
	padding:10px;
	line-height: 2em;
}

Why do you have a span holding the <h1>? That’s invalid. What’s the end look you are going for? Why did you make those headers inline? Are you looking for a more inline-block kinda thing?

If I understand you right, you just basically want padding to display on all sides, not just the top and bottom? Change h1,h2 to display:inline-block. I am as confused as Ryan as to why you have a span as a parent element of an h1. Changing/eliminating the line-height makes it look more natural, in case you were looking at the disproportionate amount of padding appearing on the top and bottom

Why not approach the problem this way:

    *{
	font-family: sans-serif;
}

.block{
	margin: 50px;
	width: 360px;
    background: red;
    padding:20px;
    line-height:1.1;
}
h1{ font-size:300%;}

 

h1, h2{
	display: inline;

}

<div class="block">
    <div>
		<h1>Heading</h1>
		<h2>Sub heading Sub heading Sub heading Sub heading Sub heading Sub heading</h2>
</div>

I’m trying to create something like this.

I wanted something like this, but this example uses two blocks positioned absolutely to create the cut off effect.

It can be done, but the HTML is “ugly”.

If you can write a small JS that inserts &nbsp;&#8203; (a non-breaking space followed by a zero-width space) between each word in the <h2><span>, it would hide the dirt and the inserts would be rendered in the HTML.

The non-breaking space applies the space to the right of the word when the next word wraps. The zero-width space allows the words to wrap. The space along the left side of the box is applied by a border.

Unfortunately, I don’t know of a better way if the sentences need to wrap between different words responsively.

What are you willing to accept?

This is the gist of it:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>line-box w/color</title>
<!--
http://www.sitepoint.com/community/t/css-heading-background-color-with-padding/193064
ttmt
Heading background color with padding
-->
    <style>
body {
    font-family: sans-serif;
}
.outer {
    margin:50px 0 0 50px;
    width:400px;
    display:inline-block;
    border-left:.5em solid pink;
    overflow:hidden;
}
h1 {
    background:pink;
    display: inline-block;
    padding:.25em .25em .25em 0;
    margin:0;
}
h2 {
    line-height:1.75em;
    margin:0;
}
h2 span {
    display:inline;
    background-color:pink;
    padding:.375em .5em .375em 0;
}
    </style>
</head>
<body>

<div class="outer">
    <h1>Heading</h1>
<h2><span>Sub&nbsp;&#8203;heading&nbsp;&#8203;Sub&nbsp;&#8203;heading&nbsp;&#8203;Sub&nbsp;&#8203;heading&nbsp;&#8203;Sub&nbsp;&#8203;heading&nbsp;&#8203;Sub&nbsp;&#8203;heading&nbsp;&#8203;Sub&nbsp;&#8203;heading</span></h2>
</div>

</body>
</html>

Try this alternative:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title>  CSS - Heading background color with padding</title>
<style type="text/css">
  body {font-family: Tahoma, Arial, sans-serif;}
</style>
</head>
<body>
<!--
http://www.sitepoint.com/community/t/css-heading-background-color-with-padding/193064
ttmt
Heading background color with padding
-->
<style>
.outer-jb    {background-color: lightgreen; padding: 1em 0 8.8em 4.2em;}

.outer-jb h1,
.outer-jb h2 {float:left; clear:left; 
              margin:0; 
              padding:0 0.42em .12em;
              background-color:pink;}
.outer-jb br {clear:left;} /* REQUIRED TO REMOVE FLOAT */
</style>
</head>
<body>

<div class="outer-jb">
    <h1>Heading</h1>
    <h2>
      Sub heading Sub heading
    </h2>
    <h2>
      Sub heading Sub heading
    </h2>
    <h2>
      Sub heading Sub heading
    </h2>
    <br /> <!-- REQUIRED TO REMOVE FLOATS -->
</div>
</body>
</html>

@PaulOB - is it possible to make this even leaner and meaner?

We’ve had this question a few times over the years and the simplest answer went like this.

There was also a later article on CSS tricks but none of the methods were better than the ones we found and indeed most of the ones listed are broken in various browsers now.

Erik J and I came up with a method that doesn’t require extra mark up but doesn’t quite work as well as my codepen example as id doesn’t give the complete ragged edge…

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
h1, h2 {
	margin:0;
}
div {
	position:relative;
	font:bold 100%/1.3 arial;
	overflow:hidden;
	text-align:justify;
}
.linewrap {
	float:left;
	position:relative;
	z-index:1;
	padding:0 10px 0 10px;
	color:#fff;
	overflow:hidden;
	line-height:1.3em;
}
.linewrap:before {/*supplies background colour for all but the last line*/
	position:absolute;
	z-index:-2;
	top:0;
	right:0;
	bottom:1.3em;/* matches the line-height setting to avoid the last line*/
	left:0;
	width:999em;
	background:red;
	content:"\00a0";/* non breaking space*/
}
.linewrap:after {/*supplies background colour for the wrapped text only*/
	content:"\00a0";
	position:absolute;
	color:#fff;
	width:999em;
	padding:0 0 0 10px;
	background:red;
	margin-left:-999em;
	z-index:-1;
	bottom:0;
}
.block {
	width:400px;
}
</style>
</head>
<body>
<div class="block">
		<h1 class="linewrap">Heading</h1>
		<h2 class="linewrap">Sub heading Sub heading Sub heading Sub heading Sub heading Sub heading</h2>
</div>
</body>
</html>

Many thanks @PaulOB, that is way too complicated for my liking and prefer adding an extra H2 to split the block.

You can do it more semantically by just using spans inside your h2 and h1.

e.g.

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>CSS - Heading background color with padding</title>
<style type="text/css">
body {
	font-family: Tahoma, Arial, sans-serif;
}
</style>
</head>
<body>
<!--
http://www.sitepoint.com/community/t/css-heading-background-color-with-padding/193064
ttmt
Heading background color with padding
-->
<style>
h1,h2{margin:0}
h1:after,h2:after{
	content:"";
	display:table;
	clear:both;	
}
.outer-jb    {background-color: lightgreen; padding: 1em 0 8.8em 4.2em;}
.outer-jb h1 span,
.outer-jb h2 span{
	float:left; 
	clear:left; 
  padding:0 0.42em .12em;
  background-color:pink;
}

</style>
</head>
<body>
<div class="outer-jb">
		<h1><span>Heading</span></h1>
		<h2><span>Sub heading Sub heading</span> <span>Sub heading Sub heading</span> <span>Sub heading Sub </span></h2>
</div>
</body>
</html>

The difference with your example and my example is that mine is an automatic wrap and will work on any line length that needs to wrap. It’s not just a series of short lines like your example. In a percentage layout the effect is still the same for my example and you don’t need to split the text at set points to create the effect. Just change the width of the block in my example to a percentage and the effect is still maintained.

It only used 2 nested spans so is hardly more complicated than your example and you don’t need to manage the text :smile:

1 Like

Paul, the nested span is brilliant. Thanks for the CodePen.

Cheers

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.