gpt4 book ai didi

html - CSS 避免容器调整大小以适合粗体文本

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:40 25 4
gpt4 key购买 nike

有点复杂的标题,但很容易演示

#txt {
padding: 5px;
}
#txt:hover {
font-weight: bold;
}
#test {
display: inline-block;
border: solid 1px black;
min-width: 100px;
}
<div id="test">
<div id="txt">This is some text</div>
</div>

所以我就有了上面的情况。

我可以用 CSS 做些什么来避免容器(测试)在我将鼠标悬停在文本上时调整大小?就像占用调整大小的填充或边距一样。或者设置宽度,使其足以处理粗体文本(但仍然是动态的)。

最佳答案

这里有一个想法,使用 before 和 after 属性根据粗体文本宽度调整元素的大小。不幸的是,这需要更改您的标记。

#txt {
line-height: 1.4em;
padding: 5px;
text-align: center;
}
#txt::before {
content: attr(data-content);
display: block;
font-weight: bold;
height: 0px;
visibility: hidden;
}
#txt::after {
content: attr(data-content);
font-weight: normal;
}
#txt:hover::after {
font-weight: bold;
}
#test {
display: inline-block;
border: solid 1px black;
min-width: 100px;
}
<div id="test">
<div id="txt" data-content="This is some text"></div>
</div>

或者,使用文本阴影将文本加粗。

#txt {
padding: 5px;
}
#txt:hover {
text-shadow: 0.5px 0px 0px #555, -0.5px 0px 0px #555;
}
#test {
display: inline-block;
border: solid 1px black;
min-width: 100px;
}
<div id="test">
<div id="txt">This is some text</div>
</div>

关于html - CSS 避免容器调整大小以适合粗体文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36345498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com