0

I am trying to create a very simple button in CSS, but am unable to get the arrow in my button to be in the center of my circle.

I used:

top: 50% left: 50% 

to try and center my arrow div inside of my circle div, but of course this only centers the top left corner of my arrow div, and not the center position.

How can I center my div perfectly inside of the div? I would ideally like a solution where I the width and height of both divs does not matter, so I can easily change them at a later date.

My current code: http://jsfiddle.net/zhWGx/

1

7 Answers 7

2

Try this one, make your triangle position:absolute, then top, left bottom, right will be 0. Make it margin:auto. Then you're done.

.triangle { width: 0; height: 0; border-style: solid; border-width: 5px 0 5px 5px; border-color: transparent transparent transparent red; position: absolute; top: 0; left: 0; right:0; bottom:0; margin:auto; } 

Link: http://jsfiddle.net/Vigiliance/zhWGx/4/

P.S.

You will curse IE, when you use this method.

Sign up to request clarification or add additional context in comments.

Comments

2

If you want to able to center an element with unknown dimensions, you can use transformations (translate) on that element.

transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); 

http://jsfiddle.net/zhWGx/23/

Note that this is only supported on modern browser.

1 Comment

Indeed, as long as you don't need to support Internet Explorer 7 & 8, this is an easy solution and will work in each situation.
1

You can try this, if your circle has always the same size : http://jsfiddle.net/zhWGx/1/

.triangle { width: 0; height: 0; border-style: solid; border-width: 10px 0 10px 17.3px; border-color: transparent transparent transparent #ffffff; position: relative; display: block; margin: 10px auto ; top: 10px; } 

Comments

1

This should fix it

 .triangle { width: 0; height: 0; border-style: solid; border-width: 10px 0 10px 17.3px; border-color: transparent transparent transparent #ffffff; background:linear-gradient(transparent, transparent, transparent, #FFFFFF); position: relative; margin-left: 15px; top:24% } 

Comments

0

http://jsfiddle.net/Oliboy50/zhWGx/14/

If you want to adjust the triangle position the way you want :

.triangle { width: 0; height: 0; border-style: solid; border-width: 10px 0 10px 17.3px; border-color: transparent transparent transparent #ffffff; position: absolute; /* absolute (the parent is relative) */ top: 50%; left: 50%; margin-top: -10px; /* triangle height/2 */ margin-left: -6px; /* triangle width/2 (6.85px) or what you want */ } 

Comments

0

You can try:

.triangle { width: 0; height: 0; border-style: solid; border-width: 10px 0 10px 17.3px; border-color: transparent transparent transparent #ffffff; position: relative; top: 25%; left: 40%; } 

Comments

0

If there is only text inside of your div you can do:

.inside-div { display: inline: // for IE display: inline-block; text-align: center; } 

Otherwise:

.parent-div { position: relative; } .inside-div { position: absolute; left: 50%; top: 50%; width: 500px; margin-left: -250px; height: 500px; margin-top: -250px; } 

Something like this http://jsfiddle.net/zhWGx/27/, but in this sutiation you should calculate geomethric position of triangle and than set margins.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.