Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I place a button next to a heading?

Tags:

html

css

position

I thought using float: right; would fix this, but it makes the button appear outside of the div. How do I solve this?

HTML

<div id="main">   <h1>Title</h1> <button>Button</button> </div> 

CSS

#main {   width: 200px;   border: 1px dotted black; } h1 {   margin: 0; } button {   float: right; } 

JSFiddle

like image 997
Joe Avatar asked Apr 11 '15 13:04

Joe


People also ask

Can a heading be inside a button?

As such, nesting a heading element inside of a button element will cause failures for WCAG requirement 1.3. 1, Info and Relationships, because the heading has lost semantic meaning. This rule checks <button> elements to see if they contain heading ( <h1> - <h6> ) elements, and gives an error message if they are found.

How can I get h1 button in same line?

Give your h1 display: inline-block to allow your elements to occupy the same row... In my case I wanted the heading text centered, so I was able to get that by replacing "display: inline-block" with "text-align: center".

How do you position a button on the top of a page?

You need to absolutely position the buttons inside the header. To do so, add position:relative to the button's parent element and add position: absolute and something like top: 10px; right: 30px; to the buttons.


1 Answers

Give your h1 display: inline-block to allow your elements to occupy the same row...

#main {    width: 200px;    border: 1px dotted black;  }  h1 {    margin: 0;      display: inline-block;  }  button {    float: right;  }
<div id="main">    <h1>Title</h1> <button>Button</button>  </div>
like image 93
Turnip Avatar answered Sep 21 '22 12:09

Turnip