What's causing so much space in my div containers? [closed] - html

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to create two divs with a heading that have a grey header that only have a small amount of padding.
Here is my html
<div class="container">
<div class="row">
<div class="col-6">
<div class="small-grey-header">
<p class="center-text">User Login</p>
</div>
</div>
<div class="col-6">
<div class="small-grey-header">
<p class="center-text">Help Links</p>
</div>
</div>
</div>
</div>
And here is my css
.center-text{
text-align: center;
display: block;
}
.small-grey-header{
border-radius: 3px;
background-color: lightgray;
height: auto;
padding-top: 1px;
padding-bottom: .5px;
margin-bottom: 5px;
}
The problem is that they look way to big and I'm not sure why.
Here is what I want
And this is what im getting
Here is a the fiddle
https://jsfiddle.net/Ljsgtq0o/
I am using a grid system to set these side by side but I'm excluded it for simplicity. Even setting 1 pixel of padding on the divs is causing a huge amount of space. What am I doing wrong? Please note that I'm not concerned with the spacing between these two headings. I just want these headings to not be so large. They almost look like buttons.

There is a default margin-bottom: 10px on p from bootstrap. You can use another element for that text (like a div) or remove the margin from the p
And if you want them to be side-by-side, use the col-size-# class structure.
.center-text {
text-align: center;
display: block;
}
.small-grey-header {
border-radius: 3px;
background-color: lightgray;
height: auto;
padding-top: 1px;
padding-bottom: .5px;
margin-bottom: 5px;
}
.small-grey-header p {
margin: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="small-grey-header">
<p class="center-text">User Login</p>
</div>
</div>
<div class="col-xs-6">
<div class="small-grey-header">
<p class="center-text">Help Links</p>
</div>
</div>
</div>
</div>

Try to add max-width property to small-grey-header class. For example:
max-width:100px;
There you have shorter buttons.
If you want to have lower buttons you can simply edit height property, you don't have to use padding in this case and you can add line-height property.
https://jsfiddle.net/Ljsgtq0o/2/
height: 25px;
line-height:25px;

Related

Float in CSS affect the containing divs

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).

Why GitHub use both of padding and margin?

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.

Why does Bootstrap grid system overflow when cols add up to 12?

What is happening
What I want
As you can see, the Amy grey box and rectangle grey box are not positioning themselves on the same line. This is strange considering my codes uses the Bootstrap col sys to put both grey boxes on the same row.
My cols add up to 12 so I'm not sure what is happening. I do notice, however, if I make the cols add up to 11, then both grey boxes are put on the same line. But this is not a fix I want as I would like my cols to add up to 12 and have both boxes appear on the same line.
If anyone could help me solve this it would be greatly appreciated!
My code
HTML
<div class="row student-row">
<div class="col-xs-2 student-box">
<h1>Amy</h1>
</div>
<div class="col-xs-10 worksheet-box">
...
</div>
</div>
CSS
.student-row{
margin: 20px 10px 10px 10px;
}
.student-box{
margin-right: 10px;
}
.worksheet-cell{
margin-top: 10px;
margin-left: 10px;
width: 20%;
}
.worksheet-group{
margin: 10px 15px 20px 15px;
}
JSFiddle
You've overridden the margins that Bootstrap set and set your own. Now the box needs more space so it doesn't fit.
Remove those margins and the two boxes fit side by side.
.student-row{
/* margin: 20px 10px 10px 10px;*/
}
.student-box{
background-color: #F5F5F5;
font-weight: 700;
text-transform: uppercase;
/* margin-right: 10px; */
}
Your style is breaking since you are adding the following to your .row element in Bootstrap. If you play with the margin of the row it will break.
.student-box{
margin-right: 10px;
}
I recommend to use the following pattern:
<div class="row student-row">
<div class="col-xs-2 student-box">
<h1>Amy</h1>
</div>
<div class="col-xs-10 worksheet-box">
<!-- Add new elements row wise here -->
<div class="row">
</div>
</div>
</div>

