We currently have an input that we want to align baseline to the content around it. The label wrappers the input and for the most part everything aligns correctly. However we have been wanting to pull the input outside the label, but this seems to be throwing the layout off.
This snippet demonstrates the issue and possible solutions. However, I want to know is there a better option? Or a way to get the label to be ignored by the baseline layout?
<h1>Original <span style="font-size: 14px;">(input within label)</span></h1>
<div style="display: flex; flex-direction: row; align-items: baseline; gap: 8px;">
<div>Test value</div>
<div>
<label for="myInput" style="display: inline-block;">
<div>My label</div>
<input id="myInput" placeholder="test" />
</label>
</div>
<button>My Button</button>
</div>
<h1>Problem <span style="font-size: 14px;">(splitting label and input breaks layout)</span></h1>
<div style="display: flex; flex-direction: row; align-items: baseline; gap: 8px;">
<div>Test value</div>
<div>
<label for="myInput" style="display: block;">My label</label>
<input id="myInput" placeholder="test" />
</div>
<button>My Button</button>
</div>
<h1>Option 1 <span style="font-size: 14px;">(absolute position the label)</span></h1>
<div style="display: flex; flex-direction: row; align-items: baseline; gap: 8px;">
<div>Test value</div>
<div style="position: relative;">
<label for="myInput" style="position: absolute;">My label</label>
<input id="myInput" placeholder="test" style="margin-top: 1rem"/>
</div>
<button>My Button</button>
</div>
<h1>Option 2 <span style="font-size: 14px;">(float label, but label is outside container)</span></h1>
<div style="display: flex; flex-direction: row; align-items: baseline; gap: 8px;">
<div>Test value</div>
<div>
<label for="myInput" style="position: absolute; transform: translateY(-1rem);">My label</label>
<input id="myInput" placeholder="test"/>
</div>
<button>My Button</button>
</div>
2
Answers
Yes, there is a better option: a grid. Grids support
align-items: baseline
, which will achieve the layout you want.Instead of "baseline", use "last baseline"