简体   繁体   English

内联元素的垂直对齐并消除内嵌块元素之间的空间

[英]Vertical alignment of inline elements and remove space between from inline-block elements

I have made some problem to solve. 我已经解决了一些问题。 For example, I have two blocks with constant width and display: inline-block ( http://jsfiddle.net/mgs8jLwk/ ). 例如,我有两个宽度恒定并display: inline-blockhttp://jsfiddle.net/mgs8jLwk/ )。 How can I force left one to stay at top? 我该如何强迫左手留在最前面? Sorry if it is duplicate of some question. 抱歉,如果它与某些问题重复。

PS Is there any way to make two or more columns without commenting spaces in html markup? PS有没有办法在不注释html标记中的空格的情况下制作两列或更多列?

Add vertical-align: top to your CSS rule for #one : 在您的CSS规则中为#one添加vertical-align: top

#one
{
    display: inline-block;
    width: 50px;
    height: 50px;
    background: blue;
    vertical-align: top;
}

http://jsfiddle.net/mgs8jLwk/1/ http://jsfiddle.net/mgs8jLwk/1/

You can float them both left: 您可以将它们都向左浮动:

#one, #two {
    float:left;
}

jsFiddle example jsFiddle示例

For Question 1 - Make Blocks Align Top: 对于问题1-使块对齐顶部:

Use " vertical-align:top; " for " #one " div. 对“ #one ” div使用“ vertical-align:top; ”。

#one {
    display: inline-block;
    width: 50px;
    height: 50px;
    background: blue;
    vertical-align:top; /*FIX ADDED*/
}

For Question 2 - Alternate of <!-- COMMENT--> to Remove Space Between DIVS : 对于问题2-替代<!-- COMMENT-->以消除DIVS之间的空间:

Add " font-size:0; " to the parent element: 在父元素中添加“ font-size:0; ”:

#wrapper {
    margin: auto;
    width: 300px;
    font-size:0;
}

Here are few more options to get rid of the space between inline-block elements: http://css-tricks.com/fighting-the-space-between-inline-block-elements/ 这里有一些其他选项可以消除内联块元素之间的空间: http : //css-tricks.com/fighting-the-space-between-inline-block-elements/

Working Demo: http://jsfiddle.net/8cfmjzg9/ 工作演示: http : //jsfiddle.net/8cfmjzg9/

Hope this will help! 希望这会有所帮助!

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

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