issue with centering using margin auto

Below is the HTML and CSS snippet for reference:

    * {
        margin: 0;
        padding: 0;
    }
    
    html, body {
        height: 100%;
        width: 100%;
    }
    
    header {
        height: 100px;
        background-color: #35424a;
        border-bottom: #e8491d 5px solid;
    }
    
    h1 {
        float: left;
        margin-top: auto;
        margin-bottom: auto;
        color: white;
    }
    
    .acme {
        color: #e8491d;
    }
    
    nav {
        float: right;
        margin-top: auto;
        margin-bottom: auto;
    }
    
    li {
        display: inline;
        font-size: 150%;
        padding: 0px 20px;
    }
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="style.css">
        <title>Document</title>
    </head>
    <body>
       
        <header>
            <h1><span class="acme">Acme </span>Web Design</h1>
    
            <nav>
                <ul>
                    <li>HOME</li>
                    <li>ABOUT</li>
                    <li>SERVICES</li>
                </ul>
            </nav>
        </header>
        
    </body>
    </html>

The issue I encountered is with the h1 and nav elements where despite setting the top and bottom margins to auto and specifying a height of 100px for the header (105px including the border), the margin auto property does not seem to work as expected. As a troubleshooting step, I attempted to add

display: block;

however, this adjustment did not have any effect on the layout.

Answer №1

Simply update your header CSS with the following code:

header {
  display: flex;
  justify-content: space-between;
  width: 100%;  
  align-items: center;
  height: 100px;
  background-color: #35424a;
  border-bottom: #e8491d 5px solid;
}

This should resolve the issue

Answer №2

-> Please make sure to include the following code in your files.

* {
        margin: 0;
        padding: 0;
    }
    
    html, body {
        height: 100%;
        width: 100%;
    }
    
    header {
        height: 100px;
        background-color: #35424a;
        border-bottom: #e8491d 5px solid;
    }
    
    h1 {
        float: left;
        margin-top: auto;
        margin-bottom: auto;
        color: white;
    }
    
    .acme {
        color: #e8491d;
    }
    
    nav {
        float: right;
        margin-top: auto;
        margin-bottom: auto;
    }
    
    li {
        display: inline;
        font-size: 150%;
        padding: 0px 20px;
    }
    .display-table {
        display: table;
        height: 100%;
        width: 100%;
    }
    .vertical-content {
        display: table-cell;
        vertical-align: middle;
        height: 100%;
        width: 100%;
    }
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Stack</title>
        <link href="css/style.css" rel="stylesheet">
    </head>
    <body> 
        <header>
            <div class="display-table">
                <div class="vertical-content">
                    <h1><span class="acme">Acme </span>Web Design</h1>
                    <nav>
                        <ul>
                            <li>HOME</li>
                            <li>ABOUT</li>
                            <li>SERVICES</li>
                        </ul>
                    </nav>
                </div>
             </div>
        </header>
    </body>
</html>

Answer №3

If you're having trouble getting HTML to work with margin-top or margin-bottom, consider using a flex display instead. Here's a helpful guide: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

To achieve what you're looking for, try the following CSS:

display: flex;
align-items: center;

Explore the link for more information on utilizing flex properties.

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

Adjust the dimensions of the dropdown menus

I've set up 2 dropdown menus, but I'm having trouble adjusting their size. I need them to fill the screen width. Here's my code: <select id="repeatSelect" ng-model="selectedArea"> <option ng-repeat="(key,prop) in result" value=" ...

Struggling to position CSS div in the middle of computer screen

I have created a main login div that is always displayed in the middle of the screen. However, I also have two additional divs - one that is supposed to show above the login div and the other below it. While the login div displays perfectly in the middle ...

Issue with Google Maps iFrame not loading when utilizing JavaScript variables in the "src" attribute

Currently, I am facing an issue with utilizing JavaScript variables containing GPS latitude and longitude values in the "src" attribute of an iFrame in an HTML file for displaying image EXIF data on a Google Maps iFrame. When I hardcode specific latitude ...

Is there a way to adapt my existing navigation bar to collapse on smaller devices?

Struggling to make my navigation bar collapsible on my site designed for a class project. Wondering if this requires both HTML and CSS, or can it be achieved with just HTML since this is my first time working on responsive design. Below is the code I have ...

