Optimal techniques for designing an HTML CSS navigation menu (using my float demonstration)

I'm trying to figure out how to create a menu that complies with the latest standards of HTML and CSS. I put together this code but was informed that using float is generally not recommended nowadays. However, I'm unsure of the reasons behind this and what would be the best practice for controlling/positioning the menu. Can someone provide guidance on this?

#header {
  width: auto;
  height: auto;
  border-bottom: 1px solid #d2d2d2;
}

#main_logo {
  width: 200px;
  float: left;
  padding: 8px 0px 0px 20px;
}

nav {
  float: right;
}

.ul-menu {
  display: flex;
}

.ul-menu li {
  display: inline;
  padding: 42px 20px 41.7px 20px;
  background-color: var(--brand-color-second);
  transition: background-color 0.24s linear;
  /* fade-in effect on menu hover */
  border-left: 1px solid #d2d2d2;
}

.ul-menu li a {
  text-decoration: none;
  font-size: 0.86rem;
  color: #757575;
  letter-spacing: 2.5px;
  font-family: var(--secondary-font);
}

.ul-menu li:hover {
  /* background-color: #48763b; */
  /* ISSUE WITH HOVER: how can we make the entire li a link when hovering instead of just the href? */
  background-color: var(--brand-color-main);
  color: #fff;
  border-bottom: 1px solid var(--brand-color-main);
}

.ul-menu li:hover a {
  color: #fff;
}
<header>
  <div id="header">
    <div id="main_logo">
      <a href="index.html"><img src="img/logo.png" alt="logo"></a>
    </div>
    <nav class="main-menu">
      <button class="toggle-btn">Menu</button>
      <ul class="ul-menu">
        <li><a href="index.html">Home</a></li>
        <li><a href="about-us.html">About Us</a></li>
        <li><a href="services.html">Services</a></li>
        <li><a href="pricing.html">Pricing</a></li>
        <li><a href="contact.html">Contact</a></li>
      </ul>
    </nav>
  </div>
  <!-- header -->
  <div style="clear:both"></div>
</header>

Answer №1

When I was just starting to learn CSS, I encountered this issue, which is quite common among beginners. While it's possible to solve it using floats, it can be a real headache. The modern and more efficient approach would be to utilize flexbox. It may take some time to grasp, but the benefits are well worth it - you won't regret making the switch.

If you're looking for a helpful written guide, check out: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

To practice and enhance your flexbox skills, try out this fun game:

For a fantastic video tutorial on flexbox by Kevin Powell, check this out: https://youtu.be/u044iM9xsWU

I hope this information proves helpful to you :)

Answer №2

The reason why floats are generally not recommended for modern browsers is because they often disrupt layouts and struggle with responsiveness. While floats were commonly used to position text around images, they were never meant to be used for overall layout purposes.

Simply resizing my browser while testing a menu using floats causes buttons to shift randomly and words from the menu to disappear unpredictably.

For a more stable and reliable option, consider using Flexbox. Here's a helpful guide on implementing Flexbox for a navigation bar:

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

information not visible on the webpage

Recently, I made an alarming discovery on my website. I have implemented two different types of menus - one for the front page (designated as .navbar-inverse) and another for all other pages (.navbar-default). The front page menu is static while the other ...

Challenges arise when utilizing the foundation 6 grid for image overlays within a React application

Having some trouble using the foundation 6 xy grid for the first time and aligning three images with text overlays at the bottom of each image. I am struggling to make the background of the text fill the full width of the image while also ensuring it is re ...

Adjustable textarea with automatic height based on content and line breaks

Imagine you have a textarea with text that includes line breaks. If you style it like this: textarea { height: auto; overflow: hidden; resize: none; } Upon loading the page, the textarea will only adjust its height based on the content until it enc ...

What steps do I need to take in order to arrange my cards into binary groups for PC viewing

Is there a way to display cards as binary groups for PC view? The layout looks fine on phone but the cards are all at the bottom when viewed on a computer. This is how it currently appears: https://i.sstatic.net/XBgJc.jpg I want the card layout to resem ...

