简体   繁体   English

如何在div的边界内对齐文本标题

[英]How to align text header inside boarder of div

I'm trying to put text inside the border of a box on the page. 我正在尝试将文本放在页面上框的边框内。 The solution I have kind of works, but the text in the box writes over the header and doesn't scale the way I'd like it to. 我有点工作的解决方案,但框中的文字写在标题上,并没有按照我喜欢的方式缩放。 Is there a better solution? 有更好的解决方案吗?

I've tried a few things like this https://jsbin.com/atupup/edit?html,output but so far have not been successful. 我尝试了一些这样的事情https://jsbin.com/atupup/edit?html,output但到目前为止还没有成功。

This is the code I landed on: 这是我登陆的代码:

 <div style="width: 100%; height: 20px; border-bottom: 1px solid black; text-align: center"> <span style="font-size: 40px; background-color: white; padding: 0 10px;"> Section Title <!--Padding is optional--> </span> </div> <div style="border: 1px solid black; padding:none;"> <p>more content here</p> </div> <div style="width: 100%; height: 20px; border-bottom: 1px solid black; text-align: center"> <span style="font-size: 40px; background-color: white; padding: 0 10px;"> Section Title <!--Padding is optional--> </span> </div> <div style="border: 1px solid black; padding:none;"> <p>more content here</p> </div> 

I'm not sure if I'm doing this correctly, so any help would be appreciated! 我不确定我是否正确这样做,所以任何帮助将不胜感激!

Use a <fieldset> with a <legend> , which provides this effect natively and can be styled any way you like: 使用带有<legend><fieldset> ,它本身可以提供此效果,并且可以按照您喜欢的方式设置样式:

 fieldset { background-color:rgba(100,50,175,.2); border-top:3px solid black; } legend { text-align:center; font-size:3em; font-weight:bold; background-color:rgba(100,100,255,.5); padding:10px; border:3px dashed #e0e0e0; } 
 <fieldset> <legend>This is the title</legend> Other content here Other content here Other content here Other content here Other content here Other content here Other content here Other content here Other content here Other content here Other content here Other content here Other content here Other content here Other content here <ul> <li>Item</li> <li>Item</li> <li>Item</li> </ul> <p>Just about any HTML can go in here</p> </fieldset> 

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

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