如何将 H2 和 <p> 标签包裹在边框中,如图所示:
How to wrap an H2 and <p> tag in a border like in the image:
有没有一种简单的方法可以通过在 h2 和 p 标签上使用 css 来实现与此图片相同的设计?
你可以试试下面的代码。
.box {
margin-top : 50px;
border : 1px solid #000;
padding : 15px;
width : 500px;
margin-left : auto;
margin-right : auto;
position : relative;
}
.title {
position: absolute;
top: -20px;
font-size: 24px;
background: #fff;
padding: 3px 0;
width: 300px;
text-align: center;
left: calc(50% - 150px);
}
<html>
<head>
</head>
<body>
<div class="box">
<div class="title"><h2>Sample Text</h2></div>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
</body>
有没有一种简单的方法可以通过在 h2 和 p 标签上使用 css 来实现与此图片相同的设计?
你可以试试下面的代码。
.box {
margin-top : 50px;
border : 1px solid #000;
padding : 15px;
width : 500px;
margin-left : auto;
margin-right : auto;
position : relative;
}
.title {
position: absolute;
top: -20px;
font-size: 24px;
background: #fff;
padding: 3px 0;
width: 300px;
text-align: center;
left: calc(50% - 150px);
}
<html>
<head>
</head>
<body>
<div class="box">
<div class="title"><h2>Sample Text</h2></div>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
</body>