【问题标题】:Having Issue with CSS styling and select boxes with buttonsCSS 样式问题和带有按钮的选择框
【发布时间】:2018-11-14 21:03:27
【问题描述】:

我目前正在尝试设计一系列简单的多选框。

https://next.plnkr.co/edit/wBfAMTYvPPjWncsY?open=lib%2Fscript.js&deferRun=1

这是 HTML

<div class="listBoxSpacing">
    <span class="listBoxHeaders">Days Of The Week:</span>
    <select class="listBoxSelectStyle" multiple size="7" ng-options="day as day.day for day in vm.daysOfWeek track by day.day" ng-model="vm.daysOfWeekSelected"></select>
    <br />
    <button>Select All</button>
  </div>
  <div class="listBoxSpacing">
    <span class="listBoxHeaders">Dates Of The Month:</span>
    <select class="listBoxSelectStyle" multiple size="7" ng-options="date as date.date for date in vm.datesInTheMonth track by date.date" ng-model="vm.datesInTheMonthSelected"></select>
    <br />
    <button>Select All</button>
  </div>
  <div class="listBoxSpacing">
    <span class="listBoxHeaders">Days Of The Week:</span>
    <select class="listBoxSelectStyle" multiple size="7" ng-options="day as day.day for day in vm.daysOfWeek track by day.day" ng-model="vm.daysOfWeekSelected"></select>

    <button>Select All</button>
  </div>

这是 Javascript...

 vm.daysOfWeek = [
    {day: 'Monday'},
    {day: 'Tuesday'},
    {day: 'Wednesday'},
    {day: 'Thursday'},
    {day: 'Friday'},
    {day: 'Saturday'},
    {day: 'Sunday'},
  ];
  vm.datesInTheMonth = [];
  for (let i = 1; i < 32; i++) {
    vm.datesInTheMonth.push({date: i});
  }
  vm.daysOfWeekSelected = [];
  vm.datesInTheMonthSelected = [];

这是CSS

.listBoxHeaders {
  display:block;
  margin-top: 10px;
}
.listBoxSpacing {
  float:left;
}
.listBoxSpacing:nth-child(n+2) {
   margin-left:30px;
}
.listBoxSpacing button{
  width: 100%;
}
.listBoxSelectStyle {
  width:100%;
  margin-bottom: 0;
  padding-bottom: 0;
}

从上面的代码中可以看出,第一个和第二个(带有 br 标签)与标题正确对齐。但是,与第一个具有相同代码的第三个没有正确对齐标题​​(没有 br 标签)。

我很好奇为什么会出现这种行为,谁能将我链接到文档/解释这个概念,以便我将来能理解这种行为?

【问题讨论】:

  • 我已将代码直接添加到问题中,抱歉,我认为 plunker 会更有益,因为它是一个视觉问题。谢谢你让我知道:)
  • 为什么你必须浮动一个元素来提供间距?如果你想使用剩余空间,你可以将 display 属性更改为 inline-block。
  • @vssadineni 感谢您的建议,我现在已经在我的实际代码环境中切换了这些(不是 plunker)。对这个问题有任何见解吗? :)

标签: css angularjs


