使用float,flex和tailwind实现同一个表单注册效果

float方式
html结构

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="index.css">
</head>
<body>
  <form action="#" class="container">
    <h1>用户注册</h1>
    <div class="form-item">
      <input type="text" placeholder="请输入11位手机号" class="txt"/>
    </div>

    <div class="form-item clearfix">
      <div class="left fl">
        <input type="text" placeholder="请填写验证码" class="txt"/>
      </div>
      
      <button type="button" class="fr">发送验证码</button>
    </div>

    <div class="form-item">
      <input type="password" placeholder="请输入密码" class="txt"/>
    </div>

    <div class="form-item">
      <input type="password" placeholder="请再次确认密码" class="txt"/>
    </div>

    <div class="form-item" style="height: 100px;">
      <select multiple>
        <option>爱好1</option>
        <option>爱好2</option>
        <option>爱好3</option>
        <option>爱好4</option>
        <option>爱好5</option>
        <option>爱好6</option>
        <option>爱好7</option>
        <option>爱好8</option>
        <option>爱好9</option>
        <option>爱好10</option>
      </select>
    </div>

    <div class="form-item clearfix">
      <div class="fl title">性别</div>
      <div class="fl">
        <label class="label">
          <input type="radio" name="sex" />
          <span>男</span>
        </label>

        <label class="label">
          <input type="radio" name="sex" />
          <span>女</span>
        </label>
      </div>
    </div>

    <div class="form-item" style="height: 100px;">
      <textarea placeholder="请填写个人简介"></textarea>
    </div>

    <div class="form-item">
      <label>
        <input type="checkbox" />
        <span class="privacy">同意台湾是中国领土不可分割的一部分</span>
      </label>
    </div>

    <div class="form-item clearfix">
      <button class="fl">立即注册</button>
      <button type="reset" class="fr">重置表单</button>
    </div>
  </form>
</body>
</html>

css

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

body {
  background: #071218;
}

.clearfix::after {
  content: '';
  display: block;
  clear: both;
}

.fl {
  float: left;
}

.fr {
  float: right;
}

.container {
  width: 400px;
  margin: 30px auto;
  background: #fff;
  padding: 25px;
  border-radius: 10px;
  border: 1px solid #ccc;
}

.container h1 {
  text-align: center;
  margin-bottom: 25px;
}
/* //父元素只设置高度和margin */
.form-item {
  height: 35px;
  margin: 20px 0;
}

/* //边框设置在子元素的input上 */
.form-item .txt {
  width: 100%;
  height: 40px;
  line-height: 40px;
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 0 10px;
}

.form-item .left{
  width: 50%;
}

.form-item button {
  width: 45%;
  height: 40px;
  border-radius: 5px;
  border: none;
  background: #4a7ded;
  color: #fff;
}

.form-item input:focus {
  outline: 1px solid #4a7ded;
}

.form-item select {
  width: 100%;
  height: 100px;
  border-radius: 5px;
  border: 1px solid #ccc;
  padding: 10px;
}

.form-item select:focus {
  outline: 1px solid #4a7ded;
}

.form-item .title {
  margin-right: 20px;
}

.form-item .label {
  padding: 10px;
}

.form-item textarea {
  width: 100%;
  height: 100px;
  padding: 10px;
  border: 1px solid #ccc;
}

.form-item textarea:focus {
  outline: 1px solid #4a7ded;
}

.form-item input[type="checkbox"] {
  outline: none;
  font-size: 12px;
}

.form-item .privacy {
  color: #ccc;
}

flex方式
html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="flexindex.css">
</head>
<body>
  <form class="container">
    <h1>用户注册</h1>

    <div class="form-item">
      <input type="text" class="txt" placeholder="请输入11位手机号">
    </div>

    <div class="form-item m-flex flex-between column-gap-10">
      <input type="text" class="txt captcha-item"  placeholder="请填写验证码">
      <button type="button" disabled>发送验证码</button>
    </div>

    <div class="form-item">
      <input type="password" class="txt"  placeholder="请输入密码">
    </div>

    <div class="form-item">
      <input type="password" class="txt"  placeholder="请再次输入密码">
    </div>

    <div class="form-item">
      <select multiple class="favorite-item">
        <option>爱好1</option>
        <option>爱好2</option>
        <option>爱好3</option>
        <option>爱好4</option>
        <option>爱好5</option>
        <option>爱好6</option>
        <option>爱好7</option>
        <option>爱好8</option>
        <option>爱好9</option>
        <option>爱好10</option>
      </select>
    </div>

    <div class="form-item m-flex item-center">
      <span class="sex-title">性别</span>

      <div>
        <label class="label">
          <input checked class="m-vertical-align" type="radio" name="sex">
          <span>男</span>
        </label>
        
        <label class="label">
          <input class="m-vertical-align"  type="radio" name="sex">
          <span>女</span>
        </label>
      </div>
    </div>

    <div class="form-item">
      <textarea class="txt profile-item"  placeholder="请填写个人简介"></textarea>
    </div>

    <div class="form-item">
      <label class="label">
        <input  class="m-vertical-align" type="checkbox">
        <span>同意台湾是中国领土不可分割的一部分</span>
      </label>
    </div>

    <div class="form-item m-flex flex-center column-gap-50">
      <button disabled>立即注册</button>
      <button type="reset">重置表单</button>
    </div>
  </form>
</body>
</html>

css

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

body {
  background: #071218;
}

.container {
  width: 400px;
  margin: 30px auto;
  padding: 30px;
  background: #fff;
  border-radius: 10px;
}

.m-flex {
  display: flex;
}

.flex-between {
  justify-content: space-between;
}

.flex-center {
  justify-content: center;
}