Creating a personalized inline MailChimp form

Check out this Code Pen link. My company's website has an opt-in form but it's not aligning properly. I want the name, email, and subscribe fields to be on the same line. Any suggestions? I suspect there might be a conflict with bootstrap. HT ...

Is there a way to overlap shapes (transform) using ::before and ::after in CSS? I'm facing an issue where Edge is not showing the shape on top

I designed two diagonal shapes to overlay a container with a background image using the pseudo elements ::before and ::after. While this setup functions correctly in Firefox and Chrome, it is not working as expected in Edge. What could be causing this di ...

Position the icon to the left side of the button using Bootstrap

Need help with centering text on a bootstrap 3 button while aligning the font awesome icon to the left side. <button type="button" class="btn btn-default btn-lg btn-block"> <i class="fa fa-home pull-left"></i> Home </button> Usi ...

"Troubleshooting: Issue with ng-click in AngularJS not triggering ng-show

I can't figure out why my ng-click isn't showing the ng-show message. I've tried searching for a solution but no luck. Here is my controller function: $scope.showLogoutMessage = function() { $scope.logoutmsg = true; }; This is my Logi ...

What is the best way to make the div inside the table cells expand to fill the available space?

I'm having trouble making the inner divs within table cell divs expand and fit their parent div without overlapping the next cell below it. Check out the sandbox for reference I've outlined the areas in grey where I want the cells to be filled. ...

Exploring the world of JQuery Ajax selectors

I am attempting to create a comment system similar to the one found at . I have the AJAX code below, but I am having trouble uniquely selecting text areas for each post. The issue is that the number of posts can vary, so it's important that each post ...

Is there a way to identify which modification codes are utilized throughout an entire CSS file within an HTML page?

Is there a way for a webpage or developer tool to identify the specific CSS codes used in a heavily modified CSS file when loading a page? ...

Mastering the fundamentals of integrating/augmenting a fresh template in Umbraco

Let me be completely honest. Umbraco is unlike any other CMS I have worked with before. I am currently struggling to grasp the proper method of creating a Template and integrating it into Umbraco. How exactly can I add a template? I am making changes to t ...

Utilizing CSS to style the disabled selection in the absence of an optgroup

I am encountering an issue with a select component that has grouped options and a default option. The CSS styling is not being applied to the option without a group. I would like to change the color of the hidden disabled option to match the placeholder. ...

Incorporate Unicode characters into the structure of an HTML tag

I'm having an issue with the C# script below that is supposed to remove HTML tags from a description column in SSIS. I attempted to include the unicode value &#58 in the htmlTagPattern string, but it's not working as expected. Any help would ...

What is the process for incorporating an external script into a Vue component?

Seeking assistance urgently... I am encountering an issue with a Vue component and an endpoint that provides a script containing a small menu with actions. However, once the script is loaded, the actions do not seem to function on the page and I cannot det ...

Burger menu animation activated by a single click anywhere on the page

After following a tutorial on creating an animated menu burger, I encountered a few bugs. The animation is working as intended, but it triggers no matter where I click on the page. Here is the code snippet: const toggleMenu = document.querySelector( ...

Implementing Bootstrap Tags Input for a fresh start!

Currently, I am using Bootstrap Tags Input. When I press "ENTER," it automatically converts the text into a tag. However, I would like to be able to combine free text with tags without the "ENTER" key converting the text. Is there a way to achieve this? & ...

How to use TypeScript to modify button styling with an OnClick() event handler

Learning TypeScript with Existing Code Transition Currently, I am delving into the world of TypeScript and in the process of converting my CoffeeScript code to TypeScript (specifically Lit Web Component). Confusion on Translation Process I'm encount ...

The html() method is not functioning correctly in ajax requests

I would like to implement a feature where users can like and unlike a book. However, I am facing an issue where the code only executes once without refreshing the page. JAVASCRIPT <script type="text/javascript"> $(document).ready(function() { ...

How can you apply margin to an element within a column in Bootstrap 4?

Within a column, I have several span elements: <div class="container-fluid"> <div class="row"> <div class="col"> <span class="myClass">some text</span> <span class="myClass">some text</span> ...