如何并排放置两个div

问题描述 投票:0回答:9

所以我对编写代码还很陌生(大约几周),并且在为我的网站编写代码时遇到了困难。我想要这样的布局:

Image 但我不知道如何将两个盒子并排放置。一个盒子是解释我的网站的视频,另一个盒子是注册表格。 我希望这些盒子彼此相邻,彼此之间间隔大约一英寸。

我还需要有关网站标题宽度的帮助。现在看起来标题不适合页面,导致水平滚动。有点像这样:

Image 我希望整个网站就像一个大盒子,所有的内容都在那个盒子里。有人可以帮我吗?非常感谢。预先感谢您。

html css
9个回答
80
投票

http://jsfiddle.net/kkobold/qMQL5/

#header {
    width: 100%;
    background-color: red;
    height: 30px;
}

#container {
    width: 300px;
    background-color: #ffcc33;
    margin: auto;
}
#first {
    width: 100px;
    float: left;
    height: 300px;
        background-color: blue;
}
#second {
    width: 200px;
    float: left;
    height: 300px;
    background-color: green;
}
#clear {
    clear: both;
}
<div id="header"></div>
<div id="container">
    <div id="first"></div>
    <div id="second"></div>
    <div id="clear"></div>
</div>


44
投票

这会起作用

<div style="width:800px;">
  <div style="width:300px; float:left;"></div>
  <div style="width:300px; float:right;"></div>
</div>
<div style="clear: both;"></div>

8
投票
<div style="display: inline">
    <div style="width:80%; display: inline-block; float:left; margin-right: 10px;"></div>
    <div style="width: 19%; display: inline-block; border: 1px solid red"></div>
</div>

6
投票

我只是并排给出两个响应式 div 的代码

*{
  margin: 0;
  padding: 0;
}

#parent {
  display: flex;
  justify-content: space-around;
}

#left {
  border: 1px solid lightgray;
  background-color: red;
  width: 40%;
}

#right {
  border: 1px solid lightgray;
  background-color: green;
  width: 40%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="parent">
        <div id="left">
        lorem ipsum dolor sit emet
        </div>
        <div id="right">
        lorem ipsum dolor sit emet
        </div>
    </div>
</body>
</html>


3
投票

这只是您提供的线框的简单(非响应式)HTML/CSS 翻译。

enter image description here

HTML

<div class="container">

  <header>
    <div class="logo">Logo</div>
    <div class="menu">Email/Password</div>
  </header>

  <div class="first-box">
    <p>Video Explaning Site</p>
  </div>

  <div class="second-box">
    <p>Sign up Info</p>
  </div>

  <footer>
    <div>Website Info</div>
  </footer>

</div>

CSS

.container {
  width:900px; 
  height: 150px;
}

header {
  width:900px; 
  float:left; 
  background: pink; 
  height: 50px;
}

.logo {
  float: left;
  padding: 15px
}

.menu {
  float: right;
  padding: 15px
}

.first-box {
  width:300px; 
  float:left; 
  background: green; 
  height: 150px;
  margin: 50px
}

.first-box p {
  color: #ffffff;
  padding-left: 80px;
  padding-top: 50px;
}

.second-box {
  width:300px; 
  height: 150px; 
  float:right; 
  background: blue;
  margin: 50px
}

.second-box p {
  color: #ffffff;
  padding-left: 110px;
  padding-top: 50px;
}

footer {
  width:900px; 
  float:left; 
  background: black; 
  height: 50px;
  color: #ffffff;
}

footer div {
  padding: 15px;
}

2
投票

您可以通过三种方式做到这一点:

浮动法

<div class="float-container">
  <div class="float-child">
    <div class="green">Float Column 1</div>
  </div>
  <div class="float-child">
    <div class="blue">Float Column 2</div>
  </div>
</div>

.float-container {
    border: 3px solid #fff;
    padding: 20px;
}

.float-child {
    width: 50%;
    float: left;
    padding: 20px;
    border: 2px solid red;
}

Flexbox方法

<div class="flex-container">
  <div class="flex-child magenta">
    Flex Column 1
  </div>
  <div class="flex-child green">
    Flex Column 2
  </div>
</div>
.flex-container {
    display: flex;
}

.flex-child {
    flex: 1;
    border: 2px solid yellow;
}  

.flex-child:first-child {
    margin-right: 20px;
} 

CSS网格方法

<div class="grid-container">
    <div class="grid-child purple">
        Grid Column 1
    </div>
    <div class="grid-child green">
        Grid Column 2
    </div>
</div>
.grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-gap: 20px;
}

来源


1
投票

深入研究 CSS 和 HTML,你就会明白这一点。它只是左右浮动框,并且这些框需要位于同一个 div 内。 http://www.w3schools.com/html/html_layout.asp可能是一个很好的资源。


1
投票

关于网站的宽度,您需要考虑使用包装类来包围您的内容(这应该有助于限制元素宽度并防止它们超出内容太多):

<style>
.wrapper {
  width: 980px;
}
</style>

<body>
  <div class="wrapper">
    //everything else
  </div>
</body>

就内容框而言,我建议尝试使用

<style>
.boxes {
  display: inline-block;
  width: 360px;
  height: 360px;
}
#leftBox {
  float: left;
}
#rightBox {
  float: right;
}
</style>

我会花一些时间研究盒子对象模型和所有“显示”属性。他们将永远有帮助。特别关注“inline-block”,我几乎每天都使用它。


0
投票

你可以试试这个。

div 是一个块元素,因此默认情况下它将占用一个块空间 所以这里我们给它显示属性 inline-block。

.box {
    width: 100px;
    height: 100px;
    margin: 10px 10px 10px 10px;
    border: 5px solid black;
    border-radius: 8px;
    text-align: center;
    align-content: center;
    display: inline-block;
}


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Frontend Learning</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <div class="box">box1</div>
    <div class="box">box2</div>
    <div class="box">box3</div>
    
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.