You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTML复选框与H2标签垂直居中对齐问题咨询

Fix Vertical Alignment Between Checkbox and H2 Tag

Got it, let's get that checkbox and your "Childcares" heading aligned perfectly vertically. Here are two straightforward, reliable solutions:

Flexbox is the simplest way to handle vertical alignment these days. Just add a few styles to your parent container, and it'll take care of the rest:

/* Style the parent container */
.custom-control.custom-checkbox {
  display: flex;
  align-items: center; /* This is the key for vertical centering */
  gap: 10px; /* Optional: adds clean space between checkbox and heading */
}

/* Keep your existing h2 styles (no need to change much here) */
h2.childcares {
  font-family: Source Sans;
  color: black;
  background-color: #ff9f01;
  display: inline;
  padding: 5px;
  border: 1px solid blue;
  border-radius: 15px;
}

Why this works: The align-items: center property tells the flex container to line up all child elements along the vertical axis. The gap property gives you consistent spacing without relying on messy margins that might break layout elsewhere.

2. Inline-Block + Vertical-Align (For Older Browser Compatibility)

If you need to support older browsers that don't handle flexbox well, this method does the trick:

/* Target both checkbox and h2 to align them */
.custom-control.custom-checkbox .custom-control-input,
h2.childcares {
  display: inline-block;
  vertical-align: middle; /* Aligns elements to their vertical center */
}

/* Update h2 styles (swap inline for inline-block for better stability) */
h2.childcares {
  font-family: Source Sans;
  color: black;
  background-color: #ff9f01;
  padding: 5px;
  border: 1px solid blue;
  border-radius: 15px;
}

Why this works: Setting both elements to inline-block lets them sit side-by-side while retaining layout properties like padding. The vertical-align: middle rule ensures they line up perfectly along their center points, no awkward misalignment.

Quick Check

If things still look off, use your browser's dev tools to inspect the elements—sometimes existing line-height on the parent container or unexpected margins/padding can override your alignment styles.

内容的提问来源于stack exchange,提问作者ziggy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:51:55