简体   繁体   English

没有应用样式或br元素。 为什么其垂直对齐

[英]No styling or br element applied. Why its getting aligned vertically

I have following html without any styles applied. 我有以下html,未应用任何样式。

<div>
    <div>
        <h2>Image1 Description</h2>
        <img src="\images\Image1.jpg">
    </div>
    <div>
        <h2>Image2 Description</h2>
        <img src="\images\Image2.jpg">
    </div>
    <div>
        <h2>Image3 Description</h2>
        <img src="\images\Image3.jpg">
    </div>
    <div>
        <h2>Image4 Description</h2>
        <img src="\images\Image4.jpg">
    </div>
</div>

When I run this ( http://jsfiddle.net/zQKVu/ ), it shows me h2 text then its images vertically even though I haven't added any line break <br/> element and I am just wondering why these elements are getting aligned vertically and not horizontally? 当我运行此代码( http://jsfiddle.net/zQKVu/ )时,即使没有添加任何换行<br/>元素,它也会向我显示h2文本,然后垂直显示其图像,我只是想知道为什么这些元素是垂直对齐而不是水平对齐?

That's the nature of header tags, they default to display: block . 那就是标题标签的性质,它们默认display: block You can change it with 您可以用

h2 { 
  display: inline; 
}

See the forked fiddle: http://jsfiddle.net/c3UCs/ 请参阅分叉的提琴: http : //jsfiddle.net/c3UCs/

Because div is not an inline element,it's a block same as H2 element. 由于div不是内联元素,因此与H2元素相同。

to display in same line : 在同一行中显示:

use css property: diplay:inline; 使用css属性: diplay:inline;

The default style for <h2> includes display: block; <h2>的默认样式包括display: block; .

header are block elements so they take all of the space availble clearing all other elements. 标头是块元素,因此它们会占用所有可用空间以清除所有其他元素。

To display the pictures inline with the headers use: 要以标题内联显示图片,请使用:

h2{
    display:inline;
}

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

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