The height of this div will never exceed that of its parent div

After multiple attempts and reviewing some solutions, I'm still struggling to align the text properly within the parent div and extend the white separators to match the full length of the parent.

https://jsfiddle.net/oxgg6qrf/ <-- This is the code I've been working on, but I can't figure out why the separator divs are not extending correctly.

Here's the code snippet:

<div id="ambMenu">
    <ul id="ulMenu">
        <li id="liMenuBar1"><div class="ambMenuDivider"></div></li>
        <li id="liMenuOption1">Test1</li>
        <li id="liMenuBar2"><div class="ambMenuDivider"></div></li>
        <li id="liMenuOption2">Test2</li>
        <li id="liMenuBar3"><div class="ambMenuDivider"></div></li>
        <li id="liMenuOption3">Test3</li>
        <li id="liMenuBar4"><div class="ambMenuDivider"></div></li>
    </ul>
</div>

#ambMenu { width: 360px; height: 25px; background-color: #333; color: #f2f2f2; }
#ulMenu { list-style-type: none; margin: 0; padding: 0; display: inline-block; }
#liMenuBar1 { padding-left: 30px; float: left; display: inline-block; margin: 0; }
#liMenuBar2, #liMenuBar3, #liMenuBar4 { float: left; display: inline-block; margin: 0; padding-left: 10px; padding-right: 5px; }
#liMenuOption1, #liMenuOption2, #liMenuOption3 { float: left; display: inline-block; margin: 0; vertical-align: middle; }
.ambMenuDivider { border-left:5px solid #f2f2f2; display: inline; padding-left: 5px; padding-right: 5px;}

Although there are many ids in the code, they are necessary for a future JavaScript script.

Is there a more efficient way to create a menu like this with consistent alignment? What am I overlooking?

Answer №1

Your main container has a set height of 25px:

#ambMenu { 
    width: 360px; 
    height: 25px; /* height of container */
    background-color: #333; 
    color: #f2f2f2;
}

However, the child container does not have a specified height. To fix this, make sure to give the child container the same height as the parent container:

#ulMenu {
    list-style-type: none; 
    margin: 0; 
    padding: 0;
    height: 25px; /* new */
}

To vertically center the text, add a line-height to the list items that matches the container's height:

#liMenuBar1 { 
    padding-left: 30px; 
    float: left; 
    display: block; 
    margin: 0;
    line-height: 25px; /* new */
}

#liMenuBar2, #liMenuBar3, #liMenuBar4 { 
    float: left; 
    display: block; 
    margin: 0; 
    padding-left: 10px; 
    padding-right: 5px;
    line-height: 25px; /* new */
}

#liMenuOption1, #liMenuOption2, #liMenuOption3 {
    float: left; 
    display: block; 
    margin: 0; 
    vertical-align: middle;
    line-height: 25px; /* new */
}

.ambMenuDivider { 
    border-left:5px solid #f2f2f2; 
    display: inline; 
    padding-left: 5px; 
    padding-right: 5px;
    line-height: 25px; /* new */
}

After these adjustments, the text should now be centered in the container.

To make sure the menu dividers stretch the entire height, follow these steps:

  1. Apply height: 25px
  2. Change display: inline to display: inline-block *

    .ambMenuDivider { 
        border-left:5px solid #f2f2f2; 
        display: inline-block; /* adjusted */
        padding-left: 5px; 
        padding-right: 5px;
        line-height: 25px; /* new */
        height: 25px; /* new */
    }
    

DEMO: https://jsfiddle.net/oxgg6qrf/2/

* You cannot apply height to inline elements..

Answer №2

Check out this functional demonstration

The key adjustment made was inserting height: 25px; after eliminating unnecessary CSS classes.

There is room for improvement, but it aligns with your current requirements. I eliminated the unnecessary classes as requested.

Answer №3

Here is an alternative solution to address the problem at hand. Kindly update the specified IDs with the following:

#navList {
        list-style-type: none;
        margin: 0;
        padding: 0;
        margin-left: 14px;
    }

#navList {
    list-style-type: none;
    margin: 0;
    padding: 0;
    margin-left: 14px;
}

Answer №4

Another option is to add a right-hand border to each menu item and a left-hand border only to the first item.

This eliminates the need for separate div elements for menu dividers, making everything simpler.

Sample HTML:

<div id="newMenu">
    <ul id="listMenu">
        <li id="item1">Option 1</li>
        <li id="item2">Option 2</li>
        <li id="item3">Option 3</li>
    </ul>
</div>

CSS Style:

#newMenu {
    width:300px;
    height:30px;
    background-color:#444;
    color:#eee;
}

#listMenu {
    list-style-type:none;
    margin:0;
    padding:0;
}

#listMenu li {
    float: left;
    display: block;
    vertical-align: middle;
    line-height: 30px;
    padding: 0px 15px;
    border-right: 5px solid #eee;
}

/* Styling for the first list item */

#listMenu li:first-child {
    margin-left: 20px;
    border-left: 5px solid #eee;
}

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

Ways to incorporate text into a div container

