radio-container { position: relative; height: 4em; /* 3em (being the max-height of the inner container) + 1em ("margin") */ } .radio-container:hover { z-index: 9999; } .radio-options { position: absolute; max-height: 3em; width: 100%; overflow: hidden; transition: 0.7s; } .radio-options:hover { max-height: 100em; } .radio-options.toggle { position: relative; cursor: pointer; padding: 0.75em; background: darkgreen; border-radius: 10px; z-index: 1; } /* li are stacked at the same position as .toggle, only .toggle is visible */ .radio-optionsli { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .radio-optionslabel { display: block; opacity: 0; transition: 0s; }
We hide the inputs, we could just use display : none, but that would not work in browsers (some mobile ones) where clicking the label does not focus the associated input.
/* li elements have a normal flow within the .radio-options container */ .radio-options:hoverli { position: relative; } .radio-options:hoverlabel { opacity: 1; transition: 0.5s; }
4.选中时 radio checked 但鼠标没触发时
1 2 3 4 5 6 7 8 9 10 11 12 13
.radio-optionsinput:checked ~ label { position: absolute; top: 0; left: 0; right: 0; opacity: 1; /* is above the .toggle so is visible */ z-index: 2; /* has tha same styles as .toggle */ padding: 0.75em; background: darkgreen; border-radius: 10px; }