首页 > 解决方案 > 为什么我的 div 元素没有按预期响应?

问题描述

上图是CSS代码的输出;它应该分别显示 2 列 (#leftcolumn#rightcolumn),其中 2 列内有一些随机文本,但输出只显示了#leftcolumn其中的文本。请问,我该如何解决?

body {
  margin: 0;
  padding: 0;
  line-height: 1.5em;
}

#header {
  background: black;
  font-family: Arial;
  height: 100px;
}

#contentwrapper {
  float: left;
  width: 100%;
}

#contentcolumn {
  margin: 0px 230px 0px 230px;
}

#leftcolumn {
  float: left;
  width: 230px;
  margin-left: -230px;
  background: #f0071b;
}

#rightcolumn {
  float: left;
  margin-left: -230%;
  width: 230px;
  background: #20e6d2;
}

.innertext {
  margin: 20px;
  font-family: Arial;
  color: #d6a7c4;
  text-align: justify;
}

#footer {
  clear: left;
  /* Cannot float on left */
  width: 100%;
  background: #000000;
  color: #FFF;
  font-family: Arial;
  text-align: center;
  padding: 4px;
}


/* Navigation Bar Begins here */

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  background-size: #000;
}

li {
  float: left;
  border-right: 1px solid #bbb;
}

li:last-child {
  border-right: none;
}

li a {
  display: block;
  color: #FFF;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover:not(.active) {
  background-color: #d2f7f0;
}

.active {
  background-color: #025dfa;
}


/* Responsive Css Layout */

@media (max-width: 840px) {
  /* drop Right Column Drop Down */
  #leftcolumn {
    margin-left: -100px;
  }
  #rightcolumn {
    float: none;
    width: 100% margin-left: 0;
    clear: both;
  }
  #contentcolumn {
    margin-right: 0;
    /* left margin to leftcolumnWidth */
  }
}

@media (max-width: 600px) {
  /* Drop left column down  */
  #leftcolumn {
    float: none;
    width: 100%;
    clear: both;
    margin-left: 0;
  }
  #contentcolumn {
    margin-left: 0;
  }
}
<div id="container">
  <div id="header">
    <ul style="display: inline;">
      <li><a class="active" href="#home">Home</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
    </ul>
  </div>
  <div id="contentwrapper">
    <div id="contentcolumn">
      <div class="innertext">
        <b>Welcome!</b><br />Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make
        a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
        and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. <br />Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at
        its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors
        now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and
        the like).

      </div>
    </div>
  </div>


  <div id="leftcolumn">
    <div class="innertext">
      <b>The left Column: <em>230px</em></b><br />Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor
      at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes
      from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum"
    </div>
  </div>
  <div id="rightcolumn">
    <div class="innertext">
      <b>The right column:<em>230px</em></b><br />There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable.
      If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first
      true generator on the Internet.
    </div>
  </div>
  <div id="footer">Footer</div>
</div>

标签: htmlcss

解决方案


margin-left: -230%;将元素移出可见区域(向左 230%)。删除它,你会再次看到它。

或者你的意思是像素而不是百分比?


推荐阅读