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

Reviewing the element names in a jQuery-selected array of objects

I'm new to javascript and the jquery library and I need to work with an array of input fields that have specific names for validation. Each input field is named NS[0], NS[1], etc., and the total number of these fields is generated dynamically in the c ...

TypeAhead.js and Bloodhound displaying an uneven quantity of search outcomes

I have a frontend setup with TypeAhead and Bloodhound integration, fetching JSON data from a Play/Scala server. The version of Typeahead being used is 0.11.1. Here is how the implementation looks: HTML: <div id="typeahead" class="col-md-8"> < ...

What is the best way to retrieve the input value from post.ejs?

app.js var express = require('express'); var bodyParser = require('body-parser'); var app = express(); var passport = require('passport'); var localStrategy = require('passport-local'); var axios = require("axi ...

What is preventing select2's multiselect feature from functioning properly when using a CDN?

I have implemented the following code snippet: <head> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel=& ...

Testing infinite scrolling with the help of protractor

It seems like the infinite scrolling feature in ng-grid is not exactly infinite, but it's the closest comparison I could come up with for what I am observing. In our application, we are using ng-grid to display data in a table with approximately 170 ...

Retrieve information from internet sources

I'm attempting to extract the initial paragraph from Wikipedia by utilizing only JavaScript. Essentially, my goal is to: document.getElementsByTagName("P")[0] However, this content is not present on my own website; instead, I aim to retrieve a speci ...

The dominance of CSS over other attributes in styling

Imagine having a main body text div and a navigation div for a website. What if you want to make links in the body text green instead of blue, but have the links in the navigation div be red? If you use CSS to make links green, then all links turn green. ...

Exploring design techniques in React Native

I am currently part of a team working on a react native project with multiple contributors. My goal is to find an effective way to segregate the styling tasks from coding, allowing UI developers to work independently without relying on code developers. In ...

Checkbox not appearing in datagrid when using Material-UI

There are some key code snippets related to the datagrid below const apiRef = React.useRef(null); const [gotApiRef, setGotApiRef] = useState(false); const [gotApiRef2, setGotApiRef2] = useState(false); console.log("apiRef.current: ", apiR ...

looping through a linked data object with ng-repeat

I am working with a Node object in my Angular controller. Each Node contains a next property which points to the next item: $scope.Stack = function () { this.top = null; this.rear = null; this.size = 0; this.max_size = 15; ...

Jquery trigger causing href links to malfunction

Here is a JavaScript example: var sampleFunction = function(){ return { initialize : function(data) { this.sendRequest({ action : 'login' }); }, loginAction : function() { ...

My image is being cropped on larger screens due to the background-size: cover property

I am currently in the process of designing a website with a layout consisting of a Header, Content, and Footer all set at 100% width. To achieve a background image that fills the screen in the content section, I utilized CSS3 with background-size:cover pr ...

Issue in Jasmine test: 'Spy should have been invoked'

I've encountered an issue while writing a Jasmine test case for the following Angular function. The test case failed with the message "Expected spy [object Object] to have been called". $scope.displayTagModelPopup = function() { var dial ...

Unending React cycles - invoking setState() within a render onClick

Recently delving into React and facing an issue with rendering a button component. My goal is to create a button that, upon being clicked, fetches data and displays it as a list below the button. To achieve this, I am attempting conditional rendering. I ut ...

React components do not re-render when the context value changes

The issue with React not re-rendering when the context value changes persists I am using tailwindcss, React(v18.2.0), and vite(3.2.4). I have already attempted i want that clicking on TodoItem should change the completed value in the todo using React con ...

Fancybox is experiencing some technical difficulties

Can anyone help me troubleshoot why my fancybox code isn't working properly after adding the script below to the body? Any thoughts on what might be causing this issue? $(document).ready(function() { $("a#example4").fancybox({ 'opa ...

Show a modal component from another component in Angular 2

As a newcomer to Angular, I'm working on a modal component that changes from hiding to showing when a button with (click) is clicked. The goal is to integrate this modal into my main component, allowing me to display the modal on top of the main conte ...

Choose a looping function in React JS that iterates over an array of objects

I have an array of objects let arr = [0: {received: "Return Received", approved: "Approved", rejected: "Rejected"} 1: {authorized: "Authorized", received: "Return Received"}} I am looking to populate a < ...

Searching MySQL data with ng-repeat filter

I am facing a challenge in populating a div tag with ng-repeat and data from a MySQL database. My intention is to utilize ng-repeat for its filtering capabilities in the future. The dilemma lies in the integration of Angular and SQL. My desired HTML struc ...

The jQuery change event does not fire once the DIV has been rendered, while working with CakePHP

Whenever a room_id is chosen from the drop-down menu, I utilize the JS helper to automatically fill in the security_deposit and room_rate input fields. However, there seems to be an issue where selecting a room_id causes the room_rate_update jQuery change ...