If I do this in HTML (which works perfectly inside GitHub markdown readmes by the way):
30% width each:
<p align="center" width="100%">
<img width="30%" src="https://i.stack.imgur.com/RJj4x.png">
<img width="30%" src="https://i.stack.imgur.com/RJj4x.png">
<img width="30%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
I get this: a nicely-centered row of images. See my demo here.
This is exactly what I want. The problem is, the align
HTML attribute is deprecated entirely in HTML5, and therefore shouldn't be used, and the width
attribute is deprecated in HTML5 for most tags, including the p
tag, and so it also shouldn't be used. So, in my GitHub Pages Jekyll site I'm trying to use CSS instead, so I don't rely on deprecated HTML attributes.
I can't figure out how to do this in CSS, however. How can I do this in CSS? Note that I'd like to be able to align this row of images, left, right, or center, just as can be done by setting align="left"
, align="right"
, or align="center"
in the deprecated HTML element above.
Here's what I've tried so far, but it creates a row of images which remains aligned left:
<style>
/* align center withOUT word wrap--the element goes on its own line */
.alignCenterNoWrap
{
display: block;
float: none;
/* Set both the left and right margins to `auto` to cause the image to be centered. */
margin-left: auto;
margin-right: auto;
/*width: 60%;*/
}
</style>
<div class="alignCenterNoWrap" style="width:100%;">
<img style="width:30%;" src="https://i.stack.imgur.com/RJj4x.png">
<img style="width:30%;" src="https://i.stack.imgur.com/RJj4x.png">
<img style="width:30%;" src="https://i.stack.imgur.com/RJj4x.png">
</div>
Other CSS classes I've been working on to try to get the alignment I seek:
/* align left WITH word wrap to the right of the element */
.alignLeftWrap
{
display: inline-block;
float: left;
margin-left: 0px;
/* provide a gap between the element and the text to its right */
margin-right: 15px;
/*width: 60%;*/
}
/* align right WITH word wrap to the left of the element */
.alignRightWrap
{
display: inline-block;
float: right;
/* provide a gap between the element and the text to its left */
margin-left: 15px;
margin-right: 0px;
/*width: 60%;*/
}
/* align left withOUT word wrap--the element goes on its own line */
.alignLeftNoWrap
{
display: block;
float: none;
margin-left: 0px;
margin-right: auto;
/*width: 60%;*/
}
/* align center withOUT word wrap--the element goes on its own line */
.alignCenterNoWrap
{
display: block;
float: none;
/* Set both the left and right margins to `auto` to cause the image to be centered. */
margin-left: auto;
margin-right: auto;
/*width: 60%;*/
}
/* align right withOUT word wrap--the element goes on its own line */
.alignRightNoWrap
{
display: block;
float: none;
margin-left: auto;
margin-right: 0px;
/*width: 60%;*/
}
/* Force this element to NOT wrap around any floating elements above it which have word wrap.
Therefore, this element will begin on a new line, even if it would otherwise try to wrap
around the above element instead. */
.noWrapAroundAbove
{
clear: both;
}