其中包含UL的下拉 - >应该因列表项的数量而扩展

I have a very specific problem with the div in a dropdown.

The <ul> within the dropdown is created by a php-foreach function and the amount of <li> varies.

The width of the dropdown should expand if there are more than one <li>, but not more than four in a row. I have to have margin left/right of 20px, a margin top of 12px and a margin between the <li> of 10px.

I already built it with a fixed width for four or more than four <li> (with nth-child etc) but now I have the order to make it smaller if there are only one or two items in it.

I tried different things but nothing seems to work.

Has anyone an idea how to do this? Thanks in advance!