Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stick button to right side of div

Tags:

html

css

http://jsfiddle.net/kn5sH/

What do I need to add in order to stick the button to the right side of the div on the same line as the header?

HTML:

<div>     <h1> Ok </h1>     <button type='button'>Button</button> </div> 

CSS:

div {     background: purple; }  div h1 {     text-align: center; }  div button { } 

More specifically:

  1. The right side of the button should be x pixels away from the right edge of the div.
  2. It should be on the same line as the header.
  3. Should be contained in the div (stuff like float or relative positioning pops it out of the div visually)

What CSS techniques can be used to do this? Thanks.

like image 982
Andrew Avatar asked May 30 '13 09:05

Andrew


People also ask

How do I put the button on the right side of a div?

The right side of the button should be x pixels away from the right edge of the div. It should be on the same line as the header. Should be contained in the div (stuff like float or relative positioning pops it out of the div visually)

How do I move a button to the right CSS?

You can also move you button to right by applying text-align: right; to it's parent. In your case it's parent is body.

How do I fix the button on the right side in HTML?

Answer: Use the text-right Class You can simply use the class . text-right on the containing element to right align your Bootstrap buttons within a block box or grid column. It will work in both Bootstrap 3 and 4 versions.

How do you align a button to the right Flexbox?

Approach #1: Use align-self: flex-end on the button. This would make the button appear in the rightmost corner of your parent. Approach #2: In case you want more control over the position of the button in terms of alignment with the text area, you can wrap the button in a div and keep the float right on the button.


2 Answers

Normally I would recommend floating but from your 3 requirements I would suggest this:

position: absolute; right: 10px; top: 5px; 

Don't forget position: relative; on the parent div

DEMO

like image 116
Andy Avatar answered Sep 22 '22 16:09

Andy


Another solution: change margins. Depending on the siblings of the button, display should be modified.

button {   display: block;   margin-left: auto;   margin-right: 0; } 
like image 26
Tonatio Avatar answered Sep 23 '22 16:09

Tonatio