如何像 bootstrap 一样形成 css

How to do form css same like bootstrap

提问人:sarann 提问时间:11/17/2023 最后编辑:Harrisonsarann 更新时间:11/19/2023 访问量:43

问:

我正在尝试像 bootstrap 一样为形式做 css,我的意思是没有 bootstrap。我已经从 bootstrap 复制了所有相关的 css,但它的工作方式并不相同。怎么做?

Bootstrap 表单 CSS:https://stackblitz.com/edit/angular-9-reactive-form-validation-olv-kkg5cn?file=app%2Fapp.component.html

我的表单CSS没有引导: 演示:https://stackblitz.com/edit/angular-9-reactive-form-validation-olv-oya8er?file=app%2Fapp.component.html

.card {
  position: relative;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-color: #fff;
  background-clip: border-box;
  border: 1px solid rgba(0, 0, 0, 0.125);
  border-radius: 0.25rem;
}

.card-header:first-child {
  border-radius: calc(0.25rem - 1px) calc(0.25rem - 1px) 0 0;
}

.card-header {
  padding: 0.75rem 1.25rem;
  margin-bottom: 0;
  background-color: rgba(0, 0, 0, 0.03);
  border-bottom: 1px solid rgba(0, 0, 0, 0.125);
}

.card-body {
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  min-height: 1px;
  padding: 1.25rem;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: inline-block;
  margin-bottom: 0.5rem;
}

.form-control {
  display: block;
  width: 100%;
  height: calc(1.5em + 0.75rem + 2px);
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 0.25rem;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.form-row {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -5px;
  margin-left: -5px;
}

.form-row > .col,
.form-row > [class*='col-'] {
  padding-right: 5px;
  padding-left: 5px;
}

.col {
  -ms-flex-preferred-size: 0;
  flex-basis: 0;
  -ms-flex-positive: 1;
  flex-grow: 1;
  max-width: 100%;
  position: relative;
  width: 100%;
  padding-right: 15px;
  padding-left: 15px;
}

button,
input {
  overflow: visible;
}

.text-right {
  text-align: right !important;
}

.btn-primary {
  color: #fff;
  background-color: #007bff;
  border-color: #007bff;
}

.btn {
  display: inline-block;
  font-weight: 400;
  color: #212529;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background-color: transparent;
  border: 1px solid transparent;
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  line-height: 1.5;
  border-radius: 0.25rem;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
    border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.mr-1,
.mx-1 {
  margin-right: 0.25rem !important;
}

.btn-secondary {
  color: #fff;
  background-color: #6c757d;
  border-color: #6c757d;
}

.m-3 {
  margin: 1rem !important;
}
<div class="card m-3">
  <h5 class="card-header">Angular 12 Reactive Form Validation</h5>
  <div class="card-body">
    <form [formGroup]="registerForm" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <label>URL</label>
        <input type="text" formControlName="url" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.url.errors }" />
        <div *ngIf="submitted && f.url.errors" class="invalid-feedback">
          <div *ngIf="f.url.errors.required">URL is required</div>
          <div *ngIf="f.url.errors.invalidURL">URL is not valid</div>
        </div>
      </div>
      <div class="form-group">
        <label>Email</label>
        <input type="text" formControlName="email" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.email.errors }" />
        <div *ngIf="submitted && f.email.errors" class="invalid-feedback">
          <div *ngIf="f.email.errors.required">Email is required</div>
          <div *ngIf="f.email.errors.email">Email must be a valid email address</div>
        </div>
      </div>
      <div class="form-row">
        <div class="form-group col">
          <label>Password</label>
          <input type="password" formControlName="password" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.password.errors }" />
          <div *ngIf="submitted && f.password.errors" class="invalid-feedback">
            <div *ngIf="f.password.errors.required">Password is required</div>
            <div *ngIf="f.password.errors.minlength">Password must be at least 6 characters</div>
          </div>
        </div>
        <div class="form-group col">
          <label>Confirm Password</label>
          <input type="password" formControlName="confirmPassword" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.confirmPassword.errors }" />
          <div *ngIf="submitted && f.confirmPassword.errors" class="invalid-feedback">
            <div *ngIf="f.confirmPassword.errors.required">Confirm Password is required</div>
            <div *ngIf="f.confirmPassword.errors.mustMatch">Passwords must match</div>
          </div>
        </div>
      </div>

      <div class="text-center">
        <button class="btn btn-primary mr-1">Register</button>
        <button class="btn btn-secondary" type="reset" (click)="onReset()">Cancel</button>
      </div>
    </form>

  </div>
</div>

我期待这样的:

enter image description here

购买我得到这样的:enter image description here

CSS 飒爽

评论


答:

0赞 Harrison 11/17/2023 #1

您缺少一个“重置 CSS”,它会覆盖浏览器的默认样式。 这应该可以解决问题 Eric Meyer CSS 重置

注意:您可能缺少其他不相关的填充/边距样式(例如输入元素之间的间距)

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

/**/

