51黑料不打烊

自定义表单的外观?

此功能可通过提前访问计划使用。 要请求访问,请将包含您的GitHub组织名称和存储库名称的电子邮件(从您的官方地址发送到aem-forms-ea@adobe.com)。 例如,如果存储库URL为https://github.com/adobe/abc,则组织名称为adobe,存储库名称为abc。

表单对于网站上的用户交互至关重要,用户可以在其中输入数据。您可以使用级联样式表 (CSS) 来设置表单字段的样式,以增强表单的视觉呈现效果,并改善用户体验。

自适应表单块可为所有表单字段生成一致的结构。一致的结构使得开发 CSS 选择器更容易根据字段类型和字段名称来选择和设置表单字段的样式。

本文档概述了各种表单组件的 HTML 结构,有助于您了解如何为各种表单字段创建 CSS 选择器,以便设置自适应表单块的表单字段的样式。

在文章的最后:

  • 您可以了解到自适应表单块中包含的默认 CSS 文件的结构。
  • 您可以了解到 Adaptive Forms Block 提供的表单组件中的 HTML 结构,其中包括常规组件和特定组件,例如下拉列表、单选按钮组和复选框组。
  • 您将会学习到如何使用 CSS 选择器根据字段类型和字段名称设置表单字段的样式,从而根据需求实现一致或独特的样式。

了解表单字段类型

在深入研究样式设置之前,让我们回顾一下 Adaptive Forms Block 支持的常见表单字段类型

  • 输入字段:包括文本输入、电子邮件输入、密码输入等。
  • 复选框组:用于选择多个选项。
  • 单选按钮组:用于从一个组中仅选择一个选项。
  • 下拉菜单:也称为选择框,用于从列表中选择一个选项。
  • 面板/容器:用于将相关的表单元素组合在一起。

基本样式设置准则

在设置特定表单字段的样式之前,了解至关重要:

  • :CSS 选择器可让您针对特定的 HTML 元素进行样式设置。您可以使用元素选择器、类选择器或 ID 选择器。
  • :CSS 属性定义元素的外观。用于设置表单字段的样式的常见属性包括颜色、背景颜色、边框、间距、边距等。
  • :CSS 框模型将 HTML 元素的结构描述为由间距、边框和边距包围的内容区域。
  • 贵濒别虫产辞虫/网格:颁厂厂 和是用于创建响应式和灵活设计的强大工具。

为 Adaptive Forms Block 设置表单样式

Adaptive Forms Block 提供了标准化 HTML 结构,简化了选择表单组件并设计其样式的过程:

  • 更新默认样式:您可以通过编辑 /blocks/form/form.css file 来修改表单的默认样式。此文件为表单提供全面的样式,并支持多步骤向导表单。它强调使用自定义颁厂厂变量来轻松进行自定义、维护和跨表单的统一样式。

  • Forms 的 CSS 样式:为确保正确应用样式,请在 main .form form 选择器中包含表单特定的 CSS。这可确保您的样式仅针对主要内容区域内的表单元素,从而避免与网站的其他部分发生冲突。
    示例:

    code language-css
      main .form form input {
          /* Add styles specific to input fields inside the form */
      }
    
      main .form form button {
          /* Add styles specific to buttons inside the form */
      }
    
      main .form form label {
          /* Add styles specific to labels inside the form */
      }
    

组件结构

Adaptive Forms Block 为各种表单元素提供一致的 HTML 结构,确保更轻松地设置样式和管理。您可以使用 CSS 来操作组件以设置样式。

常规组件(下拉菜单、单选按钮组和复选框组除外):

所有表单字段(下拉列表、单选按钮组和复选框组除外)都具有以下 HTML 结构:

通用组件的 HTML 结构
code language-html

  <div class="{Type}-wrapper field-{Name}   field-wrapper" data-required={Required}>
     &lt;label for="{FieldId}" class="field-label">First   Name&lt;/label>
     &lt;input type="{Type}" placeholder="{Placeholder}"   maxlength="{Max}" id={FieldId}" name="{Name}"   aria-describedby="{FieldId}-description">
     <div class="field-description" aria-live="polite"  id="{FieldId}-description">
      Hint - First name should be minimum 3 characters  and a maximum of 10 characters.
     </div>
  </div>
  • 类:div 元素包含几个用于定位特定元素和样式的类。您需要 {Type}-wrapperfield-{Name} 类来开发 CSS 选择器以设置表单字段的样式:
  • {Type}:通过字段类型标识组件。例如,文本 (text-wrapper)、数字 (number-wrapper)、日期 (date-wrapper)。
  • 调狈补尘别皑:通过名称标识组件。字段名称只能包含字母数字字符,名称中的多个连续破折号将替换为单个破折号 (-),并且字段名称中的开头和结尾破折号将被删除。例如,名字 (field-first-name field-wrapper)。
  • 调贵颈别濒诲滨诲皑:它是自动生成的字段的唯一标识符。
  • 调搁别辩耻颈谤别诲皑:它是一个布尔值,指示该字段是否为必填字段。
  • 标签:label 元素为字段提供描述性文本,并使用 for 属性将它与输入元素关联。
  • 输入:input 元素定义要输入的数据类型。例如:文本、数字、电子邮件。
  • 描述(可选):带类 field-descriptiondiv 为用户提供附加信息或说明。

HTML 结构示例

code language-html

<div class="text-wrapper field-first-name field-wrapper" data-required="true">
  &lt;label for="firstName" class="field-label">First Name&lt;/label>
  &lt;input type="text" placeholder="Enter your first name" maxlength="50" id="firstName" name="firstName" aria-describedby="firstName-description">
  <div class="field-description" aria-live="polite" id="firstName-description">
    Please enter your legal first name.
  </div>
</div>
常规组件的 CSS 选择器
code language-css


  /* Target all input fields within any .{Type}-wrapper  */
  main .form form .{Type}-wrapper  &lbrace;
    /* Add your styles here */
    border: 1px solid #ccc;
    padding: 8px;
    border-radius: 4px;
  &rbrace;

  /* Target all input fields within any .{Type}-wrapper  */
  main .form form .{Type}-wrapper input &lbrace;
    /* Add your styles here */
    border: 1px solid #ccc;
    padding: 8px;
    border-radius: 4px;
  &rbrace;

  /* Target any element with the class field-{Name}  */
  main .form form .field-{Name} &lbrace;
    /* Add your styles here */
    /* This could be used for styles specific to all elements with   field-{Name} class, not just inputs */
  &rbrace;
  • .{Type}-wrapper:根据字段类型锁定外部 div 元素。例如,.text-wrapper 会锁定所有文本字段。
  • .field-{Name}:根据特定字段名称进一步选择元素。例如:.field-first-name锁定“名字”文本字段。虽然此选择器可用于定位具有 field-{Name} 类的元素,但务必谨慎。在这种特定情况下,它对于设置输入字段的样式不是很有帮助,因为它不仅针对输入本身,还针对标签和描述元素。建议使用更具体的选择器,例如用于定位文本输入字段(.text-wrapper input)的选择器。

常规组件的示例 CSS 选择器

code language-css

/*Target all text input fields */
main .form form .text-wrapper input &lbrace;
  border: 1px solid #ccc;
  padding: 8px;
  border-radius: 4px;
  color: red;
&rbrace;

/*Target all fields with name first-name*/
main .form form .field-first-name input &lbrace;
  border: 1px solid #ccc;
  padding: 8px;
  border-radius: 4px;
&rbrace;

下拉菜单组件

对于下拉菜单,使用 select 元素而不是 input 元素:

下拉组件的 HTML 结构
code language-html

<div class="{Type}-wrapper field-{Name} field-wrapper" data-required={Required}>
   &lt;label for="{FieldId}" class="field-label">First Name&lt;/label>
   &lt;input type="{Type}" placeholder="{Placeholder}" maxlength="{Max}" id={FieldId}" name="{Name}" aria-describedby="{FieldId}-description">
   <div class="field-description" aria-live="polite" id="{FieldId}-description">
    Hint - First name should be minimum 3 characters and a maximum of 10 characters.
   </div>
