gpt4 book ai didi

html - 在 CSS 中排列两个独立的 Div

转载 作者:太空宇宙 更新时间:2023-11-04 09:35:25 25 4
gpt4 key购买 nike

我正在尝试排列两个列表,一个包含 .jpg 文件,在一个 div 中,另一个是另一个列表中的文本文件。

<!DOCTYPE html>
<html>
<style>
.left{height:auto; float:left;}
.right{height:auto; float:right;}

</style>
<body>
<br>
<br>
<?php
$files = scandir('files');
sort($files); // this does the sorting
foreach($files as $file){
$extension = pathinfo($file,PATHINFO_EXTENSION);
if($extension == 'jpg')
echo'<br><div class="left">'.$file .'</div>';
}

$files2 = scandir('files');
sort($files2); // this does the sorting
foreach($files2 as $file2){
$extension2 = pathinfo($file2,PATHINFO_EXTENSION);
if($extension2 == 'txt')
echo'<br><div class="left">'.$file2 .'</div>';
}
?>

</body>
</html>

我想把它们排在浏览器窗口的中间,像这样:

                       test.jpg  test.txt
test2.jpg test2.txt
here.jpg here.txt

我得到的输出是这样的:

test.jpg
test2.jpg
here.jpg
test.txt
test2.txt
here.txt

即使我使用的是左浮动。我尝试对文本文件使用 float:right,但这只是将 div 放在右边,高度又是错误的。

我尝试添加 margin:auto;,display inline-block;

但这些似乎都没有帮助。

某种标记...

<!DOCTYPE html>
<html>
<style>
.left{float:left; height:auto;}
.right{float:left; height:auto;}

</style>
<body>
<br>
<br>

test.jpg test.txt<br>
test2.jpg test2.txt<br>
here.jpg here.txt<br>

</body>
</html>

我需要使用 php,因为我需要扫描某个目录中的文件,它应该是这样的(文件列表位于屏幕中间)。

最佳答案

这个你也可以考虑用flex

检查下面的片段

.container {
display: flex;
flex-wrap: wrap;
border: 1px solid black;
justify-content: center;
}
.container div {
width: 40%;
text-align: center;
}
<div class="container">
<div>
<img src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg" height="20px" width="20px"></img>
</div>

<div>
text1
</div>

<div>
<img src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg" height="20px" width="20px"></img>
</div>
<div>
text2
</div>

<div>
<img src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg" height="20px" width="20px"></img>
</div>
<div>
text3
</div>
</div>

希望对你有帮助

关于html - 在 CSS 中排列两个独立的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40385518/

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