联系表格7将图像添加到每个单选输入

时间:2019-01-29 17:13:37

标签: wordpress radio-button

我正在使用Wordpress中的联系表7构建一个表单,该表单具有一些无线电输入。我想在每个单选按钮附近放一个小图像(某种显示织物纹理的标签)。 有谁知道我该如何实现? 也许有一些工作正常的插件,例如ACF,它们与CF7或jQuery代码段兼容,可以插入带有<img>标签的html行。

我需要这样的布局: enter image description here

2 个答案:

答案 0 :(得分:2)

联系表格7在每个输入字段中都有唯一的ID或类别,您可以定位该类别或ID来添加样式

我已经完成了此示例以演示该过程,您的代码可能与此不同

<input class="radio1" type="radio">

CSS:

input.radio1 {
    height: 50px;
    width: 50px;
    display: block; /* You Can remove display block, since your buttons are already stacked */
}
input.radio1:after {
    background-image: url('images/image.img');
    content: '';
    width: 50px;
    height: 50px;
    display: inline-block;
    background-size: cover;
    margin-left: 35px;
}

enter image description here

或者,如果您已经计划使其看起来那样,则可以将背景图像应用于该绿色框。

答案 1 :(得分:0)

Wasim的解决方案可以工作,但是如果您需要使用img标签制作它,可以使用以下方法:

$(".radio-image span span").unwrap(); //Remove spans between input and label tags
$(".wpcf7-list-item-label").remove(); //Remove the label that added by Contact Form 7
$(".radio-image input+label").each(function () {
    const id = $(this).prop("for");
    $(this).parent().find("input").attr("id", id);
});

这是联系表格7的标记

    <div class="radio-image">                
        [radio radio-name "Value"]
        <label for="label-1"><img src="path/to/image.jpg"></label>
    </div>

其余是CSS工作。

.radio-image {
  margin-top: 20px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
  }
  [type="radio"] {
    display: none;

    + label {
      position: relative;

      &:before {
        content: "";
        position: absolute;
        top: -5px;
        left: -5px;
        border-radius: 50%;
        background: $white;
        width: 20px;
        height: 20px;
        z-index: 2;
        border: 1px solid rgba(0, 0, 0, 0.3);
        box-shadow: 0 0 0 5px $white;
      }
      &:after {
        content: "";
        width: 12px;
        height: 12px;
        background: $primary-color;
        border-radius: 50%;
        position: absolute;
        top: 5px;
        left: 5px;
        transform: translateX(-50%) translateY(-50%);
        display: none;
        z-index: 3;
      }
    }

    &:checked + label::after {
      display: block;
    }

    &:checked + label img {
      filter: grayscale(0) !important;
    }
  }
}