Why won't divs align vertically in the center?

I'm struggling to create a navigation bar layout where the icons are centered both horizontally and vertically within their cells.

http://jsfiddle.net/digorydoo/j2v5m7gr/

I can't seem to pinpoint what's causing the issue with my current layout.

HTML:

<div class="outer-frame">
    <div class="nav-frame">
        <div class="nav-cell">
            <div class="nav-icon">&#127968;</div>
        </div>
        <div class="nav-cell">
            <div class="nav-icon">&#128138;</div>
        </div>
        <div class="nav-cell">
            <div class="nav-icon">&#127915;</div>
        </div>
    </div>
</div>

CSS:

/* box around everything */
.outer-frame {
position: relative;
/* origin for absolute pos'ed children */
margin-left: auto;
margin-right: auto;
margin-bottom: 12pt;
width: 200px;
height: 190px;
border: 1px solid #f0f0f0;
background-color: #fafafa;
}
/* grey area to the left */
.nav-frame {
    position: absolute;
    left: 0px;
    top: 0px;
    box-sizing: border-box;
    width: 36px;
    height: 100%;
    background-color: grey;
}
/* the outer container of the icon */
.nav-cell {
    position: relative;
    display: block;
    box-sizing: border-box;
    width: 36px;
    height: 38px;
    background-color: yellow;
    margin-top: 4px;
}
/* the inner container of the icon */
 .nav-icon {
    display: block;
    background-color: white;
    border: 1px solid orange;
    width: 8px;
    height: 8px;
    margin:auto;
    position:absolute;
    top:0;
    right:0;
    bottom:0;
    left:0;
}

Answer №1

A helpful method for centering content both vertically and horizontally is to use absolute positioning, with margin set to auto and all four sides adjusted to zero (or with a specified offset):

position: absolute;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;

Check it out here: http://jsfiddle.net/h9k3z6pv/5/

To achieve centered icons within cells, horizontal and vertical alignment must be applied.

Take a look at this example: http://jsfiddle.net/h9k3z6pv/8/ Using the same technique as previously mentioned.

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

Bootstrap 3 Full-Width Responsive Image Overlapping

I have designed a grid layout with col-md-7 on the left side and col-md-5 on the right side. The images in the col-md-5 are responsive and have a full-width of col-md-12. <div class="container"> <div class="row"> <div class="col ...

Why am I experiencing varying outcomes between createShadowRoot and attachShadow methods?

Recently, I've encountered an issue related to shadow dom while working on a project. After referring to an older tutorial that uses createshadowroot, I realized that this method is now considered deprecated and should be replaced by attachshadow. Un ...

Express.js - display the complete information

Trying to display an array of objects (highcharts points) is giving me some trouble. Instead of the actual data, I'm seeing [object Object]. It seems that JSON.stringify() doesn't play well with HTML. util.inspect also doesn't work as expe ...

How to position an absolute div in the center of an image both vertically and horizontally

Can someone help me figure out how to center a div inside an image? I seem to be having trouble with the positioning and alignment. Any suggestions or advice would be greatly appreciated. Thanks! .template-banner{ width: 100%; height: auto; margin: 0; } ...

Struggling to understand how to assign a specific color to individual divs within a loop

Featured on the main page of my website are 3 posts, each accompanied by a div link below. I want each of these 3 divs to have a distinct color. Any new posts within this category will appear here as they are published, but only the latest three will be vi ...

Adjust the text size to fit perfectly within the boundaries of a textarea input field

Is there a way to ensure that users type within a specific area with formatting and returns in a textarea element? I'm looking for a solution that limits overflow and ensures users stay within the designated area. How can this be achieved? I am worki ...

What is the best method to assign a CSS class to specific nodes within a TreeView using CodeBehind?

I need to apply unique styling to certain nodes in the codebehind. Within my TreeView, parents have two categories of children. One category matches the parent type (e.g. organizationalUnit), while the other does not (e.g. organizationalMembers). I aim t ...

Tips for embedding a file within a text box in HTML and enabling users to make direct edits

I am currently working on a feature that allows users to open a .txt or .html file from their file explorer and paste the contents into a textarea for editing and saving purposes. My question is whether it's possible to read the file content and auto ...

Show the div in the right position for both desktop and mobile screens, utilize an accordion or consider shifting the div

My webpage is designed using bootstrap 4 and showcases images of our team members along with accordion functionality to display bios upon clicking the image. On a desktop screen, everything functions smoothly with four images of team members displayed in ...

Assistance with Validating Forms Using jQuery

I have a form located at the following URL: . For some reason, the form is not functioning properly and I am unsure of the cause. Any suggestions or insights on how to fix it? ...

Chrome isn't displaying the text color of the link

I'm having an issue with a link that is styled like a button. It looks fine in Internet Explorer and Firefox, but not on Chrome. For example: The problem lies with the teal button in the center of the page that reads "follow issue." I intended for ...

Having trouble accessing the value of a node in JavaScript, as it keeps returning as "null"

Experimenting with converting HTML elements into lists. The objective is to generate a list of all values in a table upon clicking the "page next" buttons on it. Afterward, an alert should display the value of the first item in the list, which corresponds ...

Breaking the loop with a simple statement

I'm unsure if anyone will notice if I make changes to my preceding post, so I am here now to create a new one. Below is the code snippet: $(document).ready(function(){ var a; var b; var c; var d; var pmmain = ["Citrix", "Coach", ...

Incorporate new content into JavaScript using the input element

I have a unique question - can text be added to JavaScript using the input tag? For example, <input type="text" id="example" /> And let's assume the JavaScript function is: function display_text() { alert("The text entered in #example wi ...

Creating a Canvas Viewport Tailored for Multiplayer Gaming

Lately, I've been playing around with the HTML5 Canvas while working on an io game. However, I've hit a roadblock when it comes to handling the viewport. Setting up the viewport itself isn't too complicated, but accurately displaying other p ...

What strategies can I employ with flexbox to achieve my design goals?

How can I achieve my design requirements using flexbox? design requirements The current code that I have implemented follows the flexbox approach to meet the design requirements. However, the output of this code does not match the screenshot of my design ...

Alignment of React page gets messed up upon inclusion of Gallery component

I have searched for solutions online, but nothing seems to work for me. I am facing an issue with my webpage that has multiple sections, and I want to display them one after the other vertically. Here is the code snippet: import React from 'react&ap ...

Tips for displaying JSON data by formatting it into separate div elements for the result object

Having recently started using the Amadeus REST API, I've been making AJAX calls to retrieve data. However, I'm facing a challenge when it comes to representing this data in a specific format and looping through it effectively. function showdataf ...

Semantic UI dropdown field not displaying selected option text

I've encountered an issue while working with React Semantic UI. I'm trying to render a dropdown and have configured it so that the selected option's text should display in the field. However, when I choose an option from the dropdown, instea ...

employing iframes dynamically to overlay a webpage

I inserted this iframe into a webpage <iframe src='http://redbug.redrocksoftware.com.au:80/Pages/chamara.html' style="position:absolute;z-index:1;" ></iframe> The chamara.html page has a button that, when clicked, should cover the c ...