2011-11-15 14 views
9

下面是一些CSS和HTML,使下面的數據點列表的文本區域:製作值列表排隊用一個textarea和標籤

form label { 
    width: 140px; 
    float: left; 
} 
form ol li { 
    background: #98c8dc; 
    list-style: none; 
    padding: 5px 10px; 
} 

<form> 
<ol> 
<li> 
    <label><br/><br/><br/><br/>Recent data</label> 
    <ol> 
    <li>3 99</li> 
    <li>5 98</li> 
    <li>15 97</li> 
    <li>28 96</li> 
    </ol> 
</li> 
<li> 
    <label>New data</label> 
    <textarea placeholder="30 95" rows="4"></textarea> 
</li> 
</ol> 
</form> 

它呈現這樣的:

enter image description here

你會如何建議我把它排隊恰到好處? 也就是說,「最近的數據」應該與「28 96」一致,也許最棘手的是,儘管在文本區域中,「30 95」應該排隊,就好像它只是另一排在「28 96」 」。

+2

誰能告訴我的賞金禮儀?我想把它交給@Bee,以便在我提出要求的24小時內跳進來並釘住它。沒有人在提供的內容方面對她的回答進行了改進,但是它在代碼的優雅性方面一直處於領先地位。例如,我能否將最優雅的解決方案作爲接受的解決方案,但是根據答案的速度來獎勵賞金?我的意思是,我知道我可以,但是stackoverflow精神中最重要的是什麼? (披露:Bee和我是現實生活中的共同黑客) – dreeves

+1

據我所知,賞金可以拆分並且不會被接受的答案 – HerrSerker

+1

在您的HTML代碼中,您不應該爲最近的數據使用標籤,因爲沒有相應的表單元素。與下面的標籤具有完全相同樣式的跨度或內聯p應該沒問題(我的首選命名是類.label_like ^^)。並且您應該照常爲與標籤關聯的表單元素添加label和textarea之間的for/id關聯。 – FelipeAls

回答

3

這是一個很好的情況下,對CSS的定位。具有position:absolute的元素相對於它們最接近的位置親本定位於。這意味着我們可以使用標籤上的<ol>position:absolute上的position:relative將標籤錨定到其容器的頂部/左側。

這裏舉例:http://jsfiddle.net/YhQYS/1/

HTML

<form action="." method="post"> 
    <ol> 
     <li class="recent-data"> 
      <strong>Recent data</strong> 
      <ol> 
       <li>3 99</li> 
       <li>5 98</li> 
       <li>15 97</li> 
       <li>28 96</li> 
      </ol> 
     </li> 
     <li class="new-data"> 
      <label>New data</label> 
      <textarea placeholder="30 95" rows="4"></textarea> 
     </li> 
    </ol> 
</form> 

CSS

form > ol { 
    background: #98c8dc; 
    font-family: serif; 
} 
.recent-data > ol, 
.new-data > ol { 
    list-style: none; 
    padding: 5px 10px 5px 0; 
    line-height:20px; 
} 
.recent-data li { padding-left:5px; } 
.recent-data, 
.new-data { 
    position:relative; 
    padding-left:140px; 
} 
.recent-data strong, 
.new-data label { 
    position:absolute; 
    left:10px; 
    line-height:20px; 
} 
.recent-data strong { bottom:5px; } 
.new-data label { top:5px; } 
.new-data textarea { 
    font-family:serif; 
    font-size:100%; 
    padding:4px; 
} 

這是很簡單的推理,以及可靠的跨瀏覽器。請注意,您不應使用沒有對應表格控件的<label>


的東西看起來像表格數據...這是你的選擇,我們沒有足夠的上下文知道加價是比較合適的。因此,這裏是一個語義上更正確的方法使用表,rowspanvertical-align

HTML

<form action="." method="post"> 
    <table id="results"> 
     <tbody> 
      <tr> 
       <th rowspan="4" scope="row" class="recent-label">Recent data</th> 
       <td>2</td> 
       <td>47</td> 
      </tr> 
      <tr> 
       <td>3</td> 
       <td>99</td> 
      </tr> 
      <tr> 
       <td>5</td> 
       <td>98</td> 
      </tr> 
      <tr> 
       <td>5</td> 
       <td>98</td> 
      </tr> 
      <tr> 
       <th rowspan="1" scope="row" class="new-label">New data</th> 
       <td colspan="2" class="new-data"> 
        <textarea>23</textarea> 
       </td> 
      </tr> 
     </tbody> 
    </table> 
</form> 

CSS

