Where do you put your CSS Margins?
I tend to use a bottom margin on elements when I want them to have space before the next element, and then to use a ".last" class in the css to remove the margin from the last element.
<body>
<h1>This is the heading</h1>
<p>This is a paragraph</p>
<h1>Here's another heading</h1>
<div class="last">This is a footer</div>
</body>
div { margin-bottom: 1em; }
p { margin-bottom: 1em; }
h1 { margin-bottom: 1em; }
.last {margin-bottom: 0; }
In your example though, this probably isn't that applicable, as a footer div would most likely have it's own class and specific styling.
Still the ".last" approach I used works for me when I have several identical elements one after the other (paragraphs and what-not).
Of course, I cherry-picked the technique from the "Elements" CSS framework.
Order of CSS margin
margin: top right bottom left //4 parameters 10px 12px 13px 15px
margin: top horizontal bottom //3 parameters 10px 12px 13px, gives left & right same margin of 12px
margin: vertical horizontal //2 parameters 10px 20px, gives 10px for top/bottom and 20px for left/right
What is the difference between `margin` and `padding` in CSS?
TL;DR: By default I use margin everywhere, except when I have a border or background and want to increase the space inside that visible box.
To me, the biggest difference between padding and margin is that vertical margins auto-collapse, and padding doesn't.
Consider two elements one above the other each with padding of 1em
. This padding is considered to be part of the element and is always preserved.
So you will end up with the content of the first element, followed by the padding of the first element, followed by the padding of the second, followed by the content of the second element.
Thus the content of the two elements will end up being 2em
apart.
Now replace that padding with 1em margin. Margins are considered to be outside of the element, and margins of adjacent items will overlap.
So in this example, you will end up with the content of the first element followed by 1em
of combined margin followed by the content of the second element. So the content of the two elements is only 1em
apart.
This can be really useful when you know that you want to say 1em
of spacing around an element, regardless of what element it is next to.
The other two big differences are that padding is included in the click region and background color/image, but not the margin.
div.box > div { height: 50px; width: 50px; border: 1px solid black; text-align: center; }div.padding > div { padding-top: 20px; }div.margin > div { margin-top: 20px; }
<h3>Default</h3><div class="box"> <div>A</div> <div>B</div> <div>C</div></div>
<h3>padding-top: 20px</h3><div class="box padding"> <div>A</div> <div>B</div> <div>C</div></div>
<h3>margin-top: 20px; </h3><div class="box margin"> <div>A</div> <div>B</div> <div>C</div></div>
CSS: How to add a margin to each middle element in a list?
Use gap
, e.g.:
ul {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
gap
basically puts a gap BETWEEN elements. Hence, no padding/spacing will be added above/below or to the left/right unless there is a neighbor item.
If you only need a gap between columns use column-gap
or row-gap
if only between rows.
In css when to use padding and when to use margin
This image can help you understand better.
How to margin the body of the page (html)?
For start you can use:
<body style="margin:0;padding:0">
Once you study a bit about css, you can change it to:
body {margin:0;padding:0}
in your stylesheet.
What is difference between CSS margin and padding?
In short padding make "space" inside your box, and margin do this outside of your box.
This picture explain it pretty good :)
Is there any specific way to set padding and margins?
Here are a few ideas that might be helpful.
margin: auto
-> this can help you quickly center an element without too much hassle- flex and grids -> this answer might provide some useful links
- check out different CSS units -> this answer gives a nice, brief overview
- this might be beyond what you need, but if you need to do a calculation in CSS, there's a function for that ->
calc()
Edit: margin-left: auto;
and margin-right: auto;
may be useful on their own too - you don't have to automatically set all margins.
Set a page margins
Okay, well number one your
div.document {
width: {{ page_width }}px;
margin: 0 auto;
}
is set to margin: 0 auto;
which makes it centered so your page will be 940px
and will be centered (Resizing your page will confirm this). You probably have to change your content width along with the wrapper width, but keep in mind that the amount of padding depends on how large your viewport is.
Alternate
If you just want to make it strictly 3cm on the left and right sides and make the content in the middle have a fluid layout you can do this:
margin: 0 3cm;
position: absolute;
width: auto;
Related Topics
Javafx: Styling Application with CSS Selectors
Overriding CSS on Github Pages Using Slate Theme
How to I Solve This SASS Interpolation Issue
CSS: Nth-Of-Type() and: Not() Selector
CSS Form Checkbox Styling with Checked and After Tags
CSS3 Transform Property Working Differently in Internet Explorer
Definition List with Inline Pairs
How to Make The New Facebook Post Embed Feature Responsive
CSS Selector Wildcard Inside Class Name
Right Aligning Cell Content in a Datatable Column
Disable HTML5 Video Download at CSS Breakpoint
How to Apply Linear Gradient for Ie8
Is It Wise to Use Fractional/Decimal Pixels for Borders in CSS
CSS3 Element with Opacity:0 (Invisible) Responds to Mouse Events
Fixed Width Variable Height Grid CSS
Using an Image File Vs Data Uri in The CSS
How to "Hack" The Thunderbird Lightning Extension to Fully Color Categories