html - 如何让内容部分置顶?

标签 html css

我尝试布局我的 html 页面并将内容部分放在顶部的右侧。

与:

float: right;>> 内容部分向右移动,但不在页面顶部。

float: top;>> 内容部分向左移动。

请参阅屏幕截图以获取说明!

enter image description here

    form.contact{
      margin: 1% 1.5%;
      padding: 5px;
      border-style: solid;
      width: 37.5%;
      hight: auto;
    }
    #content{
      float: right;
      padding: 10px;
      border:1px solid red;
      width: 50%;
      hight: auto;
    }
    form {
      float: left;
      margin: 1% 1.5%;
      width: 63%;
    }
    nav{
      float: left;
      margin: 0 1.5%;
      width: 63%;
    }
    footer{
      float: left;
      margin: 1% 1.5%;
      width: 37.5%;
      border-style: solid;
    }
        <form class="contact">
          <label>Contact</label></br></br>
          First name:
          <input type="text" name="firstname" value="Your name please?">
          <br><br>
          Email:
          <input type="text" name="lastname" value="Your email please?">
          <br><br>
          <input type="submit" value="Press the button">
        </form>
        <nav>
           <a href = #>Just Me</a><br>
           <a href = #>Portfolio</a>
        </nav>
        <section id = "content"><h2><strong>Content section</strong></h2></section>
        <footer>
          <label>Socializing</label>
        </footer>

最佳答案

div#content中添加position也可以让内容向右走....

body {
  position: relative;
}
form.contact {
  margin: 1% 1.5%;
  padding: 5px;
  border-style: solid;
  width: 37.5%;
  hight: auto;
}
#content {
  border: 1px solid red;
  display: block;
  float: right;
  padding: 10px;
  position: absolute;
  right: 10px;
  top: 10px;
  width: 50%;
}
form {
  float: left;
  margin: 1% 1.5%;
  width: 63%;
}
nav {
  float: left;
  margin: 0 1.5%;
  width: 63%;
}
footer {
  float: left;
  margin: 1% 1.5%;
  width: 37.5%;
  border-style: solid;
}
<html>

<head>
  <title>title</title>
</head>

<body>
  <form class="contact">
    <label>Contact</label>
    </br>
    </br>
    First name:
    <input type="text" name="firstname" value="Your name please?">
    <br>
    <br>Email:
    <input type="text" name="lastname" value="Your email please?">
    <br>
    <br>
    <input type="submit" value="Press the button">
  </form>
  <nav>
    <a href=#>Just Me</a>
    <br>
    <a href=#>Portfolio</a>
  </nav>
  <section id="content">
    <h2><strong>Content section</strong></h2>
  </section>
  <footer>
    <label>Socializing</label>
  </footer>
</body>

</html>

关于html - 如何让内容部分置顶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41314468/

相关文章:

javascript - 使用按钮更改多个div的边框颜色

javascript - 使用 detach() 将每个元素放置在其他 div 中的每个其他元素之后

javascript - 如何使用 javascript 激活 CSS3 (webkit) 动画?

html - 在类内的链接上设置悬停

javascript - Dojo 过滤小部件数据溢出

css - Safari Flexbox,可滚动,100vh 问题

html - 应用样式表后两个按钮(超链接、提交)大小不一样

HTML/CSS - 左对齐并在同一行居中

javascript - 如何为网站演示制作响应式面板切换选项?

javascript - jquery ajax调用后跳转到页面顶部?