css - 多行下划线按钮
问题描述
我在使用 box-shadow 为按钮文本添加下划线并将其包裹在多行时遇到问题。它应该只使用CSS来解决。
需要使用以下HTML(不能添加HTML标签、特殊字符等):
<button class="button">
<span class="button__inner">
<span class="button__text">
Button with very very very long example text
</span>
<svg class="button__icon" width="24" height="24" viewBox="0 0 24 24">
<path d="M4,11V13H16L10.5,18.5L11.92,19.92L19.84,12L11.92,4.08L10.5,5.5L16,11H4Z" />
</svg>
</span>
</button>
要求:
- 使用box-shadow为多行文本添加下划线。
- 不要添加额外的 HTML(需要使用上面提供的 HTML 结构)。
.button__inner 必须是 display: flex 或 inline-flex。- 图标需要右对齐(在它自己的列中),垂直居中并且没有下划线。
.button {
background: none;
border: none;
font-size: 12px;
cursor: pointer;
}
.button:focus {
outline: none;
}
.button__inner {
display: flex;
align-items: center;
justify-content: flex-start;
text-align: left;
}
.button__text {
box-shadow: inset 0 -1px currentColor;
}
.button:hover .button__text {
box-shadow: none;
}
.button__icon {
width: 1em;
height: 1em;
}
p {
font-size: 14px;
line-height: 1.2em;
}
<div style="max-width: 200px">
<p>
Following button using correct HTML, however box-shadow does not underline correctly on multiple lines. The icon needs to be on the right in its own column so therefore adding display: inline; to .button__inner is not an option.
</p>
<button class="button">
<span class="button__inner">
<span class="button__text">
Button with very very very long example text
</span>
<svg class="button__icon" width="24" height="24" viewBox="0 0 24 24">
<path d="M4,11V13H16L10.5,18.5L11.92,19.92L19.84,12L11.92,4.08L10.5,5.5L16,11H4Z" />
</svg>
</span>
</button>
<p>
Below is the visually desired result, however it uses an additional span element. How to achieve this using just CSS and without altering the HTML?
</p>
<button class="button">
<span class="button__inner">
<span>
<span class="button__text">
Button with very very very long example text
</span>
</span>
<svg class="button__icon" width="24" height="24" viewBox="0 0 24 24">
<path d="M4,11V13H16L10.5,18.5L11.92,19.92L19.84,12L11.92,4.08L10.5,5.5L16,11H4Z" />
</svg>
</span>
</button>
</div>
这甚至可能只使用 CSS 吗?
解决方案
在您的情况下使用text-decoration: underline
代替是否有效box-shadow
?像这个例子:https ://codepen.io/annaazzam/pen/PoZEJwX
推荐阅读
- kubernetes - ClusterIP 服务没有在 Pod 之间平均分配流量
- r - R在嵌套的for循环中附加数据帧
- flutter - 渲染库捕获的异常 A RenderFlex 在右侧溢出了 98349 像素。相关的导致错误的小部件是 AppBar
- mysql - MySQL 5.7 对未知级别的层次结构数据的递归查询
- python - 如何确定 3D DataSet 样条插值的系数
- html - 滚动时,如果没有正文内容与导航重叠,就无法使用模糊过滤器使导航栏保持粘性?
- kubernetes - kubectl rollout status - 命令何时完成?
- java - 传递数组列表
从android本机代码到反应本机中的java脚本代码的bluetoothList - dialogflow-es - 接收“(我的应用程序名称)现在没有响应。请稍后再试”在 Google 上的操作上
- linux - 某些 Cassandra 节点上的高 CPU 使用率和流量