【解决方案1】:
    :root {
      font-family: 'Arial', Helvetica, Helvetica Neue, sans-serif;
      font-size: 100%;
    }
    body,
    nav,
    section,
    article,
    main,
    aside,
    header,
    figure,
    div,
    p,
    a,
    span,
    h1,
    h2,
    h3,
    h4,
    h5,
    h6,
    ul,
    ol,
    li,
    table,
    thead,
    tbody,
    tfoot,
    tr,
    th,
    td,
    dl,
    dd,
    dt,
    blockquote,
    fieldset,
    legend,
    input,
    textarea,
    select,
    select option,
    button,
    label,
    img,
    footer {
      box-sizing: border-box;
    }
    html,
    body,
    input,
    select,
    select option,
    label,
    textarea,
    button {
      font-family: 'Arial', Helvetica, Helvetica Neue, sans-serif;
      font-size: 100%;
    }
    nav,
    section,
    article,
    main,
    aside,
    header,
    figure,
    footer {
      display: block;
    }
    dl,
    dd,
    dt {
      margin: 0;
    }
    blockquote {
      margin: 0;
      padding-left: 1.75em;
    }
    fieldset {
      margin: 0;
      padding: 0.25em;

      border: 0 solid transparent;
    }
    body {
      color: rgba(23, 23, 23, 0.99);
    }
    body,
    ul,
    figure {
      margin: 0;
      padding: 0;
    }
    svg {
      width: 100%;
      height: auto;
    }
    iframe {
      overflow: hidden;

      margin: 0.02em;

      border: none;
    }
    a {
      text-decoration: none;

      color: inherit;
    }
    p {
      margin: 0;

      line-height: 1.75;
    }
    hr {
      display: -ms-flexbox;
      display: flex;

      min-width: 100%;
      max-width: 100%;
      margin: 0;

      border-top: 0.125px solid rgba(10, 10, 10, 0.25);
      border-right: 0;
      border-bottom: 0;
      border-left: 0;

      font-size: 0;
      line-height: 0;

      -ms-flex-preferred-size: 100%;

      flex-basis: 100%;
    }
    img {
      width: 100%;
      height: auto;

      border: 0 solid transparent;
    }
    [ng\:cloak],
    [ng-cloak],
    [data-ng-cloak],
    [x-ng-cloak],
    .ng-cloak,
    .x-ng-cloak {
      display: none;
    }

    h1,
    p {
      font-family: sans-serif;
    }
    .listBoxSpacing {
      display: inline-block;
      width: auto;
      vertical-align: top;
    }
    .listBoxSpacing:not(:first-child),
    .listBoxSpacing:not(last-child) {
      margin: 0 1em;
    }
    .listBoxHeaders,
    .listBoxSelectStyle,
    .listBoxSpacing button {
      width: 100%;
      display: block;
    }

<!doctype html>

<html>
  <head>
    <link rel="stylesheet" href="lib/style.css">
    <script src="lib/script.js"></script>
  </head>

  <body ng-app="plunker" ng-cloak>
    <div ng-controller="MainCtrl as vm">
      <div class="listBoxSpacing">
        <span class="listBoxHeaders">Days Of The Week:</span>
        <select class="listBoxSelectStyle" multiple size="7" ng-options="day as day.day for day in vm.daysOfWeek track by day.day" ng-model="vm.daysOfWeekSelected"></select>
        <button>Select All</button>
      </div>
      <div class="listBoxSpacing">
        <span class="listBoxHeaders">Dates Of The Month:</span>
        <select class="listBoxSelectStyle" multiple size="7" ng-options="date as date.date for date in vm.datesInTheMonth track by date.date" ng-model="vm.datesInTheMonthSelected"></select>
        <button>Select All</button>
      </div>
      <div class="listBoxSpacing">
        <span class="listBoxHeaders">Dates Of The Month:</span>
        <select class="listBoxSelectStyle" multiple size="7" ng-options="day as day.day for day in vm.daysOfWeek track by day.day" ng-model="vm.daysOfWeekSelected"></select>
        <button>Select All</button>
      </div>
    </div>
  </body>
</html>

【讨论】:

  • 感谢您花时间添加所有这些信息,我已经对其进行了测试,它的工作原理与描述(和显示)一样。谢谢。
【解决方案2】:

br 在行之间创建空间(中断)并开始新行。但是我在这里没有看到任何关于图像的对齐问题。

【讨论】:

  • 顺便说一下,我从你的 plunker 参考中获取了图像。
  • 您看到第三个选项比标题更宽,出于某种原因添加了 br 修复,我试图理解为什么会这样。这有意义还是我应该以某种方式改写它?
【解决方案3】:

我将 css 和 html 原样复制到我的本地系统中。 div.listBoxSpacing ,我删除了 float 属性,将其替换为 display:inline-block。我要求你使用一些 reset.css 并避免浮动。我坚信 Float 导致了这个问题。如果您看到标题现在在没有和有 br' 的情况下对齐。

【讨论】:

  • 感谢您抽出宝贵时间回答,您能否提供一个插件,编辑我的插件或您使用的代码的 sn-p。我已经尝试实现您在我提供的 plunker 中显示的内容,并且间距问题仍然存在。图像看起来很棒!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2020-07-16
  • 1970-01-01
  • 2013-01-22
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多