A `<div>` element placed outside of a `<button>` element

I'm currently facing an issue while trying to validate a mobile dropdown in an HTML5 page.

Upon validation, I encountered an error stating that div elements are not allowed inside buttons. When attempting to rectify this by wrapping the button with a div, the layout doesn't display correctly. The first button appears fine, but the second button or "X" is always visible and incorrectly positioned.

FIRST BUTTON - This is how the first button (open navigation) should appear.
SECOND BUTTON - This is how the second button (close navigation) should look like.

The HTML code in question:

<button class="navbar-toggler pull-xs-right hidden-md-up" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar">
  <div class="hamburger-icon"></div>
</button>
<ul class="nav-dropdown collapse pull-xs-right nav navbar-nav navbar-toggleable-sm" id="exCollapsingNavbar">
  <li class="nav-item nav-btn"><a class="nav-link btn btn-primary" href="login.php" id="c1">Login</a></li>
  <li class="nav-item nav-btn"><a class="nav-link btn btn-primary" href="index.php" id="c2">Register</a></li>
</ul>
<button hidden="" class="navbar-toggler navbar-close" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar">
  <div class="close-icon"></div>
</button>

The CSS styling involved:

  .navbar-dropdown .hamburger-icon {
content: "";
width: 16px;
-webkit-box-shadow: 0 -6px 0 1px,0 0 0 1px,0 6px 0 1px;
-moz-box-shadow: 0 -6px 0 1px,0 0 0 1px,0 6px 0 1px;
box-shadow: 0 -6px 0 1px,0 0 0 1px,0 6px 0 1px; }
  .navbar-dropdown .close-icon {
position: relative;
width: 21px;
height: 21px;
overflow: hidden; }
.navbar-dropdown .close-icon::before, .navbar-dropdown .close-icon::after {
  content: '';
  position: absolute;
  height: 2px;
  width: 100%;
  top: 50%;
  left: 0;
  margin-top: -1px; }
.navbar-dropdown .close-icon::before {
  transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -webkit-transform: rotate(45deg); }
.navbar-dropdown .close-icon::after {
  transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg); }

My attempts at resolving this by nesting div elements within buttons resulted in:

<div class="hamburger-icon">
<button>
</div>

And so forth...

  1. The second button (close navigation) is displayed when not in mobile view.
  2. The first button (open navigation) isn't visible, yet still clickable where it should be, with the second button (close navigation) being shown.
  3. The second button (close navigation) goes missing entirely.

How can I achieve the desired functionality as shown in the first two images without placing div elements inside the buttons?

Answer №1

Consider using span instead of div in your code for better structure

<button class="navbar-toggler pull-xs-right hidden-md-up" 
             type="button" data-toggle="collapse" data-target="#exCollapsingNavbar">
    <span class="hamburger-icon"></span>
</button>

It's important to note that the button tag should contain Inline Elements, whereas div is a Block Element

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

Adjusting the width of individual cells or columns within a table through my HTML code is proving to be a challenge

I am struggling to adjust different column widths in my table. Despite trying various approaches, such as changing the width of specific td/th elements and using inline styling or nth-child property, I have been unsuccessful in altering the width. The tabl ...

Count the start and end rows of a table within the <tfoot> using pdfHTML/iText 7

Currently, I am working on a project where I am developing a document generator that utilizes pdfHTML 3.0.3 and iText 7.1.14. The document includes a table displaying 'items'. It's worth noting that these item rows will likely span across mu ...

div with a fixed element inside that is scrollable

Check out this jsfiddle: http://jsfiddle.net/devboell/kjFps/ I am trying to create a div that overflows both horizontally and vertically. Additionally, I need an element that scrolls only horizontally while remaining fixed vertically. Here is an explanat ...

What tools are available for maintaining compatibility with older versions in HTML5?

Is there a way to mimic HTML5 features or utilize JS libraries when the browser lacks full support for HTML5? My focus is on mobile web applications in particular. Thanks, Sri ...

What are the steps to create a button with similar design using css3?

