Html table style margin left

CSS Cell Margin

In my HTML document, I have a table with two columns and multiple rows. How can I increase the space in between the first and second column with css? I’ve tried applying «margin-right: 10px;» to each of the cells on the left hand side, but to no effect.

16 Answers 16

A word of warning: though padding-right might solve your particular (visual) problem, it is not the right way to add spacing between table cells. What padding-right does for a cell is similar to what it does for most other elements: it adds space within the cell. If the cells do not have a border or background colour or something else that gives the game away, this can mimic the effect of setting the space between the cells, but not otherwise.

As someone noted, margin specifications are ignored for table cells:

Internal table elements generate rectangular boxes with content and borders. Cells have padding as well. Internal table elements do not have margins.

What’s the «right» way then? If you are looking to replace the cellspacing attribute of the table, then border-spacing (with border-collapse disabled) is a replacement. However, if per-cell «margins» are required, I am not sure how that can be correctly achieved using CSS. The only hack I can think of is to use padding as above, avoid any styling of the cells (background colours, borders, etc.) and instead use container DIVs inside the cells to implement such styling.

I am not a CSS expert, so I could well be wrong in the above (which would be great to know! I too would like a table cell margin CSS solution).

Источник

Left and right margin on a table row

http://i54.tinypic.com/eqce83.jpg

How can I set the left and right margin of a table row (or something similar to achieve the same result)? I tried border-collapse:separate; border-spacing:20px; but that removed the rows border bottom. This is what I am trying to create:

2 Answers 2

add a class to the table, then add padding to the left and right.

If it doesnt, then add a class to the left and attach the padding to the left and right col instead.

If I have a padding on the table the header wouldn’t be 100% wide (have a look at the image). If I set the padding on the left and right td the border-bottom would still go all the way from left to right.

just for reference, here is the table with the spacing on the bottom border. junk.hostsaba.com/table2.html

I do not think this is possible with tables.

Here it is with divs and spans. I hope you like it:

   div#outer < border: 10px solid black; width: 30em; >div#header < background-color: red; color: white; text-align: center; >div.row < border-bottom: 1px dashed red; margin: 10px 20px; clear: both; >span.left < float: left; >span.right  
Some table head text
Col Col
Col Col
Col Col

@AwdEr — Wow this was really hard with tables, perhaps impossible because of the way trs expand to fill their containing tables. However I put together a self-contained page that uses divs and spans. Hope it works for you.

Читайте также:  Dex file to java

Yes that is true, but this practice is frowned upon because it uses tables for formatting. Having the first and last columns blank and border-bottom just for the middle td’s gives the right look, but horribly violates the separation of content (HTML) and presentation (CSS). I could not do this in good conscience! 😉 Or perhaps you were thinking of something else?

Источник

Styling HTML Tables: How to Apply Margin, Border and z-index on Table Elements

The screenshot above illustrates the final result we want to achieve: a table with the first row being the main header and multiple sections, which all have their subheaders.

table class="table" > thead > tr > th >MO/th> th >TU/th> th >WE/th> th >TH/th> th >FR/th> th >SA/th> th >SU/th> /tr> /thead> tbody class="section section-step" > tr class="sub-header" > th colspan="7" > Working hours /th> /tr> tr > td >4/td> td >5/td> td >5/td> td >5/td> td >5/td> td >0/td> td >0/td> /tr> /tbody> tbody class="section section-step" > tr class="sub-header" > th colspan="7" > Workout /th> /tr> tr > td >0.5/td> td >0.5/td> td >0.5/td> td >1/td> td >0.5/td> td >2/td> td >0/td> /tr> /tbody> tbody class="section" > tr class="section-header" > th colspan="7" > Total /th> /tr> tr > td >8.5/td> td >8.5/td> td >9.5/td> td >10/td> td >5.5/td> td >2/td> td >0/td> /tr> /tbody> /table>

Above you see the HTML structure of the table. Inside the element we have our main header and beneath it several elements that represent separate sections of our table, each of which has its own sub header.

Using margin on table elements

As you can see in the screenshot at the beginning of this article, there is some space between the main header and the first section and also between the individual sections. Naive as I am, I first tried to apply margin-top to the elements.

Using border

The simplest solution to achieve a similar result as using margin is to add border-top: 1em onto the elements.

// 1. Needed for making border-top spacing work. .table  border-collapse: collapse; // 1 border-spacing: 0; > .section  border-top: 1em solid transparent; >

All our elements, which need some space around them, have a class .section . For the border-top to work, we have to put border-collapse: collapse on our table.

Using ::before and ::after pseudo elements

Another way of applying some margin on a element is to use a ::before or ::after pseudo element.

.section::before  height: 1em; display: table-row; content: ''; >

This way we basically add a new (empty) row which we can use to add some space at the beginning of our elements.

Depending on the circumstances you might want to reach for either the border method or the pseudo element trick.

Using border-radius on table elements

Next, we want to give our elements a border and apply a border radius. Again we’re out of luck if we try to apply border and border-radius onto the element itself.