form { 
    background: #98c8dc; 
    font-family: serif; 
} 
#results th, 
#results td { 
    padding:3px 5px; 
} 

#results .recent-label { 
    vertical-align:bottom; 
} 
#results .new-label { 
    vertical-align:top; 
} 
#results .new-data { 
    padding-left:0px; 
} 
#results textarea { 
    padding:4px; // +1px border 
    font-size:100%; 
    font-family:serif; 
} 

樣品在http://jsfiddle.net/quqf8/1/

2

textarea中刪除邊框和填充並將其設置爲相同的字體。

粗略地說,http://jsfiddle.net/vP7As/1/

+0

啊,謝謝你的jsfiddle鏈接!但我不認爲這是有效的。在textarea上沒有邊界看起來很糟糕,你不覺得嗎? – dreeves

+0

是真的,你總是可以將邊框設置爲1px,然後用1px爲其他項目添加邊距。 –

5

垂直對齊不聯元素工作,但確實在表工作(more details on vertical-align)。

所以,這裏有一個解決方案:我只是將第一個li中的兩個主要元素封裝在一個表格行中,並將垂直對齊設置爲底部以強制「最近的數據」標籤位於底部。 (有可能是使用display屬性將li從一個內聯元素改變一些方式?)

您還需要調整對ol填充和label S和table標籤讓一切都行了。在現實生活中,您可能使用某種重置CSS來規範所有這些不同元素的默認樣式規則,因此您可能需要進行不同的調整才能使所有內容完美排列,但以下是我想到的。

Altogether now

現在總共:

<style> 
table,tbody,tfoot,thead,tr,th,td { 
    margin:0; 
    padding:0; 
    border:0; 
    outline:0; 
    font-size:100%; 
    vertical-align:baseline; 
    background:transparent} 
body{line-height:1} 
ol,ul{list-style:none} 
table {border-spacing: 0px;} 
table td{ 
    vertical-align: bottom; 
} 

* { 
    font-size: 14px; 
    font-family: Times New Roman, serif; 
} 
form label { 
    width: 140px; 
    float: left; 
} 
form ol li { 
    background: #98c8dc; 
    list-style: none; 
    padding: 5px; 
    margin-bottom: 2px; 
} 
form ol li:last-child { 
    margin-bottom: 0px; 
} 
form li label{ 
    padding: 4px 0 4px 0; 
} 
</style> 

<form> 
<ol> 
<li> 
<table><tr><td> 
    <label>Recent data</label> 
</td><td> 
    <ol> 
    <li>3 99</li> 
    <li>5 98</li> 
    <li>15 97</li> 
    <li>28 96</li> 
    </ol> 
</td> 
</tr></table> 
</li> 
<li> 
    <label>New data</label> 
    <textarea placeholder="30 95" rows="4"></textarea> 
</li> 
</ol> 
</form> 
0

一些JQuery的

您可以使用列表元素數量的jQuery動態設置標籤填充。

http://jsfiddle.net/uSehQ/2/

<script type="text/javascript"> 
$(document).ready(function() { 
    var size = $("#data").children().size(); 
    var top = 5+30*(size-1); 
    $("#label").css("padding", top+"px 0 0 0") 
}); 
</script> 
<style> 
form label { 
    width: 140px; 
    float: left; 
} 
form ol li { 
    background: #98c8dc; 
    list-style: none; 
    padding: 5px 10px; 
} 
</style> 
<form> 
<ol> 
<li> 
    <label id="label">Recent data</label> 
    <ol id="data"> 
    <li>3 99</li> 
    <li>5 98</li> 
    <li>15 97</li> 
    <li>20 25</li> 
    <li>30 95</li> 
    </ol> 
</li> 
<li> 
    <label>New data</label> 
    <textarea placeholder="40 76" rows="4"></textarea> 
</li> 
</ol> 
</form> 
2

這裏是一個解決方案。 no tableno jquery,只是在第一lable

Demo in fiddle

在Safari 15,火狐6和IE8測試添加一個p標籤。

我想你應該設置一個line-weigh爲計算。在例子中,我設置了18px,這樣<ol><li>3 99...</ol>的整個高度就是(18+5+5)*4=112px(兩個5px爲你的li:填充高度),然後添加一個p標籤,方便控制position

HTML部分:

<form> 
<ol> 
<li> 
    <label><p>Recent data</p></label> 
    <ol> 
    <li>3 99</li> 
    <li>5 98</li> 
    <li>15 97</li> 
    <li>28 96</li> 
    </ol> 
