2015-01-08 2 views
8

私はブロック内のページに4つのスパンを入れています。それぞれのコンテンツは、私のビューモデルからのノックアウトによって設定されます。コンテンツがいくつかのスパンで空の場合もあります。私は素敵なコンマ区切りの方法でそれらを表示し、空の可能性を考慮したいと思います。純粋なCSSでカンマ区切りにされた

私は次のHTMLとCSSを試しました。

VERSION 1

これは、最後のスパンが空である場合は、(視覚)、最後のコンマを示す空のスパンのためのカンマも

.comma:not(:last-child):after { 
 
    content: ", "; 
 
}
<span class="comma">A</span> 
 
<span class="comma">B</span> 
 
<span class="comma"></span> 
 
<span class="comma">D</span>

VERSION 2

を示します。私はそれは、正しく常にギャップが(もしあれば)あるどんなにをレンダリングするために微調整することができる方法

.comma:not(:empty):not(:last-child):after { 
 
    content: ", "; 
 
}
<span class="comma">A</span> 
 
<span class="comma">B</span> 
 
<span class="comma">C</span> 
 
<span class="comma"></span>

? 最新のブラウザー(IE9 +など)のみをサポートする必要があります。

+0

あなたは正確にA、B、C、Dをどのようなフォーマットを必要としていますか? – Chris

+0

あなたの2番目の解決策は、私にとってはフィドルでうまくいくようですか? –

+0

ああ、いや、待たないよ。 –

答えて

8

私はbefore pseudoelementのコンテンツとしてカンマを置く、ロジックを逆転

例:http://codepen.io/anon/pen/NPdbbw

/* remove extra space before the comma */ 
.comma:not(:first-child) { 
    margin-left: -.3em; 
} 

/* no need to display empty elements */ 
.comma:empty { 
    display: none; 
} 

.comma:not(:first-child):before { 
    content: ", "; 
} 

最初.comma要素を空にすることができ、また場合は、ここでより複雑ですアプローチ

http://codepen.io/anon/pen/BypQRd

.comma:not(:first-child) { 
    margin-left: -.3em; 
} 

/* next 2 rules are for spacing when the first .comma is empty too */ 
.comma:first-child:empty ~ .comma:not(:empty) { 
    margin-left: 0; 
} 

.comma:first-child:empty ~ .comma:not(:empty) ~ .comma:not(:empty) { 
    margin-left: -.3em; 
} 

.comma:empty { 
    display: none; 
} 

.comma:not(:first-child):before { 
    content: ", "; 
} 

.comma:not(:first-child):before { 
    content: ", "; 
} 

.comma:empty + .comma:not(:empty):before { 
    content : ""; 
} 

.comma:not(:empty) ~ .comma:empty + .comma:not(:empty):before { 
    content : ", "; 
} 

最後の例では、正常MDN

+0

いいえ、しかし、あなたは、空白のものを隠しました;) – mattytommo

+0

よく ':empty'はIE9からサポートされているので、正当なようです:D – fcalderan

+1

これは最初のスパンが空ではないと仮定します。 – Alex

0

スイッチそのラウンドで利用可能な4つの要素


さらなる情報について:empty疑似クラスのすべての可能な組み合わせに対してテストされています。代わりにあなたのスパンの後にコンマを置くと、先行され前に各スパン(任意の距離バックを!)それを置く非空のスパンによって:

.comma:not(:empty) ~ .comma:not(:empty):before { 
    content: ", "; 
} 

Fiddle

+0

これは醜い空間を残す。 –

+0

これをFabrizioの 'margin-left:-.3em; 'と組み合わせることはすべきことです。 – Alex

0

http://jsfiddle.net/xtrabhwh/3/

<span class="comma">A</span><span class="comma">B</span><span class="comma">C</span><span class="comma"></span> 

.comma ~ .comma:not(:empty):before { 
    content: ", "; 
} 
+1

がすべて1行にある理由は、各スパンの後にスペースが追加されます(nl文字がある場合)。 –

0

以下は、何かを隠すことなく、どんな組み合わせであってもそうです。

.comma:before { 
 
    content: ',' 
 
} 
 
.comma:empty:before, 
 
.comma:first-of-type:before, 
 
.comma:first-of-type+.comma:empty:before, 
 
.comma:empty+.comma:not(:last-of-type):before, 
 
.comma:empty+.comma:empty:before { 
 
    content: ''; 
 
}
<h4>Missing first</h4> 
 
<div> 
 
    <span class="comma"></span> 
 
    <span class="comma">item</span> 
 
    <span class="comma">item</span> 
 
    <span class="comma">item</span> 
 
</div> 
 
<h4>Missing last</h4> 
 
<div> 
 
    <span class="comma"></span> 
 
    <span class="comma">item</span> 
 
    <span class="comma">item</span> 
 
    <span class="comma">item</span> 
 
</div> 
 
<h4>Missing first two</h4> 
 
<div> 
 
    <span class="comma"></span> 
 
    <span class="comma"></span> 
 
    <span class="comma">item</span> 
 
    <span class="comma">item</span> 
 
</div> 
 
<h4>Missing last two</h4> 
 
<div> 
 
    <span class="comma">item</span> 
 
    <span class="comma">item</span> 
 
    <span class="comma"></span> 
 
    <span class="comma"></span> 
 
</div> 
 
<h4>Missing middle</h4> 
 
<div> 
 
    <span class="comma">item</span> 
 
    <span class="comma">item</span> 
 
    <span class="comma"></span> 
 
    <span class="comma">item</span> 
 
</div> 
 
<h4>Missing middle two</h4> 
 
<div> 
 
    <span class="comma">item</span> 
 
    <span class="comma"></span> 
 
    <span class="comma"></span> 
 
    <span class="comma">item</span> 
 
</div>

+1

これはきれいですが、最後の2つの要素が空の場合は余分なカンマが表示されます – fcalderan

+0

@FabrizioCalderan - リビジョンとユースケースを参照してください - 改訂されたルールはそれぞれを渡します – SW4

+0

ニースですが...ほとんど:Dのみの場合は余分なカンマが表示されます。とにかくいい仕事。 – fcalderan

関連する問題