Mitro
Mitro

Reputation: 1260

Border add pixel to div

I've modified a navbar in html and css. In the bar there are links to other pages, I wanted that when I go over them with the mouse the div inside the navbar dedicated to the link change color with a medium red border, the problem is that with border when I go over a link the navbar became wider.

 #access {
       background: #222; /* Show a solid color for older browsers */
       background: -moz-linear-gradient(#252525, #0a0a0a);
       background: -o-linear-gradient(#252525, #0a0a0a);
       background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#252525), to(#0a0a0a)); /* older webkit syntax */
       background: -webkit-linear-gradient(#252525, #0a0a0a);
       -webkit-box-shadow: rgba(0, 0, 0, 0.4) 0px 1px 2px;
       -moz-box-shadow: rgba(0, 0, 0, 0.4) 0px 1px 2px;
       box-shadow: rgba(0, 0, 0, 0.4) 0px 1px 2px;
       clear: both;
       display: block;
       margin: 0 auto 6px;
       width: 100%;
       text-align: center;
       line-height: 0;
}
#access ul {
    font-size: 13.5px;
    list-style: none;
    margin: 0 0 0 -0.8125em;
    padding-left: 0;
    display:inline-block;
}
    #access li:hover > a,
    #access ul ul :hover > a,
    #access a:focus {
        background: #efefef;
        border-bottom-style:solid;
        border-width:5px;
        border-color:red;
    }

Upvotes: 0

Views: 1312

Answers (2)

gvee
gvee

Reputation: 17161

Correct. This is because CSS is based on the box model where the total visible width of an object is actually:

visible width = width + padding + border

box-sizing is new to the CSS specification but has good browser support (albeit with vendor prefixes). This basically makes the total visible width = width. The borders, padding, etc, are all taken away from the width!

visible width = width

Read this article for more information: http://css-tricks.com/box-sizing/

DEMO

JSFiddle: http://jsfiddle.net/ErY9T/

Note the distinct difference in the widths.

HTML

<div class="default"></div>
<div class="box-sized"></div>

CSS

div {
    width: 300px;
    height: 100px;
    background-color: lime;
    margin: 10px;
}
.default {
    border: 10px solid red;
}
.box-sized {
    border: 10px solid red;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

Upvotes: 1

Matt Saunders
Matt Saunders

Reputation: 4371

put a transparent border on the default state, or use the same colour as the background it sits on, i.e:

a {
  border-bottom: 2px solid #222
}
a:hover {
  border-bottom: 2px solid red;
}

Upvotes: 3

Related Questions