html - 在标题元素内添加水平线

标签 html css

我想复制这个:

enter image description here

所以我试过这个:

.test {
		position:relative;
		width:100%;
		margin:0 auto;

			}

	.test hr {
		display:inline;
		width:40%;
		float:left;
		border:2px solid black;
	}

	.test h1 {
		display:inline;
		float:right;
		font-size:56px;
	}
<div class="test">
	<hr />
	<h1>Here is how the magic <br/> works.</h1>

</div>

如您所见,还差得远。

我该怎么做?

最佳答案

一个内部 span,一个伪元素和 span 的背景颜色(您指定的始终为白色)

h1 {
  width: 80%;
  margin: 1em auto;
  text-align: right;
  position: relative;
  overflow: hidden;
}

.narrow {
  width: 50%;
}

.thin {
  width: 50%;
  font-size: 16px;
}

span {
  background: white;
  position: relative;
  z-index: 2;
  padding-left: ..5em;
}

h1::after {
  content: "";
  width: 100%;
  height: 2px;
  background: currentcolor;
  position: absolute;
  left: 0;
  bottom: .5em;
}
<h1><span>This is how the magic works</span></h1>


<h1 class="narrow"><span>This is how the magic works</span></h1>

<h1 class="thin"><span>This is how the magic works, no really it does with any font-size!</span></h1>

关于html - 在标题元素内添加水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46368787/

相关文章:

html - 文本与显示 :table 的容器边框重叠

html - Dreamweaver 显示与 Firefox 不同

html - CSS - 使文本保持在响应图像上的同一位置

javascript - 可调整大小的 div 中的 Bootstrap 网格系统响应实用程序

CSS:IE 上的边距/填充导致 float 放置不正确

javascript - Angular.js 与 Ruby On Rails Forms 的集成

javascript - 附加不超过一行的交替子图像和文本项

javascript - 如何为文本提供 readmore?

html - CSS 线性渐变显示为不同颜色的交替线

javascript - 第一次单击后 JQuery 函数停止工作