Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to margin an anchor in css?

Tags:

I have a fixed header height 50px. In my body, I have a lot of anchors. The problem is that, when I click on links pointing to anchors, the anchor appears under my fixed header and I lose 50px of content (I need to scroll up of 50px to read content under the header).

Is there a way to margin an anchor of 50px? My body is filled with a lot of box (divs) with a margin between themself, so I can't put in place an empty div of 50px and then anchor after it..

html:

<div id="header"></div> <div id="content">      <div class="box" id="n1"></div>      <div class="box" id="n2"></div>      <div class="box" id="n3"></div> </div> 

css:

#header{     height: 40px;     width: 100%;     margin: 0px;     padding: 0px;     position: fixed;     text-align: center;     z-index:2; }  #content{     padding-top: 50px;      margin: 0px; }  .box {     width: 80%;     margin-left: auto;     margin-right: auto;     vertical-align : top;     padding: 1.4%; /* Keep it in percent (%) */     margin-bottom: 30px;     min-height: 200px; } 
like image 538
Pier-Alexandre Bouchard Avatar asked Feb 18 '12 02:02

Pier-Alexandre Bouchard


People also ask

Why margin is not working on anchor tag?

And on the a tag the margin doesn't work because it's an inline element. You may need to change its display property to inline-block or block .

How can we set the margin for an element in CSS?

The CSS margin properties are used to create space around elements, outside of any defined borders. With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).

How do I change the position of an anchor tag in HTML?

The only solution without involving JS and repositioning your elements is to put the anchor point inside the element you want to scroll to, make it absolute, give it a 100px top margin and hide it. That way, you'd actually scroll to that invisible element, making the wanted element appear right at the top.


2 Answers

If the header is truly fixed then place your anchors in a scrollable div. Then the div containing the anchor will scroll instead of the entire page. Visit the fiddle and click on anchor1. It goes to anchor2. And so forth.

http://jsfiddle.net/mrtsherman/CsJ3Y/3/

css - set overflow hidden on body to prevent default scrolling. Use position absolute on the new content area with top and bottom set. This forces it to stretch to fit the remaining viewport window.

body { overflow: hidden; } #header { position: fixed; top: 0; left: 0; width: 100%; height: 50px; background: gray; border: 1px solid black; } #content {      overflow: scroll;      position: absolute;     top: 50px;     bottom: 0px;     width: 100%;     border: 1px solid blue;  } 

html

<div id="header">header</div> <div id="content">     <div>         Page Content <br />         <a id="a1" href="#anchor2" name="anchor1">Anchor 1</a>                         <a id="a2" href="#anchor1" name="anchor2">Anchor 2</a>        </div> </div>​ 
like image 199
mrtsherman Avatar answered Oct 07 '22 23:10

mrtsherman


Welcome to the future:

.box {   scroll-margin-top: 50px; } 

Source: https://css-tricks.com/almanac/properties/s/scroll-margin/

like image 25
dmoz Avatar answered Oct 07 '22 22:10

dmoz