What is the best way to create this kind of dropdown menu?

How would I make the item of the list adjust its position depending on how long the length of values of previous item of the list?

In this case „Steuerrecht“ is moving down if you tap on „Arbeits- und Sozialrecht“ and returning to its previous position after we tap „Steuerrecht“ again. How could I achieve it?

Nevermind, thank you.

@Cecilia can you kindly answer this one for me, I have a menu and would like to have sub categories that appear when one of the menu categories is clicked on.