</div>

示例 HTML 结构

code language-html

<div class="drop-down-wrapper field-country field-wrapper" data-required="true">
  &lt;label for="country" class="field-label">Country&lt;/label>
  &lt;select id="country" name="country">
    &lt;option value="">Select Country&lt;/option>
    &lt;option value="US">United States&lt;/option>
    &lt;option value="CA">Canada&lt;/option>
  &lt;/select>
  <div class="field-description" aria-live="polite" id="country-description">
    Please select your country of residence.
  </div>
</div>
下拉组件的 CSS 选择器

以下 CSS 列出了下拉组件的一些 CSS 选择器示例。

code language-css

/* Target the outer wrapper */
main .form form .drop-down-wrapper &lbrace;
  /* Add your styles here */
  display: flex;
  flex-direction: column;
  margin-bottom: 15px;
&rbrace;

/* Style the label */
main .form form .drop-down-wrapper .field-label &lbrace;
  margin-bottom: 5px;
  font-weight: bold;
&rbrace;
  • 锁定包装器:第一个选择器 (.drop-down-wrapper) 锁定外部包装器元素,确保样式应用于整个下拉组件。
  • Flexbox 布局:Flexbox 垂直排列标签、下拉菜单和描述以实现干净布局。
  • 标签样式:标签以更粗的字体和微小边距脱颖而出。
  • 下拉样式:select 元素接收边框、间距和圆角以获得精美外观。
  • 背景颜色:设置一致的背景颜色以实现视觉和谐。
  • 箭头自定义:可选样式隐藏默认下拉箭头,并使用 Unicode 字符和定位创建自定义箭头。

单选按钮组

与下拉组件类似,单选按钮组也拥有自己的 HTML 结构和 CSS 结构:

单选按钮组 HTML 结构
code language-html

&lt;fieldset class="radio-group-wrapper field-{Name} field-wrapper" id="{FieldId}" name="{Name}" data-required="{Required}">
   &lt;legend for="{FieldId}" class="field-label">....&lt;/legend>
   <% for each radio in Group %>
   <div class="radio-wrapper field-{Name}">
      &lt;input type="radio" value="" id="{UniqueId}" data-field-type="radio-group" name="{FieldId}">
      &lt;label for="{UniqueId}" class="field-label">...&lt;/label>
   </div>
   <% end for %>
&lt;/fieldset>

示例 HTML 结构

code language-html

&lt;fieldset class="radio-group-wrapper field-color field-wrapper" id="color_preference" name="color_preference" data-required="true">
  &lt;legend for="color_preference" class="field-label">Favorite Color:&lt;/legend>
  <% for each radio in Group %>
    <div class="radio-wrapper field-color">
      &lt;input type="radio" value="red" id="color_red" data-field-type="radio-group" name="color_preference">
      &lt;label for="color_red" class="field-label">Red&lt;/label>
    </div>
    <div class="radio-wrapper field-color">
      &lt;input type="radio" value="green" id="color_green" data-field-type="radio-group" name="color_preference">
      &lt;label for="color_green" class="field-label">Green&lt;/label>
    </div>
    <div class="radio-wrapper field-color">
      &lt;input type="radio" value="blue" id="color_blue" data-field-type="radio-group" name="color_preference">
      &lt;label for="color_blue" class="field-label">Blue&lt;/label>
    </div>
  <% end for %>
&lt;/fieldset>
单选按钮组的 CSS 选择器
  • 定位字段集
code language-css

  main .form form .radio-group-wrapper &lbrace;
    border: 1px solid #ccc;
    padding: 10px;
  &rbrace;

此选择器针对具有 radio-group-wrapper 类的任何字段集。这对于将通用样式应用于整个单选按钮组非常有用。

  • 定位单选按钮标签
code language-css

main .form form .radio-wrapper label &lbrace;
    font-weight: normal;
    margin-right: 10px;
  &rbrace;
  • 根据名称锁定特定字段集中的所有单选按钮标签
code language-css

main .form form .field-color .radio-wrapper label &lbrace;
  /* Your styles here */
&rbrace;

复选框组

