简体   繁体   English

使第二个子元素居中于父div宽度float子元素内

[英]make center the second child element inside a parent div width float child element

I have this html structure. 我有这个html结构。

<div id="parent">
    <div class="child">
        <h1>title</h1>
        <p>content</p>
    </div>
    <div class="child">
        <h1>title</h1>
        <p>content</p>
    </div>
    <div class="child">
        <h1>title</h1>
        <p>content</p>
    </div>
</div>

and a css for those elements. 以及这些元素的CSS。

#parent{
    padding: 0px 8px;
    overflow: auto;
    max-width: 100%;
    max-height: 100%;
}
.child{
    width: 300px;
    display: block;
}
.child:first-child{
    float: left;
}
.child:last-child{
    float: right;
}
.child:nth-child(2){
/* what is the style here to make it center? */
}

as you can see from above codes, the objective is to make those child elements align correctly in a neat and clean way so the first child element is floated left, the last child element is floated right and the second child element should be exactly on between those left and right child elements so what im trying to achieve is a three box that align on a equal patern inside a parent div. 从上面的代码中可以看到,目标是使这些子元素以整齐干净的方式正确对齐,因此第一个子元素向左浮动,最后一个子元素向右浮动,第二个子元素应恰好位于这些左,右子元素,因此我要实现的是在父div内等距排列的三个框。 So far I tried margin: 0 auto; 到目前为止,我尝试了margin:0 auto; on the middle child element but unfortunately does not work so currently Im looking for a precise solution to achieve my desired output. 在中间子元素上,但不幸的是无法正常工作,因此目前我正在寻找一种精确的解决方案以实现所需的输出。

Just float it: 只要浮动它:

#parent{
    padding: 0px 8px;
    overflow: auto;
    max-height: 100%;
    float:left;
    width:900px;
}
.child{
    width: 300px;
    display: block;
    float:right;
}

here's a fiddle 这是一个小提琴

Float your second div to the left and apply a margin left if needed. 将第二个div浮动到左侧,并在需要时向左侧应用边距。 If you are trying to create a responsive template, simply use % instead of pixels. 如果您尝试创建自适应模板,只需使用%而不是像素。 I hope that makes sense. 我希望这是有道理的。

.child:first-child, .child:nth-child(2) {
    float:left;
}

.child:nth-child(2) {
    /* what is the style here to make it center? */
    margin-left: what ever pixels or %;
}

.child:last-child {
    float:right;
}

JSFIDDLE (Responsive): JSFIDDLE(响应式):

http://jsfiddle.net/83Gg2/ http://jsfiddle.net/83Gg2/

You don't need to float the elements one to other, what you need, is to use display: inline-block property on them, is an hacky but an cleaner approach: 您不需要将元素相互浮动,您需要的是使用display: inline-block property在它们上,这是一种骇人但更干净的方法:

#parent{                                                                        
     width: 100%; 
     height: 100%;                                                  
     max-width: 100%;
     max-height: 100%;                                          
     overflow: hidden;                                                           
}                                                                                
.child{                                                                         
     width: 33%;  // Since there are 3 childs.                                                               
     display: inline-block;                                                       
}     

The trick and hack here is that you need to comment the space between the child elements in your HTML code, since the property display:inline-block only align elements that have not space between them, so your html code should look like this: 这里的技巧和窍门是,您需要注释HTML代码中子元素之间的空间,因为属性display:inline-block仅对齐没有空格的元素,因此html代码应如下所示:

<div id="parent">                                                               
     <div class="child">                                                         
         <h1>title</h1>                                                          
         <p>content</p>                                                          
     </div><!--                                                                  
     --><div class="child">                                                      
         <h1>title</h1>                                                          
         <p>content</p>                                                                               
     </div><!--                                                                  
     --><div class="child">                                                      
         <h1>title</h1>                                                          
         <p>content</p>                                                          
     </div>                                                                      
 </div>                                                                          
 ~     

This is the link to the JsFiddle Check it out 这是链接到的jsfiddle 检查出来

~
~

Ok, since you tag jquery also, here is the JQ way. 好的,因为您也标记了jquery,所以这是JQ方式。

If you dont want to set fix width to #parent and you want a fix width to .child , use this. 如果您不想将固定宽度设置为#parent并且想要将固定宽度设置为.child ,请使用此选项。 Also works in cross browsers and old browsers. 也可以在跨浏览器和旧浏览器中使用。

DEMO http://jsfiddle.net/yeyene/VW9mw/ 演示http://jsfiddle.net/yeyene/VW9mw/

$(document).ready(function(){
    moveCenter();
    $(window).resize(function() {
        moveCenter();
    });
});
function moveCenter(){
    var mar = ($('#parent').width()-$('.child').width()*3)/2;    
    $('.child').eq(1).css({
        'margin-left': mar+'px',
        'margin-right':mar+'px'
    });
}

Flexbox makes this trivial: Flexbox使这个琐碎的事:

#parent {
  display: flex;
}
.child {
  flex: 1;
}

Add prefixes and older syntax as necessary, conforming to http://caniuse.com/#search=flexbox 根据需要添加前缀和较早的语法,符合http://caniuse.com/#search=flexbox

I whipped up an example for you: http://codepen.io/anon/pen/tribL 我为您提供了一个示例: http : //codepen.io/anon/pen/tribL

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

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