114

I have below ipython notebook code (markdown):

#### example table |Name|Description| |--|-------------------------------| |Mary |She is a nice girl.| |Jackie |He is a very naughty boy.| 

The output looks like below: enter image description here

How can I:

  1. Left align the table of the cell, it's center by default now.
  2. Right align the second col text.
3
  • 22
    3 years later and this question is still helping people. :-) Commented Mar 18, 2017 at 15:56
  • 2
    @TMWP almost 5 ;) Commented Jan 9, 2019 at 14:00
  • 3
    @Yatrix almost 8 :) Commented Nov 29, 2021 at 7:42

10 Answers 10

75

Well, yes !

| Name | Description | age | :- |-------------: | :-: |Mary| She is a nice girl. | 20 | Jackie Junior | He is a very naughty boy. | 5 
  • :--- or --- = left align
  • ---: = right align
  • :---: = centered

table

Sign up to request clarification or add additional context in comments.

5 Comments

Cool, you have answered my second question. how about the first one? BTW, the first question means the whole table is in the center of the page, I wish to align it to the left of page border.
Ah, I didn't understood you wanted the table to be left align. For that I don't know, I'm not sure it's possible in Markdown. But it might be possible with nbconvert to html by using a template which left align table by modifying CSS properties of tables
Doesn't work in a fresh markdown cell in jupyter notebook (they changed the name from ipython notebook a few years ago): All cell content is right-justified. Any ideas?
As @JoachimWagner notes, this seems to not be working, and to be related to this bug: github.com/jupyter/notebook/issues/3919
@nealmcb, you're right, but it works in jupyter lab. github.com/jupyterlab/jupyterlab/pull/5301
65

Answer to the 1st question - left-align the table - create and run a code cell above the table markdown cell, with the following content:

%%html <style> table {float:left} </style> 

6 Comments

This forces all subsequent lines to be to the right of the table, and Roberto's solution below doesn't work. So how do you put it on the left without messing up the surrounding format?
@orodbhen I have edited my solution to work with the new styles of the jupyter-notebook, does it solve your problem?
it does not work for me, use table { text-align: left } instead works well.
table {align:left;display:block} Try this to above the subsequent lines float to the right of the table
Just a tip on best practices, float should generally be avoided in HTML/CSS except in certain specific situations, because of how it has unintended effects on the flow of the surrounding document.
|
42

I would suggest to use this variant of the knuth's answer that doesn't affect the flow of the remaining elements around the table. Put this code inside a cell above the one containing the table:

%%html <style> table {margin-left: 0 !important;} </style> 

7 Comments

I just tested both solutions on Python 2.7 in Jupyter / Anaconda 4.2. The "float" solution works. The margin-left solution put forward in your post did not change the alignment of the table in my notebooks. Out of curiosity,did you test on 3.x or 2.7?
@TMWP you are right, something has changed in the default css styles. To fix this add !important at the end of the style (as in the edited solution) to overcome the problem. Thank you for the report.
That fixes it for 2.7. Output is also slightly cleaner than the original posted output which seems to add a little verticle space.
Works for me in 3.5 as well
Works for me - Dec 2020
|
12

Yeah, I don't like that centered table either. Insert this at the top of your notebook after the imports section:

from IPython.core.display import HTML table_css = 'table {align:left;display:block} ' HTML('<style>{}</style>'.format(table_css)) 

The IPython.core.display namespace allows you to imbed audio, local filelinks among others - as well as HTML within your notebook.

2 Comments

I use Jupyter-lab, all the other solutions don't work, except for this one. Thanks!
How to adapt this for plotting figures left aligned instead of tables?
10

I know many have already answered this, but personally, I found their answer lacks a little more description, and because of this many are not able to implement this.

Clarification


Also, I want to clear one thing that I am not giving the solution for aligning the values inside the table but for aligning the whole table rendering itself. And if you are looking solution for the table's cell alignment then you can consider the first answer itself


Solution

Here are the steps:

  • First create a Code cell (not markdown) just above your markdown cell where you want to show your table.
  • Then write the following in your Code cell inside it.
%%html <style> table {float:left} </style> Run your Code cell. 
  • Now just create your table as normally you do, no need to add anything extra. And Voila! Your table should now render to the left.

For every notebook, you have to do this step for changing the table alignment. But if you don't want to do this, you can follow @Anderson answer. For ease, I am copying his answer here.

  • First you need to create a file named custom.css where you will put the following code
table {float: left}; 
  • Then you have to move this file to your ipython directories, it will be something like this
~/.ipython/profile_default/static/custom/custom.css 

Hope it helped 😊

1 Comment

Use <style>table {align:left; display:block}</style> otherwise tables may be overlapping with other texts.
7

You can make a custom preference in Ipython.

Just make the following file

~/.ipython/profile_default/static/custom/custom.css 

and add the following code.

table {float: left}; 

and You don't have to put the custom css in all ipython files.

3 Comments

How could I set cells width to be wider?
There's a fairly exhaustive discussion on setting cell widths here: stackoverflow.com/questions/21971449/…. Best of luck with it.
For a Jupyter notebook, use ~/.jupyter//custom/custom.css.
7

!important overrides the css of rendered _html

Use your styles with !important

<style> table td, table th, table tr {text-align:left !important;} </style>

Comments

4

Since none of the answers really did the job for me concerning the first question (e.g. how to align the whole table to the left), here's mine:

It seems like wrapping the table in an inline-block div does the job nicely!
(and this also works with the accepted answer concerning the second question...)

<div style="display: inline-block"> | left | aligned | table | | --- | ------- | ----- | | 1 | 2 | 3 | </div> 

2 Comments

This deserves more upvotes. Just remember to add a space after the opening <div> tag to make it work. This comes in handy when you don't want an additional code cell just to format tables.
I found out that you also need to add a space before the opening <div> tag (so total of 2 spaces: 1 before + 1 after the opening div) to make the table left-aligned
3

Answering the fist question: When creating a table and assigning it the float: left attribute you end up having to add more CSS to resolve the text that surrounds the table. Place this in a code cell before your markdown cell

%%html <style> table { display: inline-block } </style> 

However, if you have a lot of CSS, might be best to put it in another file for the overall beauty of the document.

@jrjc answers the 2nd question perfectly ;)

Comments

2

If you look at the table in the inspector, you'll see that the cause of the issue is the fact that the margin-left and margin-right CSS properties on the table are set to auto, making it centered. You can make it left-aligned by doing something like this in your custom.css:

.rendered_html table { margin-left: 0; } 

That should only left-align those specific tables and not affect anything else.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.