// 1. Using box-shadow because otherwise // border-radius doesn't work on . .section-step  border-radius: 0.25em; // 1 box-shadow: 0 0 0 1px #ccc; // 1 >

Above you can see how we can use box-shadow instead of border in order to achieve (almost) the same result.

Styling table cells instead

As you may have noticed, our current implementation doesn’t look exactly like the screenshot you saw at the beginning of this article.

The spacing hack works like padding instead of margin

Now that we’ve added the borders, we can see that our spacing hacks do not work like margin but rather like padding . Unfortunately, under these circumstances, if you have a border around a element that must have some space to the previous element, there is no easy solution to achieve this. The only way to solve this is to apply our border styles to the table cells and use some :first-child / :last-child selector magic to achieve the desired layout.

.section-step th, .section-step td  border: 0 solid #ccc; > .section-step th:first-child, .section-step td:first-child  border-left-width: 1px; > .section-step th:last-child, .section-step td:last-child  border-right-width: 1px; > .section-step tr:first-child th, .section-step tr:first-child td  border-top-width: 1px; > .section-step tr:first-child th:first-child, .section-step tr:first-child td:first-child  border-top-left-radius: 0.25em; > .section-step tr:first-child th:last-child, .section-step tr:first-child td:last-child  border-top-right-radius: 0.25em; > .section-step tr:last-child th, .section-step tr:last-child td  border-bottom-width: 1px; > .section-step tr:last-child th:first-child, .section-step tr:last-child td:first-child  border-bottom-left-radius: 0.25em; > .section-step tr:last-child th:last-child, .section-step tr:last-child td:last-child  border-bottom-right-radius: 0.25em; >

In the code snippet above we apply the necessary border styles to the relevant th and td table cell elements. The elements at the corners must have a border radius all element on the edges must have a border. By using :first-child and :last-child selectors we can apply the styles to the correct cells.

Using z-index on table elements

As you can see in the initial screenshot of the final result, a box-shadow has been applied to the sub header, overlaying the following row. If we try to simply apply a box-shadow to the element, we will see that the shadow of the sub header disappears behind the following row.

The box shadow disappears behind the following row

Normally, we would use z-index to raise the sub header above the following row. But as you may have guessed, using relative positioning and z-index on a element doesn’t work either. But we can use our knowledge about the CSS stacking context to solve this problem. Applying position: relative and a z-index to an element creates a new stacking context. But this is not the only way we can achieve this: for example, we can also use transform: translate(0, 0) .

Like What You Read?

Follow me to get my latest articles.

Wrapping it up

We have to dig deep into the CSS bag of tricks to make some more complicated table layouts work. But the beauty of CSS is that there is always a way to achieve certain things.

We could have made our lives a little easier by overriding the display property of our table elements. But that means you have to explicitly specify the width of each cell to make the columns equally wide. This may be okay in certain cases, but it’s often more convenient to rely on the browser to automatically determine the width of each cell.

References

Do you want to learn how to build advanced Vue.js applications?

Register for the Newsletter of my upcoming book: Advanced Vue.js Application Architecture.

  • 24 Dec 2022 How To Fix Spacing Between Text Blocks When Using Tailwind CSS
  • 21 Jun 2021 CSS: The Spacing Between Elements Should Be Determined by the Parent Element
  • 26 Apr 2020 Super Simple Progressively Enhanced Carousel with CSS Scroll Snap
  • 19 Apr 2020 Simple Solution for Anchor Links Behind Sticky Headers
  • 14 Jul 2019 Building Vue.js UI Components With HTML Semantics in Mind

Do you enjoy reading my blog?

You can buy me a ☕️ on Ko-fi!

Источник

margin-left style not working on second element

The margin-left:100px does nothing to separate the second cell from the first cell. How do I create space to the left of the second cell?

Padding Left

enter image description here

padding-left:100px Produces this result: The content of the second cell now has 100px of space added to the left. The position of the cell background however remains unchanged.

Border-spacing

enter image description here

Three Cells

If I add a third table cell, I get this result: I don’t want spacing between every cell, just the second one.

Cellspacing

cellspacing in the table element creates spacing around every cell. I don’t want that either, and one source states that cellspacing is not supported in HTML5.

3 Answers 3

Margin wont work with table cells. Try padding or cellspacing=»» to table.

Or add a div inside the table cell and apply margin to the div.

According to CSS specifications, margin properties do not apply to table cells (elements with display: table-cell ). It is valid to set them, but the setting has no effect.

The way to separate the cells of a table is to set border-spacing on the table element, e.g. with . However, this also sets the spacing between the cells and the edges of the table. This effect can be cleared using negative margins:

 
Something Here Second Cell

Caveat: IE 7 (and older) does not support border-spacing , but it supports margin properties, so on it the result would be all wrong. If this is relevant, put the CSS code in a style element and wrap it inside a “pseudocomment” that makes IE 7 and older ignore it.

Depending on the context, the simpler method of just setting left margin on the second cell may work well. However, it creates spacing inside the second cell, not between the cells. The difference between this and cell spacing matters e.g. if cells have backgrounds or borders.

Источник

Оцените статью