Changing CSS for last <li>
Question
I am wondering if there is some way to change a CSS attribute for the last
li
in a list using CSS. I have looked into using
:last-child
, but this seems really buggy and I can't get it to work for me. I will use JavaScript to do this if necessary, but I want to know if anyone can think up a solution in CSS.
How to solved?
:last-child
is really the only way to do it without modifying the HTML - but assuming you can do that, the main option is just to give it a
class="last-item"
, then do:
li.last-item { /* ... */ }
Obviously, you can automate this in the dynamic page generation language of your choice. Also, there is a
lastChild
という JavaScript プロパティが W3C DOM にあります。
でやりたいことをやる例です。 プロトタイプ :
$$("ul").each(function(x) { $(x.lastChild).addClassName("last-item"); });
あるいはもっとシンプルに
$$("ul li:last-child").each(function(x) { x.addClassName("last-item"); });
で jQuery であれば、さらにコンパクトに書くことができます。
$("ul li:last-child").addClass("last-item");
また、この
が必要です。
を使わなくても動作します。
last-child
CSS セレクタを使用せずに動作します - むしろ、JavaScript の実装が使用されます - そのため、バグが少なく、ブラウザ間でより信頼できるはずです。
関連
-
iframeフレームワークの使用
-
[解決済み] HTMLのテーブルからボーダーを完全に削除する方法
-
[解決済み] CSSの親セレクタはありますか?
-
[解決済み] CSSでcellpaddingとcellspacingを設定する?
-
[解決済み] CSSのdisplayプロパティで遷移する
-
[解決済み] HTMLの "role "属性は何のためにあるのですか?
-
[解決済み] CSSでdivのアスペクト比を維持する
-
[解決済み] inline/inline-block要素間のスペースを削除するにはどうすればよいですか?
-
[解決済み] HTMLのid属性とname属性の違い
-
[解決済み】HTML5入力のプレースホルダの色をCSSで変更する。
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
[解決済み] 横型リストアイテム
-
[解決済み] CSSでテキストや画像の背景を透明にするには?
-
[解決済み] <button> vs. <input type="button" />. Which to use?
-
[解決済み] How can I set the default value for an HTML <select> element?
-
[解決済み] リンクを新しいタブまたはウィンドウで開く[重複]。
-
[解決済み] mailto:を使ってメールの件名や内容を設定することはできますか?
-
[解決済み] Flexbox:水平方向と垂直方向の中央揃え
-
[解決済み] HTMLのid属性とname属性の違い
-
[解決済み] ラジオボタンをデフォルトで選択するには?[重複しています]
-
[解決済み] HTML Input="file" Accept Attribute File Type (CSV)