why can't I get my two divs to sit side by side [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to be able to place two divs side by side, the right div with text and the left one with a picture but I cant get it to work, I tried to float them right and left but that didn't work.
Here is my code
<div id="container">
<div id ="pic">
<img src="IMG_4847.jpg"alt="me"/>
</div>
<div id ="about">
<h1>About Me</h1>
<p>Hello my name is Rebekah, I’m 23 and this is my blog </p>
</div>
</div>
#about
{
padding-left: 500px;
padding-right: 100px;
padding-top: 60px;
}
#pic
{
padding-left: 110px;
padding-top: 60px;
}
You need to float the elements:
#pic should be floated left
#pic {
float:left;
}
#about should be floated right
#about {
float:right;
}
http://jsfiddle.net/x19wwk9n/
You also need the # on your about tag within your CSS file.
You need to give your CSS the float instruction. Try giving both parts this:
float: right;
Try this solution:
#about
{
padding-left: 500px;
padding-right: 100px;
padding-top: 60px;
display: inline-block;
}
#pic
{
padding-left: 110px;
padding-top: 60px;
display: inline-block;
}
give both the divs the property to stay side bi side this way:
#about
{
padding-top: 60px;
float: left;
}
#pic
{
padding-top: 60px;
float: left;
}
and in your html after the two divs:
<div id="container">
<div id ="pic">
<img src="IMG_4847.jpg"alt="me"/>
</div>
<div id ="about">
<h1>About Me</h1>
<p>Hello my name is Rebekah, I’m 23 and this is my blog </p>
</div>
<br style="clear:both;">
to reset the float property and keep the rest of the layout as you want it. Note I have removed a closing tag that was not necessary
use display: table
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
#container{
display: table;
width: 100%;
height: 200px;
}
#pic,
#about{
display: table-cell;
vertical-align: middle;
width: 50%;
padding: 25px;
}
#pic{
background: #f00;
}
#about{
background: #00f;
}
<div id="container">
<div id ="pic">
<img src="IMG_4847.jpg"alt="me"/>
</div>
<div id ="about">
<h1>About Me</h1>
<p>Hello my name is Rebekah, I’m 23 and this is my blog </p>
</div>
</div>
You can display divs side by side using float:left. You can also get them to display side by side using inline-block and or float:left and float:right (like others had mentioned). There are actually multiple ways this can be achieved and each one will likely depend on what uses cases you are trying to work with.
HTML:
<div></div>
<div></div>
CSS:
div {
width:50%;
border:1px solid;
height:200px;
box-sizing:border-box;
float:left;
}
Note: I added height because there is no content. Just to help show you better visually. And the box-sizing is to help the box model take the border into account. Otherwise the 1px border would cause the second div to wrap under the first.
Recommendation:
Also instead of doing this..
<div id="pic"></div>
<div id="about"></div>
I would recommend this..
<div class="column"></div>
<div class="column"></div>
And then something like..
.column {
width:50%;
border:1px solid;
height:200px;
box-sizing:border-box;
float:left;
}
Example:
https://jsfiddle.net/4eocqjur/1/

How to apply internal Bootstrap/CSS Padding?

Hi I have bunch of dynamic divs that are getting generated, but I dont know how to pad them internally and align them with left and right edges?
any help would be appreciated.
You can do something like:
[class*="col-"] {
padding-right: 15px;
padding-left: 15px;
}
[class*="col-"]:first-child {
padding-left: 0px;
}
[class*="col-"]:last-child {
padding-right: 0px;
}
You might add a content to wrap it, otherwise you'll have those rules applied to all columns in your layout!
.spaced-columns [class*="col-"] {
padding-right: 15px;
padding-left: 15px;
}
So then you can use:
<div class="spaced-columns">
<div class="col-md-4"> your content here</div>
<div class="col-md-4"> your content here</div>
<div class="col-md-4"> your content here</div>
</div>
So you'll have your spacing as you want :)
Cheers
I would recommend checking out bootstrap. Does most of the css for you:
http://getbootstrap.com/examples/grid/
You can put your div within the <div class="col-md-4"> or you can simply add a class to it like: <div class="col-md-4 my-class"> and add some padding to it like:
.my-class {
padding: 10px;
}
If you chose to add an idder div, you add the padding to the <div class="col-md-4"> and simply style your inner div.
<div class="col-md-4">
<div class="my-class">Text</div>
</div>
And style it like:
.col-md-4 {
padding: 10px;
}
.my-class {
background: red;
}
If you wrap your column boxes in a you should be alright. If the padding is still off, you can alter the margin of the row, and then you don't need to mess with the individual column divs.
Example
.row {margin-left:-15px;margin-right:-15px;}