Alling a menu to the right of a button positioned in a table column

I have a webpage with a table in which the last column is a button that opens a dropdown menu (for simplicity, the example does not include JS):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<style>
.butonMenu {
display: inline-flex;
align-items: center;
border-width: 1px;
padding: 0.25rem;
text-align: center;
font-size: 1rem;
line-height: 1.25rem;
}
.menu{
position: absolute;
z-index: 10;
margin-top: 0.25rem;
display: grid;
width: max-content;
max-width: 32rem;
grid-auto-flow: row;
gap: 0.25rem;
background-color: whitesmoke;
}
.menuOption {
display: inline-flex;
width: 100%;
align-items: center;
padding: 0.25rem;
text-align: center;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 500;
}
</style>
</head>
<body>
<table
class="shadow-3xl sm:rounded-lg border border-neutral-200 text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<tr>
<th>Col1</th>
<th>Col2</th>
<th></th>
</tr>
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-blue-100">
<td class=" w-20">Val1</td>
<td class=" w-20">Val2</td>
<td><button type="button" title="Mai multe optiuni" class="butonMenu">...</button>
<div class="menu">
<button type="button" class="menuOption ">Option1</button>
<button type="button" class="menuOption ">Option2</button>
<button type="button" class="menuOption ">Option3</button>
</div>
</td>
</tr>
</table>
</body>
</html>
</code>
<code><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdn.tailwindcss.com"></script> <style> .butonMenu { display: inline-flex; align-items: center; border-width: 1px; padding: 0.25rem; text-align: center; font-size: 1rem; line-height: 1.25rem; } .menu{ position: absolute; z-index: 10; margin-top: 0.25rem; display: grid; width: max-content; max-width: 32rem; grid-auto-flow: row; gap: 0.25rem; background-color: whitesmoke; } .menuOption { display: inline-flex; width: 100%; align-items: center; padding: 0.25rem; text-align: center; font-size: 0.875rem; line-height: 1.25rem; font-weight: 500; } </style> </head> <body> <table class="shadow-3xl sm:rounded-lg border border-neutral-200 text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400"> <tr> <th>Col1</th> <th>Col2</th> <th></th> </tr> <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-blue-100"> <td class=" w-20">Val1</td> <td class=" w-20">Val2</td> <td><button type="button" title="Mai multe optiuni" class="butonMenu">...</button> <div class="menu"> <button type="button" class="menuOption ">Option1</button> <button type="button" class="menuOption ">Option2</button> <button type="button" class="menuOption ">Option3</button> </div> </td> </tr> </table> </body> </html> </code>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        .butonMenu {
            display: inline-flex;
            align-items: center;
            border-width: 1px;
            padding: 0.25rem;
            text-align: center;
            font-size: 1rem;
            line-height: 1.25rem;
        }

        .menu{
            position: absolute;
            z-index: 10;
            margin-top: 0.25rem;
            display: grid;
            width: max-content;
            max-width: 32rem;
            grid-auto-flow: row;
            gap: 0.25rem;
            background-color: whitesmoke;
        }

        .menuOption {
            display: inline-flex;
            width: 100%;
            align-items: center;
            padding: 0.25rem;
            text-align: center;
            font-size: 0.875rem;
            line-height: 1.25rem;
            font-weight: 500;
        }
    </style>
</head>

<body>
    <table
        class="shadow-3xl sm:rounded-lg border border-neutral-200 text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
        <tr>
            <th>Col1</th>
            <th>Col2</th>
            <th></th>
        </tr>
        <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-blue-100">
            <td class=" w-20">Val1</td>
            <td class=" w-20">Val2</td>
            <td><button type="button" title="Mai multe optiuni" class="butonMenu">...</button>
                <div class="menu">
                    <button type="button" class="menuOption ">Option1</button>
                    <button type="button" class="menuOption ">Option2</button>
                    <button type="button" class="menuOption ">Option3</button>

                </div>
            </td>
        </tr>
    </table>
</body>

</html>

This is what menu looks like:

And this is what I want it to look like:

I want the right side of the menu to aling with the right side of the button or the cell in which the button is situated.

I could achieve this effect by adding a negative margin to the menu class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>margin-left: -40px;
</code>
<code>margin-left: -40px; </code>
margin-left: -40px;

The problem with this approach is that the values of the menu options are dynamic, and they can change from page to page.

I know I can monitor the width of the menu with JS and update the CSS, but I wondered if there is a more simple and elegant solution.

If this is impossible using only CSS, I am open to simple JS methods (I used the
ResizeObserver object from Vue.js to monitor the width but it feels to complicated for such a minute problem)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật