我有一个水平导航菜单,基本上只是一个 <ul>
,其中的元素并排设置。我没有定义宽度,而是使用填充,因为我希望宽度由菜单项的宽度定义。我加粗当前选定的项目。
问题在于加粗会使单词略微变宽,这会导致其余元素略微向左或向右移动。有没有巧妙的方法可以防止这种情况发生?例如告诉填充忽略加粗导致的额外宽度?我的第一个想法是从“活动”元素的填充中减去几个像素,但这个量是变化的。
如果可能,我想避免对每个条目设置静态宽度,然后居中,而是使用我目前拥有的填充解决方案,以便将来更改条目变得简单。
我有一个水平导航菜单,基本上只是一个 <ul>
,其中的元素并排设置。我没有定义宽度,而是使用填充,因为我希望宽度由菜单项的宽度定义。我加粗当前选定的项目。
问题在于加粗会使单词略微变宽,这会导致其余元素略微向左或向右移动。有没有巧妙的方法可以防止这种情况发生?例如告诉填充忽略加粗导致的额外宽度?我的第一个想法是从“活动”元素的填充中减去几个像素,但这个量是变化的。
如果可能,我想避免对每个条目设置静态宽度,然后居中,而是使用我目前拥有的填充解决方案,以便将来更改条目变得简单。
更新:由于在IE11中,当我使用visibility:hidden时,伪类i:after没有显示,因此我不得不使用B标签来表示标题。
在我的情况下,我想要将(自定义设计的)输入复选框/单选按钮与标签文本对齐,其中文本在输入被选中时变粗。
这里提供的解决方案在Chrome中对我无效。输入和标签的垂直对齐受到了:after伪类的干扰,而-margins并不能解决这个问题。
这是一个修复方法,您可以避免垂直对齐的问题。
/* checkbox and radiobutton */
label
{
position: relative;
display: inline-block;
padding-left: 30px;
line-height: 28px;
}
/* reserve space of bold text so that the container-size remains the same when the label text is set to bold when checked. */
label > input + b + i
{
font-weight: bold;
font-style: normal;
visibility: hidden;
}
label > input:checked + b + i
{
visibility: visible;
}
/* set title attribute of label > b */
label > input + b:after
{
display: block;
content: attr(title);
font-weight: normal;
position: absolute;
left: 30px;
top: -2px;
visibility: visible;
}
label > input:checked + b:after
{
display: none;
}
label > input[type="radio"],
label > input[type="checkbox"]
{
position: absolute;
visibility: hidden;
left: 0px;
margin: 0px;
top: 50%;
transform: translateY(-50%);
}
label > input[type="radio"] + b,
label > input[type="checkbox"] + b
{
display: block;
position: absolute;
left: 0px;
margin: 0px;
top: 50%;
transform: translateY(-50%);
width: 24px;
height: 24px;
background-color: #00a1a6;
border-radius: 3px;
}
label > input[type="radio"] + b
{
border-radius: 50%;
}
label > input:checked + b:before
{
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(45deg);
content: '';
border-width: 0px 3px 3px 0px;
border-style: solid;
border-color: #fff;
width: 4px;
height: 8px;
border-radius: 0px;
}
label > input[type="checkbox"]:checked + b:before
{
transform: translate(-50%, -60%) rotate(45deg);
}
label > input[type="radio"]:checked + b:before
{
border-width: 0px;
border-radius: 50%;
width: 8px;
height: 8px;
}
<label><input checked="checked" type="checkbox"/><b title="Male"></b><i>Male</i></label>
<label><input type="checkbox"/><b title="Female"></b><i>Female</i></label>
有趣的问题。我猜测您在使用float,对吧?
嗯,我不知道任何可以用来摆脱这种字体放大的技术,因此它们会尝试适应所需的最小宽度 - 不同的字体厚度将改变这个值。
我知道的唯一解决方案是避免更改大小,但正如您所说,您不想这样做:将li设置为固定大小。
.menu {
display: flex;
justify-content: space-between;
font-family: sans-serif;
}
.text-shadow {
padding: 0 0.375rem;
}
.text-shadow:first-of-type,
.text-shadow:last-of-type {
padding: 0 0.075rem;
}
.text-shadow:hover {
text-shadow: 1px 1px white;
font-weight: 900;
padding: 0;
}
<nav class="menu">
<div class="text-shadow">Item 1</div>
<div class="text-shadow">Item 2</div>
<div class="text-shadow">Item 3</div>
</nav>