复选框组 HTML 结构
code language-html

&lt;fieldset class="checkbox-group-wrapper field-{Name} field-wrapper" id="{FieldId}" name="{Name}" data-required="{Required}">
   &lt;legend for="{FieldId}" class="field-label">....&lt;/legend>
   <% for each radio in Group %>
   <div class="radio-wrapper field-{Name}">
      &lt;input type="checkbox" value="" id="{UniqueId}" data-field-type="checkbox-group" name="{FieldId}">
      &lt;label for="{UniqueId}" class="field-label">...&lt;/label>
   </div>
   <% end for %>
&lt;/fieldset>

示例 HTML 结构

code language-html

&lt;fieldset class="checkbox-group-wrapper field-topping field-wrapper" id="topping_preference" name="topping_preference" data-required="false">
  &lt;legend for="topping_preference" class="field-label">Pizza Toppings:&lt;/legend>
  <div class="checkbox-wrapper field-topping">
    &lt;input type="checkbox" value="pepperoni" id="topping_pepperoni" data-field-type="checkbox-group" name="topping_preference">
    &lt;label for="topping_pepperoni" class="field-label">Pepperoni&lt;/label>
  </div>
  <div class="checkbox-wrapper field-topping">
    &lt;input type="checkbox" value="mushrooms" id="topping_mushrooms" data-field-type="checkbox-group" name="topping_preference">
    &lt;label for="topping_mushrooms" class="field-label">Mushrooms&lt;/label>
  </div>
  <div class="checkbox-wrapper field-topping">
    &lt;input type="checkbox" value="onions" id="topping_onions" data-field-type="checkbox-group" name="topping_preference">
    &lt;label for="topping_onions" class="field-label">Onions&lt;/label>
  </div>
&lt;/fieldset>
复选框组的 CSS 选择器
  • 定位外部包装器:这些选择器定位单选按钮组和复选框组的最外层容器,允许您将常规样式应用于整个组结构。这对于设置间距、对齐方式或其他与布局相关的属性非常有用。
code language-css


  /* Targets radio group wrappers */
  main .form form .radio-group-wrapper &lbrace;
    margin-bottom: 20px; /* Adds space between radio groups */
  &rbrace;

  /* Targets checkbox group wrappers */
  main .form form .checkbox-group-wrapper &lbrace;
    margin-bottom: 20px; /* Adds space between checkbox groups */
  &rbrace;
  • 定位组标签:此选择器定位单选按钮组和复选框组包装器中的 .field-label 元素。这使您能够专门为这些组设置标签样式,从而使它们更加突出。
code language-css

main .form form .radio-group-wrapper legend,
main .form form .checkbox-group-wrapper legend &lbrace;
  font-weight: bold; /* Makes the group label bold */
&rbrace;
  • 定位单个输入和标签:这些选择器提供对单个单选按钮、复选框及其关联标签的更精细控制。您可以使用它们来调整大小、间距或应用更独特的视觉样式。
code language-css

/* Styling radio buttons */
main .form form .radio-group-wrapper input[type="radio"] &lbrace;
  margin-right: 5px; /* Adds space between the input and its label */
&rbrace;

/* Styling radio button labels */
main .form form .radio-group-wrapper label &lbrace;
  font-size: 15px; /* Changes the label font size */
&rbrace;

/* Styling checkboxes */
main .form form .checkbox-group-wrapper input[type="checkbox"] &lbrace;
  margin-right: 5px; /* Adds space between the input and its label */
&rbrace;

/* Styling checkbox labels */
main .form form .checkbox-group-wrapper label &lbrace;
  font-size: 15px; /* Changes the label font size */
&rbrace;
  • 自定义单选按钮和复选框的外观:此技术隐藏默认输入并使用 :before:after 伪元素来创建根据“选中”状态更改外观的自定义视觉效果。
code language-css

/* Hide the default radio button or checkbox */
main .form form .radio-group-wrapper input[type="radio"],
main .form form .checkbox-group-wrapper input[type="checkbox"] &lbrace;
  opacity: 0;
  position: absolute;
&rbrace;

