Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centered header with button in the same line

I want to create a CENTERED header with button on right.
I already have something like this:

h1{
  text-align : center;
  margin-right : 32px;
}

button{
  position: absolute;
  top: 32px;
  right: 32px;
}
<html>
<body>
<div><h1>test</h1><button style="float: right;">test</button></div>
<div style="text-align: center;">text</div>
</body>
</html>

but in this case header isn't centered to full width page.
also, if header is too long it should not cover button..

Thank you for reading!

like image 588
koowalsky Avatar asked Nov 04 '16 09:11

koowalsky


People also ask

How do I align a button on the same line?

If you have multiple buttons that should sit side-by-side on the same line, add the data-inline="true" attribute to each button. This will style the buttons to be the width of their content and float the buttons so they sit on the same line.

How do I put the button on the right side of the header?

If you want to move the button to the right, you can also place the button within a <div> element and add the text-align property with its "right" value to the "align-right" class of the <div>.

How do you put a heading and paragraph on the same line in HTML?

Just make your <h1> display:inline. This way, it will render in the normal text flow.

How do I center only one heading in HTML?

For example, if you wanted the heading to be centered, you could add class="center" to the <h1> tag or another heading tag.


2 Answers

If flexbox is an option, you can center the h1 and position the button to the right:

div {
  display: flex;
  justify-content: center;
  position: relative;
}
h1 {
  text-align: center;
}
button {
  position: absolute;
  right: 0;
}
<div>
  <h1>test</h1>
  <button>test</button>
</div>

Cheers!

like image 180
kukkuz Avatar answered Nov 15 '22 00:11

kukkuz


You could solve this by using flex-box or floats. You also have to slightly modify your markup.

<div class="header">
  <h1>Headline</h1>
  <button>Button</button>
</div>

FlexBox:

.header {
  display: flex;
}

.header h1 {
  flex-grow: 1;
}

.header button {
  margin-left: 20px;
}

Floats:

.header {
  overflow: hidden;
}

.header button {
  float: right;
  margin-left: 20px;
}
like image 25
marcobiedermann Avatar answered Nov 14 '22 23:11

marcobiedermann