css 如何让文字标题显示在边框上?


如图,如何让文字标题显示在边框上?

如果需要更精确的扩展,fieldset的效果不好,还自己实现一个吧,如下:

 
<style>
    .box{
        position:relative;
        border:1px solid red;
    }
    .box::before{
        content:attr(title);
        position:absolute;
        left:50%;
        transform:translateX(-50%);
        -webkit-transform:translate(-50%,-50%);
        padding:0 10px;
        background-color:#fff;
    }
</style>
<div class="box" title="使用方法">
    <ol>
        <li>这是啥</li>
        <li>干啥</li>
    </ol>
</div>

 

不太建议使用fieldset,一般都会被reset的

 <fieldset>
    <legend>【使用方法】</legend>
  </fieldset>

 

提供一种hacked方法:

style:

  div {
    width: 400px;
    height: 200px;
    border: 1px solid #ccc;
  }
  div h1 {
    height: 20px;
    margin: -10px auto 0;
    font-size: 14px;
    padding: 0 10px;
    text-align: center;
    width: 50px;
  }

html:

  <div>
    <h1>嘿嘿嘿</h1>
  </div>

 

 

<style>
    h2:before, h2:after {
        content: "";
        display: inline-block;
        vertical-align:middle;
        width: 100px;
        border-top: 1px solid #123456;
    }
</style>

<h2>线标题</h2>

 

设置position为relative,top为-1em 既可设置位置。

background-color为背景色,padding左右0.5em。
其实就是用背景色遮盖边框颜色。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM