开发者

HTML Dropdown list item background colors

开发者 https://www.devze.com 2023-02-24 21:46 出处:网络
I have a dropdown menu 开发者_运维技巧with items and I wanted to use the ODD and EVEN to alternate the colors.

I have a dropdown menu 开发者_运维技巧with items and I wanted to use the ODD and EVEN to alternate the colors. My question is...What's the CSS code for styling Dropdown list items please?


Use the CSS nth-child pseudo-class:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>HTML Dropdown list item baground colors</title>
    <style type="text/css">
        #myForm select option:nth-child(odd) {
            color:black;
            background:yellow;
        }
        #myForm select option:nth-child(even) {
            color:white;
            background:blue;
        }
        #myForm select {
            background:green;
            color:orange;
        }
    </style>
</head>
<body>
    <form id="myForm" action="#" method="get">
        <select>
            <option>A</option>
            <option>B</option>
            <option>C</option>
            <option>D</option>
            <option>E</option>
            <option>F</option>
        </select>
    </div>
</body>
</html>


You can set different background as:

#o1{background:red;}
#o2{background:green;}
#o3{background:#FFEA76;}

<option id="o1">blah 1</option>
<option id="o2">blah 2</option>
<option id="o3">blah 3</option>


You can set a default style for the list items and then apply a more specific style to change even items. The odd items will receive the default style.

.nav{
  padding: 0;
  list-style: none;
}
.nav li{
   color: #c1a404;
   background: #007e96;
   padding: 10px;
   font-size: 20px;
   font-family: Futura, sans-serif;
}
.nav li:nth-child(even) {
   color: white;
   background: dodgerblue;
}
<ul class="nav">
  <li>Home</li>
  <li>About Us</li>
  <li>News</li>
  <li>Careers</li>
  <li>Contact</li>
</ul>

0

精彩评论

暂无评论...
验证码 换一张
取 消