# 图像对齐和网格

**URL:** https://meta.discourse.org/t/image-alignment-and-grid/271560
**Category:** Theme component
**Tags:** deprecated
**Created:** [2023年七月13日 17:10 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560 "2023-07-13T17:10:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年七月13日 17:10 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/1 "2023-07-13T17:10:37Z")

</div>

| | | |
| --- | --- | --- |
| ℹ | **摘要** | 向编辑器工具栏添加图片网格和对齐按钮 |
| 🛠 | **代码仓库** | [https://github.com/Lillinator/image-alignment](https://github.com/Lillinator/image-alignment) |
| ❓ | **安装指南** | [如何安装主题或主题组件](https://meta.discourse.org/t/how-do-i-install-a-theme-or-theme-component/63682) |
| 📖 | **Discourse 主题新手？** | [Discourse 主题使用入门指南](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966) |

安装此主题组件

此主题组件在帖子编辑器工具栏中插入按钮，用于将上传的图片与文本左对齐或右对齐、居中对齐，或使用新实现的网格标签将多张图片对齐成网格。网格将根据图片的数量和各自的尺寸自动调整大小，并且还会链接到上传的较大版本。一个帖子中可以多次使用这些按钮/对齐功能，以创建各种布局。 桌面和移动视图均适用。 🙂

要使用，只需点击所需的图片对齐按钮，然后将图片上传到结果标签之间的指定位置（由占位符文本标记，该文本可在组件设置中编辑）。

请注意，此组件的网格部分用于对齐上传的图片，而不是图片链接（其他对齐选项可用于链接图片）。

* * *

### 截图

> **编辑器工具栏网格按钮（附带网格代码示例）**
>
> ![IMG_3719](https://global.discourse-cdn.com/meta/original/4X/4/9/e/49edf383e5dc891f8b934f2173a045bc184355c3.jpeg)

> **图片网格示例**
>
> ![IMG_3713](https://global.discourse-cdn.com/meta/original/4X/3/4/4/34455851b1c978110393d4647d9e4b74e5818df9.jpeg)

> **编辑器工具栏右对齐按钮（附带右对齐代码示例）**
>
> ![IMG_3717](https://global.discourse-cdn.com/meta/original/4X/c/2/b/c2b227f8c0780cd1bbec77017fae9558fa896a06.jpeg)

> **图片在文本右侧对齐**
>
> ![IMG_3708](https://global.discourse-cdn.com/meta/original/4X/1/0/2/102ce5938ec472c00684e146531576fae8cae9d9.jpeg)

> **编辑器工具栏左对齐按钮（附带左对齐代码示例）**
>
> ![IMG_3718](https://global.discourse-cdn.com/meta/original/4X/f/5/0/f504b371937000704e8e0ed536fc12ca05ebc79c.jpeg)

> **图片在文本左侧对齐**
>
> ![IMG_3711](https://global.discourse-cdn.com/meta/original/4X/b/b/7/bb7e0f0a9040e23e7f02d1a42b1af6425c4aa4fd.jpeg)

> **编辑器工具栏居中对齐按钮（附带居中对齐代码示例）**
>
> ![IMG_3720](https://global.discourse-cdn.com/meta/original/4X/a/8/c/a8c780a10ed588d391bd7098876d2855e2a61165.jpeg)

> **图片居中对齐（带文本）**
>
> ![IMG_3714](https://global.discourse-cdn.com/meta/original/4X/4/8/7/4876d5852350dc0207e68e27bbf23c218f783e13.jpeg)

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年七月13日 17:32 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/2 "2023-07-13T17:32:35Z")

</div>

已添加用于居中图像的按钮。

---

<div class="post-metadata">

### Author: ![piffy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/piffy/32/254198_2.png) [@piffy](https://meta.discourse.org/u/piffy)
#### Post date: [2023年七月13日 20:20 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/3 "2023-07-13T20:20:49Z")

</div>

很棒的工具！您能展示一下它在编辑器中的样子吗？

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年七月13日 20:51 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/4 "2023-07-13T20:51:09Z")

</div>

谢谢你 @piffy！ 🙂

> [@piffy](#):
>
> Can you show what it looks like in the editor?

你的意思是带工具栏的编辑器吗？那就是带有网格示例的第一个截图。我将添加一些关于图片周围其他对齐代码的编辑器截图。

---

<div class="post-metadata">

### Author: ![thoka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thoka/32/115652_2.png) [@thoka](https://meta.discourse.org/u/thoka)
#### Post date: [2023年七月14日 05:43 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/5 "2023-07-14T05:43:38Z")

</div>

感谢您为 [Image Alignment](https://meta.discourse.org/t/image-alignment/15408) 提供的此解决方案

---

<div class="post-metadata">

### Author: ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)
#### Post date: [2023年九月13日 19:47 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/6 "2023-09-13T19:47:46Z")

</div>

> [@Lilly](#):
>
> 右对齐按钮（附带右对齐代码示例）
> 
> ![IMG_3717](https://global.discourse-cdn.com/meta/original/4X/c/2/b/c2b227f8c0780cd1bbec77017fae9558fa896a06.jpeg)

很棒的组件！我正想问类似的问题！

你真是个摇滚明星，制作了有价值的附加组件！

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年九月13日 19:58 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/7 "2023-09-13T19:58:59Z")

</div>

> [@Heliosurge](#):
>
> 你真是个摇滚明星，做出了有价值的附加组件！

谢谢你，Dan，我很感激你的赞美，很高兴你觉得它们有用 🙂

---

<div class="post-metadata">

### Author: ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)
#### Post date: [2023年九月14日 01:12 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/8 "2023-09-14T01:12:30Z")

</div>

不客气。请问您是如何让“Align left”等内容显示为粗体的？我尝试使用 Markdown，但它显示的是 Markdown 代码。

我看到使用 `###` 可以实现，但标准的粗体使用

```plaintext
**Text**

```

似乎不起作用。

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年九月14日 01:42 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/9 "2023-09-14T01:42:19Z")

</div>

我不确定我是否明白你的意思 @Heliosurge？

 ![image_align_left](https://global.discourse-cdn.com/meta/original/4X/b/b/b/bbbe5f4446455cc6d7ef9845c977aa36d398f865.png)

 ![image_align_right](https://global.discourse-cdn.com/meta/original/4X/a/9/4/a94ef0130395213090b8cfbcb0a00fef0df8f812.png)

 ![image_align](https://global.discourse-cdn.com/meta/original/4X/8/5/f/85f126aaff8e1a150b3f317e99576df030058f53.png)

---

<div class="post-metadata">

### Author: ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)
#### Post date: [2023年九月14日 03:18 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/10 "2023-09-14T03:18:52Z")

</div>

抱歉，我之前尝试时它不起作用。不确定有什么变化。这真的很棒，谢谢。

---

<div class="post-metadata">

### Author: ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)
#### Post date: [2023年九月14日 04:34 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/11 "2023-09-14T04:34:14Z")

</div>

如果您使用多张图片，我会遇到此问题。

 ![Screenshots_2023-09-14-01-31-01](https://global.discourse-cdn.com/meta/original/4X/a/1/d/a1d52af4b4adb757779248e84c33f41935ff828d.png)

来自此代码。我该如何让信息固定在每张图片旁边？Shawn 的信息应该在第二张图片旁边。

 ![Screenshots_2023-09-14-01-30-43](https://global.discourse-cdn.com/meta/original/4X/b/3/0/b30ff3c3a6dbd1b18e77942dc2c2c93fdf02dd93.png)

我该如何修复？

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年九月14日 04:49 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/12 "2023-09-14T04:49:59Z")

</div>

只需在第一个文本块和第二个 `<div-data-theme...>` 标签之间插入一些 `<br><br>` HTML 标签，直到它正确地间隔开。我将找时间在其中编程一个硬回车或类似的东西。

---

<div class="post-metadata">

### Author: ![Heliosurge](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/heliosurge/32/571810_2.png) [@Heliosurge](https://meta.discourse.org/u/Heliosurge)
#### Post date: [2023年九月14日 05:22 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/13 "2023-09-14T05:22:48Z")

</div>

太棒了，那就添加 `<br>` 的数量，直到文本与相应的图片对齐。

再次感谢您的帮助，以及如此出色的 #Customization > Theme component！

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2023年九月14日 07:04 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/14 "2023-09-14T07:04:08Z")

</div>

我认为这不是一种可靠的方法，因为内容的高度会因窗口大小的不同而异。

清除浮动元素，当旁边有任意内容时，会很棘手。我不确定这里有什么明显的解决方案……\[1\]

这在 WordPress 编辑器中也是一个长期存在的问题。

人们倾向于依赖一些不自然的解决方法，使用那些唯一目的是重置先前浮动元素的 `clear: both;` 属性的元素。

* * *

1. 直到 don 出现 🙃

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年九月14日 07:07 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/15 "2023-09-14T07:07:10Z")

</div>

> [@Canapin](#):
>
> 我认为这不是一个可靠的方法，因为内容的高度会因窗口大小的不同而异。
> 
> 清除浮动，当有任意内容在其旁边时，这是棘手的。我不确定这里有什么明显的解决方案……\[1\]

是的，我也不喜欢那种方法。我曾考虑过尝试使用空白属性解决方案。🤔 最终，如果对齐的图片是单独的帖子，效果最好。不幸的是，表格中的图片效果也不太好。

* * *

1. 直到 don 出现 🙃

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2023年九月14日 07:15 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/16 "2023-09-14T07:15:25Z")

</div>

也许你可以探索除浮动（float）之外的其他解决方案。

我尝试用 `flex` 替换 `float`…… 🤔 还没深入研究。

```scss
[data-theme-image=left] {
    display: flex;
    float: none;
}

[data-theme-image=right] {
    display: flex;
    float: none;
    justify-content: flex-end;
}

```

 ![image](https://global.discourse-cdn.com/meta/original/4X/b/c/b/bcbac69cccca3605bbec92ab9c048a61d3d5d012.jpeg)

```scss
[data-theme-image=left] {
    display: flex;
    float: none;
}

[data-theme-image=right] {
    display: flex;
    float: none;
    flex-direction: row-reverse;
}

```

 ![image](https://global.discourse-cdn.com/meta/original/4X/d/c/0/dc0a1a1ba4d287d4ec570dcef722a1f7fc119f97.jpeg)

或者可以看看网格布局（grid layout）？不过我对它一无所知。我的 CSS 知识已经过时好几年了。 🧓

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年九月14日 07:17 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/17 "2023-09-14T07:17:36Z")

</div>

> [@Canapin](#):
>
> 或者看看网格布局？我对它一无所知。我的 CSS 知识已经是很久以前的了。 🧓

哈哈，网格选项是这个主题组件的一部分 😁

我也绝对认为我的更老 🤯。

那是什么？饼干？我分不清它们是土豆还是饺子还是某种饼干。 😅

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2023年九月14日 07:24 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/18 "2023-09-14T07:24:45Z")

</div>

我昨天做了[大福麻糬](https://en.wikipedia.org/wiki/Daifuku)，里面填了自制的[红豆沙](https://en.wikipedia.org/wiki/Red_bean_paste)。

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年九月14日 15:23 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/19 "2023-09-14T15:23:10Z")

</div>

尝试了那些 flex 对齐属性的解决方案，但都无法找到一个始终可行的办法。不过，我确实找到了一个可行的变通方法。如果你将每个图片-文本部分都设置为块引用，它就会将它们分开（但会包含文本的引用格式）：

编辑器/预览器：

 ![image_alignment](https://global.discourse-cdn.com/meta/original/4X/d/2/a/d2a7b2f8e6339eac5b8e31d82f057a431ed0d121.jpeg)

结果：

 ![dogs](https://global.discourse-cdn.com/meta/original/4X/8/6/2/8624cde90642da87a425afaf0f5d3fc718b0eb79.jpeg)

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2023年九月15日 14:46 UTC](https://meta.discourse.org/t/image-alignment-and-grid/271560/20 "2023-09-15T14:46:56Z")

</div>

这是因为`<blockquote>`在Discourse中包含`clear: both;`。

[下一頁](https://meta.discourse.org/t/image-alignment-and-grid/271560.md?page=2)