/* Create a custom radio button */
main .form form .radio-group-wrapper input[type="radio"] + label::before &lbrace;
  /* ... styles for custom radio button ... */
&rbrace;

main .form form .radio-group-wrapper input[type="radio"]:checked + label::before &lbrace;
  /* ... styles for checked radio button ... */
&rbrace;

/* Create a custom checkbox */
main .form form .checkbox-group-wrapper input[type="checkbox"] + label::before &lbrace;
  /* ... styles for custom checkbox ... */
&rbrace;

main .form form .checkbox-group-wrapper input[type="checkbox"]:checked + label::before &lbrace;
  /* ... styles for checked checkbox ... */
&rbrace;

面板/容器组件

面板/容器组件的 HTML 结构
code language-html

&lt;fieldset class="panel-wrapper field-{PanelName} field-wrapper">
  &lt;legend for="{id}" class="field-label" data-visible="false">bannerComponent&lt;/legend>
  <div class="{Type}-wrapper field-{Name} field-wrapper">
    &lt;label for="{FieldId}" class="field-label">First Name&lt;/label>
    &lt;input type="{Type}" placeholder="{Placeholder}" maxlength="{Max}" id={FieldId}" name="{Name}">
    <div class="field-description" aria-live="polite" id="{FieldId}-description">
      Hint - First name should be minimum 3 characters and a maximum of 10 characters.
    </div>
  </div>
&lt;/fieldset>

示例 HTML 结构

code language-html

&lt;fieldset class="panel-wrapper field-login field-wrapper">
  &lt;legend for="login" class="field-label" data-visible="false">Login Information&lt;/legend>
  <div class="text-wrapper field-username field-wrapper">
    &lt;label for="username" class="field-label">Username&lt;/label>
    &lt;input type="text" placeholder="Enter your username" maxlength="50" id="username" name="username">
    <div class="field-description" aria-live="polite" id="username-description">
      Please enter your username or email address.
    </div>
  </div>
  <div class="password-wrapper field-password field-wrapper">
    &lt;label for="password" class="field-label">Password&lt;/label>
    &lt;input type="password" placeholder="Enter your password" maxlength="20" id="password" name="password">
    <div class="field-description" aria-live="polite" id="password-description">
      Your password must be at least 8 characters long.
    </div>
  </div>
&lt;/fieldset>
  • Fieldset 元素充当面板容器,具有 panel-wrapper 类和基于面板名称 (field-login) 进行样式设置的附加类。

  • 图例元素 (

    ) 用作面板标题,其中包含文本“登录信息”和类字段标签。data-visible="false" 属性可以与 JavaScript 一起使用来控制标题的可见性。

  • 在字段集中,多个。{Type}-wrapper 元素(在本例中为 .text-wrapper 和 .password-wrapper)代表面板中的各个表单字段。

  • 每个包装器都包含一个标签、输入字段和描述,与前面的示例类似。

面板/容器组件的 CSS 选择器示例
  1. 定位面板:
code language-css

  /* Target the entire panel container */
  main .form form .panel-wrapper &lbrace;
    /* Add your styles here (e.g., border, padding, background color) */
    border: 1px solid #ccc;
    padding: 15px;
    border-radius: 4px;
    margin-bottom: 20px;
 &rbrace;
  • .panel-wrapper 选择器使用 panel-wrapper 类来设置所有元素的样式,为所有面板创建一致的外观。
  1. 定位面板标题:
code language-css

  /* Target the legend element (panel title) */
  .panel-wrapper legend &lbrace;
    /* Add your styles here (e.g., font-weight, font-size) */
    font-weight: bold;
    font-size: 16px;
    padding-bottom: 5px;
    margin-bottom: 10px;
    border-bottom: 1px solid #ddd; /* Optional: create a separation line */
  &rbrace;
  • .panel-wrapper legend 选择器设置面板内图例元素的样式,使标题在视觉上脱颖而出。
  1. 定位面板中的各个字段:
code language-css

/* Target all form field wrappers within a panel */
main .form form .panel-wrapper .{Type}-wrapper &lbrace;
  /* Add your styles here (e.g., margin) */
  margin-bottom: 10px;
