html - 图标或图像居中,两边有线

标签 html css

我正在尝试创建一些 CSS 以在中心有一个图标或图像,两边都有一条线,但似乎我做错了什么,需要一些帮助。

为简单起见,我只在代码中使用了一个星号。

<div class='line-container'><div class='line-icon'>*</div></div>

.line-icon {
text-align: center;
}

.line-icon::before {
width: 25%;
height: 1px;
border: 1px solid #ccc;
}

.line-icon::after {
width: 25%;
height: 1px;
border: 1px solid #ccc;
}

最佳答案

尝试向您的 ::after::before 添加内容,并设置其显示:

.line-icon {
    text-align: center;
}

/* Joined both selectors, since were pretty much the same */
.line-icon::before,
.line-icon::after {      
    /* Styles kept */
    width: 25%;
    height: 1px;

    /* Changed to border-top (instead of border) to simulate a line better */
    border-top: 1px solid #ccc; 

    /* Styles added */
    display: inline-block;
    content: '';
  
    /* Use padding to vertical align the line */
    /* Use padding in em for a responsive icon height */
    padding-top: 0.5em; 
  
    /* Use margins to give the lines some spacement around the icon */
    /* Use margins in % for a responsive spacement */
    margin-left: 5%; 
    margin-right: 5%;
}
<div class='line-container'><div class='line-icon'>*</div></div>

关于html - 图标或图像居中,两边有线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393297/

相关文章:

android - 防止 css/jquery 弹出窗口允许点击下面的链接(仅限移动设备)

javascript - 我正在研究黑暗模式偏好。 Windows应用程序

jquery - ie9 fallback for bootstrap carousel fade variation

css - 在 Bootstrap 3 中形成并排断线

html - 如何让文本位于 div 之上?

javascript - 仅当新背景图像存在时才更改背景图像

html - Powershell:如何引用第n个元素

html - 为嵌套元素设置不同的不透明度

javascript - 如何根据先前的子宽度计算 div 的宽度

css - 在表 tr td 中使用第 n 个 child