Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery.height() and float parameter in CSS

I'd noticed a strange behaviour of jquery.height() function. Have a look at the following code.

CSS:

div.text-field {
    font-family: sans-serif;
    margin: 3px;
    float: left;
}

HTML:

<div id="someid">
  <div class="text-holder">
    <div class="text-field">text here</div>
  </div>
</div>

JS:

console.log($("someid").find("text-holder").height());

The last line outputs 0 if I have float: left; in CSS file, and otputs real height if I remove float: left;. What is the reason of such a behaviour? Can I use height() function together with float: left;?

like image 913
Eugeny89 Avatar asked Dec 12 '12 15:12

Eugeny89


People also ask

How do I get the height of an element in CSS?

Unfortunately, it is not possible to "get" the height of an element via CSS because CSS is not a language that returns any sort of data other than rules for the browser to adjust its styling. Your resolution can be achieved with jQuery, or alternatively, you can fake it with CSS3's transform:translateY(); rule.

How do you apply a float in CSS?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

How can check CSS property value in jQuery?

Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css(“propertyName”);


1 Answers

When float elements are within a container, that element does not apply the height of the container, because the element is no longer in the "flow". It is removed from the current element, and applied to it's parent, hence the issue. You can fix it by using either inline-block, or clear: both

like image 152
karthikr Avatar answered Oct 08 '22 08:10

karthikr