在响应式导航中推送内容

时间:2016-09-01 18:52:15

标签: html css responsive

我正在尝试获取一个联系表单来推动响应式屏幕尺寸的变化。如果您查看下面的JS Fiddle链接,您会看到当设置在移动级别屏幕时,它不会将其推下,但只是重叠联系表单。

下面是我的HTML和CSS。

HTML:

    <div id="menuBackground">
      <div id="menuContainer">
    <label for="show-menu" class="show-menu">Show Menu</label>
    <input type="checkbox" id="show-menu" role="button" />
    <ul id="menu">
      <li><a href="#">Home</a></li>
      <li>
        <a href="#">About</a>
        <ul class="hidden">
          <li><a href="#">Who We Are</a></li>
          <li><a href="#">What We Do</a></li>
        </ul>
      </li>
      <li>
        <a href="#">Portfolio</a>
        <ul class="hidden">
          <li><a href="#">Photography</a></li>
          <li><a href="#">Web User Interface Design</a></li>
          <li><a href="#">Illustration</a></li>
        </ul>
      </li>
      <li><a href="#">News</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </div>
</div>

        <div>
        <form action="#">
        <header>
        <h2>Example Responsive Form</h2>
        <div>This form breaks at 600px and goes from a left-label form to a top-label form. At above 1200px, the labels align right.</div>
        </header>

        <div>
          <label class="desc" id="title1" for="Field1">Full Name</label>
          <div>
            <input id="Field1" name="Field1" type="text" class="field text fn" value="" size="8" tabindex="1">
          </div>
        </div>

        <div>
          <label class="desc" id="title3" for="Field3">
            Email
          </label>
          <div>
            <input id="Field3" name="Field3" type="email" spellcheck="false" value="" maxlength="255" tabindex="3">
          </div>
        </div>

        <div>
          <label class="desc" id="title4" for="Field4">
            Message
          </label>

          <div>
            <textarea id="Field4" name="Field4" spellcheck="true" rows="10" cols="50" tabindex="4"></textarea>
          </div>
        </div>

        <div>
          <fieldset>

            <legend id="title5" class="desc">
              Select a Choice
            </legend>

            <div>
              <input id="radioDefault_5" name="Field5" type="hidden" value="">
              <div>
                <input id="Field5_0" name="Field5" type="radio" value="First Choice" tabindex="5" checked="checked">
                <label class="choice" for="Field5_0">First Choice</label>
              </div>
              <div>
                <input id="Field5_1" name="Field5" type="radio" value="Second Choice" tabindex="6">
                <label class="choice" for="Field5_1">Second Choice</label>
              </div>
              <div>
                <input id="Field5_2" name="Field5" type="radio" value="Third Choice" tabindex="7">
                <label class="choice" for="Field5_2">Third Choice</label>
              </div>
            </div>
          </fieldset>
        </div>

        <div>
          <fieldset>
            <legend id="title6" class="desc">
              Check All That Apply
            </legend>
            <div>
              <div>
                <input id="Field6" name="Field6" type="checkbox" value="First Choice" tabindex="8">
                <label class="choice" for="Field6">First Choice</label>
              </div>
              <div>
                <input id="Field7" name="Field7" type="checkbox" value="Second Choice" tabindex="9">
                <label class="choice" for="Field7">Second Choice</label>
              </div>
              <div>
                <input id="Field8" name="Field8" type="checkbox" value="Third Choice" tabindex="10">
                <label class="choice" for="Field8">Third Choice</label>
                </span>
              </div>
          </fieldset>
          </div>

          <div>
            <label class="desc" id="title106" for="Field106">
              Select a Choice
            </label>
            <div>
              <select id="Field106" name="Field106" class="field select medium" tabindex="11">
                <option value="First Choice">First Choice</option>
                <option value="Second Choice">Second Choice</option>
                <option value="Third Choice">Third Choice</option>
              </select>
            </div>
          </div>

          <div>
            <div>
              <input id="saveForm" name="saveForm" type="submit" value="Submit">
            </div>
          </div>

      </form>
        </div>

CSS:

/* Start General Styling */

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  margin: 0;
}


/* End General Styling */

#menuBackground {
  background: #2f3036;
  width: 100%;
  height: 50px;
  text-align: center;
}

