Achieve vertical center alignment for a flexible top navigation that adapts to different screen sizes

To vertically center the text in my top navigation, I attempted to set a height of 3em for my .header and use vertical-align: middle;. However, this caused an issue with my responsive fold-out menu as it could not handle the fixed height, resulting in the menu disappearing. I also experimented with padding: 50% 0; for the parent and margin: 0 auto; for the child, but couldn't achieve the desired result. Another attempt involved using line-height: 40px, which appeared fine except for centering the burger menu icon. The burger menu icon also posed challenges when using percentages and translate. While it centered using this method, it shifted with the fold-out menu to 50%, which was not desired. Switching to a fixed/absolute position caused issues with the percentages and translate...

html {
  font-size: calc(1.3em + 1vw)
}

body {
  margin: 0;
  font-family: Helvetica, sans-serif;
  background-color: #fafafa;
}

/* header */

.header {
  position: fixed;
  overflow: auto;
  width: 100%;
  max-width: 30em;
margin-left: auto;
margin-right: auto;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 1px 2px rgba(0, 0, 0, .24);
  background: #ffb347;  /* fallback for old browsers */
  background: -webkit-linear-gradient(to right, #ffb347, #ffcc33);  /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to right, #ffb347, #ffcc33); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */ 
}

.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
}

.header li a {
  display: block;
  padding: 15px 20px;
  text-decoration: none;
  font-size: 0.7em;
  color: #000;
  background-color: blue;
}

.header li a:hover,
.header .menu-btn:hover {
  background-color: blue;
}

.header .logo {
  display: block;
  float: left;
  font-size: 1.3em;
  padding: 15px 20px;
  text-decoration: none;
  color: #000;
  background-color: blue;
}

.header .logo:hover {
}

/* menu */

.header .menu {
  clear: both;
  max-height: 0;
  transition: max-height .2s ease-out;
}

/* menu icon */

.header .menu-icon {
  cursor: pointer;
  display: inline-block;
  float: right;
  display:table-cell;
  vertical-align:middle;
  text-align:center;
  padding: 0.9em 0.4em;
  user-select: none;
  background: blue; 
}

.header .menu-icon .navicon {
  background: #333;
  display: block;
  height: 7px;
  position: relative;
  transition: background .2s ease-out;
  width: 40px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
  background: #333;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}

.header .menu-icon .navicon:before {
  top: 15px;
}

.header .menu-icon .navicon:after {
  top: -15px;
}

/* menu btn */

.header .menu-btn {
  display: none;
}

.header .menu-btn:checked ~ .menu {
  max-height: 100%;

}

.header .menu-btn:checked ~ .menu-icon .navicon {
  background: transparent;
}

.header .menu-btn:checked ~ .menu-icon .navicon:before {
  transform: rotate(-45deg);
}

.header .menu-btn:checked ~ .menu-icon .navicon:after {
  transform: rotate(45deg);
}

.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
  top: 0;
}

@media (min-width: 920px) {
  .header {
    position: relative;
  }
  .header li {
    float: left;   
  }
  .header li a {
  }
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }
  .header .menu-icon {
    display: none;
  }
}
<div class="header">
    <a href="" class="logo">LOREM <b>IPSUM</b></a>
    <input class="menu-btn" type="checkbox" id="menu-btn" />
    <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
    <ul class="menu">
      <li><a href="#work"> <b>Lorem</b></a></li>
      <li><a href="#about"><b>Ipsum</b></a></li>
      <li><a href="#careers"><b>Dolor</b></a></li>
      <li><a href="#contact"><b>Sit</b></a></li>
    </ul>
  </div>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>

Launch it in Full-Page Mode!

Answer №1

If you want to apply a CSS transformation, you can use the transform property with the value translateY(10%);. See the example snippet below.

html {
  font-size: calc(1.3em + 1vw)
}

body {
  margin: 0;
  font-family: Helvetica, sans-serif;
  background-color: #fafafa;
}

/* header styles */
.header {
  position: fixed;
  overflow: auto;
  width: 100%;
  max-width: 30em;
  margin-left: auto;
  margin-right: auto;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 1px 2px rgba(0, 0, 0, .24);
  background: #ffb347;
}

/* menu icon styles */
.header .menu-icon {
  cursor: pointer;
  display: inline-block;
  float: right;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  padding: 0.9em 0.4em;
  user-select: none;
  background: blue;
  transform: translateY(10%);
}

/* media queries */
@media (max-width: 920px) {
  .header .menu {
    transform: translateY(0%);
  }
}

@media (min-width: 920px) {
  .header {
    position: relative;
  }
  .header li {
    float: left;
  }
  .header li a {}
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }
  .header .menu-icon {
    display: none;
  }
}
<div class="header">
  <a href="" class="logo">LOREM <b>IPSUM</b></a>
  <input class="menu-btn" type="checkbox" id="menu-btn" />
  <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
  <ul class="menu">
    <li>
      <a href="#work"> <b>Lorem</b></a>
    </li>
    <li><a href="#about"><b>Ipsum</b></a></li>
    <li><a href="#careers"><b>Dolor</b></a></li>
    <li><a href="#contact"><b>Sit</b></a></li>
  </ul>
</div>
<br><br><br> (repeated for space)

Answer №2

Consider implementing the following CSS :

.nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
    overflow: hidden;
    margin-top: 2%;
}

Answer №3

Check out this CSS snippet:

.header {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
    justify-content: space-between;
}

