我使用flexbox在桌面窗口屏幕中布局index.html,使两个div元素位于一行,一个长的div元素位于第二行,两个div元素位于第三行,其中一个包含列表。我需要在第一行和第三行的两个div元素之间留一些间距。当我尝试在div元素上使用margin-left或margin-right时,div元素折叠在彼此的顶部。
我试过改变每个div元素的宽度,但是它们最终会相互重叠,我也试过在内容类上使用justify-content和align-content,但是没有任何效果。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
header {
background-color: #414C6A;
background-image: url("../img/img2.jpg");
background-size: 100%;
background-position: top left;
position: relative;
background-repeat: no-repeat;
padding: 20%;
text-align: center;
color: white;
}
footer {
background-color: #7481AD;
color: white;
text-align: center;
padding: 14px;
}
.title {
margin: 0;
}
.unordered_list {
list-style: none;
}
/* STYLING CONTENT */
.content {
background-color: #554A4E;
color: white;
padding: 1.25em;
}
.div-content,
.div-list {
padding: 0.75em;
border-radius: 12px;
margin-bottom: 1em;
}
.div-content p,
.div-list .unordered_list {
margin-top: 0.3125em;
padding: 0 0.625em;
}
.div-content {
background-color: #25476c;
}
.div-list {
background-color: #564B75;
}
/* LAYOUT */
body {
display: flex;
flex-direction: column;
}
.content {
flex: 1 0 auto;
display: flex;
flex-wrap: wrap;
}
.div-content {
width: 50%;
}
.grow {
width: 100%;
}
.div-list {
flex-grow: 1;
}
footer {
flex-shrink: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<title>CSS Basics</title>
<!--
Go to this link:
http://www.cssdrive.com/imagepalette/index.php
-->
</head>
<body>
<header>
<h1 class="title">CSS Basics</h1>
</header>
<div class="content">
<div class="div-content item-1">
<h2>What is Lorem Ipsum?</h2>
<p>
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.
</p>
</div>
<div class="div-content item-2">
<h2>Diego Salas</h2>
<p>
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.
</p>
</div>
<div class="div-content grow">
<h2>Diego's Favorite Dogs</h2>
<p>
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. It
uses a dictionary of over 200 Latin words, combined with a handful of
model sentence structures, to generate Lorem Ipsum which looks
reasonable. The generated Lorem Ipsum is therefore always free
from repetition, injected humour, or non-characteristic words etc.
</p>
</div>
<div class="div-list">
<h2>Diego's Favorite Programming Language</h2>
<ul class="unordered_list">
<li>Python</li>
<li>Java</li>
<li>C/C++</li>
<li>JavaScript</li>
</ul>
</div>
<!-- <img src="img/img1.jpg" alt="Mountain">-->
<div class="div-content">
<h2>Why do we use it?</h2>
<p>
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.
</p>
</div>
</div>
<footer>
<p>CSS Basics © Diego Salas</p>
</footer>
</body>
</html>
5条答案
按热度按时间vom3gejh1#
通过将我们希望位于同一行的每一个元素 Package 在一个
<div class="row"></div>
中,你就可以让每个元素占据该行宽度的一半,这也会考虑到元素的边距。这里的重要部分是:
这使得每个
row
元素的宽度是其父对象的100%,在本例中是container
元素。然后,您只需设置
.div-content
和.div-list
的宽度和边距下面的代码片段展示了这一点。
一个二个一个一个
jxct1oxe2#
试着把它们放在一个flex容器中。你可以调整列的宽度。参见下面的代码。希望能有所帮助。
fdbelqdn3#
在这里,您可以更改宽度和应用基于全宽的边距,并在响应模式下更改全宽宽度
uqjltbpv4#
在flexbox中,边距可能有点烦人。注意我是如何将flex容器的宽度设置为1 oovw-1 em,然后将边距设置为0.5em并自动设置的。这将在所有元素周围创建一个均匀的边距,一旦我们在它们上面添加了0.5en的边距。还要注意什么都不会折叠,除非你通过增加同一行上项目的flex基础,直到它们不再适合同一行。
jc3wubiy5#
在父元素上使用
inline-flex
: