Markdown and image alignment

前端 未结 14 2367
予麋鹿
予麋鹿 2020-12-02 04:06

I am making a site that publishes articles in issues each month. It is straightforward, and I think using a Markdown editor (like the WMD one here in Stack Overflow) wo

14条回答
  •  粉色の甜心
    2020-12-02 04:52

    I have an alternative to the methods above that used the ALT tag and a CSS selector on the alt tag... Instead, add a URL hash like this:

    First your Markdown image code:

    ![my image](/img/myImage.jpg#left)
    ![my image](/img/myImage.jpg#right)
    ![my image](/img/myImage.jpg#center)
    

    Note the added URL hash #center.

    Now add this rule in CSS using CSS 3 attribute selectors to select images with a certain path.

    img[src*='#left'] {
        float: left;
    }
    img[src*='#right'] {
        float: right;
    }
    img[src*='#center'] {
        display: block;
        margin: auto;
    }
    

    You should be able to use a URL hash like this almost like defining a class name and it isn't a misuse of the ALT tag like some people had commented about for that solution. It also won't require any additional extensions. Do one for float right and left as well or any other styles you might want.

提交回复
热议问题