2014-10-07 90 views
0

我試圖用CSS的框的上邊插入文本,但我無法想出任何使它發生的事情。這是我到目前爲止的代碼:在css中插入文本框的上邊框的文本

.boxed{ 
    border: 1px solid grey; 
    width: 300px; 
    height: 180px; 
} 

,這就是我想做的事: enter image description here

如何插入一個盒子的上邊界文本?

*我已經完成了這個感謝球員。但我現在的問題是把標籤分頁。你能幫我嗎?在代碼的其餘部分是在這裏:

http://jsfiddle.net/3y539gvq/

+0

這是什麼([MCVE](http://stackoverflow.com/help/mcve/))的HTML? – 2014-10-07 16:57:47

+3

你可以使用'fieldset/legend' combo - http://jsfiddle.net/cj9mxfhj/ – 2014-10-07 16:59:47

+0

@Paulie_D很好,希望我想到:) – 2014-10-07 17:10:50

回答

2

設置讓您的機器相對定位和使用絕對定位標籤位置。另外,我建議設置容器的背景,並繼承CSS中的背景以保持兩者一致。

.boxed { 
    position: relative; 
    background: white; 
    border: 1px solid grey; 
    width: 300px; 
    height: 180px; 
} 
.label { 
    background: inherit; 
    padding: 0 5px; 
    position: absolute; 
    left: 5px; 
    top: -10px; 
} 

<div class="boxed"> 
    <span class="label">General Information</span> 
</div> 

DEMO:http://jsfiddle.net/b7a29fsd/1/

+0

謝謝。它現在好了:D – arukiri123 2014-10-07 17:15:35

+1

不客氣!感謝您接受答案。 – 2014-10-07 17:19:50

1

您可以使用:

.boxed { 
    ... your existing styles ... 
    position:relative; 
} 


.boxed:after { 
    position:absolute; 
    top:-5px; 
    left:15px; 
    padding:3px; 
    content: "your label text"; 
    background-color: (something to cover up the border underneath) 
} 
+0

使用內容不會允許此樣式可以重複使用多個框。 – 2014-10-07 16:56:40

+0

這實際上取決於你是否需要不同的標籤,這也可以用其他只能定義內容的類來完成: – 2014-10-07 16:59:21

+1

好主意,但我仍然寧願將可見性內聯標籤。 – 2014-10-07 17:01:17

0

你可以把格內的文字你有一個像這樣:

<div> 
    <p> Some text </p> 
</div> 

CSS:

.boxed{ 
    border: 1px solid grey; 
    width: 300px; 
    height: 180px; 
    position:absolute; 
} 

.boxed p{ 
    position:relative; 
    top:-28px; 
    left:5px; 
    z-index:1; 
    background-color:white; 
    width:80px; 
} 
} 

示例here

+0

我現在的問題是在分頁上分頁。你能幫我嗎?其餘的代碼在這裏: http://jsfiddle.net/3y539gvq/ – arukiri123 2014-10-07 17:55:02