Exploring JQuery 3.1.1 in combination with the powerful CSS flexbox techniques

When utilizing the fade functions in JQuery, an element that initially has 'display: none' and is then faded out will have JQuery inject 'display: block' when faded in.

In a flexbox layout, this can cause the element to wrap to the next line.

If I remove display:flex from the parent container, it behaves as intended.

Is there a way to remove display:flex only from specific elements that should be hidden initially?

One workaround is to use:

$('el').fadeIn().css('display','inline-block');

However, this approach may not be suitable for this situation. Any alternative suggestions?

Check out the demo below:

$('#clickme').click(function() {
  $('#test').fadeToggle();
  return false;
});
#flex {
  display: flex; /* BREAKS JQUERY FADE, JQUERY INJECTS DISPLAY:BLOCK; */
  flex-direction: column;
}
#main {
  flex: 1;
}
.hidden {
  display: none;
}
<script type="text/javascript" src="//code.jquery.com/jquery-3.1.1.min.js"></script>
<body id="flex">
  <div id="main">
    <span class="tools"> 
      <a class="a" href="" id="clickme">Click Me</a>
      <span id="test" class="hidden">I should be inline!</span>
    </span>
  </div>
</body>

If you remove display:flex from #flex, the behavior is as desired.

Answer №1

It is a known fact that when you display an element using jQuery, it has to guess the appropriate display style because of CSS's flawed design. The existence of dislay: none should never have been allowed.

jQuery tackles this issue through its getDefaultDisplay function:

function getDefaultDisplay( elem ) {
    var temp,
        doc = elem.ownerDocument,
        nodeName = elem.nodeName,
        display = defaultDisplayMap[ nodeName ];

    if ( display ) {
        return display;
    }

    temp = doc.body.appendChild( doc.createElement( nodeName ) );
    display = jQuery.css( temp, "display" );

    temp.parentNode.removeChild( temp );

    if ( display === "none" ) {
        display = "block";
    }
    defaultDisplayMap[ nodeName ] = display;

    return display;
}

This function essentially creates a temporary element of the same type and places it in the body to determine the default display value for the element being shown. While this method may seem naive, some argue that older versions of jQuery handled this better.

The main issue arises when styling the body as a flex container, causing child elements to become blockified. As a result, the computed display of the dummy element inserted by jQuery will be set to "block", affecting the target element even if it is not part of the flex layout.

To avoid this problem, it is recommended to refrain from using display: flex on the body and instead wrap the content in a separate container.

$('#clickme').click(function() {
  $('#test').fadeToggle();
  return false;
});
#flex {
  display: flex;
  flex-direction: column;
}
#main {
  flex: 1;
}
.hidden {
  display: none;
}
<script type="text/javascript" src="//code.jquery.com/jquery-3.1.1.min.js"></script>
<div id="flex">
  <div id="main">
    <span class="tools"> 
      <a class="a" href="" id="clickme">Click Me</a>
      <span id="test" class="hidden">I should be inline!</span>
    </span>
  </div>
</div>

Answer №2

To achieve the desired layout, flexbox must be applied to the .tools span.

  $(document).ready(function() {

    $('#clickme').click(function() {
      $('#test').fadeToggle();
      return false;
    })

  });
#flex {
  display: flex;
  /* Applying this property breaks the jQuery fade effect as it injects display:block; */
  //flex-direction: column;

}
#main {
  flex: 1;
}
.tools {
  display: flex;
}
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body id="flex">

  <div id="main">

    <span class="tools"> 
    <a class="a" href="#01" id="clickme">Click Me</a>
    <span id="test" class="hidden">I should be inline!</span>
    </span>

  </div>

Answer №3

Finally, a solution like this is the key:

$.fn.inline = function() {
    this.css({'display':'inline'});
};

To implement:

$('element').fadeToggle().inline();

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

Place a list with scrolling and overflow features side by side

Having trouble getting my headers to scroll with overflow auto and white-space nowrap. Can't figure out why it's not working. I want to create hyperlinks within headers to link to specific parts of the website. I have the code for hyperlinking s ...

Using flexbox auto margin within nested elements: A guide

