Is the position relative malfunctioning when using display table-cell?

I have designed a horizontal menu consisting of buttons that I want to resize in width so that together they occupy 100% of the menu container. I need them to function like TD elements inside a TABLE.

Here is the code snippet I have developed:

<div id="menubar">
    <div id="menu">
        <div class="button">
            <Button>Button 1</Button>
        </div>
        <div class="button">
            <Button>Button 2</Button>
        </div>
        <div class="button">
            <Button>Button 3</Button>
        </div>
        <div class="button">
            <Button>Button 4</Button>
        </div>
    </div>
</div>

Here is the corresponding CSS:

#menubar {
    width: 100%;
    height: 100%;
    display: table;
    table-layout: fixed;
}

#menu {
    display: table-row;
}

#menu .button {
    position: relative;
    display: table-cell;
}

#menu .button Button {
    position: absolute;
    right: 0px;
    bottom: 0px;
    top: 0px;
    left: 0px;
}

While this setup works well across most browsers, it encounters an issue with Mozilla. The browser fails to recognize the relative position of the button class, causing the Buttons to overlap instead of being positioned absolutely within the "button" DIV.

Further investigation reveals that Mozilla has trouble respecting position "relative" when display is set to "table-cell."

If anyone has a workaround for achieving the desired layout, your input would be greatly appreciated.

It's worth noting that the menu content is dynamic, and the number of buttons may vary. Thus, specifying percentage widths for each button upfront is not feasible.

Answer №1

Avoid Using position: relative in Table Cells

According to the CSS specification on W3.org, using position: relative in table-cell elements is not defined.

For more information, refer to: http://www.w3.org/TR/CSS21/visuren.html#choose-position

While some browsers may interpret table cells as containing blocks for absolutely positioned child elements within them, others may ignore position: relative in table cells altogether.

This behavior may vary depending on the browser's compliance with the CSS specification.

Solution to this Issue

In such cases, it is recommended to place a block-level wrapper element within the cell with absolute positioning and then position inner child elements relative to this wrapper.

Creating Responsive Buttons in Table Cells

The following CSS code ensures that buttons scale both in width and height within table cells:

body, html {
    height: 100%; /* set page height */
}
#menubar {
    width: 100%;
    height: 100%; /* adjust height accordingly */
    display: table;
    table-layout: fixed;
}
#menu {
    display: table-row;
}
#menu .button {
    display: table-cell;
    border: 1px dotted blue;
    height: 100%; /* scale height within the table */
}
#menu .button Button {
    width: 100%;
    height: 100%; /* scale height within the table cell */
}

To ensure proper scaling, set a height for #menubar and apply height: 100% to both table-cell and button elements. This establishes a chain of inheritance from table to table-cell and button.

Try out the demonstration on Fiddle: http://jsfiddle.net/audetwebdesign/7b9h9/

Answer №2

Eliminate right, bottom, top, and left positioning for the #menu .button Button

For Example,

#menu .button Button {
    position: absolute;
    /*right: 0px;
    bottom: 0px;
    top: 0px;
    left: 0px;*/
}

Check out the DEMO IN ACTION

If you prefer using only display:table-cell;, simply remove the positioning

For Example,

#menubar {
    width: 100%;
    height: 100%;
    display: table;
    table-layout: fixed;
}
#menu {
    display: table-row;
}
#menu .button {

    display: table-cell;
}
#menu .button Button {

    right: 0px;
    bottom: 0px;
    top: 0px;
    left: 0px;
}

See the DEMO IN ACTION - 2

EDIT

To expand the buttons to fill in the width, apply this solution.

The Code:

#menu .button Button {
    width: 100%;
}

See the DEMO IN ACTION - 3

EDIT - 2

Based on the OP's request for an option to set a height for the buttons, here is the solution including height:100%;. The addition of this style is the OP's input to this solution.

#menu .button Button {
    display:table-cell;
    width: 100%;
    height:100%;
}

Check out the DEMO IN ACTION - 4

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

HTML list with clickable elements for querying the database and displaying the results in a <div> element

Patience please; I am a newcomer to StackOverflow and this is my inaugural question. Struggling with writing an effective algorithm, I wonder if my attempts to "push" it forward are causing me to complicate what should be a straightforward concept, or if i ...

Generate an image, PDF, or screenshot of a webpage with the click of a button

I've been searching for a solution that would enable users to click a button and save an image or PDF of the current page. The content on the page is dynamic and dependent on user input, resulting in sequences displayed in various colors. I'm loo ...

