简体   繁体   English

如何对齐父 div 两侧的子 div,CSS

[英]How to Align children div either side of parent div, CSS

I've got two images that need to be aligned on either side of the title, preferable on the edges of the page.我有两张图片需要在标题的两侧对齐,最好是在页面的边缘。 I tried to use position relative but it's not responsive to other screen sizes.我尝试使用 position 相对,但它不响应其他屏幕尺寸。 Can someone show me an efficient way to do this, please?有人可以告诉我一个有效的方法吗?

Currently its like this, but i want the two images to be on either side of the title.目前它是这样的,但我希望这两个图像位于标题的两侧。

在此处输入图像描述

 title{ width: 100%; }.web{ font-size: 120px; }.js{ height: 230px; width: 240px; }.css{ height: 230px; width: 440px; }
 <div class="intro py-3 bg-white text-center"> <img class="js" src="images/js-flat.png" alt="js"> <div class="title"> <h2 class="web text-primary display-3 my-4">Wev Dev Quiz</h2> <img class="css" src="images/css2.png" alt="css"> </div> </div>

You can use display:flex property in your parent div.您可以在父 div 中使用display:flex属性。 I did some changes of your html and css codes below.我在下面对您的 html 和 css 代码进行了一些更改。

Here is the html part.这是 html 零件。 I deleted the div that has a title class because all elements that are needed to be align should be seperated each other if you use display:flex我删除了title class 的 div,因为如果使用display:flex ,所有需要对齐的元素都应该相互分离

Here is the HTML and CSS codes:这是 HTML 和 CSS 代码:

 .intro { display: flex; justify-content: center; flex-direction: column; align-items: center }.web{ font-size: 120px; }.js{ width: 240px; }.css{ width: 240px; }
 <div class="intro bg-white "> <img class="js" src="images/js-flat.png" alt="js"> <h2 class="web text-primary">Wev Dev Quiz</h2> <img class="css" src="images/css2.png" alt="css"> </div>

When you use display:flex all items are aligned as horizontally.当您使用display:flex时,所有项目都水平对齐。 But we want to align them vertically.但我们想垂直对齐它们。 So we need to use flex-direction:column (its default is row).所以我们需要使用flex-direction:column (它的默认值是 row)。 To align these items center, we also need align-items because this is used to align in cross axis which is perpendicular to the main axis.为了使这些项目居中对齐,我们还需要align-items因为它用于在垂直于主轴的交叉轴上对齐。

Codepen: you can check it here Codepen:你可以在这里查看

never-mind, i fixed it with:没关系,我修复了它:

.intro { 。介绍 {

 display: flex;
  justify-content: center;
  flex-direction: row;
  align-items: center

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM