简体   繁体   English

无法在div内居中放置元素

[英]Can't center an element inside div

I have three elements inside a div. 我在一个div中有三个元素。 I want to position one on the left, one in the middle and the last one on the right. 我想在左边放置一个,在中间放置一个,在右边放置最后一个。 I have tried the following: 我尝试了以下方法:

  .search-form { float: right; } .menu_icon:before { content:url(/sites/all/themes/mytheme/images/hamburger.png); } .main_logo { margin: 0% auto; } .main_logo:before { content:url(/sites/all/themes/mytheme/images/logop.png); } .menu_icon { float: left; } 
  <div class="fixed"> <div class="fixed-container"> <a href="#menu" title="Menu" class="menu_icon"> </a> <form action="/search/node" method="post" id="search-form" class="search-form"> <div class="form-item"> <input type="text" class="input-text" value="Search the website" onFocus="this.value=''" onBlur="if(this.value=='') this.value='Search the website';" size="25" name="keys" /> <input type="submit" value="" name="op" alt="Search" /> <input type="hidden" value="<?php print drupal_get_token('search_form'); ?>" name="form_token" /> <input type="hidden" value="search_form" id="edit-search-form" name="form_id" /> <input type="hidden" name="type[your_content_type]" id="edit-type-your_content_type" value="your_content_type" /> </div> </form> <a href="/" title="Home page" rel="home" class="main_logo"> </a> </div> </div> 

Also I have tried to individually wrap those elements in divs and applying styles to those divs instead of elements. 我也尝试将这些元素分别包装在div中,并将样式应用于这些div而不是元素。 But the logo always sticks to the menu icon as it can be seen from the snippet below. 但是徽标始终粘贴在菜单图标上,如下面的代码片段所示。 Why it's not working? 为什么不起作用?

在此处输入图片说明

Currently your a is displayed as inline, which makes it unable to be centered as a block. 当前,您的a显示为内联,这使其无法居中定位为块。

Also, display:block does not work on its own with margin:0 auto , as blocks have an understood width of 100%. 另外, display:block不能单独使用margin:0 auto ,因为块的可理解宽度为100%。 You also need a given width for it to work. 您还需要给定宽度才能工作。

 .first { float:right;background:red; } .second{ float:left;background:blue; } .third{ margin:0% auto;background:gold; display:block;width:10em; /*Unnecessary but may be added*/text-align:center; } 
 <div class="fixed"> <div class="fixed-container"> <span class="first">First</span> <div class="second">Second</div> <span class="third">Third</span> </div> </div> 

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

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