Help:Gallery tag

维基百科,自由的百科全书

画廊标签可以以阵列或类似布局排列并显示图像,即图像可以按行和列显示,其原理是利用对<gallery>...</gallery>标签的语法分析将Wikitext转化为HTML。请注意,不同的屏幕和浏览器显示尺寸或有可能影响可读性。

参数

参数需要放在gallery,和HTML结束标签之前,并且和前面间隔一个空格: <gallery 具体参数...>

  • mode=
    • traditional 默认模式
    • nolines 无边框,减少图像偏移,图注居中
    • packed 图像等高,图注居中
    • packed-overlay 类似packed,但图注会在透明浮框里居中显示
    • packed-hover 类似packed-overlay,但浮框仅在鼠标悬停时显示
    • slideshow 幻灯片模式
  • caption= 在画廊上方添加整体图注
  • widths= 设定图像固定宽度(单位:px), mode=packed/packed-overlay/packed-hover/slideshow时无效
  • heights= 设定图像固定高度(单位:px),mode=slideshow时无效
  • perrow= 每行图片数量,mode=packed/packed-overlay/packed-hover/slideshow时无效,详见使用说明
  • showfilename=yes 显示图片文件名
  • class= 例如class="class_name"
  • style= 例如style="css-attr1:xx;..."

mode=slideshow在2023年已失效

Galleries using both mode=slideshow and caption=... are broken as of mid January 2023. It has been reported at Wikipedia:Village pump (technical)#gallery mode="slideshow" broken and phab:T326990 with work on a fix. In the meantime, consider commenting out caption like this so it's easy to revert later:

<gallery mode=slideshow <!--caption=... (slideshow captions were broken in January 2023) -->

使用说明

  • 应该避免使用perrow参数,因为它不能根据图像原有尺寸和长宽比例灵活布局,在移动设备或大屏设备上可能会导致图像放缩失当。如果图像适合固定比例网格布局时,例如图像有“前/后”“低/中/高”等效对比及其它同类情况,最好使用这一参数。
  • 如果perrow省略,图像宽度自会遵循自适应布局,自动根据用户显示宽带决定每行图片数量。
  • Omitting perrow is now the recommended default. Prior to MediaWiki 1.17, the default was 4.
  • 默认长宽120px,在<Gallery>...</Gallery>内的图像并不遵循用户偏好。
  • packed模式下,图片总会自动调整大小,以更加有效利用显示空间
  • 每一行都需要文件名,没有必要加上文件前缀File:
  • 图注英语Photo caption可选,在文件名后添加豎線 ("|"),在竖线右侧写图注。
  • 一定、一定、一定要记得结尾的</gallery>标签。
  • 使用时主要许多参数,尤其是和图像外观有关的参数,并不会在所有网站外观下工作(特别是移动版的Minerva外观)。

Loading in images

When loading in images using the Insert > Images and media, you will get the following: {{tsl|en|File:Name of file||thumb|Caption}}

In a gallery, you must remove all brackets ("[" or "]") (Except in wikilinks that are embedded within captions). You must also remove the |thumb. Finally, remove the File: prefix. You will be left with the following: Name of file|Caption

This is acceptable to the gallery. If you do not want to include a caption, remove |Caption.

语法

There are several "modes" of galleries.

traditional:

"Traditional" mode is currently the default, and, as such, need not be specified. The basic syntax is:

<gallery>

Detroit Publishing Co. - A Yeoman of the Guard (N.B. actually a Yeoman Warder), full restoration.jpg|1
Official_program_-_Woman_suffrage_procession_March_3,_1913_-_crop.jpg|2
Thurston, the famous magician - East Indian Rope Trick.jpg|3
Joseph Ferdinand Keppler - The Pirate Publisher - Puck Magazine - Restoration by Adam Cuerden.jpg|4
</gallery>

Which gives:

If you want to make sure that any future changes to the default gallery mode will keep this style of gallery, you may specify "mode=traditional", similar to the examples below.

nolines:

By specifying the "nolines" mode parameter, the boxes are removed. Using the same syntax as before, but replacing <gallery> with <gallery mode=nolines> we get:

packed:

Changing <gallery> to <gallery mode=packed> produces a centered gallery, with slightly to significantly less space around each image (depending on its relative width and height), as shown below. It may relatively enlarge some images that were smaller in the above views.

For lengthy captions under narrow images, it's probably best to add a heights= parameter to make the images somewhat larger, as the default small size, can lead to ugly images. See below.

Packed-overlay: This uses <gallery mode=packed-overlay> to produce captions overlaying the bottom of the image. The captions are probably best kept short.

Packed-hover:

This uses <gallery mode=packed-hover>. Hover your mouse over the images below. NOTE: This defaults to the behaviour of packed-overlay on the mobile site.

扩展语法

