首页 > 解决方案 > 在电子邮件中的 css 边框三角形中居中文本

问题描述

我正在尝试在电子邮件中使用 css 边框生成一个三角形并在其中放入一些动态文本(数字),但我想将其设置为三角形中心,并且我不能使用定位,因为它已从内联 css 中删除

我已经尝试将三角形 css 的 div 和文本跨度放在一个 div 容器中并使用位置对齐它 - 没有用

<div style="<%= MailComponents.create_style('display' => 'inline-block', 'width' => '2px', 'height' => '0px', 'border-radius' => '4px' ,'border-left' => '20px solid transparent', 'border-right' => '20px solid transparent' ,'border-bottom => '30px solid #FC577A', 'color' => '#fff', 'vertical-align' => 'middle', 'line-height' => '28px', 'margin-top' => '-10px') %>">
            <span style="<%= MailComponents.create_style('position' => 'relative', 'left' => '-8px','vertical-align' => 'middle', ) %>">
              <%= some dynamically generated number %>
            </span>
          </div>

尝试使用这个技巧

http://jsfiddle.net/VScFS/57/

没有帮助我想知道这是否可以用表格来完成

标签: cssemailbordercss-shapestext-alignment

解决方案


* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: grid;
  grid-template-columns: auto;
  justify-content: center;
  align-items: center; 
}

.wrapper {
  background: #000;
  width: 200px;
  height: 200px;
  -webkit-clip-path: polygon(0% 87%, 50% 0%, 50% 0%, 100% 87%);
  clip-path: polygon(0% 87%, 50% 0%, 50% 0%, 100% 87%);
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div class="container">
  <div class="wrapper">
  </div>
</div>
</body>
</html>

在父类上使用display:grid和justify-content: center; 将其水平居中对齐!作为参考,您可以查看https://www.w3schools.com/cssref/pr_grid.asp这个链接。希望它会有所帮助


推荐阅读