The initial recommendation advised utilizing inline-block
and float
for element positioning, but it is now outdated. A more modern and adaptable approach would involve using flex
.
#main {
border: 1px dotted black;
display: flex;
align-items: center; /* Align elements vertically to the center */
}
h1 {
margin: 0;
}
button {
margin-left: auto; /* Move this element to the right */
}
<div id="main">
<h1>Title</h1> <button>Button</button>
</div>
Previous solution
To have your h1
element and other elements on the same row, apply display: inline-block
to h1
...
#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>