【问题标题】:The left side bar is being pushed to the right左侧栏被推到右侧
【发布时间】:2021-11-29 12:56:06
【问题描述】:

我刚刚制作了我的网站标题,现在我正在尝试构建侧边栏,但是它没有粘在左边,而是被推到了右边,我不知道为什么......但我想这是因为我在标题中添加的浮动元素?我尝试将边距设置为 0,将其浮动到左侧,但没有任何效果。

代码如下:

body {
  font-size: 62.5%;
}

:root {
  --color-primary: #b22222;
}

/* Box */

#box {
  margin: 2% 20%;
}

/* Header */

#header {
  background-color: var(--color-primary);
  width: 100%;
  height: 100px;
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
}

.oldenhook_icon {
  height: 90px;
  width: 90px;
  margin-left: 15px;
  float: left;
}

.header_h1 {
  color: #800000;
  font-family: "Noto Sans Mono", monospace;
  font-size: 2.5rem;
  font-weight: 900;
  display: inline-block;
  margin-left: 30%;
  margin-bottom: 0;
}

.flexbox {
  display: flex;
  flex-direction: row;
  float: right;
  list-style-type: none;
  margin-right: 22%;
}

.list-item {
  color: #fff;
  padding-left: 5px;
  text-decoration: none;
  font-size: 12px;
  font-family: "lucida grande", tahoma, verdana, arial, sans-serif;
}

/* Search box */

#searchbox {
  top: 40px;
  position: relative;
}
 <div id="box">
      <div id="header">
        <i
          ><img
            class="oldenhook_icon"
            src="icons/icon2.png"
            alt="The Oldenhook Icon"
        /></i>
        <div>
          <h1 class="header_h1">[&nbsp;oldenhook&nbsp;]</h1>
          <ul class="flexbox">
            <a href="#" class="list-item">home</a>
            <a href="#" class="list-item">search</a>
            <a href="#" class="list-item">global</a>
            <a href="#" class="list-item">social net</a>
            <a href="#" class="list-item">invite</a>
            <a href="#" class="list-item">faq</a>
            <a href="#" class="list-item">logout</a>
          </ul>
        </div>
        <div id="searchbox">
            
           <label for="email">Email:</label>
           <input name="email" id="email" type="text">
           <p>oifjqoifjq</p>
           <p>ofijqofiqjf</p>
      </div>
    </div>

【问题讨论】:

  • 我试过你的代码,你只需要删除float: right你的侧边栏会在左边,否则我误解了你的问题
  • 你应该改掉一般使用浮动的习惯。经过 15 年的 CSS 开发,在我看来,它们只有一个用例,就是在图像周围环绕文本。否则,他们的麻烦多于他们的价值。

标签: html css layout


【解决方案1】:

您的代码很好,只需从 css 中的 flexbox 类中删除 float: right;

【讨论】:

    【解决方案2】:

    我认为你可以将position: absolute; 添加到.flexbox{} 类中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-04
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 2014-10-15
      • 1970-01-01
      相关资源
      最近更新 更多