</li> 
<li> 
    <label>New data</label> 
    <textarea placeholder="30 95" rows="4"></textarea> 
</li> 
</ol> 
</form> 

CSS部分:

form{font:14px/18px arial;} 

form label { 
    width: 140px; 
    float: left; 
} 
form>ol>li>label { 
    height: 112px; 
    position:relative; 
} 
form>ol>li>label>p { 
    position:absolute; 
    bottom:5px; /* 5px for the last li(28.96) padding-bottom:5px */ 
    left:0; 
} 
form ol li { 
    background: #98c8dc; 
    list-style: none; 
    padding: 5px 10px; 
} 
2

試試這個: 使用一些position: reltive;你可以使它全部排隊。

<html> 
<head> 
<style type="text/css"> 
form { 
background: #98c8dc; 
padding: 5px 10px; 
} 
form label { 
width: 140px; 
float: left; 
position: relative; 
bottom: 20px; 
display: block; 
} 
form label span { 
display: block; 
} 
form ol li { 
display: table-row; 
list-style: none; 
} 
#area { 
position: relative; 
right: 5px; 
} 
</style> 
</head> 
<body> 
<form> 
<label>&nbsp;</label> 
<ol> 
<li>3 99</li> 
<li>5 98</li> 
<li>15 97</li> 
<li>28 96</li> 
</ol> 
<label><span>Recent data</span>New data</label> 
<textarea id="area" placeholder="30 95" rows="4"></textarea> 
</li> 
</ol> 
</form> 
</body> 
</html> 

但是它看起來像你與表格數據的工作,在這種情況下,只需使用一個表很可能會做到這一點的最好辦法。

3

如果你想讓東西像表格一樣行爲,就像表格一樣顯示它。使用CSS的table,table-rowtable-cell作爲元素的display屬性。然後,您可以使用垂直對齊和其他表'唯一'的東西。請注意,我使用>作爲CSS選擇器,因此內部列表也不會獲得這些樣式。

http://jsfiddle.net/nQWBw/2/

CSS:

.table-like { 
    display: table; 
    border: 1px solid red; 
    background: #98c8dc; 
    margin: 0; 
    padding: 0; 
} 
.table-like > LI { 
    display: table-row; 
    border: 1px solid lime; 
    list-style: none; 
    margin: 0; 
    padding: 0; 
} 
.table-like > LI > LABEL , 
.table-like > LI > OL , 
.table-like > LI > TEXTAREA { 
    display: table-cell; 
    border: 1px dashed magenta; 
    /* padding: 5px 10px; */ 
    vertical-align: top; 
} 
.table-like LI:first-child > *{ 
    vertical-align: bottom; 
} 
.table-like > LI > OL > LI { 
    display: block; 
    margin: 0; 
    padding: 0; 
} 

HTML:

<form> 
    <ol class="table-like"> 
     <li> 
      <label>Recent data</label> 
      <ol> 
       <li>3 99</li> 
       <li>5 98</li> 
       <li>15 97</li> 
       <li>28 96</li> 
      </ol> 
     </li> 
     <li> 
      <label>New data</label> 
      <textarea placeholder="30 95" rows="4"></textarea> 
     </li> 
    </ol> 
</form> 
1

垂直對齊可與inline-block元素
http://jsfiddle.net/s3ayp/


<!-- HTML --> 
<form> 
    <ol> 
    <li class="lineup lineup-bottom"> 
     <label >Recent data</label> 
     <ol> 
     <li class="first">3 99</li> 
     <li>5 98</li> 
     <li>15 97</li> 
     <li class="last">28 96</li> 
     </ol> 
    </li> 
    <li class="lineup lineup-top"> 
     <label>New data</label> 
     <ol> 
     <li> 
      <textarea placeholder="30 95" rows="4"></textarea> 
     </li> 
     </ol> 
    </li> 
    </ol> 
</form> 

/* CSS */ 
.lineup > * { 
    display: -moz-inline-box; /* FF 2 */ 
    display: inline-block; 
} 
*:first-child + html .lineup > * { /* IE7 hack */ 
    display: inline; 
} 

.lineup.lineup-top > * { 
    vertical-align: top; 
} 

.lineup.lineup-bottom > * { 
    vertical-align: bottom; 
} 
form label { 
    width: 140px; 
    vertical-align: bottom; 
} 
ol { 
    background: #98c8dc; 
    list-style: none; 
} 
ol li { 
    padding: 5px 10px; 
} 
ol li.first { 
    padding-top: 0 
} 
ol li.last { 
    padding-bottom: 0 
}