Is it possible to achieve consistent margins between the heading and list items using flexbox? section { display: flex; background: #eee; } h1 { background: #bbb; } ul { display: flex; flex: 1; list-style-type: none; background: #ccc; ...

Struggling to center a div within another div using margin auto

Hey there, I'm looking for some help with my code. Here's what I have: .center{ width: 100%; margin: 0 auto; border: 1px solid red; } .nav{ background: #606060; width: 90%; } HTML <!DOCTYPE html> <html> < ...

Pair up balls that have matching numbers

A software application was designed with the following features: It can create 4 balls on the screen, each containing a numerical value Users have the ability to input values for new circles to be generated Upon clicking a button, 4 new circles with num ...

Send a JavaScript variable to Twig

I am trying to pass a JavaScript variable to a twig path but the current method I am using is not working as expected. <p id="result"></p> <script> var text = ""; var i; for (varJS = 0; varJS < 5; varJS++) { text += "<a href= ...

The Angular Material Theme is not being properly displayed on the mtx-datetimepicker component

Issue: While using directives from ng-matero for DateTime with Angular Material for application design, the dateTimepicker element is not applying the angular material theme as desired. https://i.sstatic.net/zPBp3.png Code Example The app.module.ts file i ...

Aligning a picture with a heading

I'm currently working on a website header design where I am struggling to align the logo and site title horizontally. As someone who is new to CSS, any guidance or tips would be greatly appreciated! The logo image has the following styling: .logo { ...

jQuery has identified the JSON object as a string

Currently, I am attempting to extract information from JSON that is received through an AJAX call using jQuery. To be more specific, the JSON is being encoded by PHP and appears as follows: {"1":{"key1":"value1","key2":"value2"},"0":{"key1":"value1","key ...

javascript code for subtracting values in arrays

I am facing a scenario where I have 3 arrays of the same length. My requirement is to automatically subtract the values in the first two arrays without any user input. If the result of subtraction is negative, I need to identify the index of that array num ...

The jQuery toggle function is malfunctioning when applied to the rows of a table

I have been experimenting with toggling table rows using jquery. Initially, the state is hidden This feature is functioning properly $('.table tr.items.' + name).each(function() { $(this).show(); }); Furthermore, this snippet of code is wor ...

Add rows to the table dynamically and then execute the script

My table dynamically creates rows when a button is clicked, and there is an input box with an auto suggest script. The auto complete works fine on the default first box, but not on the dynamically added row. How can I make the auto complete script work o ...

Printed pages featuring neatly organized two-column div layout

Forgive me if this question has already been asked, but I need some guidance: I am designing reports using HTML and CSS which are then converted into PDF format. The report requires a two-column layout on most pages, so I am using: <div class="two_col ...

Ways to determine if an AngularJS modal is currently displayed

I am currently in the process of determining whether a modal is opened or closed. However, I keep encountering an error that says "cannot read property of open." To address this issue, I realize that I need to connect with $modal.open and retrieve the resu ...

Looking to remove a specific field from a URL with jQuery

I am trying to extract a specific tag from a string, like in this example: url = 'https://example.com/tag33?tag=17&user=user123'; The goal is to retrieve the value of the tag. If anyone has a solution or suggestion on how to achieve this, ...

I encountered a frustrating issue with saving user input data using localStorage

One of my goals is to store the current inputs even if the user refreshes or closes the browser. The issue I'm facing is that when I select Yes in the radio button, then refresh the page or close the browser and reopen it, the No button is checked wh ...

Utilize jQuery to dynamically assign classes to list items within a unordered list based on the length of an array

HTML: <ul class="tickboxes"> <li><i class="fa fa-check"></i></li> <li><i class="fa fa-check"></i></li> <li><i class="fa fa-check"></i>< ...

How can I prevent CSS styles from affecting all the tables on my website?

One issue I'm facing is that the CSS code intended for one table is affecting all the other tables on my website. Here is an example of the CSS code: tr:last-child td:last-child { border-bottom-right-radius:3px; } /*effects map*/ td { background: ...

Manipulating Width in CSS

I am facing a challenge with the HTML code below where an automated software inserts the initial CSS style g2f0 <div class="linelevel row"> <div class="g2f0 col-sm-6 form-group"> <label for="name">Name</label> < ...

Why isn't my output being shown on the screen?

Why is the output (refresh value) not being displayed? function myFunction() { $a = document.getElementById("examplehtml").value; document.write("<big><bold>"); document.write($a); document.write("</bold></big>"); $b = ...

At times, the animation in SetInterval may experience interruptions

I have created an animation using a sequence of images. The animation runs smoothly with the setinterval function, but for some reason, it occasionally pauses. I've shared a fiddle where you can see this pause happening. Click Here to See the Unwante ...