如何在css拉伸

时间:2018-06-14 16:39:01

标签: html css css3

我已阅读this回答,但我使用here中的以下代码制作六边形。虽然六边形是响应但是不能使它拉长。我不太喜欢使用边框解决方案,并希望添加类来覆盖现有样式。



.socialIcon {
    width: 100px;
    height: 100px;
    color: black;
    transition: color 400ms ease-in;
}
.color {
    background-color: #1ae694;
    transition: background-color 500ms ease-in;
}
.hexagonWrapper {
    text-align: center;
    position: relative;
    display: inline-block;
    margin: 50px;
}
.iconContainer {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
.hexagon {
    height: 100%;
    width: calc(100% * 0.57735);
    display: inline-block;
}
.hexagon:before {
    position: absolute;
    top: 0;
    right: calc((100% / 2) - ((100% * 0.57735) / 2));
    background-color: inherit;
    height: inherit;
    width: inherit;
    content: "";
    transform: rotateZ(60deg);
}
.hexagon:after {
    position: absolute;
    top: 0;
    right: calc((100% / 2) - ((100% * 0.57735) / 2));
    background-color: inherit;
    height: inherit;
    width: inherit;
    content: "";
    transform: rotateZ(-60deg);
}

<a class="socialIcon hexagonWrapper" href="#" target="_blank">
  <div class="color hexagon"></div>
  <div class="iconContainer">
    <span>H</span>
  </div>
</a>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

更新回答

我建议您使用此answer中的Temani方法 它使用多个linear-gradient来创建背景,并且在HTML中需要更少的元素:

.socialIcon {
  color: black;
  transition: color 400ms ease-in;
}

.color {
  --color: #1ae694;
  background-color: var(--color);
}

.hexagonWrapper {
  text-align: center;
  position: relative;
  display: inline-block;
}

.hexagon {
  padding: 10px 60px;
  display: inline-block;

  background:
   linear-gradient(to top right,    var(--color) 49.5%, transparent 50.5%) top right    / 20px 50%,
   linear-gradient(to bottom right, var(--color) 49.5%, transparent 50.5%) bottom right / 20px 50%,
   linear-gradient(to bottom left,  var(--color) 49.5%, transparent 50.5%) bottom left  / 20px 50%,
   linear-gradient(to top left,     var(--color) 49.5%, transparent 50.5%) top left     / 20px 50%,
   linear-gradient(var(--color), var(--color)) center / calc(100% - 40px) 100%;
  background-repeat:no-repeat;
}

/* Added after comment */
.hexagon:hover {
  --color: #aae;
}
<a class="socialIcon hexagonWrapper" href="#" target="_blank">
  <div class="color hexagon"><p>xxx</p></div>
</a>

⋅ ⋅ ⋅

旧答案

由于拉长的六边形不再是六边形,我提出了一种完全不同的方法来实现这一点,使用:

  • heightcolor
  • 的CSS变量
  • border在伪元素上创建“三角形”形状。

这是一个片段:

.socialIcon {
  --color: #1ae694; /* CSS Variable */
  --h: 100px; /* CSS Variable */
  width: 100px;
  height: var(--h); /* Using CSS var */
  color: black;
  transition: color 200ms ease-in;
}

.socialIcon:hover {
  --color: #aae;
}

.color {
  background-color: var(--color); /* Using CSS var */
  transition: background-color 500ms ease-in;
}

.hexagonWrapper {
  text-align: center;
  position: relative;
  display: inline-block;
  margin: 50px;
}

.iconContainer {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.hexagon {
  height: 100%;
  width: 100%;
  display: inline-block;
}

/* TAKIT: I changed everything below */

.hexagon::before,
.hexagon::after {
  position: absolute;
  height: 0;
  width: 0;
  content: "";
  border: 0 solid transparent;
  /* border is full height but half width, to render a thin arrow shape
  This can be parameterized here: */
  border-width: calc(var(--h)/2) calc(var(--h)/4); /* Using CSS var */
  transition:
    border-left-color 500ms ease-in,
    border-right-color 500ms ease-in;
}

.hexagon::before {
  left: 0;
  transform: translate(-100%, 0);
  border-right-color: var(--color); /* Using CSS var */
}

.hexagon::after {
  right: 0;
  transform: translate(100%, 0);
  border-left-color: var(--color); /* Using CSS var */
}
<a class="socialIcon hexagonWrapper" href="#" target="_blank">
  <div class="color hexagon"></div>
  <div class="iconContainer">
    <span>H</span>
  </div>
</a>

使用该解决方案,唯一需要调整的是var: --h以获得实际的六边形或细长的六边形......

答案 1 :(得分:0)

创建&#34;可扩展&#34;的最简单方法六角形只是在容器的每一端放一个,并且中心的颜色相同。这是执行该操作,您可以为中心文本提供负水平边距,以便在必要时收紧间距。我还清理了六角生成代码以使其更具动态性 - 如果您更改.hexagon元素的宽度,其他所有内容都会随之调整大小。

&#13;
&#13;
.color {
    background-color: #1ae694;
    transition: background-color 500ms ease-in;
}
.text-content {
  position: relative;
  z-index: 3;
  display: flex;
}
.text-content span {
  margin: auto;
  font-size: 18px;
}
.expandableHex {
  display: flex;
  flex-direction: row;
  justify-content: center;
  font-size: 0;
}
.hexagon {
  width: 80px;
}
.hexagon-inner {
    width: 100%;
    margin: auto;
    padding-bottom: 173%;
    display: inline-block;
    position: relative;
}
.hexagon-inner:before, .hexagon-inner:after {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: center center;
    background-color: inherit;
    height: 100%;
    width: 100%;
    content: "";
}
.hexagon-inner:before {
    transform: rotate(60deg);
}
.hexagon-inner:after {
    transform: rotate(-60deg);
}
&#13;
<!--<a class="socialIcon hexagonWrapper" href="#" target="_blank">
  <div class="color hexagon"><div class="hexagon-inner"></div></div>
  <div class="color hexagon"><div class="hexagon-inner"></div></div>
  <div class="iconContainer">
    <span>H</span>
  </div>
</a>-->

<div class="expandableHex">
 <div class="hexagon"><div class="color hexagon-inner"></div></div>
 <div class="color text-content"><span>I'm content</span></div>
 <div class="hexagon"><div class="color hexagon-inner"></div></div>
</div>
 
&#13;
&#13;
&#13;