Besides mode, the extended syntax is (see gallery tag help and hereafter for full list of parameters):

<gallery caption="Sample gallery" widths="180px" heights="120px" >

Wiki.png|Captioned
Wiki.png|alt=The Wikipedia logo|Captioned with alt text
Wiki.png|{{tsl|en|Help:Contents/Links||Links}} can be put in captions.
Wiki.png|Full [[MediaWiki]]<br/>[[句法|syntax]] may now be used...
</gallery>

Which produces:

The widths= and perrow= parameters do nothing in the packed, packed-overlay, or packed-hover modes.

However, the heights= parameter can be extremely useful in these modes. This parameter specifies the initial height to render every image thumbnail, before images are possibly scaled up (keeping their size ratio) by JavaScript to fill rows; when needed the Javascript will query the image server to get resized thumbnails for several scales between 100% (the initial height specified) and about 125%. This gives good-looking galleries that can fill pages even when their size ratio are not exactly the same and allows using the actual area available for displaying on narrow mobile and large notebook/desktop/TV, and printing on various paper formats. (This mode also allows horizontally auto-scrollable galleries by embedding them in a large container within a scrollable container sized to fit the page width).

For example, take the following:

<gallery mode=packed>

Detroit Publishing Co. - A Yeoman of the Guard (N.B. actually a Yeoman Warder), full restoration.jpg|A Yeoman Warder, from a Victorian-era {{tsl|en|photochrom||photochrom}}
Official_program_-_Woman_suffrage_procession_March_3,_1913_-_crop.jpg|The Official program of the 1913 Woman Suffrage Procession held in Washington, D.C.
Thurston, the famous magician - East Indian Rope Trick.jpg|[[霍華·薩斯頓|Howard Thurston]] performing the [[印度通天绳|Indian rope trick]]
Joseph Ferdinand Keppler - The Pirate Publisher - Puck Magazine - Restoration by Adam Cuerden.jpg|"The Pirate Publisher", a satire of copyright infringement from the magazine ''[[Puck (雜誌)|Puck]]''.
</gallery>

gives:

As you can see, the narrowness of the image distorts the caption. This may easily be fixed by adding the heights= parameter, as such.

<gallery mode=packed heights=200px>

Detroit Publishing Co. - A Yeoman of the Guard (N.B. actually a Yeoman Warder), full restoration.jpg|A Yeoman Warder, from a Victorian-era {{tsl|en|photochrom||photochrom}}
Official_program_-_Woman_suffrage_procession_March_3,_1913_-_crop.jpg|The Official program of the 1913 Woman Suffrage Procession held in Washington, D.C.
Thurston, the famous magician - East Indian Rope Trick.jpg|[[霍華·薩斯頓|Howard Thurston]] performing the [[印度通天绳|Indian rope trick]]
Joseph Ferdinand Keppler - The Pirate Publisher - Puck Magazine - Restoration by Adam Cuerden.jpg|"The Pirate Publisher", a satire of copyright infringement from the magazine ''[[Puck (雜誌)|Puck]]''.
</gallery>

gives:

Other advice for using packed mode is to keep captions for particularly narrow images as short as is convenient.

Captions

For all modes except traditional, captions are centered. To left-align the captions, specify the CSS declaration text-align:left in the style= attribute:

<gallery mode=packed heights=150px style="text-align:left">

Detroit Publishing Co. - A Yeoman of the Guard (N.B. actually a Yeoman Warder), full restoration.jpg|A Yeoman Warder, from a Victorian-era {{tsl|en|photochrom||photochrom}}
Official_program_-_Woman_suffrage_procession_March_3,_1913_-_crop.jpg|The Official program of the 1913 Woman Suffrage Procession held in Washington, D.C.
Joseph Ferdinand Keppler - The Pirate Publisher - Puck Magazine - Restoration by Adam Cuerden.jpg|"The Pirate Publisher", a satire of copyright infringement from the magazine ''[[Puck (雜誌)|Puck]]''.
</gallery>

gives:

To center a caption when other options are ineffective, perhaps the most-reliable alternative is to use:

yourimage.jpg | <div style="text-align: center;">yourcaption</div>

居中

To center a gallery use class="center":

<gallery class="center">

Detroit Publishing Co. - A Yeoman of the Guard (N.B. actually a Yeoman Warder), full restoration.jpg|1
Official_program_-_Woman_suffrage_procession_March_3,_1913_-_crop.jpg|2
Thurston, the famous magician - East Indian Rope Trick.jpg|3
Joseph Ferdinand Keppler - The Pirate Publisher - Puck Magazine - Restoration by Adam Cuerden.jpg|4
</gallery>

Which gives:

The use of the perrow attribute will cause this to not work. Where a specific number of images per row is desired, use separate <gallery> elements. Centered galleries are the default for most variant gallery modes.

参见