Ways to prevent a <a href> element with a class linked to javascript from behaving like a block element

I am currently facing an issue with formatting an inline navigation. The last link, which is associated with a JavaScript class, is causing the entire link to become a block element instead of aligning inline with the rest of the links in the navigation.

After experimenting with removing the class and changing it to something unrelated to any script, I noticed that the link aligns back in line. This led me to believe that the problem lies within the JavaScript it's linked to. I've even attempted calling a.show in CSS to display it as inline or inline-block with no success. It feels like I may be overlooking a common rule or guideline.

The

<a href="#" class="show">
tag is found on Line 20, while the associated <script> tag can be located on Line 25 within the HTML code.

CSS


#nameTag {
  max-width: 800px;
  min-width: 320px;
  margin: 0 auto;
  background-color: #FFFFFF;
  -webkit-box-shadow: 2px 2px 2px 1px hsla(0,0%,0%,0.72);
  box-shadow: 2px 2px 2px 1px hsla(0,0%,0%,0.72);
  border-radius: 43px;
  border: 2px solid #4B4949;
}

... (CSS continues)

HTML


<div id="nameTag">
 ... (HTML code snippet continues)
</script>

Answer №1

UPDATE2

To avoid the jerking behavior when clicking the .hide and .show links, simply include event.preventDefault() in your jQuery function.

<script>
$("#container").hide();
 $(".show, .hide").click(function(event) { // Pass the event object here
     event.preventDefault();              // Then use the preventDefault() property
  $("#container").slideToggle("slow");
});
</script>

UPDATE

I misinterpreted the question initially. It seems that you wanted the toggle image inline with the other anchors. This would be quite complex to achieve. The .show is located in another div and nested. Therefore, just add a similar img inside .miniNav and ensure that the other image disappears. Also, I used a background-image for the one inside .miniNav as it's easier to handle as a link. It would be better for you to refer to the jQuery code than for me to explain it all. Additionally, I changed the class of the "Close CV" link to .hide to avoid sharing styles and then added .hide to the jQuery for consistent functionality.

The last link "Close CV" will align properly if your #nameTag is @ 550px wide. Otherwise, the link may wrap to the next line if less than 550px in width. To prevent wrapping, make .miniNav and the anchors behave like table components by adding display:table-row to .miniNav and display:table-cell to each anchor.

I adjusted padding so that the links remain consistent even when #nameTag is compact. The

&nbsp;&nbsp;|&nbsp;&nbsp;
was removed and replaced with border-right: 1px solid blue;. For centering the links, margin: 0 auto; display: table; was included in #container.

You might consider using percentages or ems instead of pixels for margins and padding to keep your text consistently on one line. Some experimentation may be required, so it's up to you to decide.

By the way, when specifying selectors in CSS, be specific for each one if there are multiple selectors that apply to a ruleset.

Example

 .miniNav a:hover,
  a:active {
     color: #000;
  }

Anchors that are descendants of .mini-nav turn black on hover / Any active anchor becomes black.

   .miniNav a:hover,
   .miniNav a:active {
        color: #000;
   }

Anchors that are descendants of .mini-nav change to black on hover or when active.

Changes

#container {
  padding: 10px 0px 15px 7px;
  margin: 0 auto;
  display: table;
}
.miniNav {
  text-align: center;
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 10px;
  display: table-row;
}
.miniNav a,
.miniNav a:link,
.miniNav a:visited {
  color: #0033AA;
  text-decoration: none;
  display: table-cell;
  border-right: 1px solid blue;
  padding: 0 7px;
}
...
.miniNav a:last-of-type {
  border-right: 0px none transparent;
}

SNIPPET

