gpt4 book ai didi

html - 问题让 Div 靠近另一个拥抱

转载 作者:太空狗 更新时间:2023-10-29 15:55:14 30 4
gpt4 key购买 nike

我在一个容器 Div 中有 6 个 Div。这些 div 必须以某种方式对齐以满足客户的需求。我已经为我正在处理的客户网站采用了基本布局,并创建了一个要在此处发布的示例。我有一个常规 CSS 文件和一个移动 CSS 文件。在移动 View 中时,Divs 将按顺序 1-6 对齐。在常规 View 中,Divs 1、3、4 和 5 将向右对齐。 Div 2 和 6 将向左对齐。除了大约 10px 之外,我需要 div 6 来拥抱 div 2 的底部。使用我当前的代码,Divs 之间存在巨大差距,并且 6 被进一步推到页面下方,右侧 Divs 具有更多内容。所有这些 Div 都有动态内容,可以使它们增加或减少显示的内容量。我怎样才能让 Div 6 忽略右侧的 Div 并拥抱 Div 2 而不会搞砸这些在移动设备中的压缩和显示方式? (您只需调整浏览器窗口的大小,即可让移动版本显示此代码。)

这是我的 HTML 和 CSS:

/*
exampleCSS.css
*/
#containerDiv {
width: 500px;
margin-left: auto;
margin-right: auto;
padding: 10px;
}

#div1 {
border: 1px solid red;
background-color: red;
margin-bottom: 10px;
width: 25%;
float: right;
}

#div2 {
border: 1px solid blue;
background-color: blue;
margin-bottom: 10px;
width: 65%;
float: left;
}

#div3 {
border: 1px solid green;
background-color: green;
margin-bottom: 10px;
width: 25%;
float: right;
}

#div4 {
border: 1px solid orange;
background-color: orange;
margin-bottom: 10px;
width: 25%;
float: right;
clear: right;
}

#div5 {
border: 1px solid purple;
background-color: purple;
margin-bottom: 10px;
width: 25%;
float: right;
clear: right;
}

#div6 {
border: 1px solid pink;
background-color: pink;
margin-bottom: 10px;
width: 65%;
float: left;
}

/*
exampleCSSMobile.css
*/
#containerDiv {
width: 100%;
}

#div1 {
border: 1px solid red;
background-color: red;
margin-bottom: 10px;
width: 100%;
}

#div2 {
border: 1px solid blue;
background-color: blue;
margin-bottom: 10px;
width: 100%;
}

#div3 {
border: 1px solid green;
background-color: green;
margin-bottom: 10px;
width: 100%;
}

#div4 {
border: 1px solid orange;
background-color: orange;
margin-bottom: 10px;
width: 100%;
}

#div5 {
border: 1px solid purple;
background-color: purple;
margin-bottom: 10px;
width: 100%;
}

#div6 {
border: 1px solid pink;
background-color: pink;
margin-bottom: 10px;
width: 100%;
}
<!DOCTYPE html>
<html>
<head>
<title>Example HTML</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="exampleCSSMobile.css" media="(max-width: 940px)" rel="stylesheet" type="text/css" />
<link href="exampleCSS.css" media="(min-width: 940px)" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="containerDiv">
<div id="div1"><p>Test 1 Lorem Ipsum.</p></div>
<div id="div2"><p>Test 2 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim.</p></div>
<div id="div3"><p>Test 3 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. </p></div>
<div id="div4"><p>Test 4 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim.</p></div>
<div id="div5"><p>Test 5 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim.</p></div>
<div id="div6"><p>Test 6 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim.</p></div>
</div>
</body>
</html>

示例图片,空格介于 2 和 6 之间: enter image description here

最佳答案

float 元素会尽可能地向左或向右移动在它原来的位置(这很重要#1)。

所以这样说:

我们有2个div

<div class="div5">div5</div>
<div class="div6">div6</div>

.div-blue{
width:100px;
height:100px;
background: blue;
}

.div-red{
width:50px;
height:50px;
background: red;
}

没有float他们会一个比一个低

enter image description here

如果我们 float: right div5 , div6当它是div5时被定位在线上,

/*the lines are just for illustrate*/

enter image description here

所以如果现在我们 float: left div6它会尽可能地向左移动,“在这一行”(参见上面的#1),所以如果div5改行,div6将遵循它。

现在让我们将其他 div 添加到等式中

<div class="div4">div4</div>
<div class="div5">div5</div>
<div class="div6">div6</div>

.div-gree{
width:150px;
height:150px;
background: green;

float:right;
}

我们有这个

enter image description here

如果我们设置 clear: rightdiv5 ,我们强制它走下面的线div4

enter image description here

所以你知道为什么会发生。 Here the jsfiddle where I code this

现在,如何修复它

只需删除 <div id="div6"> 的 float 并设置 display: inline-block

像这样:

#div6 {
border: 1px solid pink;
background-color: pink;
margin-bottom: 10px;
width: 65%;

/*float: left;*/ /*removed*/

display: inline-block; /*added*/
}

这将保持没有 float 的元素的正常行为( float 元素之后的元素将围绕它流动。)。 display: inline-block是为了你的情况维护margindiv2 .

HERE A JSFIDDLE EXAMPLE WORKING FOR YOUR CASE

我希望这对现在和将来有所帮助:)

关于html - 问题让 Div 靠近另一个拥抱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813047/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com