@media only screen and (max-width: 600px) {
  .header {
        display: block;
    }
}

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

How come my jQuery validation needs two clicks to activate?

I am experiencing an issue with a comments form that has 2 fields: Author and Message. Upon clicking the submit button, the validation does not trigger initially. However, if I click the submit button again, then the validation works as expected. Any tho ...

Using Javascript to select a radio button in a form depending on the value entered in a text box

I have a form that interacts with a Google Sheet to insert and retrieve data. For instance, the form contains two radio buttons: <input id="Rdio_1" name="RdioSelect" type="radio" class="FirstCheck" value="1" onchange="RadioValInsert ()"/> < ...

What is the best way to trigger a sound to play once when the document is loaded?

I was looking for an easy and straightforward method to play a sound as soon as the document is loaded. After browsing through various online resources, I couldn't find a clear explanation. Can someone please assist me on how to create this in a brow ...

When switching between classes, it is not possible to apply a different value to the same CSS property

I am currently working on a project that involves toggling a class on a ul tag with an id of sideNav using JavaScript. In this scenario, I have set the left attribute of the id to 0, while the same attribute in the class has a value of -100%. After sever ...

Issue where the background color remains the same and does not change

I'm having trouble identifying the issue in this code. I am attempting to change the background color using the following CSS: body { background-color: black; padding: 0px; margin:0px; height: 1500px; } However, it doesn't seem to ...

What is the process for resetting a search filter list box?

I developed a search feature for filtering a list based on the instructions provided in this W3Schools tutorial: function myFunction() { var input, filter, ul, li, a, i, txtValue; input = document.getElementById("myInput"); filter = input.va ...

What is the best method for pulling specific tags from HTML in Android with Jsoup?

How can I use jsoup to extract the HTML tag (div id="content-body-14269002-17342313) from this specific link? Could someone kindly share a code snippet demonstrating how to accomplish this task? ...

What steps can I take to make the cards in bootstrap 5.3 responsive for mobile devices?

I'm struggling with my code and need some help. I've been editing it, but things seem to be getting worse instead of better. Specifically, I can't figure out how to set up my container, row, and columns in Bootstrap properly. My site uses ca ...

"Easily toggle the visibility of values in checkboxes and organize them into groups with the power of JavaScript

Hey there! I am currently working on a project that involves grouping checkboxes and hiding/unhiding their content based on user interaction. Essentially, when the page first loads, the "All CARS" checkbox will be checked by default. If I then check the "C ...

Enhancing code with new Javascript functionality

Currently utilizing the WordPress Contact Form 7 plugin and in need of updating my existing JavaScript code to include an onclick function and data-img attribute for checkboxes. Due to the limitations of Contact Form 7 shortcode, adding attributes beyond i ...

Mobile website includes a share button that activates share dialogs for iOS and Android systems

Can a share button be created on my website that will trigger share dialogs on iOS and Android devices? Specifically, I am referring to the dialogs shown below for each system: https://i.sstatic.net/dw759.jpg https://i.sstatic.net/NS8W3.png I am lookin ...

Implementation demonstration / library for complete URL encoding

Currently, I am developing a Java application that extracts links from HTML and utilizes them to request their content. One particularly challenging area is URL encoding when the intent of the URL author is unknown. Determining whether to use %20 or + can ...

How to Build Two Navbars in BootstrapVue with a Sticky Bottom Navbar at the Top

My current project involves creating two navigation bars. The first navbar at the top consists of just a brand logo, while the second navbar at the bottom serves as the main navigation menu. I want the top navbar to scroll off the screen when users scroll ...

How can I use JavaScript to trigger a button click event inside an iframe element

Is there a way to retrieve the inner HTML contents of a clicked element in an iframe loaded content using JavaScript? This code uses jQuery: var elements = document.getElementsByTagName('iframe'); [].forEach.call(elements, function(elem ...

unable to download file and missing image display

I need assistance with creating a download link using the anchor tag in my code. However, the link is not working as expected and the image is not displaying either. <td><img src="{{book.book_image}}" alt="{{book.name}}" width= ...

Instructions on how to activate scrolling for a div element that becomes visible upon clicking a button

When a button in the navbar is clicked, a div appears with a height that exceeds the viewport/page size. How can I enable scrolling for the entire page when this happens? I attempted to use overflow: scroll, but it only applied scrolling to the internal d ...

The height of the absolute div tag does not match the height of the parent div

I am currently designing an editor that requires a line-number bar (gutter) on the left side. I have set a div with a height of 100% and its parent div with overflow: auto. My expectation is that if the content exceeds the given height, the editor-body sh ...

What is the best way to ensure my jQuery slides are responsive?

I have been developing a personal practice website and have successfully integrated a jQuery slide show for showcasing photographs. However, I am facing a challenge in making these slides responsive when the screen size changes. I have gone through numerou ...

What is the best way to incorporate multiple Bootstrap templates into an Angular project without causing conflicts between them?

When incorporating a bootstrap template into the admin interface, it is important to consider that its design may vary from the template used for visitors. Mixing multiple styles based on different templates can lead to conflicting styles and can potenti ...

Guide on converting HTML datetime picker datetime-local to moment format

I am looking to convert an input type : <input type="datetime-local" name="sdTime" id="stTimeID" onChange={this.stDateTime} /> into a specific date format: const dateFormat = 'MM/DD/YYYY hh:mm:ss a'; To achieve this, I need to transfer ...