$("#container").hide();
$(".show, .hide").click(function() {
  $('.show').toggle();
  $("#container").slideToggle("slow");
});
#nameTag {
  max-width: 800px;
  min-width: 550px;
  margin: 0 auto;
  background-color: #FFFFFF;
  -webkit-box-shadow: 2px 2px 2px 1px hsla(0, 0%, 0%, 0.72);
  box-shadow: 2px 2px 2px 1px hsla(0, 0%, 0%, 0.72);
  border-radius: 43px;
  border: 2px solid #4B4949;
} 
#tagTop {
  width: 100%;
  height: auto;
  display: block;
  color: #fff;
  font-size: 30px;
  text-align: center;
  border-top-left-radius: 40px;
  border-top-right-radius: 40px;
  background-color: #0033AA;
  padding-top: 5px;
  padding-bottom: 10px;
}
#tagBottom {
  width: 100%;
  height: 50px;
  display: block;
  color: #FFFFFF;
  text-align: center;
  border-bottom-left-radius: 40px;
  border-bottom-right-radius: 40px;
  background-color: #0033AA;
  padding-top: 5px;
  padding-bottom: 10px;
}
#tagBottom > a:link,
a:visited {
  color: #fff;
}
#container {
  padding: 10px 0px 15px 7px;
  margin: 0 auto;
  display: table;
}
.miniNav {
  text-align: center;
  font-size: 18px;
  font-weight: 600;
  margin: 0px auto 10px;
  display: table-row;
}
.miniNav a,
.miniNav a:link,
.miniNav a:visited {
  color: #0033AA;
  text-decoration: none;
  display: table-cell;
  border-right: 1px solid blue;
  padding: 0 7px;
}
.miniNav a:hover,
.miniNav a:active {
  color: #000;
}
.miniNav a:last-of-type {
  border-right: 0px none transparent;
}
a.img {
  background: url(http://placehold.it/80x50/eea/e00?text=First=slide+image)no-repeat;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="nameTag">
  <div id="tagTop">
    <h3>HELLO</h3>
    my name is
  </div>
  <div id="name">
    <div class="show">
      <a href="#">
        <img src="images/name.jpg" width="100%" alt="First slide image" class="center-block">
      </a>
    </div>
  </div>

  <div id="container">

    <div class="miniNav">
      <a href="#" class='img'>First slide image</a>

      <a href="#" class="toggle">Change Font</a>​
      <a href="documents/EvanGrabenstein_graphicCV.pdf" target="_blank">Download Graphic CV</a>​
      <a href="documents/EvanGrabenstein_typedCV.pdf" target="_blank">Download Typed CV</a>

      <a class="hide" href="#">Close CV</a>
    </div>
  </div>
  <div id="tagBottom">
  </div>

</div>

Answer №2

It seems that by default, jQuery's slideToggle() function uses display:block. However, you can manually set it to display: inline-block using a callback, as detailed in this response from user black.

To achieve this, your code should be:

$("#container").hide();
$(".show").click(function() {
    $("#container").slideToggle("slow", function() {
        if ($("#container").is(':visible'))
            $("#container").css('display','inline-block'); 
    });
});

Furthermore, make sure to style your container with display: inline-block, unless I have misunderstood your query.

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

The submit option fails to appear on the screen in the JsonForm library

I've been using the JsonForm library ( https://github.com/jsonform/jsonform ) to define a form in HTML. I have set up the schema and form of the JsonForm structure, but for some reason, the "onSubmit" function that should enable the send button is not ...

Personalizing the mat-checkbox

I'm trying to customize the checked icon in angular material mat-checkbox. Currently, it displays a white tick icon inside a colored background box, but I want to replace the tick with a cross when it is checked. After spending all day searching, I ha ...

Click the edit button to access the options in the material table

https://i.stack.imgur.com/Inyow.png Currently, I am utilizing Material Table within Reactjs to display the table Data. However, I have encountered a hurdle where I need to alter state upon clicking on the edit option/icon. My objective is not to modify th ...

What could be causing my function to execute thrice?

I cannot seem to figure out why my tag cloud is causing the required function to run multiple times instead of just once when I click on a tag. This issue persists whether using jQuery or plain JavaScript. What am I missing? The code I have is very simple, ...

Tips for implementing the same autocomplete feature across multiple form fields

Struggling to add multiple products and provide auto-suggest for each product name? You're not alone. It seems like the auto suggest feature is only working for the first product. Can anyone point out what's going wrong here? Here's my HTML ...

Issue with updating robot's direction using string in Javascript not resolving

Javascript Developing a simple game where a robot moves on a 5x5 board based on user input commands 'L' (move left), 'R' (move right), and 'F' (move forward) from a starting position while facing North (N). Clicking the Move ...

`The dilemma of z-index in a dropdown menu nested within an animated card`

Having an issue that I have attempted to simplify in this StackBlitz example (the actual project is created with Angular components and Bootstrap, etc.): https://stackblitz.com/edit/angular-bootstrap-4-starter-njixcw When incorporating a dropdown within ...

Transferring information from Vue Component to Vuex storage

I am currently working with a Laravel API route that looks like this: Route::get('c/maintenances/{contractor_user_id}', 'Maintenance\Api\ApiContractorMaintenanceController@index'); The contractor_user_id parameter is dynamic ...

How to create list item bullets with a star icon using reactstrap/react?

As a machine learning professional looking to enhance my frontend skills, I am curious about how to create list item bullets using a custom star bullet image. Could anyone please share a complete HTML/CSS example with me? Thank you in advance! ...

Having trouble initiating a "curl:localhost:3000" connection, receiving a URI Error message

Recently delving into the realm of node js, I have embarked on a journey to start up a server and experiment with an app designed to trim URLs. However, I find myself at an impasse. Environment: Windows Text Editor: VSCode Below is my code for index.js ...

When using Firestore in Android, I encounter a nodejs error regarding headers being sent prematurely

Currently, I am utilizing a webview in order to display content from a nodejs server. While requesting a page works as expected, accessing firestore results in an error where it seems like nodejs is attempting to resend the page. Below is the code for an a ...

Help kids find solutions

Here is the HTML code I am working with: <div class = "touch" onclick="do(this)"> <span>text01</span> <span>text02</span> <span>text03</span> <span>text04</span> <div class = "findMe">a ...

Guide on printing in an Ionic application using print.js without the need to open the printer setup page

Our team is currently working on an Ionic web application that utilizes printer functionality. To enable printing, we have integrated the Print.js npm package. However, when we initiate the print method, a setup page displaying details such as printer na ...

The AbortController feature does not initiate the cancellation of an axios.get request

Currently, I'm experimenting with utilizing AbortController to cancel an API call. The axios library is being used for this particular call. Before integrating it into my project, I decided to test the cancellation procedure with a simple call: const ...

Utilize Angular to transform a standard text string within a JSON object into a properly formatted URL

Welcome to my first Stack Overflow question! I usually find the answers I need on my own, but this time I could use some guidance as I delve into Angular. I have a directive that pulls the name and URL from a JSON object and generates HTML with the name a ...

Passing a JavaScript Object to an ASP.NET Handler

How can I send a JavaScript object to an ASP.NET Handler and extract its values? I have defined a complex type object as follows: function AccountObjCreate() { var AccountsView = {}; AccountsView.Username = null; AccountsView.Email = null; AccountsView.P ...

The versions of my npm and node are not compatible, despite using nvm

I have recently started working with node and npm. I need to run a program repository for my job, which requires compatibility with node version 10.13.0 or even 8.11. I attempted to install nvm, but now every time I try to execute any npm command (includ ...

How come my gifs appear tiny even though I've adjusted their width to 32% each?

I am trying to display three gifs in a row, each taking up one-third of the width of the page. However, when I preview the page, the gifs appear tiny. I have set the divs to 32% each and the gifs should fill 100% of their respective div. Here is the code s ...

Error message: An uncaught promise was encountered, despite adding a catch function. I am unable to identify the issue causing this error

Why is the added catch block not functioning properly? function maxRequest(url = ``, times = 3) { // closure function autoRetry (url, times) { console.log('times = ', times); times--; return new Promise((resolve, reject) => ...

Exporting JSON data to an Excel file using an array of objects with embedded arrays

I am currently developing a fitness application that allows users to create workout routines and download them as an excel file. The data is structured as an array of objects, with each object representing a workout date and containing details of the exerc ...