0

I'm using jquery autocomplete (+ jquery-ui.min.css), and I'm having issues with the styling. Most previous answers conclude with adding !important to the css, which I'd like to avoid.

Simple code (Autocomplete connects with the search-input id):

<div class="user-search">
  <form>
    <%= text_field_tag :user, params[:query], id: "search-input" %>
  </form>
</div>

However, viewing the dev tools, the this ul class, associated with autocomplete, gets pushed out of the containing div, just inside the body tag.

<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content">

Is there a better way to override jquery ui styling?

Thanks!

2
  • The usual way to manage the widget's position is through its position option, not via CSS. Can you add more details about your particular use case? The question as it stands a bit vague Commented Sep 21, 2015 at 11:29
  • Thanks @blgt I've updated the question to include the html. That's good to know. Do you suggest to override most the styling in the js file with options? Commented Sep 21, 2015 at 11:39

2 Answers 2

1

Just use this selector:

.ui-autocomplete.ui-front.ui-menu.ui-widget.ui-widget-content {
  /* Overridden Styles */
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @PreveenKumar. That works for certain styles like background-color, however others, like position, top, width are all overridden by generated inline styles. Might you have suggestions?
Ah okay. I understand the issue now.
1

It looks like you're concerned about the menu's containment, so you're looking for is the appendTo option. From the documentation:

the parents of the input field will be checked for a class of ui-front. If an element with the ui-front class is found, the menu will be appended to that element. Regardless of the value, if no element is found, the menu will be appended to the body.

So, to change that to the containing form element:

$("#search-input").autocomplete({
    // other options...
    appendTo: ".user-search > form"
});

For the scenario described there's no need to mess around with the CSS. Use caution if you do

ref. the list of options: http://api.jqueryui.com/autocomplete/

1 Comment

Thats it! Thanks! Also found a similar good answer here stackoverflow.com/a/20817840/2956085

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.