How to include right-aligned images in your content to go with your text without modifying the default content types or using features or custom modules

The Social Base and Social Blue themes lack some classes used by CK Editor to align inline content...
Just sharing how I solved this need, for anyone with a similar need or interest.

Before explaining, here's the result, just in case it's not clear:
A medium picture is embedded inline, aligned to the right of a paragraph, in this case the bio of a person described in the text.
See attached file
Desired result
Or see it live at http://www.civ.do/team

1. First you need to add your own custom CSS file to the theme.
Under your Open Social root file go to the Social Blue Theme directory.

cd html/profiles/contrib/social/themes/socialblue

Edit socialblue.libraries.yml to have the team look for custom.css.
sudo nano socialblue.libraries.yml

And add assets/css/custom.css: {} under base assets, so that it looks like this:

# The SMACSS category "base" is loaded first.
base:
  css:
    base:
      assets/css/base.css: {}
      assets/css/custom.css: {}

2. Then create custom.css and add the required classes for achieving the desired look:
sudo nano assets/css/custom.css

Inside, paste:

/* ----------------------------------------------
   CSS Alignment Classes used by CKEditor plugins 
   missing from OpenSocial Base and Blue Themes 
   ---------------------------------------------- */

.align-right {
/* copied from bootstrap .pull-right 
   http://getbootstrap.com/css/#helper-classes-floats */
        float: right;
}

.align-left {
/* copied from bootstrap .pull-left 
   http://getbootstrap.com/css/#helper-classes-floats */
        float: left;
}

.align-center {
/* copied from bootstrap .center-block
http://getbootstrap.com/css/#helper-classes-center
*/
        display: block;
        margin-left: auto;
        margin-right: auto;
}

/* ----------------------------------------------
   Alignment of Inline Images and limiting caption
   captions to the width of their images 
   ---------------------------------------------- */

figure.align-right,
figure.align-left { 
        padding: 10px;
        margin: 0px;
        display: table;
}

figure.align-center { 
        padding: 10px;
        display: table;
}

figure.align-right figcaption,
figure.align-left figcaption,
figure.align-center figcaption { 
        font-size: small;
        text-align: center;
        display: table-caption;
        caption-side: bottom ;
        margin-bottom: 5px;
}

Now you can simply embed images, align them and add captions to them using the regular WYSIWYG editor and without having to install or do anything else.

Do note that composer update will override these changes, so you will have to apply them again (changing socialblue.libraries.yml and creating assets/css/custom.css: {}) after an upgrade.

Hope these helps anyone.

PS: I have a copy of these on my personal sandbox notes sites, in case code looks broken here, at
http://www.codigoslibres.com/drupal/opensocial

PS: Maintainers are free to add this to any knowledge base and modify it if desired. I'd also be happy to provide a more detailed description if required to post it where it can help others.

Comments

Carlos Miranda Levy created an issue. See original summary.

peter.polman’s picture

StatusFileSize
new101.91 KB

Hey man! Thanks for contributing in our good idea's space!;)

I'd like to add this feature to socialbase's ckeditor library (socialbase/components/wysiwyg/ckeditor.scss), but I'm missing some responsive behaviour (see attachment)..

Would you be able to do the following:
- Add these styles to the ckeditor.scss file in the socialbase theme
- Add missing responsive behaviour (e.g 100% width om small screens or a smaller width of the image)
- Compile CSS asset with Gulp
- Create a patch of the diff.

We can then review the patch, merge it in the distro and credit your work:) Like to hear from you!

Carlos Miranda Levy’s picture

I'll work on it by the end of the week and report back :-)

peter.polman’s picture

Cool! I see you've found our Slack channel already:) Feel free to send me a DM there if you need input.

Carlos Miranda Levy’s picture

I'm busy at Startup Weekend this weekend, but I promised something by the end of the week, so here it is.

Do keep in mind that my knowledge of responsive CSS is mostly from stackoverflow, inspect element and view source of other modules and themes, so there might be a more standard or proper way to achieve this.

I did test the outcome on Safari, Chrome, Firefox on a Mac and on Galaxy S7, iPhone 5 and 7" Kindle Fire and it looks fine.

The following css makes it responsive, so that when viewing the page on a small screen, the images are resized to the screen width and displayed centered with text flowing above and below them, and we also make the caption text smaller:

@media (max-width: 480px) {
	figure.align-right,
	figure.align-left,  
	figure.align-center {
		width: 100%;
	}

        figure.align-right img,
        figure.align-left img,  
        figure.align-center img {
		width: 100%;
	}

	figcaption {
		padding: 0 10px 0 10px;
		font-size: x-small;
	}
}
 

So, the complete css that needs to be added (in my case to custom.css), to support left-aligned, centered and right-aligned images inserted via CKEditor within the text is:

/* ----------------------------------------------
   CSS Alignment Classes used by CKEditor plugins 
   missing from OpenSocial Base and Blue Themes 
   ---------------------------------------------- */

.align-right {
/* copied from bootstrap .pull-right 
   http://getbootstrap.com/css/#helper-classes-floats */
        float: right;
}

.align-left {
/* copied from bootstrap .pull-left 
   http://getbootstrap.com/css/#helper-classes-floats */
        float: left;
}

.align-center {
/* copied from bootstrap .center-block
http://getbootstrap.com/css/#helper-classes-center
*/
        display: block;
        margin-left: auto;
        margin-right: auto;
}

/* ----------------------------------------------
   Alignment of Inline Images and limiting caption
   captions to the width of their images 
   ---------------------------------------------- */

figure.align-right,
figure.align-left { 
        padding: 10px;
        margin: 0px;
        display: table;
}

figure.align-center { 
        padding: 10px;
        display: table;
}

figure.align-right figcaption,
figure.align-left figcaption,
figure.align-center figcaption { 
        font-size: small;
        text-align: center;
        display: table-caption;
        caption-side: bottom ;
        margin-bottom: 5px;
}

@media (max-width: 480px) {
	figure.align-right,
	figure.align-left,  
	figure.align-center {
		width: 100%;
	}

        figure.align-right img,
        figure.align-left img,  
        figure.align-center img {
		width: 100%;
	}

	figcaption {
		padding: 0 10px 0 10px;
		font-size: x-small;
	}
}

You can see it working on these 2 urls:

Basic page example: http://www.civ.do/team
User information example: http://www.relief20.com/carlos-miranda-levy/info

(just to make sure, css is being read regardless of content type)

jochemvn’s picture

Component: Other » Front-End
maikelkoopman’s picture

Status: Active » Needs review
StatusFileSize
new11.54 KB
jochemvn’s picture

Status: Needs review » Fixed

Reviewed and merged. Will be part of the 1.6 release

  • maikelkoopman committed 52266f8 on 8.x-1.x
    #2890462 by maikelkoopman, carlos-miranda-levy: Add support for ckeditor...
  • maikelkoopman committed e40500c on 8.x-1.x
    #2890462 by maikelkoopman: support for inline forms (alignment options...
  • maikelkoopman committed 7e4ff8b on 8.x-1.x
    #2890462 by maikelkoopman: update with develop
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.