For my assignment, I am working on setting up a menu website and I want to create a floating side div for the order summary. If you need a visual reference, you can check out this Figma design: Just to clarify, this is my first time dealing with coding s ...

horizontal alignment of row content to the right in HTML

Trying to align a text and a small image horizontally to the right: <div class="row float-right"> <div class="title" >Stream Chat</div> <img src="~assets/chat.svg" /> </div> ...

Unable to modify border color for Material-UI OutlinedInput

Having some trouble changing the border color of a v4.13 MaterialUI Outlined Input. No luck with CSS overrides. Tested multiple CSS rules targeting different elements, such as select and OutlinedInput. Here are my latest attempts. What am I missing? cons ...

Achieving the perfect horizontal alignment of images

Currently working on a new project and here's the page I'm focusing on: The elements are exceeding the set height of their container and overlapping into the content area instead of pushing the existing content down. Any suggestions to fix this ...

Creating tiles that can be easily stacked to the side

Seeking a solution to a unique layout challenge, I am hoping for some guidance. After searching and not finding exactly what I need, I turned to this platform for help. I want to create a view where multiple boxes (divs) are contained within the same paren ...

How to Customize PHP Date Format in Joomla 3

Hello, I am looking for guidance on how to customize the Date Format in Joomla 3 within mod_articles_category/default.php. The current code snippet looks like this: <?php echo $item->displayDate; ?> When rendered, it appears as a single line: ...

CSS button pressed

I came across a helpful discussion on the topic of CSS for pressed buttons, which provided some answers that I found useful. The link to the discussion can be found here: Pressed <button> CSS. Currently, I have a button within a link and I would lik ...

Ensure all division elements have the same height by utilizing a directive

My webpage currently displays a grid that is not properly arranged. However, when I executed the code below in the console, the grid was formatted correctly: var maxHeight = Math.max.apply(null, $("#activityfeeddetails").find(".course_thumb_outer").map(fu ...

Is there a way to make document flow disregard the scale attribute?

I am looking to add a unique fly-in animation using CSS, where a specific block will scale from 10 to 1. However, when I implement the scaling, a horizontal scroll bar appears: https://jsfiddle.net/r2a5w7fo/15/ body { font: 10svw sans-serif; text-ali ...

What is the best approach to implement a basic image upload functionality?

I have a vision for my new website where users can upload an image with the click of a button, store it locally on one page and then retrieve its absolute URL to share on forums or other websites for preview purposes. While I've managed to create a fu ...

Expanding the size of one div causes the surrounding divs to shift positions

I am currently working on creating a row of divs that expand when hovered over by the mouse. I have managed to achieve this functionality, but now I want the expanding div to cover its neighboring divs partially, without affecting their sizes or moving the ...

What is the best way to choose the first div when there are multiple divs with the same name?

Is there a way to target only the first div with a specific class in a series of divs with the same name using CSS? Here is an example scenario: <div class="div">dsdas</div> <div class="div">123</div> <div class="div">73</ ...

Issue encountered while trying to interpret Bootstrap 4 within Netbeans 8.2

Currently, I am working on an HTML5/JS application using Node.js in Netbeans 8.2. As I develop the welcome page, I intend to incorporate Bootstrap 4. However, when I attempt to add bootstrap.min.css to my stylesheets folder, I encounter an error as shown i ...

What is the best way to develop an expanding line animation from this starting point?

I have a vision for an animation where, upon hovering over an icon, it enlarges, increases in opacity, and reveals some accompanying text. The visual effect I am aiming for involves two lines of color expanding horizontally from the center outwards before ...

Adjust the opacity of a div as you scroll using jQuery

Check out my code snippet: http://jsfiddle.net/spadez/Fq5K4/ I am looking to create a cool effect where the image in the top section gradually turns black as the user continues scrolling down the page, until it is completely black. This example showcases ...

particular shade for button border and background

I'm looking to create a button using Material UI that has a specific design. Right now, I've only been able to achieve this: https://i.stack.imgur.com/xvv7s.png Here is the code I am currently using. If anyone can help me identify what I'm ...

Trouble with the display:none attribute in Firefox and Chrome

<tr style="height:5px" id="TRRSHeaderTrialBar" name="TRRSHeaderTrialBar" style='display:none'> <tr id="TREmail" name="TREmail" style="height:1px;" nowrap style='display:none'> Using the code snippet above to hide the bar w ...

What causes my animation to “reset” upon adding a new element using innerHTML?

One of the functionalities on my webpage involves a script that inserts a div called "doge" using innerHTML when a button is clicked. Additionally, there is a CSS keyframes animation applied to another div on the same page. However, whenever the button is ...

Ensure images are correctly aligned

Could really use some help with this issue I'm having. It's probably a simple fix for all you experts out there, but I can't seem to align my images properly so they are even and not one higher than the other. Please take a look at the scree ...

Learn how to customize button styles in ExtJS with the pressedCls configuration option

Is there a way to change the color when a button is pressed? I tried using the pressedCls config but it didn't work. How can I fix this issue or is there another method to set the CSS when a button is pressed? Thank you so much! Javascript: Ext.crea ...