&rbrace;
  • .panel-wrapper .{Type}-wrapper 选择器针对面板中具有 .{Type}-wrapper 类的所有包装器,允许您设置表单字段之间的间距样式。
  1. 定位特定领域(可选):
code language-css

  /* Target the username field wrapper */
  main .form form .panel-wrapper .text-wrapper.field-username &lbrace;
    /* Add your styles here (specific to username field) */
  &rbrace;

  /* Target the password field wrapper */
  main .form form .panel-wrapper .password-wrapper.field-password &lbrace;
    /* Add your styles here (specific to password field) */
  &rbrace;
  • 这些可选选择器允许您在面板中锁定特定的字段包装器以实现独特的样式,例如突出显示用户名字段。

可重复面板

可重复面板的 HTML 结构
code language-html

&lt;fieldset class="panel-wrapper field-{PanelName} field-wrapper">
  &lt;legend for="{id}" class="field-label" data-visible="false">bannerComponent&lt;/legend>
  <div class="{Type}-wrapper field-{Name} field-wrapper">
    &lt;label for="{FieldId}" class="field-label">First Name&lt;/label>
    &lt;input type="{Type}" placeholder="{Placeholder}" maxlength="{Max}" id={FieldId}" name="{Name}">
    <div class="field-description" aria-live="polite" id="{FieldId}-description">
      Hint - First name should be minimum 3 characters and a maximum of 10 characters.
    </div>
&lt;/fieldset>

示例 HTML 结构

code language-html

&lt;fieldset class="panel-wrapper field-contact field-wrapper" data-repeatable="true">
  &lt;legend for="contact-1" class="field-label" data-visible="false">Contact Information&lt;/legend>
  <div class="text-wrapper field-name field-wrapper">
    &lt;label for="name-1" class="field-label">Name&lt;/label>
    &lt;input type="text" placeholder="Enter your name" maxlength="50" id="name-1" name="contacts[0].name">
    <div class="field-description" aria-live="polite" id="name-1-description">
      Please enter your full name.
    </div>
  </div>
  <div class="email-wrapper field-email field-wrapper">
    &lt;label for="email-1" class="field-label">Email&lt;/label>
    &lt;input type="email" placeholder="Enter your email address" maxlength="100" id="email-1" name="contacts[0].email">
    <div class="field-description" aria-live="polite" id="email-1-description">
      Please enter a valid email address.
    </div>
  </div>
&lt;/fieldset>

&lt;fieldset class="panel-wrapper field-contact field-wrapper" data-repeatable="true">
  &lt;legend for="contact-2" class="field-label" data-visible="false">Contact Information&lt;/legend>
  <div class="text-wrapper field-name field-wrapper">
    &lt;label for="name-2" class="field-label">Name&lt;/label>
    &lt;input type="text" placeholder="Enter your name" maxlength="50" id="name-2" name="contacts[1].name">
    <div class="field-description" aria-live="polite" id="name-2-description">
      Please enter your full name.
    </div>
  </div>
  <div class="email-wrapper field-email field-wrapper">
    &lt;label for="email-2" class="field-label">Email&lt;/label>
    &lt;input type="email" placeholder="Enter your email address" maxlength="100" id="email-2" name="contacts[1].email">
    <div class="field-description" aria-live="polite" id="email-2-description">
      Please enter a valid email address.
    </div>
  </div>
&lt;/fieldset>

