代码之家  ›  专栏  ›  技术社区  ›  Dreams

如何强制元素的默认CSS

  •  1
  • Dreams  · 技术社区  · 6 年前

    我有一个标准的复选框模板,它在一个简单的HTML页面上工作:

    但是,在我的网页上,由于其他CSS样式的干扰,它不会呈现为复选框。

    只有文本可见,没有复选框图标。无法强制此处的复选框行为。这可能是因为在我的CSS文件中,我检查了标签和输入的样式。但是,我想强制使用这个标签的默认行为。

    *, *:before, *:after {
          margin: 0;
          padding: 0;
          box-sizing: border-box;
        }
        
        html, body {
          height: 120vh;
        }
        
        body {
          font: 14px/1 'Open Sans', sans-serif;
          color: #555;
          background: #eee;
        }
        
        h1 {
          padding: 20px 0;
          font-weight: 100;
          text-align: center;
        }
        
        p {
          margin: 0 0 20px;
          line-height: 1.5;
        }
        
        main {
          min-width: 520px;
          /*max-width: 800px;*/
          padding: 50px;
          margin: 0 auto;
          background: #fff;
        }
        
        section {
          display: none;
          padding: 20px 0 0;
          border-top: 1px solid #ddd;
        }
        
        input {
          display: none;
        }
        
        label {
          display: inline-block;
          margin: 0 0 -1px;
          padding: 15px 25px;
          font-weight: 600;
          text-align: center;
          color: #bbb;
          border: 1px solid transparent;
        }
        
        label:before {
          font-family: fontawesome;
          font-weight: normal;
          margin-right: 10px;
        }
        
        label[for*='1']:before { content: '\f1cb'; }
        label[for*='2']:before { content: '\f17d'; }
        label[for*='3']:before { content: '\f16b'; }
        label[for*='4']:before { content: '\f1a9'; }
        
        label:hover {
          color: #888;
          cursor: pointer;
        }
        
        input:checked + label {
          color: #555;
          border: 1px solid #ddd;
          border-top: 2px solid orange;
          border-bottom: 1px solid #fff;
        }
        
        #tab1:checked ~ #content1,
        #tab2:checked ~ #content4,
        #tab3:checked ~ #content2,
        #tab4:checked ~ #content3 {
          display: block;
        }
        
        @media screen and (max-width: 650px) {
          label {
            font-size: 0;
          }
          label:before {
            margin: 0;
            font-size: 18px;
          }
        }
        
        @media screen and (max-width: 600px) {
          label {
            padding: 15px;
          }
        }
        
        /*Table */
        .table-wrapper {
        	-webkit-overflow-scrolling: touch;
        	overflow-x: auto;
        }
        
        table {
        	margin: 0 0 2rem 0;
        	width: 100%;
        }
        
        	table tbody tr {
        		border: solid 1px;
        		border-left: 0;
        		border-right: 0;
        	}
        
        	table td {
        		padding: 0.75rem 0.75rem;
        	}
        
        	table th {
        		font-size: 0.9rem;
        		font-weight: 700;
        		padding: 0 0.75rem 0.75rem 0.75rem;
        		text-align: left;
        	}
        
        	table thead {
        		border-bottom: solid 2px;
        	}
        
        	table tfoot {
        		border-top: solid 2px;
        	}
        
        	table.alt {
        		border-collapse: separate;
        	}
        
        		table.alt tbody tr td {
        			border: solid 1px;
        			border-left-width: 0;
        			border-top-width: 0;
        		}
        
        			table.alt tbody tr td:first-child {
        				border-left-width: 1px;
        			}
        
        		table.alt tbody tr:first-child td {
        			border-top-width: 1px;
        		}
        
        		table.alt thead {
        			border-bottom: 0;
        		}
        
        		table.alt tfoot {
        			border-top: 0;
        		}
        
        table tbody tr {
        	border-color: rgba(144, 144, 144, 0.25);
        }
        
        	table tbody tr:nth-child(2n + 1) {
        		background-color: rgba(144, 144, 144, 0.075);
        	}
        
        table th {
        	color: #555;
        }
        
        table thead {
        	border-bottom-color: rgba(144, 144, 144, 0.25);
        }
        
        table tfoot {
        	border-top-color: rgba(144, 144, 144, 0.25);
        }
        
        table.alt tbody tr td {
        	border-color: rgba(144, 144, 144, 0.25);
        }
    <form>
      <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
      <input type="checkbox" name="vehicle" value="Car" checked> I have a car<br>
      <input type="submit" value="Submit">
    </form>

    请建议。 同时附加CSS文件,以防:

    2 回复  |  直到 6 年前
        1
  •  3
  •   wscourge Kiran Balakrishnan    6 年前

    input {
        display: none;
    }
    

    *, *:before, *:after {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html, body {
      height: 120vh;
    }
    
    body {
      font: 14px/1 'Open Sans', sans-serif;
      color: #555;
      background: #eee;
    }
    
    h1 {
      padding: 20px 0;
      font-weight: 100;
      text-align: center;
    }
    
    p {
      margin: 0 0 20px;
      line-height: 1.5;
    }
    
    main {
      min-width: 520px;
      /*max-width: 800px;*/
      padding: 50px;
      margin: 0 auto;
      background: #fff;
    }
    
    section {
      display: none;
      padding: 20px 0 0;
      border-top: 1px solid #ddd;
    }
    
    input {
      display: none;
    }
    
    label {
      display: inline-block;
      margin: 0 0 -1px;
      padding: 15px 25px;
      font-weight: 600;
      text-align: center;
      color: #bbb;
      border: 1px solid transparent;
    }
    
    label:before {
      font-family: fontawesome;
      font-weight: normal;
      margin-right: 10px;
    }
    
    label[for*='1']:before { content: '\f1cb'; }
    label[for*='2']:before { content: '\f17d'; }
    label[for*='3']:before { content: '\f16b'; }
    label[for*='4']:before { content: '\f1a9'; }
    
    label:hover {
      color: #888;
      cursor: pointer;
    }
    
    input:checked + label {
      color: #555;
      border: 1px solid #ddd;
      border-top: 2px solid orange;
      border-bottom: 1px solid #fff;
    }
    
    #tab1:checked ~ #content1,
    #tab2:checked ~ #content4,
    #tab3:checked ~ #content2,
    #tab4:checked ~ #content3 {
      display: block;
    }
    
    @media screen and (max-width: 650px) {
      label {
        font-size: 0;
      }
      label:before {
        margin: 0;
        font-size: 18px;
      }
    }
    
    @media screen and (max-width: 600px) {
      label {
        padding: 15px;
      }
    }
    
    /*Table */
    .table-wrapper {
        -webkit-overflow-scrolling: touch;
        overflow-x: auto;
    }
    
    table {
        margin: 0 0 2rem 0;
        width: 100%;
    }
    
        table tbody tr {
            border: solid 1px;
            border-left: 0;
            border-right: 0;
        }
    
        table td {
            padding: 0.75rem 0.75rem;
        }
    
        table th {
            font-size: 0.9rem;
            font-weight: 700;
            padding: 0 0.75rem 0.75rem 0.75rem;
            text-align: left;
        }
    
        table thead {
            border-bottom: solid 2px;
        }
    
        table tfoot {
            border-top: solid 2px;
        }
    
        table.alt {
            border-collapse: separate;
        }
    
            table.alt tbody tr td {
                border: solid 1px;
                border-left-width: 0;
                border-top-width: 0;
            }
    
                table.alt tbody tr td:first-child {
                    border-left-width: 1px;
                }
    
            table.alt tbody tr:first-child td {
                border-top-width: 1px;
            }
    
            table.alt thead {
                border-bottom: 0;
            }
    
            table.alt tfoot {
                border-top: 0;
            }
    
    table tbody tr {
        border-color: rgba(144, 144, 144, 0.25);
    }
    
        table tbody tr:nth-child(2n + 1) {
            background-color: rgba(144, 144, 144, 0.075);
        }
    
    table th {
        color: #555;
    }
    
    table thead {
        border-bottom-color: rgba(144, 144, 144, 0.25);
    }
    
    table tfoot {
        border-top-color: rgba(144, 144, 144, 0.25);
    }
    
    table.alt tbody tr td {
        border-color: rgba(144, 144, 144, 0.25);
    }
    
    /* ADD THIS */
    
    input {
      display: block!important;
    }
    <form>
      <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
      <input type="checkbox" name="vehicle" value="Car" checked> I have a car<br>
      <input type="submit" value="Submit">
    </form>
        2
  •  3
  •   Aravind S    6 年前

    display: none input

    *, *:before, *:after {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html, body {
      height: 120vh;
    }
    
    body {
      font: 14px/1 'Open Sans', sans-serif;
      color: #555;
      background: #eee;
    }
    
    h1 {
      padding: 20px 0;
      font-weight: 100;
      text-align: center;
    }
    
    p {
      margin: 0 0 20px;
      line-height: 1.5;
    }
    
    main {
      min-width: 520px;
      /*max-width: 800px;*/
      padding: 50px;
      margin: 0 auto;
      background: #fff;
    }
    
    section {
      display: none;
      padding: 20px 0 0;
      border-top: 1px solid #ddd;
    }
    
    
    
    label {
      display: inline-block;
      margin: 0 0 -1px;
      padding: 15px 25px;
      font-weight: 600;
      text-align: center;
      color: #bbb;
      border: 1px solid transparent;
    }
    
    label:before {
      font-family: fontawesome;
      font-weight: normal;
      margin-right: 10px;
    }
    
    label[for*='1']:before { content: '\f1cb'; }
    label[for*='2']:before { content: '\f17d'; }
    label[for*='3']:before { content: '\f16b'; }
    label[for*='4']:before { content: '\f1a9'; }
    
    label:hover {
      color: #888;
      cursor: pointer;
    }
    
    input:checked + label {
      color: #555;
      border: 1px solid #ddd;
      border-top: 2px solid orange;
      border-bottom: 1px solid #fff;
    }
    
    #tab1:checked ~ #content1,
    #tab2:checked ~ #content4,
    #tab3:checked ~ #content2,
    #tab4:checked ~ #content3 {
      display: block;
    }
    
    @media screen and (max-width: 650px) {
      label {
        font-size: 0;
      }
      label:before {
        margin: 0;
        font-size: 18px;
      }
    }
    
    @media screen and (max-width: 600px) {
      label {
        padding: 15px;
      }
    }
    
    /*Table */
    .table-wrapper {
        -webkit-overflow-scrolling: touch;
        overflow-x: auto;
    }
    
    table {
        margin: 0 0 2rem 0;
        width: 100%;
    }
    
        table tbody tr {
            border: solid 1px;
            border-left: 0;
            border-right: 0;
        }
    
        table td {
            padding: 0.75rem 0.75rem;
        }
    
        table th {
            font-size: 0.9rem;
            font-weight: 700;
            padding: 0 0.75rem 0.75rem 0.75rem;
            text-align: left;
        }
    
        table thead {
            border-bottom: solid 2px;
        }
    
        table tfoot {
            border-top: solid 2px;
        }
    
        table.alt {
            border-collapse: separate;
        }
    
            table.alt tbody tr td {
                border: solid 1px;
                border-left-width: 0;
                border-top-width: 0;
            }
    
                table.alt tbody tr td:first-child {
                    border-left-width: 1px;
                }
    
            table.alt tbody tr:first-child td {
                border-top-width: 1px;
            }
    
            table.alt thead {
                border-bottom: 0;
            }
    
            table.alt tfoot {
                border-top: 0;
            }
    
    table tbody tr {
        border-color: rgba(144, 144, 144, 0.25);
    }
    
        table tbody tr:nth-child(2n + 1) {
            background-color: rgba(144, 144, 144, 0.075);
        }
    
    table th {
        color: #555;
    }
    
    table thead {
        border-bottom-color: rgba(144, 144, 144, 0.25);
    }
    
    table tfoot {
        border-top-color: rgba(144, 144, 144, 0.25);
    }
    
    table.alt tbody tr td {
        border-color: rgba(144, 144, 144, 0.25);
    }
    <form>
          <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
          <input type="checkbox" name="vehicle" value="Car" checked> I have a car<br>
          <input type="submit" value="Submit">
        </form>
    推荐文章