2

I am having a submenu made with an Ul Li. When you display the sub menu, the li are shown without any border.

With a simple javascript code, when you click on a li I do this :

.actif-link {
  border: 1px solid rgb(173, 178, 178);
  padding: 5px;
}

About JS :

$("ul.filter-ul > li > label > a").on("click", function(event) {
    event.preventDefault();
    $this = $(this);

    if ($this.hasClass("actif-link")) {
        $this.removeClass("actif-link");
    } else {
        $this.addClass("actif-link");           
    }
});

Well. The problem is that my items are aligned this way :

item1-item2-item3

Imagine I click on the first item, when I add my class (with the border + inner padding) I have this :

item1--item2-item3

The clicked item move.

This is really troublesome and I don't find a way to solve it actually.

Should I put each item in a DIV with a fix width and apply the border to the width ? Is there no other way ?

I thank you in advance for the time spent on my request.

2
  • can you post a fiddle? Commented Jun 23, 2015 at 15:46
  • You are right, here is a fiddle : jsfiddle.net/bj14efaj Commented Jun 23, 2015 at 16:11

3 Answers 3

2

The items are moving because you are adding an extra 6 pixels for each side that weren't there to begin with. When the item is first rendered, it might be x pixels wide, but when clicked, it is now border + padding + x + padding + border pixels wide.

A quick workaround around is to set the border and padding on the items right away, but set border-color: transparent. Then on the click event, you can change the color to whatever you want. The padding is never effected, and the border is always there for size, but only seen when it is needed.

edit: here's your updated fiddle https://jsfiddle.net/bj14efaj/3/

Sign up to request clarification or add additional context in comments.

Comments

0

Can you add display:block; or display:inline-block; in your css?

.actif-link {
  border: 1px solid rgb(173, 178, 178);
  padding: 5px; 
}

The padding does work. If you want the padding to only appear on the right please add padding-right instead of padding.

Comments

0

If i understand your problem well i suggest to add

position:relative

and in class .actif-link also add

left:-1px; right:-1px; top:-1px; bottom:-1px;

EDIT

Ok sorry without fiddle it was hard to imagine.

So here is my solution :

border : 1px solid transparent

This one fix all the problem becouse it is existing but it dont move , just colorize

check out the fiddle and let me know if it is what are you looking for :)

i also overload you classes to help it works :)

2 Comments

Well it changes nothing, then I added a fiddle in order to show what happen when you click and what I mean by "the item is moving after click"
Ok , i change it. Please check and let me know :)

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.