每个面板具有与单个面板示例相同的结构,并具有附加属性:

  • data-repeatable="true":此属性指示可以使用 JavaScript 或框架动态重复面板。

  • 唯一 ID 和名称:面板中的每个元素都有一个唯一 ID(例如 name-1、email-1)和基于面板索引的名称属性(例如 name="contacts[0 ].name”)。这样可以在提交多个面板时进行正确的数据收集。

可重复面板的 CSS 选择器
  • 定位所有可重复面板:
code language-css

  /* Target all panels with the repeatable attribute */
 main .form form .panel-wrapper[data-repeatable="true"] &lbrace;
    /* Add your styles here (e.g., border, margin) */
    border: 1px solid #ccc;
    padding: 15px;
    border-radius: 4px;
    margin-bottom: 20px;
  &rbrace;

选择器对所有可重复的面板进行样式设置,确保一致的外观和感觉。

  • 定位面板中的各个字段:
code language-css

/* Target all form field wrappers within a repeatable panel */
main .form form .panel-wrapper[data-repeatable="true"] .{Type}-wrapper &lbrace;
  /* Add your styles here (e.g., margin) */
  margin-bottom: 10px;
&rbrace;

此选择器对可重复面板中的所有字段包装器进行样式设置,从而保持字段之间的间距一致。

  • 定位特定领域(在面板内):
code language-css

/* Target the name field wrapper within the first panel */
main .form form .panel-wrapper[data-repeatable="true"][data-index="0"] .text-wrapper.field-name &lbrace;
  /* Add your styles here (specific to first name field) */
&rbrace;

/* Target all

文件附件

文件附件的 HTML 结构
code language-html

<div class="file-wrapper field-{FileName} field-wrapper">
  &lt;legend for="{id}" class="field-label" data-visible="false"> File Attachment &lt;/legend>
  <div class="file-drag-area">
    <div class="file-dragIcon"></div>
    <div class="file-dragText">Drag and Drop To Upload</div>
    &lt;button class="file-attachButton" type="button">Attach&lt;/button>
    &lt;input type="file" accept="audio/*, video/*, image/*, text/*, application/pdf" id="{id}" name="{FileName}" autocomplete="off" multiple="" required="required">
  </div>
  <div class="files-list">
    <div data-index="0" class="file-description">
      <span class="file-description-name">ClaimForm.pdf</span>
      <span class="file-description-size">26 kb</span>
      &lt;button class="file-description-remove" type="button">&lt;/button>
    </div>
  </div>
</div>

示例 HTML 结构

code language-html

<div class="file-wrapper field-claim_form field-wrapper">
  &lt;legend for="claim_form" class="field-label" data-visible="false">File Attachment&lt;/legend>
  <div class="file-drag-area">
    <div class="file-dragIcon"></div>
    <div class="file-dragText">Drag and Drop To Upload</div>
    &lt;button class="file-attachButton" type="button">Attach&lt;/button>
  </div>
  <input type="file" accept="audio/*, video/*, image/*, text/*, application/pdf" id="claim_form"
         name="claim_form" autocomplete="off" multiple="" required="required" data-max-file-size="2MB">
  <div class="files-list">
    </div>
</div>
  • 类属性使用为文件附件提供的名称(肠濒补颈尘冲蹿辞谤尘)。
  • 输入元素的 id 和名称属性与文件附件名称 (claim_form) 匹配。
  • 文件列表部分最初是空的。当文件上传时,它会用 JavaScript 动态填充。
文件附件组件的 CSS 选择器
  • 定位整个文件附件组件:
code language-css

/* Target the entire file attachment component */
main .form form .file-wrapper &lbrace;
  /* Add your styles here (e.g., border, padding) */
  border: 1px solid #ccc;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 20px;
&rbrace;

该选择器设置整个文件附件组件的样式,包括图例、拖动区域、输入字段和列表。

  • 定位特定元素:
code language-css

/* Target the drag and drop area */
main .form form .file-wrapper .file-drag-area &lbrace;
  /* Add your styles here (e.g., background color, border) */
  background-color: #f0f0f0;
  border: 1px dashed #ddd;
  padding: 10px;
  text-align: center;
&rbrace;

/* Target the file input element */
main .form form .file-wrapper input[type="file"] &lbrace;
  /* Add your styles here (e.g., hide the default input) */
  display: none;
&rbrace;

/* Target individual file descriptions within the list (populated dynamically) */
main .form form .file-wrapper .files-list .file-description &lbrace;
  /* Add your styles here (e.g., margin, display) */
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
&rbrace;

/* Target the file name within the description */
main .form form .file-wrapper .files-list .file-description .file-description-name &lbrace;
  /* Add your styles here (e.g., font-weight) */
  font-weight: bold;
&rbrace;

这些选择器允许您单独设置文件附件组件各个部分的样式。您可以调整样式以符合您的设计偏好。

设置组件的样式

您还可以根据表单字段的特定类型 ({Type}-wrapper) 或单个名称 (field-{Name}) 来设置其样式。这允许更精细地控制和自定义表单外观。

基于字段类型的样式设置

您可以使用 CSS 选择器来锁定特定字段类型并一致地应用样式。

HTML 结构
code language-html

<div class="{Type}-wrapper field-{Name} field-wrapper" data-required={Required}>
   &lt;label for="{FieldId}" class="field-label">First Name&lt;/label>
   &lt;input type="{Type}" placeholder="{Placeholder}" maxlength="{Max}" id={FieldId}" name="{Name}" aria-describedby="{FieldId}-description">
   <div class="field-description" aria-live="polite" id="{FieldId}-description">
    Hint - First name should be minimum 3 characters and a maximum of 10 characters.
   </div>
</div>

示例 HTML 结构

code language-html

<div class="text-wrapper field-name field-wrapper" data-required="true">
  &lt;label for="name" class="field-label">Name&lt;/label>
  &lt;input type="text" placeholder="Enter your name" maxlength="50" id="name" name="name">
</div>

<div class="number-wrapper field-age field-wrapper" data-required="true">
  &lt;label for="age" class="field-label">Age&lt;/label>
  &lt;input type="number" placeholder="Enter your age" id="age" name="age">
</div>

<div class="email-wrapper field-email field-wrapper" data-required="true">
  &lt;label for="email" class="field-label">Email Address&lt;/label>
  &lt;input type="email" placeholder="Enter your email" id="email" name="email">
</div>
  • 每个字段都包含在具有多个类的 div 元素中:

    • {Type}-wrapper:标识字段的类型。例如,form-text-wrapperform-number-wrapperform-email-wrapper
    • field-{Name}:通过名称标识字段。例如,form-nameform-ageform-email
    • field-wrapper:所有字段包装器的通用类。
  • data-required 属性指示该字段是必填字段还是可选字段。

  • 每个字段都有相应的标签、输入元素和潜在的附加元素(例如占位符和描述)。

示例 CSS 选择器
code language-css

/* Target all text input fields */
main .form form .text-wrapper input &lbrace;
  /* Add your styles here */
&rbrace;

/* Target all number input fields */
main .form form .number-wrapper input &lbrace;
  /* Add your styles here */
  letter-spacing: 2px; /* Example for adding letter spacing to all number fields */
&rbrace;

基于字段名称的样式设置

您还可以按名称锁定各个字段以应用唯一样式。

HTML 结构
code language-html

<div class="{Type}-wrapper field-{Name} field-wrapper" data-required={Required}>
   &lt;label for="{FieldId}" class="field-label">First Name&lt;/label>
   &lt;input type="{Type}" placeholder="{Placeholder}" maxlength="{Max}" id="{FieldId}" name="{Name}" aria-describedby="{FieldId}-description">
   <div class="field-description" aria-live="polite" id="{FieldId}-description">
    Hint - Enter the 6 digit number sent to your mobile number.
   </div>
</div>

示例 HTML 结构

code language-html

<div class="number-wrapper field-otp field-wrapper" data-required="true">
  &lt;label for="otp" class="field-label">OTP&lt;/label>
  &lt;input type="number" placeholder="Enter your OTP" maxlength="6" id="otp" name="otp" aria-describedby="otp-description">
  <div class="field-description" aria-live="polite" id="otp-description">
    Hint - Enter the 6 digit number sent to your mobile number.
   </div>
</div>
示例 CSS 选择器
code language-css

main .form form .field-otp input &lbrace;
   letter-spacing: 2px
&rbrace;

此 CSS 针对位于具有类 field-otp 的元素内的所有输入元素。表单的 HTML 结构遵循 Adaptive Forms Block 的惯例,这意味着有一个标有“form-otp”类的容器包含名为“otp”的字段。

另请参阅

{{universal-editor-see-also}}

recommendation-more-help
fbcff2a9-b6fe-4574-b04a-21e75df764ab