Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add empty space between div's content and bottom border

Tags:

html

css

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:

enter image description here

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.

like image 622
LoveFortyDown Avatar asked Oct 22 '14 14:10

LoveFortyDown


People also ask

How do you put a space between border and bottom of text?

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. }

How do you put space between content and border in HTML?

The border-spacing property sets the distance between the borders of adjacent cells. Note: This property works only when border-collapse is separate.

How do I put a space between text and border in CSS?

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.

How do you put a space at the bottom of a border in CSS?

You can use margin-bottom: in your css.


1 Answers

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.

like image 120
James Donnelly Avatar answered Nov 03 '22 11:11

James Donnelly