.column-gap-10 {
  column-gap: 10px;
}

.column-gap-50 {
  column-gap: 50px;
}
.item-center {
  align-items: center;
}


.container h1{
  text-align: center;
  margin-bottom: 30px;
}

.form-item {
  margin: 10px 0;
}

.txt {
  width: 100%;
  height: 40px;
  line-height: 40px;
  padding: 0 5px;
  border-radius: 5px;
  outline: none;
  border: 1px solid #ccc;
  font-size: 14px;
}

.txt:focus {
  border-color: #4a7ded;
}

.captcha-item {
  width: 100%;
}

button {
  width: 60%;
  height: 40px;
  background: #4a7ded;
  color: #fff;
  border-radius: 5px;
  padding: 5px 35px;
  border: 1px solid #ccc;
  outline: none;
  cursor: pointer;
  font-size: 14px;
}

.m-vertical-align {
  vertical-align: -1px;
}

button:disabled {
  background: #92ace4;
  cursor: not-allowed;
}

.favorite-item {
  width: 100%;
  height: 150px;
  padding: 5px;
  outline: none;
  border: 1px solid #ccc;
  font-size: 14px;
}

.favorite-item option {
  margin: 3px;
}

.sex-title {
  font-size: 16px;
  margin-right: 20px;
}

.label {
  font-size: 14px;
  margin-right: 5px;
}

.profile-item {
  height: 100px;
  resize: none;
}

tailwind
只有html,因为都是原子css,而且是自适应的

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <script>
    tailwind.config = {
      prefix: 'link-',
      darkMode: 'selector',
      theme:{
        extend: {
          colors: {
            'dark':'#071218',
            'border':'#4a7ded',
            'gray':'#ccc',
            'border-disabled':'#92ace4',
          }
        }
      }
    }
  </script>
</head>
<body class="link-bg-dark">
  <form class="link-bg-white link-rounded-lg link-mx-auto link-my-5 link-w-1/4 link-p-5">
    <h1 class="link-text-center link-text-2xl link-font-bold link-mb-5">用户注册</h1>
    <div class="link-my-5">
      <input type="text" placeholder="请输入11位手机号" class="focus:link-border-border link-text-sm link-px-2 link-h-10 link-w-full link-rounded-md link-border-gray link-outline-none link-border" />
    </div>

    <div class="link-flex link-justify-between link-my-5">
      <input type="text" placeholder="请输入验证码" class="focus:link-border-border link-text-sm link-px-2 link-h-10 link-w-40 link-rounded-md link-border-gray link-outline-none link-border" />
      <button type="button" disabled class="link-cursor-pointer disabled:link-cursor-not-allowed disabled:link-bg-border-disabled link-rounded-md link-h-10 link-bg-border link-text-white link-px-10 link-py-2">发送验证码</button>
    </div>

    <div class="link-my-5">
      <input type="password" placeholder="请输入密码" class="focus:link-border-border link-text-sm link-px-2 link-h-10 link-w-full link-rounded-md link-border-gray link-outline-none link-border" >
    </div>

    <div class="link-my-5">
      <input type="password" placeholder="请再次输入密码" class="focus:link-border-border link-px-2 link-text-sm 
      link-h-10 link-w-full link-rounded-md link-border-gray link-outline-none link-border" >
    </div>

    <div class="link-my-5">
      <select multiple class="link-w-full link-h-40 link-p-2 focus:link-border-border link-border link-border-gray link-outline-none">
        <option class="link-m-1 link-text-sm">爱好1</option>
        <option class="link-m-1 link-text-sm">爱好2</option>
        <option class="link-m-1 link-text-sm">爱好3</option>
        <option class="link-m-1 link-text-sm">爱好4</option>
        <option class="link-m-1 link-text-sm">爱好5</option>
        <option class="link-m-1 link-text-sm">爱好6</option>
        <option class="link-m-1 link-text-sm">爱好7</option>
        <option class="link-m-1 link-text-sm">爱好8</option>
        <option class="link-m-1 link-text-sm">爱好9</option>
        <option class="link-m-1 link-text-sm">爱好10</option>
      </select> 
    </div>

    <div class="link-my-5 link-flex link-items-center">
      <span class="link-mr-5">性别</span>
      <div>
        <label class="link-mr-2">
          <input type="radio" checked name="sex" class="link-align-middle">
          <span class="link-text-sm">男</span>
        </label>

        <label>
          <input type="radio" name="sex" class="link-align-middle">
          <span class="link-text-sm">女</span>
        </label>
      </div>
    </div>

    <div class="link-my-5">
      <textarea placeholder="请输入个人简介" class="focus:link-border-border link-w-full link-resize-none link-h-24 link-border link-border-gray"></textarea>
    </div>

    <div class="link-my-5">
      <label>
        <input type="checkbox" class="link-align-middle"/>
        <span class="link-text-sm">同意台湾是中国领土不可分割的一部分</span>
      </label>
    </div>

    <div class="link-my-5 link-flex link-justify-between">
      <button disabled class="link-cursor-pointer link-w-40 disabled:link-cursor-not-allowed disabled:link-bg-border-disabled link-rounded-md link-h-10 link-bg-border link-text-white link-px-10 link-py-2">立即注册</button>
      <button type="reset" class="link-cursor-pointer link-w-40 disabled:link-cursor-not-allowed disabled:link-bg-border-disabled link-rounded-md link-h-10 link-bg-border link-text-white link-px-10 link-py-2">重置表单</button>
    </div>
  </form>
</body>
</html>

感觉小项目还是使用float或者flex

posted @ 2024-04-20 07:50  猝死的路上  阅读(8)  评论(0编辑  收藏  举报