Stylish CSS menu that adjusts to different screen sizes, featuring a centrally aligned h1

I am trying to center an h1 element in my navigation, alongside two other elements, without using an image. I want it to be responsive as well. I have attempted various methods but have had limited success, as most solutions require the use of background images. When trying to use inline-block, I struggle to center it perfectly.

Below is the best attempt I have made so far, but the hover effect does not work, likely due to the h1 width being set at 100%.

* {
  margin: 0;
  padding: 0;
}
a {
  text-decoration: none;
  color: white;
  text-shadow: 1px 1px 1px black;
}
li {
  list-style: none;
}
header {
  margin: 0 auto;
  text-align: center;
}
ul li {
  float: left;
  display: block;
  background-color: #232323;
  width: 50%;
  padding: 10px 0 10px 0;
}
ul li:hover {
  background-color: black;
}
.logo {
  position: relative;
  top: -38px;
}
<header>
  <nav>
    <ul>
      <li><a href='#blog'>Blog</a>
      </li>
      <li><a href='#portofolio'>Portofolio</a>
      </li>
    </ul>
  </nav>
  <a href='#/' class='logo'><h1>Tao SandBox</h1></a>
</header>

There must be a more efficient way to achieve this!

http://codepen.io/taosx/pen/vNWojo?editors=110

Answer №1

Check out this code on CodePen

To make the h1 element use the .logo class, you need to set its position to absolute and center it. Here is the CSS code:

.logo {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    text-align: center;
}

It is recommended to place the h1 markup before the nav element for better semantic flow. Here is an example:

<header>
    <nav>
        <h1 class='logo'><a href='#/'>Tao SandBox</a></h1>
        <ul>
            <li><a href='#blog'>Blog</a></li>
            <li><a href='#portfolio'>Portfolio</a></li>
        </ul>
    </nav>
</header>

Answer №2

To incorporate the logo into the menu, you can follow this straightforward method:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
a {
  text-decoration: none;
  color: white;
  text-shadow: 1px 1px 1px black;
}
ul {
  overflow: hidden;
}
li {
  list-style: none;
}
header {
  margin: 0 auto;
  text-align: center;
  background-color: #232323;
}
ul li {
  float: left;
  width: 33%;
  vertical-align: middle;
}
ul li:hover {
  background-color: black;
}
<header>
  <nav>
    <ul>
      <li><a href='#blog'>Blog</a>
      </li>
      <li><a href='#/' class='logo'><h1>Your Company Name</h1></a>
      </li>
      <li><a href='#portfolio'>Portfolio</a>
      </li>
    </ul>
  </nav>
</header>

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

After refreshing the page, users are redirected back to the login page

On my website, I created a main page and an index.html page that redirects to the main page through a login panel. The issue I am encountering is that when I log in successfully on the main page and then refresh the page, it takes me back to the login pag ...

Disabling GPS with HTML5/phonegap: A step-by-step guide

Looking to create a function that can toggle GPS on and off for both iPhone and Android devices ...

What is the best way to stop a select menu option from being highlighted once it has been clicked on?

Is there a way to prevent the highlighting of selected options in a <select> menu when a user makes a selection? <select id="my_select_menu"> <option id="1">Option 1</option> // I do not want this option to be highlighted ...

I would like the background image to perfectly fit the dimensions of the parent container

https://i.sstatic.net/PlCYU.png Is there a way to prevent the background image from overflowing and causing a horizontal scroll bar to appear? The layout of the page consists of two columns, with each column taking up 50% of the width. The left column con ...

Bar graph data remains unchanged after each iteration of the loop

I've been attempting to implement a for loop in a JavaScript bar graph that resets the bar height to 0 when a button is clicked, but it doesn't seem to be working as expected. I've tried checking the console in Google Developer tools for err ...

What could be causing the issue with the focus not being activated when clicking on the input field in Vue?

I am facing an issue where I have to click twice in order to focus on a specific input field, and I'm having trouble setting the cursor at the end of the input. I attempted to use $refs, but it seems like there may be a deeper underlying problem. Any ...

Take away the follow option on the embedded tweet

I need to customize the appearance of embedded tweets on a website by removing the follow button as well as the reply, favorite, and retweet buttons in the footer. Here is a simple HTML example: <blockquote class="twitter-tweet"> <a href="htt ...

The function .click does not function properly when used with an anchor element within a figure tag

In my JavaScript-loaded figure, there is an image description and two buttons. Sometimes the description contains a link with a fig attribute, like this: <a fig="allow" href="#tt5">[1]</a> If the anchor is not in a figure and has a fig attrib ...

Learn how to incorporate a YouTube video into your website without using Flash or JavaScript by utilizing a popup window

I am in search of assistance with coding for my website to include a video popup feature featuring a YouTube video. I prefer the use of HTML5 rather than Flash or JavaScript. Unfortunately, I have been unable to locate any suitable code examples. While I ...

Image overlay on a hovering background

I'm currently facing a challenge with creating a hover effect for a background image while keeping the image in front unchanged. I would like the background to darken on hover, but I want the image in front of it to remain the same. Essentially, I ne ...

Include content within the navigation bar of Bootstrap 3.3

I am trying to insert text at the end of the header in a jumbotron template: <div id="navbar" class="navbar-collapse collapse"> <form class="navbar-form navbar-right"> <div class="form-group"> <input type="text ...

When hovering, transition occurs unless the cursor is over a specific element

I have created a small box element with a close button that looks like this: ___ | X| ‾‾‾ In order to make it more interactive, I applied some CSS so that when hovered, the box expands like this: ___________________ | X| ‾ ...

How can I remove the unsightly scrollbar caused by overflow: auto?

I've encountered a problem while working on my website. The inner div #content wasn't expanding to the full size of the page, causing the text to overflow messily over the background image and making it difficult to read. I initially used overflo ...

The popover seems to be failing to appear

I am struggling to get a popover to appear when clicking on an appended href element dynamically. Here are the code snippets I have tried: <div id="myPopoverContent"> ...stuff... </div> <div id="div3" style= "width:200px;height:200px;" ...

Clicking again on the second onclick attribute

I have an image file named image1.png. When the button is clicked for the first time, I want it to change to image2.png. Then, when the button is clicked for a second time, I want it to change to yet another image, image3.png. So far, I've successful ...

The VueJS Hello world application is experiencing technical difficulties and is currently not functioning as

As a complete VueJS newbie, I've just started working on my very first app. It's supposed to be a simple Hello World application, but unfortunately, the code isn't quite behaving as expected :( HTML <!DOCTYPE html> <html lang=" ...

Modify animation trigger when mouse hovers over

I am looking to create a feature where a slide overlay appears from the bottom of a thumbnail when the user hovers over it, and retracts when the user is not hovering. animations: [ trigger('overlaySlide', [ state(&ap ...

When utilizing jQuery version 1.10.2, the HTML markup shows up prior to the page completing its loading process

I am trying to import one HTML page into another, but when I load or refresh the page, the HTML markup appears before the page fully loads. How can I fix this issue? <head> <script type="text/javascript" src="js/jquery-1.10.2.js" ></scr ...

Unusual behavior of diagonal viewBox in SVG

Trying to understand how the viewBox attribute works in SVG, but the code below is confusing me. Can someone please explain if this is a bug or the expected behavior of SVG? I can't seem to figure out what I'm doing wrong... <svg class="s ...

Adding several lines of HTML content from an object using jQuery's append method

Currently, this is what I have: $(document).ready(function() { $.ajax({ type:"GET" , url:'{{url("/api/getcart")}}' , dataType:"json", success: function(data){ $('#price').append(data.cartitems[1].product.pr ...