如何与Div一起垂直对齐文本?
我已经使用HTML和CSS创建了一个圆形的多步骤进度UI,在这里,我需要在圆形图标下方添加一个文本,当我添加文本时,横穿条形的线路被向下移动。因为圆的高度根据内容长度增加。即使我们添加内容,如何始终在中心保持线路。
这是它的屏幕截图。
添加内容之前:
“ rel =“ nofollow noreferrer”>
我面临CSS面临的挑战。如果您能帮助我解决这个问题,那就太好了。
这是工作 demo
import CheckIcon from "@mui/icons-material/Check";
import "./styles.css";
export default function App() {
const steps = [1, 2, 3, 4, 5];
return (
<>
<div class="container">
<div class="progress-container">
<div class="progress" id="progress"></div>
{steps.map((val) => (
<div>
<div className={`circle ${val === 4 ? "active" : ""}`}>
{val === 4 ? <CheckIcon sx={{ fontSize: "3rem" }} /> : null}
</div>
<div className="test">
{val % 2 === 0
? "Conservative"
: "Somewhat Conservative but it is okay"}
</div>
</div>
))}
</div>
</div>
</>
);
}
@import url("https://fonts.googleapis.com/css?family=Muli&display=swap");
.container {
text-align: center;
}
.progress-container {
display: flex;
justify-content: space-between;
position: relative;
max-width: 100%;
width: 700px;
}
.progress-container::before {
content: "";
background-color: #767676;
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
height: 4px;
width: 100%;
z-index: -1;
}
.progress {
/* background-color: var(--line-border-fill); */
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
height: 4px;
width: 0%;
z-index: -1;
transition: 0.4s ease;
}
.circle {
background-color: #fff;
color: #999;
border-radius: 50%;
height: 85px;
width: 85px;
display: flex;
align-items: center;
justify-content: center;
border: 3px solid #767676;
transition: 0.4s ease;
cursor: pointer;
font-size: 20px;
}
.circle.active {
border-color: #3769ff;
background-color: #3769ff;
color: #ffffff;
}
.test {
max-width: 85px;
}
**
演示链接在发布的答案评论中添加
I have created a circular multi-step progress UI with HTML and CSS, here I need to add a text underneath the circular icon, When I have added the text, the line which crosses the bar is moved down. Because the height of the circle is increasing based on the content length. How to make the line always at the center even when we are adding content.
here is the screenshot of it.
Before adding the content:
After adding the content:
I am facing challenges with the CSS. It would be great if u could help me with this issue.
Here is the working demo
App.js
import CheckIcon from "@mui/icons-material/Check";
import "./styles.css";
export default function App() {
const steps = [1, 2, 3, 4, 5];
return (
<>
<div class="container">
<div class="progress-container">
<div class="progress" id="progress"></div>
{steps.map((val) => (
<div>
<div className={`circle ${val === 4 ? "active" : ""}`}>
{val === 4 ? <CheckIcon sx={{ fontSize: "3rem" }} /> : null}
</div>
<div className="test">
{val % 2 === 0
? "Conservative"
: "Somewhat Conservative but it is okay"}
</div>
</div>
))}
</div>
</div>
</>
);
}
style.css:
@import url("https://fonts.googleapis.com/css?family=Muli&display=swap");
.container {
text-align: center;
}
.progress-container {
display: flex;
justify-content: space-between;
position: relative;
max-width: 100%;
width: 700px;
}
.progress-container::before {
content: "";
background-color: #767676;
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
height: 4px;
width: 100%;
z-index: -1;
}
.progress {
/* background-color: var(--line-border-fill); */
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
height: 4px;
width: 0%;
z-index: -1;
transition: 0.4s ease;
}
.circle {
background-color: #fff;
color: #999;
border-radius: 50%;
height: 85px;
width: 85px;
display: flex;
align-items: center;
justify-content: center;
border: 3px solid #767676;
transition: 0.4s ease;
cursor: pointer;
font-size: 20px;
}
.circle.active {
border-color: #3769ff;
background-color: #3769ff;
color: #ffffff;
}
.test {
max-width: 85px;
}
Results
** <hr/>
tag length is reduced when minimizing the screen size.**
Demo link is added in the posted answer comment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
希望这是您要实现的目标:
这是我所做的:我使用了HR而不是::之前,但这并不重要,
在您的情况下,以前是
.progress_container的中心
哪个高度包括文本所占据的空间,而不是将其放在父级的中心,将其放在圆圈的中心,如<代码> top:calc(circle_height/2)这是我的更改:
CSS
***
.container的效果.container
移动视图上的宽度***hope this is what you aim to achieve :
this is what i did : i used hr instead of ::before but it doesnt matter,
in your case the before is at the center of
.progress_container
which height includes the space occupied by the text so instead of putting it at the center of the parent put it at the center of the circles like thistop:calc(circle_height / 2 )
here are my changes:
css
*** effect of the
.container
width on mobile view ***