<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Ken Thompson</title>
    <link rel="alternate" type="text/html" href="http://www.kthompson.org/" />
    <link rel="self" type="application/atom+xml" href="http://www.kthompson.org/atom.xml" />
    <id>tag:www.kthompson.org,2009-05-23://1</id>
    <updated>2009-05-25T18:37:42Z</updated>
    
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.25</generator>

<entry>
    <title>Positioning Anchor Links With CSS</title>
    <link rel="alternate" type="text/html" href="http://www.kthompson.org/2009/05/positioning-anchor-links-with-css.html" />
    <id>tag:www.kthompson.org,2009://1.5</id>

    <published>2009-05-25T06:09:21Z</published>
    <updated>2009-05-25T18:37:42Z</updated>

    <summary>Are you using same page anchor links on your website? You may have noticed that when clicked that, by default, the target goes directly to the top of the window with no margin. I personally find this annoying because I...</summary>
    <author>
        <name>Ken Thompson</name>
        
    </author>
    
        <category term="CSS" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="HTML" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="anchorlinks" label="Anchor Links" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="css" label="CSS" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://www.kthompson.org/">
        Are you using same page anchor links on your website? You may have noticed that when clicked that, by default, the target goes directly to the top of the window with no margin. I personally find this annoying because I can&apos;t think of any text in any format that begins immediately at the top of the page. So, here&apos;s a way to stop anchor links from going to the top of the page. 
        <![CDATA[<p>I am not sure how common of a problem nor am I aware of how popular the solution is. I find this particularly useful for sites with a static navigation at the top. Using anchors on this page would normally cause the targeted element to scroll to the top of the page, below the navigation, and invisible to the user.</p>

<h2>First Attempt: Top Margin</h2>
<p>My first attempt at accomplishing this involved adding a top margin to the desired elements. The expected result was to see appear that distance below the top of the window. Unfortunately all this does is start the first element at the desired position but it does not keep it there when linked to it. It also creates an undesired space between this element and the preceding.</p>

<h2>Second Attempt: Top Padding</h2>
<p>For my next attempt I used a top padding. This worked. Using a top padding causes the element to appear that distance below the top of the window this is because padding is on the inside of the element's border and is considered part of the element. Unfortunately, this still gives undesired space between items using this technique.</p>

<h2>Solution: Top Padding, Negative Top Margin</h2>

<p>Since the margin does not affect the anchor positioning while the padding does and both affect the visual position on the page I used a top padding on the desired list of elements and a negative margin to bring them back up to the correct position visually.</p>

<h2>The CSS</h2>

<pre class='brush: css'>

/*Add this class to desired elements*/
.anchor {
	padding-top: 50px; /*Provides a 50px top margin*/
	margin-top: -40px; /*Leave 10px of space between elements*/
}
</pre>

An example can be found <a href="http://examples.kthompson.org/css/anchor_positioning.html">here</a>

]]>
    </content>
</entry>

</feed>
