Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'align' option to figure shortcode #256

Merged

Conversation

kdkasad
Copy link
Contributor

@kdkasad kdkasad commented Feb 20, 2021

Allows centering figures (images) by adding an align parameter to the figure shortcode.

Adding align=center to a figure causes the resulting figure to be centered.

Example screenshot:
image

Parameter values:
  - 'center': align image and captions to center of page
  - anything else, including unset: align according to language direction
@kdkasad
Copy link
Contributor Author

kdkasad commented Feb 20, 2021

Actually now that I'm thinking about it, there's no need for an align parameter with a value of center, there could just be a center parameter which takes effect based on whether or not it's there.

E.g.

{{< figure src=image.jpg center >}}

instead of

{{< figure src=image.jpg align=center >}}

Edit: nevermind, it appears it is not possible to do named parameters without values. Hugo thinks that the center parameter without a value is a positional parameter, and it doesn't allow mixing positional and named parameters.

I would like to ignore any value other than center though. Currently, the align parameter is basically the same as class except it prefixes the value with align-.

@adityatelange
Copy link
Owner

adityatelange commented Feb 21, 2021

@kdkasad I guess we can make it simpler with already implemented code

.post-content img[src*='#center'] {
margin: auto
}

we can do this in figure.html

- <img src="{{ .Get "src" }}"
+ <img src="{{ .Get "src" }}{{- if eq (.Get "align") "center" }}#center{{- end -}}"

and remove extra css as show below

 .post-content figure.align-center {
-       display: flex;
-       flex-direction: column;
-       align-items: center;
        text-align: center;
 }

Appends "#center" to the image's URL in order to utilize existing CSS
rules for centering images.
@sonarcloud
Copy link

sonarcloud bot commented Feb 21, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@adityatelange adityatelange linked an issue Feb 22, 2021 that may be closed by this pull request
@adityatelange adityatelange merged commit 14ebcde into adityatelange:master Feb 22, 2021
@kdkasad kdkasad deleted the feature/figure-align branch February 22, 2021 09:12
kylethedeveloper pushed a commit to kylethedeveloper/hugo-PaperMod that referenced this pull request Feb 21, 2023
Usage: 

{{< figure align=center src="image.jpg" >}}

Parameter values:
  - 'center': align image and captions to center of page
  - anything else neglected

* Appends "#center" to the image's URL in order to utilize existing CSS
rules for centering images.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to center the text in a caption?
2 participants