GitHub use margin:40px and padding:24px. To use only margin or padding is not good? ex. margin:64px or padding:64px. I want to know the reason why GitHub use both of them.
<style text="css">
.mb-6 {
margin-bottom: 40px;
}
.pb-4 {
padding-bottom: 24px;
}
</style>
<p class="alt-lead text-center text-gray mb-6 pb-4 col-md-10 mx-auto">
Open source software is free for you to use and explore. Get involved to perfect your craft and be part of something big.
</p>
<div class="clearfix gut-lg">
<div class="float-left col-md-4">
<div class="clearfix mb-4 text-md-center">
<div class="float-left mr-4 float-md-none mr-md-0 mb-md-3"><img src="https://assets-cdn.github.com/images/modules/site/iconsnsource-ico-future.svg?sn" alt="" aria-hidden></div>
<div class="overflow-hidden">
<h3 class="alt-h4 mb-2">Shape the future of software</h3>
<p class="alt-text-small text-gray">Your contributions help make technology better for everyone, developers and non-develo alike.</p>
</div>
</div>
</div>
<div class="float-left col-md-4">
<div class="clearfix mb-4 text-md-center">
<div class="float-left mr-4 float-md-none mr-md-0 mb-md-3"><img src="https://assets-cdn.github.com/images/modules/site/iconsnsource-ico-best.svg?sn" alt="" aria-hidden></div>
<div class="overflow-hidden">
<h3 class="alt-h4 mb-2">Work with the best in the field</h3>
<p class="alt-text-small text-gray">Amazing developers use GitHub. Contribute code to projects that change how software&nbs built.</p>
</div>
</div>
</div>
<div class="float-left col-md-4">
<div class="clearfix mb-4 text-md-center">
<div class="float-left mr-4 float-md-none mr-md-0 mb-md-3"><img src="https://assets-cdn.github.com/images/modules/site/iconsnsource-ico-grow.svg?sn" alt="" aria-hidden></div>
<div class="overflow-hidden">
<h3 class="alt-h4 mb-2">Grow your skills and help others</h3>
<p class="alt-text-small text-gray">Whatever your skill level, working on open source software is a great way to learn newp;things.</p>
</div>
</div>
</div>
</div>
I guess you already know different between margin and padding. but wondering why they using both combined instead of one thing.
If you check their code. you will see they come from different class.
.mb-6 {
margin-bottom: 40px;
}
.pb-4 {
padding-bottom: 24px;
}
and If you dig a bit deeper you will see they have these classes in their framework.
.mb-1{ margin-bottom: 4px }
.mb-2{ margin-bottom: 8px }
.mb-3{ margin-bottom: 16px }
.mb-4{ margin-bottom: 24px }
.mb-5{ margin-bottom: 32px }
.mb-6{ margin-bottom: 40px }
and same things for padding pb-1 to pb-6
Now, If they want 64px space they have options to define a new class or re-use those class.
And they choose to reuse .pb-4 + .mb-6 to get 64px instead of define a new class just for one time using and without messing around with their framwork.
The reason why people would use margin and padding together would usually be due to the use of a background color, or background image.
If the background is left blank/transparent, it does not matter if you use a padding or a margin. However once you set the background color, the padding will increase the size of the element which includes the background color, while the margin will separate it from other elements creating white space in between.
Hope this helps you understand!
What I am understanding is that you went through GitHub's styles and noticed that they used both margin and padding in their CSS. Your question appears to be "Is using one/both preferred or does one method have an advantage?"
The answer to which is no, there isn't an advantage to using either, but you need to understand what margin and padding are
Margin
Margin is space between that element and elements around it. so saying margin:5px on something will put a five pixel wide margin around the entirety of the element, ensuring other elements do not "touch" it.
Example:
Notice that there is a very visible gab between the first element and the second element. And there is even a gap between the left side of the container and the first element.
.row > * {
float: left;
min-width: 25%;
max-width: 30%;
margin: 5px;
background-color: blue;
color: white;
}
<div class="row">
<div>Hi</div>
<div>Hello</div>
</div>
Padding
Padding, on the other hand, is how much space there should be between the edges of an element and the element's own contents. padding:5px says that there is a a sort of boundary inside the element five pixels wide on each side. To extend our first example:
Notice that there is a very small gap between the contents of each element's wall (where the background begins or ends) and the text content.
.row > * {
float: left;
min-width: 25%;
max-width: 30%;
margin: 5px;
padding: 5px;
/*Try removing/changing this value to see what effect it has.*/
background-color: blue;
color: white;
}
<div class="row">
<div>Hi, this text is longer so that we can see the border around the element and how much space there is between the walls of the element and the text.</div>
<div>Hello</div>
</div>
Tl;Dr
Margin is used to create a gap or some space between elements. Padding is used to create space between an elements contents and it's "walls."
So you seem to know
Padding is space inside the border, whereas Margin is space outside
the border.
Do you also know that that means, if you have margin set to elements following by the same elements it will just take the biggest possible value. So if margin-bottom is bigger than margin-top of the following element it will take margin-bottom.
So example gap will be margin-bottom from first element 20px.
* {margin:0; padding:0;}
div {
width: 100px; height: 100px;
background-color: orange;
border: solid 1px black;
}
div.one {
margin-bottom: 20px;
}
div.two {
margin-top: 5px;
}
<div class="one"></div>
<div class="two"></div>
Kinda same example gap is again 20px but this time it is the margin top from the second element.
* {margin:0; padding:0;}
div {
width: 100px; height: 100px;
background-color: orange;
border: solid 1px black;
}
div.one {
margin-bottom: 5px;
}
div.two {
margin-top: 20px;
}
<div class="one"></div>
<div class="two"></div>
And here what happens if you use padding. If you use your browser debugger you will see that now the gap should be 27px (25px from both elements padding + 2x1px border)
* {margin:0; padding:0;}
div {
width: 100px; height: 100px;
background-color: orange;
border: solid 1px black;
}
div.one {
padding-bottom: 5px;
}
div.two {
padding-top: 20px;
}
<div class="one"></div>
<div class="two"></div>
So to answer the why. If you know this you can have reasons to use one over the other.
Related
I'm practicing with HTML/CSS using Bootstrap v5.0 and there are some problems with the strange reactions between floats and divs. Particularly, I want to achieve something as below:
And I succeeded by applying the following piece of code:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="container">
<div class="center-div new-page">
<div class="row g-3 d-flex d-md-block">
<div class="col-lg-6 col-md-12 float-end">
<div class="third-slogan">
<h2 class="d-none d-md-block">Perfect for Operations HR and Finance</h2>
<h2 class="d-block d-md-none">OpenType features and Variable fonts</h2>
<p class="sub-slogan">Most calendars are designed for teams. Slate is designed for freelancers who want a simple way to plan<br>their schedule.</p>
</div>
</div>
<div class="col-lg-6 col-md-12 float-start">
<div class="screen3"><img src="https://via.placeholder.com/300x100" alt="Screen 3"></div>
</div>
<div class="col-lg-6 col-md-12 center-div float-end">
<div class="buttons-page-3">
<button id="button-button" class="btn btn-rounded btn-couple-2" style="color: #FFFFFF; background-color: #03D6F3; margin-top: 0;">
Button
</button>
</div>
</div>
</div>
</div>
</div>
My custom CSS:
.new-page {
margin-top: 10%;
}
.center-div {
text-align: center;
}
.third-slogan {
margin-top: 18%;
padding-right: 10%;
padding-left: 10%;
}
.third-slogan h2, p {
text-align: left;
}
.sub-slogan {
font-size: 16px;
font-weight: 700;
letter-spacing: 0.2px;
color: #5C5C5C;
margin-top: 10%;
}
.screen3 img {
width: 85%;
}
.buttons-page-3 {
text-align: left;
padding-left: 10%;
}
.btn-rounded {
border-radius: 39px;
font-size: 16px;
padding-top: 18px;
padding-bottom: 18px;
padding-left: 46px;
padding-right: 46px;
}
.btn-couple-2 {
margin-top: 5%;
box-shadow: 0px 4px 31px rgba(0, 0, 0, 0.15);
margin-right: 3%
}
But the problem is, after I apply the 2 float-end for the text and the button, and 1 float-start for the image, the divs which contains them does not display properly:
And it cause me a lot of troubles to continue to work with the divs after that. Could anyone please explain why this happens and how to fix it? Thank you very much.
P/s: The divs return to normal if I remove the float of the image or the button, but then it would not display as I desire, the button is pushed below the image.
The div around the button, which is the third div in the .row element is redundant and messes this up. This layout should have 2 columns (col-*) and the button should be inside of the second column. Title, intro text and button should be block elements without any floats, so they will stack on top of each other like your design mockup.
I have removed redundant html markup and cleaned up the CSS in order to let Bootstrap do most of the job for you: https://jsfiddle.net/3johtdxk/3/
EDIT: OP wants responsivity for mobile with text and heading above the image, and button below. Added second button in markup so we can hide/display them depending on the viewport width.
.new-page {
margin-top: 10%;
}
.sub-slogan {
font-size: 16px;
font-weight: 700;
letter-spacing: 0.2px;
color: #5C5C5C;
margin-top: 1.4rem;
}
.full-width {
display: block;
width: 100%;
}
.btn-rounded {
border-radius: 39px;
font-size: 16px;
padding-top: 18px;
padding-bottom: 18px;
padding-left: 46px;
padding-right: 46px;
}
.btn-couple-2 {
margin-top: 5%;
box-shadow: 0px 4px 31px rgba(0, 0, 0, 0.15);
margin-right: 3%
}
#media (max-width: 768px) {
.stack-order-mobile {
flex-direction: column-reverse;
}
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="container">
<div class="new-page">
<div class="row g-3 stack-order-mobile">
<div class="col-lg-6 col-md-12">
<img class="full-width" src="https://via.placeholder.com/1000x600" alt="Screen 3">
<button id="mobile-button" class="btn btn-rounded btn-couple-2 d-block d-md-none d-lg-none d-xl-none mt-2" style="color: #FFFFFF; background-color: #03D6F3;">
Button
</button>
</div>
<div class="col-lg-6 col-md-12">
<h2>Perfect for Operations HR and Finance</h2>
<p class="sub-slogan">Most calendars are designed for teams. Slate is designed for freelancers who want a simple way to plan<br>their schedule.</p>
<button id="desktop-button" class="btn btn-rounded btn-couple-2 d-none d-md-block d-lg-block d-xl-block" style="color: #FFFFFF; background-color: #03D6F3; margin-top: 0;">
Button
</button>
</div>
</div>
</div>
</div>
Note that when I removed floats I had to switch the order of the columns so that your image still stays on the left side. To get your desired stacking order, I added an extra button with hide/show classes from bootstrap at 768px and a media query for viewports <768px to move your text to the top on smaller screens.
The media query could probably be done with a Bootstrap utility, but I don't know it well enough. You have to reduce your whole browser window to less than 768px to see the stacking result as neither stackoverflow nor jsfiddle editors aren't great with responsiveness.
Added a larger image with 100% width so it fills up its left column completely. You may need to introduce some right padding/margin or reduce the image with percentage.
You had added a flex class in there that was redundant. Bootstrap columns ARE flex containers from the outset, so I removed it.
Remember: Always use as little CSS as possible! This is true also with Bootstrap. Don't load it up with a lot of stuff until you know what is going on. Try little by little and keep your markup lean. No need for extra divs around elements like img in most cases.
The issue of floats is another one, you don't need any floats here. Floats for responsivity is bad now that we have flex which is a cleaner solution. I removed them all. You may need them if/when you try to float the Invision, Marvel etc. divs in the element context in the left column.
But it looks like you're planning to use an image here, so no floats needed then. Try to stick with bootstrap columns only (less code, less mess).
It is my fault, I am trying to re-ask the question.
I have some code like this:
<style>
div {
float: left; width: 150px; padding: 10px;
margin: 10px; color: #fff;
}
</style>
<div style="background: #c33">
a<br>a<br>a<br>a<br>a<br>a<br>a<br>a
</div>
<div style="background: #3c3;">
b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>
b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b
</div>
<div style="background: #33c;">
c<br>c<br>c<br>c<br>c<br>c<br>c
</div>
<div style="background: #399;">
d<br>d<br>d<br>d<br>d<br>d<br>d<br>d<br>d<br>d<br>
</div>
<div style="background: #939;">
e<br>e
</div>
<div style="background: #993;">
f<br>f<br>f<br>f<br>f
</div>
<!--
... and so on ...
-->
when my visitor's screen has enough width, it is works fine like this.
when the screen become smaller, it still works fine at beginning.
but good time doesn't last long, when continually shrink screen size, it displayed like this.
some space appeared between c(the blue one) and e(the purple one).
then a(the red one) and f(the yellow one).
when shrink to 2 columns, a c and e are totally separated.
So, my question is, every my block have certain(fixed) width, uncertain(fluid) height, there is no max-width of this "block area" or say "the parent node of these blocks" or container whatever.
Can I just remove these unnecessary spaces using pure css way?
Hope this time I explained clearly, and thank you for reading my post.
You might try to left float only two, and float right the other:
.aaa,
.bbb,
.ccc {
width: 200px;
padding: 10px;
margin: 20px;
color: #fff;
}
.bbb {
float: right;
}
.aaa,
.ccc {
float: left;
}
<div class="aaa" style="background: #933">
a<br>a<br>a<br>a<br>a<br>a<br>a<br>a
</div>
<div class="bbb" style="background: #393">
b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br> b
<br>b<br>b<br>b<br>bbr>b<br>b<br>b<br>b<br>b
</div>
<div class="ccc" style="background: #339">
c<br>c<br>c<br>c<br>c<br>c
</div>
Grid, flex... and even simply using floats and clears:
<style>
div {
width: 200px; padding: 10px;
margin: 20px; color: #fff;
}
</style>
<div style="background: #933; float: left;">
a<br>a<br>a<br>a<br>a<br>a<br>a<br>a
</div>
<div style="background: #393; float:right;">
b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>
b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b
</div>
<div style="background: #339; clear:left;">
c<br>c<br>c<br>c<br>c<br>c
</div>
To some extent you can do that, if you use left AND right floats as shown below and put a wrapper around it to let the right-floated elements not go too far right:
div {
width: 200px;
padding: 10px;
margin: 20px;
color: #fff;
}
.a {
float: left;
}
.b {
float: right;
}
.wrapper {
width: 520px;)
<div class="wrapper">
<div style="background: #933" class="a">
a<br>a<br>a<br>a<br>a<br>a<br>a<br>a
</div>
<div style="background: #393" class="b">
b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br> b
<br>b<br>b<br>b<br>bbr>b<br>b<br>b<br>b<br>b
</div>
<div style="background: #339" class="a">
c<br>c<br>c<br>c<br>c<br>c
</div>
</div>
Like others have said, there are plenty ways of doing it, but I'd use flexbox.
Just wrap the two boxes on the left in a container div, and use display:flex on that container, and set the flex-direction property to column and they should stack on top of one another.
Here's a great website to pick up the basics - http://flexboxfroggy.com/
Oddly enough, the closest you could get is using damn CSS columns..
Yeah, that's right. I just said "CSS Columns"
Declaring an auto column layout using your divs width as column width, and making sure no div will wrap into multiple columns with break-inside: avoid; you can get pretty close.
body {
columns: 150px;
column-gap: 2em;
}
div {
break-inside: avoid;
}
https://jsfiddle.net/p0yLs5sh/1/
And yes, I know. I just said columns. Thought that would never be an answer.
I've been looking all over for an answer to this but I can't find a fix anywhere. I'm just trying to move the h1 tag right over top of the icons but whenever I use margin-top or padding-top to move the h1 down the page it moves the column down as well. I put borders around all of the columns around there to see if maybe the borders were touching but that was no help. Is there like some sort of default padding around h1's or columns that you can't see?
Here is a link to my codepen: https://codepen.io/4eller/pen/eVmxeM
HTML:
<hr width="35%">
<div class="container maincon2">
<h1 class="wwd">Social Media Has Never Been Easier</h1>
<hr class="hr1">
<h1 class="whatwedo">What makes us stand out from the rest</h1>
<div class="row topicons">
<div class="col-md-4 maintab1">
<img src="images/graph.png" class="barimg">
<hr width="50%" id="hr2">
</div>
<div class="col-md-4 maintab2">
<img src="images/piggy-bank.png" class="pigimg">
<hr width="50%" id="hr2">
</div>
<div class="col-md-4 maintab3">
<img src="images/support.png" class="supportimg">
<hr width="50%" id="hr2">
</div>
</div>
</div>
</div>
</div>
<div class="container maincon3">
<div class="row">
<div class="col-lg-12 mediumcon2">
<hr class="hrgreen">
CSS:
.maincon2 {
width: 100%;
height: 500px;
background: #424242;
margin-left: auto;
margin-right: auto;
margin-top: 100px;
border: 1px solid orange;
}
.topicons {
border: 1px solid red;
margin-top: 70px;
height: 250px;
table-layout: fixed;
}
If I understand correctly what you want (not really 100% clear), you can apply position: relative to that tag and use top to move it down without affecting anything else, since position: relative plus position settings cause an element to be moved in relation to its original (static) position, but elements before and after it will remain were they are:
h1.whatwedo {
position: relative;
top:30px;
}
Changed codepen: https://codepen.io/anon/pen/MQYRKY
Give the h1 tag position:absolute; and then move it left with left:200px(replace 200 with whatever number you want)
The reason why when using margin, padding or position:relative, it will move other elements, is because then they are considered part of the "flow" of the page, meaning they will interact with and bump other elements around. position:absolute, removes the target element from the flow of the page, thus allowing you to put it wherever without moving other elements.
I am relatively new to StackOverflow and am experiencing some difficulties while making a webpage
So what I require is a one page website divided into different sections, which are full-width divs (i.e 100% width of the screen)and are consecutive with different background colors.
The problem I am facing is that the divs do not take up the full width of the screen and have white space not only on the sides, but also between 2 divs
Also, when the window size reduces, the gap between divs increases
The desired result is as observed in the following websites:
http://classrebels.com/
http://startbootstrap.com/templates/freelancer/
The code I am using is as follows:
i) HTML:
<html>
<body>
<div class="full" id="one">
<h1>Just something</h1>
</div>
<div class="full" id="two">
<h1>Just something</h1>
</div>
</body>
</html>
ii) The CSS:
.full{
width= 100%;
}
#one{
background-color: #fff;
}
#two{
background-color: #f13;
}
Please do tell me where I am going wrong
Demo
html
<div class="full" id="one">
<h1>Just something</h1>
</div>
<div class="full" id="two">
<h1>Just something</h1>
</div>
css
body, html {
width: 100%;
height: 100%;
margin:0; /* default margin set to 0 */
padding:0; /* default padding set to 0 */
}
.full {
width: 100%;
}
#one {
background-color: gray;
height: 50%; /* whatever you want to give height */
}
#two {
background-color: #f13;
height: 50%; /* whatever you want to give height */
}
.full h1 {
margin:0; /* default margin of h1 tag set to 0 */
padding: 20px 10px 10px 20px; /* padding if you want to give spaces between borders and content of div */
}
Demo Fiddle
To remove the default margin/padding on the viewport (which is giving you the whitespace), you need to add the following CSS:
html, body{
width:100%; /* <-- also a good idea to add */
margin:0;
padding:0;
}
and change:
.full{
width= 100%;
}
to:
.full{
width:100%;
}
CSS style property/value pairs are seperated with a colon : and not an equals =
have you tried setting margin and padding 0 in full and in body tag too
like
.full
{
width :100%;
margin:0px;
padding:0px;
}
similarly your heading also takes some margin so set the margin of heading as required.
for better please consult this link w3schools
You have to change the body and h1 margins, since they have default values in the browser.
I've fixed it for you in this fiddle:
h1{
margin: 0px;
}
body{
border: 0px;
padding: 0px;
margin: 0px;
}
http://jsfiddle.net/pWLgb/
the problem is that you dont give the whole div a background color but only the text.
if you ad a border you can see the real size of the div and it will fill the whole div with color.
check out the fiddle i made for that
border: 1px solid black;
http://jsfiddle.net/2h4kQ/
you can set the border to 0px so it is not shown and it will give you the right result
The demo's that you linking to aren't using full width divs. They actually use a full width <section> element which has the background color set on it.
Then, they have an inner row <div> which then has a container and column <div>. So in the Freelancer example it looks like this:
<section class="success" id="about">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>About</h2>
<hr class="star-light">
</div>
</div>
<div class="row">
<div class="col-lg-4 col-lg-offset-2">
<p>Freelancer is a free bootstrap theme created by Start Bootstrap. The download includes the complete source files including HTML, CSS, and JavaScript as well as optional LESS stylesheets for easy customization.</p>
</div>
<div class="col-lg-4">
<p>Whether you're a student looking to showcase your work, a professional looking to attract clients, or a graphic artist looking to share your projects, this template is the perfect starting point!</p>
</div>
<div class="col-lg-8 col-lg-offset-2 text-center">
<a href="#" class="btn btn-lg btn-outline">
<i class="fa fa-download"></i> Download Theme
</a>
</div>
</div>
</div>
</section>
Sample of the CSS:
section.success {
color: #fff;
background: #18bc9c;
}
.container {
width: 1170px;
margin-right: auto;
margin-left: auto;
padding-left: 15px;
padding-right: 15px;
}
Download that template, it uses Bootstrap, and play around with it to get the look you want.
See the image below. I'd like the content to extend all the way to the rounded corners.
Note that this content is not just an image and can include an overlay (.item-screen)
Markup looks like:
<div class="col-sm-6">
<div class="thumbnail">
<div class="item-picture">
<a href="blah">
<img src="blah" class="img-responsive"/>
</a>
<div class="item-screen"></div>
</div>
<div class="caption">dark picture</div>
...
</div>
When you put together a simple example, and use some browser tools, you will see a small padding 4px applied to div.thumbnail. To extend the picture to the border, remove that padding
div.thumbnail {
padding: 0;
}
JSFiddle
If you want to keep the padding at the bottom, you can do
div.thumbnail {
padding-top: 0;
padding-right: 0;
padding-left: 0;
}
and having an image with round corners
<img src="..." class="img-rounded"/>
JSFiddle
Took another look after Olaf pointed out that I could remove the padding
I ended up with applying the following CSS:
.item a img {
border-top-left-radius: 4px;
border-top-right-radius: 4px;
border: 0px solid transparent;
}
.item .thumbnail { padding: 0px; }
I'd tried adding the border radius before, but until I realized you could make a transparent border it came out looking wonky