How can I create a button with the same lower part as shown in this picture? I've already achieved 50% of it with border-radius, but need help expanding the lower part to match. Any suggestions? https://i.sstatic.net/3ZJFS.jpg .list{ height: 280px; ...

The function `splitPinCodes.split(',')` is causing a malfunction

I am encountering an issue with validating the input data. The input text area should contain 6-digit zip codes separated by commas. I have implemented the ng-change="convertToArray()" method in Angular for the input text area. If I enter more than 6 digi ...

What is the best way to create a gap between two Bootstrap buttons?

I'm attempting to create two buttons in the same line in this code, but it's not working as I want. Take a look below: <div class="w3-show-inline-block"> <div class="w3-bar"> <button class="btn btn-s ...

How can you use C# code to control the visibility of a table row in HTML?

I am trying to display or hide a table row based on the current month using DateTime.Now.Month in my HTML code, but I can't seem to remember the correct syntax. The code I have tried is not working as expected. Can anyone help me with the correct synt ...

How can I adjust the size of an element without causing the other elements to move around on the

Currently, I am working on a live web page. You can view it at the following address: The issue I am facing is with the element just before the body's closing tag, the div "games_grid." When hovering over the game capsule image, it expands, causing t ...

The Bootstrap 4 navbar hamburger icon is not appearing correctly, as it is positioned off to the side of the screen on mobile devices

I recently created a website using a mix of bootstrap 4 templates, but I am facing some issues with the navigation bar on mobile devices. The hamburger icon for the navbar is not visible until I scroll halfway down the page. In the top half of the page, ...

Adding as HTML in Angular

I am looking to insert HTML content dynamically in Angular <div class="storycontent" ng-class="{show: show}"> <a href="#/profile?{{review.id}}">{{review.name}}</a>: {{review.story}} </div> Within the {{review.story}}, there ...

Is there a way to specifically import only variables and mixins from Sass stylesheets?

Using the Zurb Foundation 4 (S)CSS framework has presented a challenge with massively duplicated styles. Each file that I import 'foundation' into brings along all of Foundation's styles, resulting in redundant declarations for body, .row, . ...

Saving an Image from HTML5 <canvas> using Java Servlet

Despite the numerous StackOverflow questions on this topic, I have gone through many of them without success. Now, I am reaching out with my own question. My objective is to save an image from an HTML5 <canvas> on my webpage to a file on my server u ...

Guide on invoking a code behind function within an aspx file using a server tag

I am working on setting the HeaderText property of a TabPanel using a code behind function like this: <asp:TabPanel ID="id" runat="server" HeaderText='<%= String.Format("{0}","some text") %>'> However, I am having trouble getting it ...

The toggle for hiding and showing, along with changing the button color, is not functioning properly due to

I'm encountering a puzzling issue with a toggle that hides and displays information and changes color on click. The functionality works flawlessly on the page where I initially wrote the code. For instance, the button's background shifts colors w ...

Issues arise with the functionality of custom jQuery sliders

I'm attempting to create a bootstrap panel slider using my own jQuery code, but it's not functioning as anticipated. My Objective: I have two links for "previous" and "next", and three panels with distinct headings. When the "previous" link is ...

Enhance the appearance of an HTML select tag for optimal viewing on iOS Safari browser for

I have customized three dropdown lists using CSS. I set the style as follows: .dropdown{ font-size: 20px; background: white; border:none; position: relative; } While the dropdowns appear fine in Chrome, there seems to be a slight differen ...

An HTML editor that provides syntax highlighting for HTML code within PHP echo statements

Whenever we employ the echo function in PHP, the content within the quotes is considered as HTML. In certain HTML editors that I have tried, they simply highlight all of the HTML code in one single color, which can be a bit perplexing. Does anyone know o ...

The function is activated once

Upon clicking a text block, a textarea should appear in its place. However, after the first click, the code fails to trigger correctly, resulting in no action. Despite this issue, there are no errors reported in the console. var blockInfo; var textare ...

Combining two table headers into one table and organizing it for a clean appearance

Is it possible to use two different <thead>'s in the same table but have them stacked neatly? Currently, the first heading has 1 fewer column than the second one, resulting in a slightly awkward appearance. If there is a way to extend that first ...