I am attempting to add a bottom border to a div for the purpose of a navigation bar. The effect I am trying to achieve:
Currently, I have the following code:
$("a").click(function() {
$("a").removeClass("current");
$(this).addClass("current");
});
.container {
}
.container .item {
float: left;
list-style-type: none;
margin: 0 1px;
}
.container .item a {
color: black;
text-decoration: none;
background-color: green;
width: 50px;
font-size: 13px;
text-align: center;
font-weight: bold;
display: table-cell;
vertical-align: middle;
height: 40px;
}
.container .item a.current {
border-bottom: 2px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="item">
<a class="current" href="#">Page 1</a>
</div>
<div class="item">
<a href="#">Page 2</a>
</div>
<div class="item">
<a href="#">Page 3</a>
</div>
<div class="item">
<a href="#">Page 4</a>
</div>
<div class="item">
<a href="#">Page 5</a>
</div>
<div class="item">
<a href="#">Page 6</a>
</div>
</div>
I cannot find a way to add the empty space in between the content of the div and the bottom border without it being the same colour as the div background.
In any case you can try this out. &. selected { color: #284660; border-bottom: 2px solid #284660; padding-bottom:10px ; // this should give you some spacing. }
The border-spacing property sets the distance between the borders of adjacent cells. Note: This property works only when border-collapse is separate.
To create extra space between elements on the page CSS uses two properties, the padding and margin properties. The picture below shows what padding and margin do. Padding is the distance between the element's content area and any border that might be applied to the element. Margin is extra space around the element.
You can use margin-bottom: in your css.
As it currently stands you can't do this. You can't add a gap between an element and its own border. You can, however, add the border to its parent element (the div.item
element in this case), then add padding-bottom
to that same element to separate it from the a
element:
$("a").click(function() {
$(".current").removeClass("current");
$(this).parent().addClass("current");
});
.container {
}
.container .item {
float: left;
list-style-type: none;
margin: 0 1px;
}
.container .item a {
color: black;
text-decoration: none;
background-color: green;
width: 50px;
font-size: 13px;
text-align: center;
font-weight: bold;
display: table-cell;
vertical-align: middle;
height: 40px;
}
.container .item.current {
border-bottom: 2px solid red;
padding-bottom: 4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="item current">
<a href="#">Page 1</a>
</div>
<div class="item">
<a href="#">Page 2</a>
</div>
<div class="item">
<a href="#">Page 3</a>
</div>
<div class="item">
<a href="#">Page 4</a>
</div>
<div class="item">
<a href="#">Page 5</a>
</div>
<div class="item">
<a href="#">Page 6</a>
</div>
</div>
Note that I've also modified your JavaScript to add this .current
class to the li
element and not the clicked a
element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With