Steps to properly insert an SVG into a button

I have a block set up with a Link and added text along with an svg image inside. How can I properly position it to the lower right corner of the button? (see photo) Additionally, is there a way to change the background color of the svg? This is within th ...

Transferring information from Node.js to HTML with the help of EJS template engine

Below is the Server Side code I am using: app.set('view engine', 'html'); app.engine('html', require('ejs').renderFile); app.use(express.static('views')); app.use(bodyParser.urlencoded({extended:true})); a ...

PHP and JQuery not working together to update database

Essentially, I am trying to update the status based on the unique id. Despite no errors being thrown, the status remains unchanged. index.php $(document).ready(function() { $('.seatCharts-seat').click(function(){ var id = jQuery(th ...

Best practices for integrating text and images within a website header using CSS and HTML

I need help figuring out the most efficient method for creating a page header that showcases an image while keeping the text visible for search engines and in case the image fails to load. According to Google, it is recommended to use a <div> for th ...

Upon selecting the checkbox, the user will be redirected to an input textbox

Can I make it so that when I check a checkbox, it automatically directs me to a textbox as if I pressed the TAB key? Is there a way to achieve this using only HTML and CSS? ...

Tips for refreshing jQuery to ensure it functions smoothly for subsequent tasks

I am facing an issue with running a second process under JQuery, as shown in the code snippet below: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <script type=" ...

Card flip animation using RotateY is experiencing delays in loading

While experimenting with jQuery and CSS3 animations, I encountered a strange issue. When hovering over the card or focusing on the CCV field in my pen, there is a delay in the color transition when the card flips over. It almost seems like it's loadin ...

Issues with CSSClass not functioning properly within C# .NET applications

I have limited knowledge of .NET as I work primarily as an ERP Implementation Consultant. My experience with C# .NET is minimal, but I encounter a strange CSS issue in my C# projects. While the CSS functions correctly in Internet Explorer when running the ...

Button for Processing PayPal Payments

Is there a security risk in using a text button to submit a payment form in PayPal Payments Standard instead of an image button like this: <input type="image" src="http://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt=""> ...

Attempting to reach <p> from numerous web pages

Currently, I am working on a project where I need to extract text data from various websites. I have successfully managed to retrieve the text data using <p> tags. I would really appreciate any assistance with this task. Thank you! ...

Calculating the dimensions of a CSS element based on its content

Is there a way to achieve this using only CSS, without needing JavaScript? I am working on a project with a lot of relative and absolute positions, where elements must be centered dynamically without specifying static widths. While I have managed to minimi ...

In IE8, CSS classes assigned to HTML5 elements are removed when they are printed

Currently, I am facing a challenge where I need to ensure proper printing functionality in IE8. The issue arises when using HTML5 features (such as sections) along with CSS on a page - the problem occurs specifically during printing. For example, when tryi ...

Changing the Color of Links in Bootstrap CSS

I'm completely new to coding and I've decided to dive into the world of Bootstrap. However, I'm facing a hurdle: I can't figure out how to customize the default Bootstrap styles with my own CSS. After researching online, here are the c ...

JavaScript code is not functioning properly on Internet Explorer 10

Upon selecting an option, I need to activate buttons. This functionality works smoothly in all browsers except for IE10. The HTML for the select element: <select id="tenants" name="tenants" size="10" class="reportOption" onchange=""> <option va ...

Utilizing AngularJS for Your Business Website

We are in the process of developing a new commercial website to replace our current one. Concerns about performance are on our minds. The amount of data we have per page is not very heavy, with a maximum of 150 data binds. We plan to use AngularJS 1.2 t ...

Trouble with Bootstrap CSS buttons: color consistency problem persists

I am facing an issue with my Bootstrap button, which currently looks like this: https://i.sstatic.net/MWHYP.png The HTML code for the button in question is as follows: <button type="button" class="btn-primary.custom-btn" data-bs-to ...