#menuContainer {
  text-align: center;
}


/*Strip the ul of padding and list styling*/

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}


/*Create a horizontal list with spacing*/

li {
  display: inline-block;
  vertical-align: top;
  margin-right: 1px;
}


/*Style for menu links*/

li a {
  display: block;
  min-width: 140px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #fff;
  background: #2f3036;
  text-decoration: none;
  font-size: 1rem;
}


/*Hover state for top level links*/

li:hover a {
  background: #19c589
}


/*Style for dropdown links*/

li:hover ul a {
  background: #f3f3f3;
  color: #2f3036;
  height: 40px;
  line-height: 40px
}


/*Hover state for dropdown links*/

li:hover ul a:hover {
  background: #19c589;
  color: #fff
}


/*Hide dropdown links until they are needed*/

li ul {
  position: absolute;
  display: none
}


/*Make dropdown links vertical*/

li ul li {
  display: block;
}


/*Prevent text wrapping*/

li ul li a {
  width: auto;
  min-width: 100px;
  padding: 0 20px
}


/*Display the dropdown on hover*/

ul li a:hover + .hidden,
.hidden:hover {
  display: block
}


/*Style 'show menu' label button and hide it by default*/

.show-menu {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  text-decoration: none;
  color: #fff;
  background: #19c589;
  text-align: center;
  padding: 16px 0;
  display: none;
  width: 100%!important
}


/*Hide checkbox*/

input[type=checkbox] {
  display: none
}


/*Show menu when invisible checkbox is checked*/

input[type=checkbox]:checked ~ #menu {
  display: block;
  margin: 0 auto
}


/*Responsive Styles*/

@media screen and (max-width: 760px) {
  /*Make dropdown links appear inline*/
  ul {
    position: static;
    display: none;
    white-space: initial;
  }
  /*Create vertical spacing*/
  li {
    margin-bottom: 1px
  }
  /*Make all menu links full width*/
  ul li,
  li a {
    width: 100%
  }
  /*Display 'show menu' link*/
  .show-menu {
    display: block
  }
}

form header {
  margin: 0 0 20px 0;
}

form header div {
  font-size: 90%;
  color: #999;
}

form header h2 {
  margin: 0 0 5px 0;
}

form > div {
  clear: both;
  overflow: hidden;
  padding: 1px;
  margin: 0 0 10px 0;
}

form > div > fieldset > div > div {
  margin: 0 0 5px 0;
}

form > div > label,
legend {
  width: 25%;
  float: left;
  padding-right: 10px;
}

form > div > div,
form > div > fieldset > div {
  width: 75%;
  float: right;
}

form > div > fieldset label {
  font-size: 90%;
}

fieldset {
  border: 0;
  padding: 0;
}

input[type=text],
input[type=email],
input[type=url],
input[type=password],
textarea {
  width: 100%;
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
  border-right: 1px solid #eee;
  border-bottom: 1px solid #eee;
}

input[type=text],
input[type=email],
input[type=url],
input[type=password] {
  width: 50%;
}

input[type=text]:focus,
input[type=email]:focus,
input[type=url]:focus,
input[type=password]:focus,
textarea:focus {
  outline: 0;
  border-color: #4697e4;
}

@media (max-width: 600px) {
  form > div {
    margin: 0 0 15px 0;
  }
  form > div > label,
  legend {
    width: 100%;
    float: none;
    margin: 0 0 5px 0;
  }
  form > div > div,
  form > div > fieldset > div {
    width: 100%;
    float: none;
  }
  input[type=text],
  input[type=email],
  input[type=url],
  input[type=password],
  textarea,
  select {
    width: 100%;
  }
}

@media (min-width: 1200px) {
  form > div > label,
  legend {
    text-align: right;
  }
}

这两个响应设置是否导致它不起作用?

JSFiddle

1 个答案:

答案 0 :(得分:0)

您只需要删除此类的height: 50px

#menuBackground {
  background: #2f3036;
  width: 100%;
  // removed height
  text-align: center;
}

您已将容器设置为最大高度50px,因此即使菜单展开,容器仍保持50px。所以你看到的只是容器溢出。

注意

不要将ID用于样式元素 - 请参阅此处LINK