Text in Border CSS HTML

2019-01-03 15:39发布

I'd like to have a div that looks like this:

border example

Is this possible to do with HTML + CSS? I will also be animating this div with jQuery. When the div is hidden I would like the title and the top line to show.

标签: html css
5条回答
混吃等死
2楼-- · 2019-01-03 16:23

You can do something like this, where you set a negative margin on the h1 (or whatever header you are using)

div{
    height:100px;
    width:100px;
    border:2px solid black;
}

h1{
    width:30px;
    margin-top:-10px;
    margin-left:5px;
    background:white;
}

Note: you need to set a background as well as a width on the h1

Example: http://jsfiddle.net/ZgEMM/


EDIT

To make it work with hiding the div, you could use some jQuery like this

$('a').click(function(){
    var a = $('h1').detach();
    $('div').hide();
    $(a).prependTo('body');    
});

(You will need to modify...)

Example #2: http://jsfiddle.net/ZgEMM/4/

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-03 16:23

You can use a fieldset tag.

<!DOCTYPE html>
<html>
<body>

<form>
 <fieldset>
  <legend>Personalia:</legend>
  Name: <input type="text"><br>
  Email: <input type="text"><br>
  Date of birth: <input type="text">
 </fieldset>
</form>

</body>
</html>

Check this link: HTML Tag

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-03 16:41

It is possible by using the legend tag. Refer to http://www.w3schools.com/html5/tag_legend.asp

查看更多
来,给爷笑一个
5楼-- · 2019-01-03 16:43

I know a bit late to the party, however I feel the answers could do with some more investigation/input. I have managed to create the situation without using the fieldset tag - that is wrong anyway as if I'm not in a form then that isn't really what I should be doing.

/* Styles go here */

#info-block section {
    border: 2px solid black;
}

.file-marker > div {
    padding: 0 3px;
    height: 100px;
    margin-top: -0.8em;
    
}
.box-title {
    background: white none repeat scroll 0 0;
    display: inline-block;
    padding: 0 2px;
    margin-left: 8em;
}
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" type="text/css" href="/style.css">
    <script src="index.js"></script>
  </head>

  <body>
    <aside id="info-block">
      <section class="file-marker">
              <div>
                  <div class="box-title">
                      Audit Trail
                  </div>
                  <div class="box-contents">
                      <div id="audit-trail">
                      </div>

                  </div>
              </div>
      </section>
    </aside>
  </body>

</html>

This can be viewed in this plunk:

Outline box with title

What this achieves is the following:

  • no use of fieldsets.

  • minimal use if CSS to create effect with just some paddings.

  • Use of "em" margin top to create font relative title.

  • use of display inline-block to achieve natural width around the text.

Anyway I hope that helps future stylers, you never know.

查看更多
等我变得足够好
6楼-- · 2019-01-03 16:44

Yes, but it's not a div, it's a fieldset

<fieldset>
  <legend>AAA</legend>
</fieldset>

CSS:

fieldset {
    border: 1px solid #000;
}
查看更多
登录 后发表回答