gpt4 book ai didi

HTML/CSS : Rectangle with fixed height, 垂直/水平居中文本,以及随着内部文本展开的最小宽度

转载 作者:可可西里 更新时间:2023-11-01 13:41:12 25 4
gpt4 key购买 nike

如标题所述,我正在尝试制作一个具有固定高度、垂直/水平居中文本和最小宽度的矩形,该宽度会在内部文本溢出时扩展。此外,我希望能够“动态”将此矩形放置在任何位置,因此我不希望对坐标进行任何绝对的硬编码。

这是我迄今为止尝试过的:https://www.w3schools.com/code/tryit.asp?filename=G830P14CNCOT

它的问题是:1)文本没有完全居中2) 不适合矩形的内容将在其下方下降而不是增加其宽度。

为方便起见,这是与上面链接的 playground 中相同的代码:

<html>
<style type="text/css">
.container {
min-height: 2em;
position: relative; /* so center-area can be positioned absolute */
min-width: 3rem;
border-style: solid;
border-width: 0.08rem;
display: inline-block;
background: #99d1ef;
}
.center-area {
/* let it fill the whole container */
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
text-align: center;
}

.center-area:before {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}

.centered {
display: inline-block;
vertical-align: middle;
background: #99dc60;
white-space: nowrap;
}
</style>
<body>
<div class="container">
<div class="center-area">
<span class="centered">
short
</span>
</div>
</div>
<div class="container">
<div class="center-area">
<span class="centered">
loooong
</span>
</div>
</div>
</body>
</html>

最佳答案

.meh {
min-height: 50px;
display: inline-flex;
min-width: 100px;
align-items: center;
justify-content: center;
border: 1px solid #ccc;
}
<div class="meh">
<span contenteditable="true">edit meh!</span>
</div>

w3schools 不是 W3C .

虽然后者是负责实现网络标准的组织,但“学校”是一家企业,从 adwords 中获利,试图从幻想中赚钱他们以某种方式隶属于财团。简而言之,一种寄生虫。

因为 w3schools 是一家企业,他们需要付费编辑他们的内容,而且他们的内容往往...不是很好。

如果您正在寻找友好且详尽的学习资源(除了 w3c 的官方 HTMLCSS 文档 - 这对初学者来说可能过于技术化),请考虑使用 MDN Web Docs .
与 w3schools 不同,它们是非营利性的,并得到网络开发社区的大力(免费)支持,包括所有主要浏览器、 W3C。

关于HTML/CSS : Rectangle with fixed height, 垂直/水平居中文本,以及随着内部文本展开的最小宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57966328/

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