Skip to content

Instantly share code, notes, and snippets.

@bezenson
Last active August 29, 2015 14:00
Show Gist options
  • Save bezenson/11344945 to your computer and use it in GitHub Desktop.
Save bezenson/11344945 to your computer and use it in GitHub Desktop.
Instead of box-sizing: border-box for inputs, textarea and others
function embedInputs($selector) {
$selector.each(function() {
var $this = $(this);
$this.width(1);
if($this.is('textarea')) {
$this.css({'min-width': '', 'max-width': ''});
}
var additionalWidth =
parseInt($this.css('border-left-width')) +
parseInt($this.css('border-right-width')) +
parseInt($this.css('margin-left')) +
parseInt($this.css('margin-right')) +
parseInt($this.css('padding-left')) +
parseInt($this.css('padding-right')),
parentWidth = $this.parent().width(),
countedWidth = parentWidth-additionalWidth;
setTimeout(function() {
$this.width(countedWidth);
if($this.is('textarea')) {
$this.css({
'min-width': countedWidth,
'max-width': countedWidth
});
}
}, 10); // avoiding lags while window resizing (for big JS files)
});
}
$(document).ready(embedInputs($('.embed')));
$(window).resize(embedInputs($('.embed')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment