边框 CSS HTML 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7731310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text in Border CSS HTML
提问by Alex Bliskovsky
I'd like to have a div that looks like this:
我想要一个看起来像这样的 div:
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 有关吗?我还将使用 jQuery 为这个 div 设置动画。当 div 隐藏时,我希望显示标题和顶行。
采纳答案by Jason Gennaro
You can do something like this, where you set a negative margin
on the h1
(or whatever header you are using)
你可以做这样的事情,你margin
在h1
(或你正在使用的任何标题)上设置一个否定
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
注意:您需要设置background
,以及一个width
上h1
Example: http://jsfiddle.net/ZgEMM/
示例:http: //jsfiddle.net/ZgEMM/
EDIT
编辑
To make it work with hiding the div
, you could use some jQuery like this
为了让它隐藏div
,你可以使用一些像这样的 jQuery
$('a').click(function(){
var a = $('h1').detach();
$('div').hide();
$(a).prependTo('body');
});
(You will need to modify...)
(您将需要修改...)
Example #2:http://jsfiddle.net/ZgEMM/4/
示例#2:http : //jsfiddle.net/ZgEMM/4/
回答by Bazzz
Yes, but it's not a div
, it's a fieldset
是的,但它不是一个div
,它是一个fieldset
<fieldset>
<legend>AAA</legend>
</fieldset>
CSS:
CSS:
fieldset {
border: 1px solid #000;
}
回答by PeterS
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.
我知道聚会有点晚了,但是我觉得答案可以通过更多的调查/投入来解决。我设法在不使用 fieldset 标签的情况下创造了这种情况 - 无论如何这都是错误的,好像我不在表单中那样,那并不是我真正应该做的。
/* 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:
这可以在这个 plunk 中查看:
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.
不使用字段集。
如果 CSS 仅使用一些填充来创建效果,则使用最少。
使用“em”边距顶部创建字体相关标题。
使用 display inline-block 实现文本周围的自然宽度。
Anyway I hope that helps future stylers, you never know.
无论如何,我希望对未来的造型师有所帮助,你永远不知道。
回答by Bashirpour
<fieldset>
<legend> YOUR TITLE </legend>
<p>
Lorem ipsum dolor sit amet, est et illum reformidans, at lorem propriae mei. Qui legere commodo mediocritatem no. Diam consetetur.
</p>
</fieldset>
回答by Rares Muresan
回答by Bashirpour
Text in Border with transparent text background
带有透明文本背景的边框中的文本
.box{
background-image: url("https://i.stack.imgur.com/N39wV.jpg");
width: 350px;
padding: 10px;
}
/*begin first box*/
.first{
width: 300px;
height: 100px;
margin: 10px;
border-width: 0 2px 0 2px;
border-color: #333;
border-style: solid;
position: relative;
}
.first span {
position: absolute;
display: flex;
right: 0;
left: 0;
align-items: center;
}
.first .foo{
top: -8px;
}
.first .bar{
bottom: -8.5px;
}
.first span:before{
margin-right: 15px;
}
.first span:after {
margin-left: 15px;
}
.first span:before , .first span:after {
content: ' ';
height: 2px;
background: #333;
display: block;
width: 50%;
}
/*begin second box*/
.second{
width: 300px;
height: 100px;
margin: 10px;
border-width: 2px 0 2px 0;
border-color: #333;
border-style: solid;
position: relative;
}
.second span {
position: absolute;
top: 0;
bottom: 0;
display: flex;
flex-direction: column;
align-items: center;
}
.second .foo{
left: -15px;
}
.second .bar{
right: -15.5px;
}
.second span:before{
margin-bottom: 15px;
}
.second span:after {
margin-top: 15px;
}
.second span:before , .second span:after {
content: ' ';
width: 2px;
background: #333;
display: block;
height: 50%;
}
<div class="box">
<div class="first">
<span class="foo">FOO</span>
<span class="bar">BAR</span>
</div>
<br>
<div class="second">
<span class="foo">FOO</span>
<span class="bar">BAR</span>
</div>
</div>
回答by JavierIEH
It is possible by using the legend tag. Refer to http://www.w3schools.com/html5/tag_legend.asp