简体   繁体   English

在div内排列元素

[英]Arranging elements inside a div

this is driving me insane... 这让我发疯...

Basically, I have this website and I want it to have header div that contains the name of the website, search bar, and sign in/log in buttons. 基本上,我有这个网站,并且我希望它具有标题div,其中包含网站的名称,搜索栏和登录/登录按钮。 I want the logo to be on the left, search bar in the middle, and sign in and log in on the right, one on top of each other. 我希望徽标在左侧,搜索栏在中间,并登录并登录在右侧,一个在另一个上方。 The logo and search should be in the middle vertically. 徽标和搜索应垂直位于中间。

here's what I have in code: 这是我在代码中的内容:

 #home { position: absolute; } #search { position: absolute; left: 30%; } #search_box { width: 500px; } #header { position: relative; height: 90px; line-height: 90px; background-color: burlywood; color: beige; padding: 10px; float: top; margin: auto; border-style: groove; } #log_in { float: right; } #sign_in { float: right; } 
 <div id="header"> <div id="home"> <h1>HatSpace</h1> </div> <div id="search"> <form> <input id="search_box" type="text" placeholder="Search the Website"> </form> </div> <div id="log_in"> <p>Log in</p> </div> <div id="sign_in"> <p>Sign in</p> </div> </div> 

This is what I get after Chheda's code 这是我在Chheda的代码之后得到的

 #header { display: flex; justify-content: space-between; align-items: center; background-color: burlywood; color: beige; border-style: groove; } #register { display: flex; flex-direction: column; } 
 <div id="header"> <div> <h1><a href="index.html"</a>HatSpace</h1> </div> <div id="register"> <form> <input style = "width: 500px;" type="text" placeholder="Search the Website"> </form> </div> <div> <div> <p>Log in</p> </div> <div> <p>Sign in</p> </div> </div> </div> 

If you remove href from HatSpace element, then it will look like exactly what I want to, but I need href there, what is wrong with this!!! 如果您从HatSpace元素中删除href,那么它将看起来完全像我想要的,但是我在那里需要href,这有什么问题!!!

Are you looking for something like this ? 您是否正在寻找这样的东西?

 #header { display: flex; justify-content: space-between; align-items: center; } #register { display: flex; flex-direction: column; } 
 <div id="header"> <div id="home"> <h1>HatSpace</h1> </div> <div id="register"> <div id="log_in"> <p>Log in</p> </div> <div id="sign_in"> <p>Sign in</p> </div> </div> <div id="search"> <form> <input id="search_box" type="text" placeholder="Search the Website"> </form> </div> </div> 

First, put the "Log in" and "Sign in" texts into the same container (eg <div id="log_in"> </div> ). 首先,将“登录”和“登录”文本放入同一容器中(例如<div id="log_in"> </div> )。 Remove the line-height:90px from #header (I believe you added it to move the search box). #header删除line-height:90px (我相信您已添加它来移动搜索框)。 Instead, add margin-top:30px to #search . 而是将margin-top:30px添加到#search If you needed the line-height for #home , too, just add it there. 如果您还需要#homeline-height ,只需在其中添加它。

Hope this matches your needs ;) 希望这符合您的需求;)

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

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