.card {
  position: relative;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-color: #fff;
  background-clip: border-box;
  border: 1px solid rgba(0, 0, 0, 0.125);
  border-radius: 0.25rem;
}

.card-header:first-child {
  border-radius: calc(0.25rem - 1px) calc(0.25rem - 1px) 0 0;
}

.card-header {
  padding: 0.75rem 1.25rem;
  margin-bottom: 0;
  background-color: rgba(0, 0, 0, 0.03);
  border-bottom: 1px solid rgba(0, 0, 0, 0.125);
}

.card-body {
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  min-height: 1px;
  padding: 1.25rem;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: inline-block;
  margin-bottom: 0.5rem;
}

.form-control {
  display: block;
  width: 100%;
  height: calc(1.5em + 0.75rem + 2px);
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 0.25rem;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.form-row {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -5px;
  margin-left: -5px;
}

.form-row > .col,
.form-row > [class*='col-'] {
  padding-right: 5px;
  padding-left: 5px;
}

.col {
  -ms-flex-preferred-size: 0;
  flex-basis: 0;
  -ms-flex-positive: 1;
  flex-grow: 1;
  max-width: 100%;
  position: relative;
  width: 100%;
  padding-right: 15px;
  padding-left: 15px;
}

button,
input {
  overflow: visible;
}

.text-right {
  text-align: right !important;
}

.btn-primary {
  color: #fff;
  background-color: #007bff;
  border-color: #007bff;
}

.btn {
  display: inline-block;
  font-weight: 400;
  color: #212529;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background-color: transparent;
  border: 1px solid transparent;
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  line-height: 1.5;
  border-radius: 0.25rem;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
    border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.mr-1,
.mx-1 {
  margin-right: 0.25rem !important;
}

.btn-secondary {
  color: #fff;
  background-color: #6c757d;
  border-color: #6c757d;
}

.m-3 {
  margin: 1rem !important;
}
<div class="card m-3">
  <h5 class="card-header">Angular 12 Reactive Form Validation</h5>
  <div class="card-body">
    <form [formGroup]="registerForm" (ngSubmit)="onSubmit()">
      <div class="form-group">
        <label>URL</label>
        <input type="text" formControlName="url" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.url.errors }" />
        <div *ngIf="submitted && f.url.errors" class="invalid-feedback">
          <div *ngIf="f.url.errors.required">URL is required</div>
          <div *ngIf="f.url.errors.invalidURL">URL is not valid</div>
        </div>
      </div>
      <div class="form-group">
        <label>Email</label>
        <input type="text" formControlName="email" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.email.errors }" />
        <div *ngIf="submitted && f.email.errors" class="invalid-feedback">
          <div *ngIf="f.email.errors.required">Email is required</div>
          <div *ngIf="f.email.errors.email">Email must be a valid email address</div>
        </div>
      </div>
      <div class="form-row">
        <div class="form-group col">
          <label>Password</label>
          <input type="password" formControlName="password" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.password.errors }" />
          <div *ngIf="submitted && f.password.errors" class="invalid-feedback">
            <div *ngIf="f.password.errors.required">Password is required</div>
            <div *ngIf="f.password.errors.minlength">Password must be at least 6 characters</div>
          </div>
        </div>
        <div class="form-group col">
          <label>Confirm Password</label>
          <input type="password" formControlName="confirmPassword" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.confirmPassword.errors }" />
          <div *ngIf="submitted && f.confirmPassword.errors" class="invalid-feedback">
            <div *ngIf="f.confirmPassword.errors.required">Confirm Password is required</div>
            <div *ngIf="f.confirmPassword.errors.mustMatch">Passwords must match</div>
          </div>
        </div>
      </div>

      <div class="text-center">
        <button class="btn btn-primary mr-1">Register</button>
        <button class="btn btn-secondary" type="reset" (click)="onReset()">Cancel</button>
      </div>
    </form>

  </div>
</div>

评论

0赞 sarann 11/17/2023
无法正常工作
0赞 Harrison 11/17/2023
如果你从 bootstrap 复制代码但没有完整使用它,你必须扩展这一点,你需要确保你已经复制了你打算使用的所有内容,并确保你已经复制了相同版本的 Bootstrap 的样式。另外,当我将代码复制到您的代码笔时,它的工作方式与我的答案相同。仔细检查,我可以看到您尚未复制到版本中的样式。我能问一下你为什么不只使用 Bootstrap 并复制小块内容吗?reboot.scss
0赞 sarann 11/17/2023
忘记了引导程序。如何自己为该表单编写相同的css。?
0赞 Harrison 11/17/2023
如果你想要完全相同的CSS,你必须检查你正在复制的页面上的每个元素,并确保你已经复制了所有的CSS。正如我之前所说,您共享的链接显示了您还必须复制的内容(例如和元素),但这样做需要很长时间,并且 Stack Overflow 社区可能会告诉您该做什么(正如我在这里和上面所说的那样),但不会确切地向您展示要复制和粘贴的内容,因为我们无法知道您想要复制的所有内容。reboot.scssinputh5