元素不会彼此相邻

时间:2018-03-31 14:10:43

标签: html css

问题在于元素并不像我希望的那样彼此相邻。

所有三个元素都已经浮动左右,但是它们仍然没有以正确的方式排列。

(很可能,问题是某些元素是位置:绝对或相对而他们不需要。问题是:你不能在不中断#的下拉菜单的情况下改变它计时器。那个)



green_button {
  background-color: #027fed;
  color: white;
  border-radius: 2px;
  cursor: pointer;
  float: right;
  position: relative;
}

.green_button:active {
  background-color: #eee;
  color: black;
}

.keuze {
  position: absolute;
  width: 100px;
  height: 100px;
  float: left
}

#timer {
  color: black;
  background: #eee;
  border: none;
  padding-left: 10px;
  font-size: 12px;
  border-radius: 2px;
  float: left;
  padding: 12px 12px;
  cursor: pointer;
  list-style-type: none;
  position: Relative;
  margin-top: -14px;
  width: 80px;
}

#timer:hover {
  color: white;
  background: #027fed;
}

li {
  background-color: #eee;
  font-size: inherit;
  width: 150px;
  position: relative;
  float: left;
  bottom: 31px;
  left: 0;
  display: block;
  font-size: 12px;
  text-decoration: none;
  font-family: tahoma;
  color: black;
  width: 50px;
  height: auto;
  border: 1px solid #;
  border-width: 1px 1px 0 0;
  background: #eee;
  background-color: rgb(238, 238, 238);
  padding-left: 10px;
  line-height: 38px;
  border-radius: 2px;
  height: auto;
  line-height: 1em;
  padding: 5px 10px;
  width: 129px;
  margin-bottom: 1px;
  margin-left: 431px;
}

li:hover {
  cursor: pointer;
  background-color: #027fed;
  color: white
}

.list {
  display: none;
  list-style-type: none;
  position: absolute !important;
}

.keuze:hover .list {
  display: block
}

.messages_compose {
  padding: 10px;
  margin-bottom: auto;
}

.messages_textarea_container {
  display: inline-block;
  width: 400px;
  margin-left: 10px;
}

.messages_textarea {
  border: 3px solid lightgray;
  margin-top: 0;
  margin-bottom: 10px;
  padding: 5px;
  width: 400px;
  height: 40px;
  resize: none;
  float: left;
  border-radius: 2px;
  position: absolute;
}

.button {
  border: none;
  font-size: 12px;
  padding: 12px 12px;
  height: 40px text-align: center;
}

<div class="messages_compose">
  <div class="vraag">CV</div>
  <div class="messages_textarea_container">
    <textarea class="messages_textarea"></textarea>
    <button class="button green_button">Stuur</button>
    <ul class="keuze">
      <button id="timer">1 Jaar</button>
      <div class="list">
        <li id="jaar">jaar</li>
        <li id="maand">maand</li>
        <li id="week">week</li>
        <li id="dag">dag</li>
      </div>
    </ul>
  </div>
  <script>
    document.getElementById("jaar").onclick = function() {
      jaar()
    };
    document.getElementById("maand").onclick = function() {
      maand()
    };
    document.getElementById("week").onclick = function() {
      week()
    };
    document.getElementById("dag").onclick = function() {
      dag()
    };
  </script>
  <script src="../scripten.js"></script>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果你想要它们并排(如果宽度允许),为了使事情更简单,请确保它们是内联元素。

默认情况下,

textarea和按钮是内联元素,默认情况下unsorted-list是块元素

Inline Elements

Block Elements

所以基本上,你只需要改变ul来显示:inline-block;

&#13;
&#13;
* {
  padding: 0;
  margin: 0;
  vertical-align: text-top;
}

green_button {
  background-color: #027fed;
  color: white;
  border-radius: 2px;
  cursor: pointer;
}

.green_button:active {
  background-color: #eee;
  color: black;
}

.keuze {
  display: inline-block; /* added */
  width: 100px;
  list-style: none;
}

.keuze li {
  width: 100%;
}

#timer {
  color: black;
  background: #eee;
  border: none;
  padding-left: 10px;
  font-size: 12px;
  border-radius: 2px;
  padding: 12px 12px;
  cursor: pointer;
  list-style-type: none;
  width: 80px;
}
&#13;
<div class="messages_compose">
  <div class="vraag">CV</div>
  <div class="messages_textarea_container">
    <textarea class="messages_textarea"></textarea>
    <button class="button green_button">Stuur</button>
    <ul class="keuze">
      <button id="timer">1 Jaar</button>
      <div class="list">
        <li id="jaar">jaar</li>
        <li id="maand">maand</li>
        <li id="week">week</li>
        <li id="dag">dag</li>
      </div>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

另外,我已经从你的css中删除了你的所有浮动和位置,我认为@Temani Afif说,你只是通过添加更多来解决问题。

我还添加了以下内容,以使其更整洁,这与您的问题无关。 (即从所有html元素中删除默认边距,填充和垂直对齐,使其更整洁,避免不同浏览器的意外行为)

* {
  padding: 0;
  margin: 0;
  vertical-align: text-top;
}