What is the process for inserting an image into a table using el-table and el-table-column components in Vue.js while utilizing ui-elements?

I'm new to Vue.js and successfully built a basic table using the ui-element. The el-table element was utilized for constructing the table, with columns displayed using el-table-column and prop (see code below). Now, I want to incorporate images/avatar ...

How to utilize methods from different pages in Ionic 2

Looking to display the total number of items in an array on a card located on the home screen, but facing issues referencing methods outside of the typescript file they're written in. Trying to extract the array size method and utilize it in a differe ...

Resolved issue with maintaining scroll position after adjustments to scroll height

Currently, I am in the process of creating a chatroom that includes pagination. The issue I am encountering is related to loading old messages when the scroll height reaches 0. However, unlike platforms such as Facebook and Slack, even when the scroll he ...

Encountering an Uncaught TypeError while trying to read properties of undefined (specifically 'remove') during a Change event is causing an issue for me

Looking to update the icons by changing the class from .fa-plus to .fa-minus for this specific menu section <div class="accordion-menu"> <h2 class="accordion-header" id="subseccOne"> ...

Tracking mouse positions across an HTML document

I've been working on a project inspired by the voxel painter example on the three.js page. In this particular example, the mouse coordinates are utilized to move a roll-over helper that indicates where a box will be placed upon click. mouse.set((even ...

The alignment of a background image in CSS is different from using margin:0 auto, especially when dealing with scrollbars

Check out the following link to view the issue: http://jsfiddle.net/7zb6P/1/ In a scrolling div, both the yellow box and the background image are centered, however their centers appear slightly different. The discrepancy seems to be because the background ...

jQuery not functioning properly for adjusting quantities with plus and minus buttons

Hey there, I've been trying to create a number input increment using jQuery but haven't had any luck so far. Could you please take a look at my code and provide some guidance? CHECK OUT THE DEMO HERE Here's the HTML: <div class="sp-qua ...

In JavaScript, alert a message once all images have been clicked

I'm encountering a small issue with my javascript code. I am developing a game for a school project where the objective is to click (remove) fish using a fishing rod. However, the game does not have an end condition set up, so players cannot win. Belo ...

Modifying the color of a specific node in jstree

I am attempting to modify the background color of the node I have selected in jstree. $('#syncrep').jstree({ 'core' : { 'data' : repository ...

spacing between text and bottom border

Is there a way to add space between text and the border below, as shown in the image linked? I'd like the space to be 5px with a font-size of 15px. My attempt: .selected { color: #284660; } .selected:after { content: ''; position: a ...

The standard color code applied to an HTML button

I need to create an anchor element <a> that resembles an HTML button. However, I am struggling to find the default background color for the input type button. ...

Troubleshooting Vue.js rendering problems on Safari_BROWSER

I'm encountering a strange issue with my portfolio website, which is built using Vue and Laravel. The problem arises specifically in Safari - the project thumbnails don't display until the browser window is resized. Here's the relevant code ...

Pick out grouped objects without the use of Javascript

I am looking to design a single page where the display of categorized items changes based on user selection. Ideally, I want this to be chapter/hash (:target) dependent, but checkboxes influencing state would also work. As an experiment, I am trying to ac ...

Animating multiple elements in Angular 2 using a single function

Currently, I am delving into Angular and faced a challenge while attempting to create a toggle categories menu. Within my navbar component, I have an animation trigger set up as follows: trigger('slideCategory', [ state('opened&apo ...

Find your way to a unique div created through a while loop

Describing my issue clearly, I am using PHP to retrieve data from MySQL. The table contains multiple records. To display table records, I have implemented a while loop with mysqli_fetch_array() as the condition. These records need to be echoed out (in HT ...

What is causing the initial activation of the <button> within a form?

Within my <form>, I have included 2 submit buttons. The first button looks like this: <button class="regular" id="geocodesubmit" style="height:40px;">Set Location</button> The second button looks like this: <button type="submit" ...

The angular content is not scrolling

I have a basic angular content window that contains an adjustable group of settings values. When the window is collapsed, the fxLayout collapses properly, but I am having difficulty scrolling vertically. I have attempted to use overflow-y and just overflow ...

Can Django implement pagination without altering the URL structure?

I discovered the fundamentals of pagination in Django at this resource: Nevertheless, I am faced with a challenge - I wish to implement pagination without modifying the URL. Specifically, I need to paginate a single table among multiple tables on an HTML ...