Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Positioning - Top and Right

Tags:

css

layout

I'm creating a div which has to have a close button in the upper right corner just like in the image image http://rookery9.aviary.com.s3.amazonaws.com/4655000/4655386_f01b_150x250.jpg

The first image was made in photoshop. I'm trying to do the same but with CSS. "Fechar" is the close button (in Portuguese). What is the better way to properly position it without workarounds, with clean CSS and Web Standards?

Here is my code: http://jsfiddle.net/wZJnd/

This is as far as I could reach.

like image 682
Rodrigo Souza Avatar asked Aug 02 '10 16:08

Rodrigo Souza


People also ask

How do you set the top right position in CSS?

If position: absolute; or position: fixed; - the right property sets the right edge of an element to a unit to the right of the right edge of its nearest positioned ancestor. If position: relative; - the right property sets the right edge of an element to a unit to the left/right of its normal position.

What will be the order of CSS positioning?

What is the CSS position property? The CSS position property defines the position of an element in a document. This property works with the left, right, top, bottom and z-index properties to determine the final position of an element on a page. There are five values the position property can take.


1 Answers

I would use absolute positioning inside a relatively positioned #header:

HTML

<div id="header"> 
  <h1>Your Title</h1>
  <a href="" class="close">Close</a>
</div>

CSS

#header {
    width: 700px;
    height: 200px;
    position: relative;

    text-align: center;

    background: #000 url(the-logo.png) no-repeat 30px 10px;
}

#header .close {
    position: absolute;
    top: 20px;
    right: 20px;
}

This will cause the a.close link to use the #header as its coordinate system and position it 20px from the top and right edge.

In my experience padding, margins and float are more sensitive to rendering inconsistency and font size changes than positioning. As a result, I use position whenever possible.

like image 147
Pat Avatar answered Sep 20 '22 23:09

Pat