简体   繁体   English

如何使用CSS并居中放置三个元素

[英]How to place three elements next to each other using css and centered

I'm pretty new to css and web development and I want to create a new pwa using react and I need a header with one I'mage on the left, a centered text in the center and another I'mage on the right of this header. 我对CSS和Web开发还很陌生,我想使用react创建一个新的pwa,我需要一个标题,其中一个我在左边,中间居中,另一个在右边。标头。 So good so far, I can't successfully place them next to each other but I can't do it in a responsive way. 到目前为止,到目前为止,我还不能成功地将它们彼此并排放置,但是我无法以响应方式进行。 It means, when I inspect with chrome and swap the landscape from vertical to horizontal, the text which should be in the center moves a bit to the left. 这意味着,当我使用chrome进行检查并将景观从垂直变为水平时,应在中间的文本向左移动一点。 I know it's my code but I don't know how to fix this issue and how to make this think responsive. 我知道这是我的代码,但我不知道如何解决此问题以及如何使此问题响应。

I guess the problem is in .headerApp p , that I do the margin left here. 我猜问题出在.headerApp p ,我在这里留有余量。 is there any way to display it on the center without needing any margins? 有什么方法可以在中心显示它而无需任何边距?

Here is my html code: 这是我的html代码:

 .headerApp { background-color: #57628d; height: 4rem; border-style: solid; } .headerApp .backButton { width: 36px; height: 36px; float: left; margin-top: 0.9rem; margin-bottom: 2%; margin-left: 1rem; } .headerApp p { color: white; float: left; width: 50%; position: relative; font-family: "Ubuntu"; font-weight: normal; font-size: 32px; margin-top: 0.8rem; margin-left: 10%; } .headerApp .toProfile { width: 44px; height: 44px; float: right; margin-right: 1rem; margin-top: 0.5rem; } 
 <header className="headerApp"> <img className="backButton" src={require( "../assets/back.png")} alt="back button" /> <p>Fahrt nach</p> <img className="toProfile" src={require( "../assets/Profil.png")} alt="go to own profile" /> </header> 

You can use grid to achieve this 您可以使用网格来实现这一目标

 .headerApp{ display:grid; grid-template-columns: 1fr 1fr 1fr; grid-auto-flow: column; } 
 <header class="headerApp"> <img class="backButton" src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=650&w=940" alt="back button" height="40px" width="40px" /> <p>Fahrt nach</p> <img class="toProfile" src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=650&w=940" alt="go to own profile" height="40px" width="40px" /> </header>` 

Using Flexbox you can do it like this 使用Flexbox可以像这样

 .headerApp{ display: flex; justify-content: space-between; } 
 <header class="headerApp"> <img class="backButton" src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=650&w=940" alt="back button" height="40px" width="40px" /> <p>Fahrt nach</p> <img class="toProfile" src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=650&w=940" alt="go to own profile" height="40px" width="40px" /> </header>` 

 .content { text-align: center; color:black; font-size: 45px; height: 60px; width: 100%; border: 3px solid black; position: sticky; background-image: url("bg2.jpeg"); } 
 <header> <div class="content"> <span>HOME</span> <span>ABOUT</span> <span>CONTACT</span> <span><img src="sett.jpg" height=60px; width=60px; align="right"></span> <span><img src="bg1.jpeg" height=60px; width=60px; align="left"></span> </div> </header> 

You can add the header content in the center and two images one to the left and the other to the right. 您可以在标题中添加标题内容,并在左侧添加两个图像,在右侧添加两个图像。

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

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