I realize the question has been asked several times over, but frankly I didn't manage to find an actual answer in any of the instances.
Whatever I try I cannot get rid of the spaces between divs? here is a simplified version of the problem, and a jsfiddle: http://jsfiddle.net/hhLopqwm/1/ . Any ideas? How can I make the divs connect?
<div class="top">
so what <br><br> is going on here
</div>
<div id="work">
<h2>no margins control this space between divs</h2>
<h4>it's like magic or something</h4>
</div>
<div class="red">
any clue what should I do?
</div>
Heading elements (in your example <h2> and <h4>) have margins too which push the parent divs apart:
div, h2, h4 {
margin:0;
padding:0;
}
html {
margin: 0;
padding: 0;
}
.top {
background-color:yellow;
text-align: center;
margin: 0;
padding:0;
}
#work {
background-color:green;
margin:0;
padding:0;
text-align: center;
}
div, h2, h4 {
margin:0;
padding:0;
}
.red {
text-align: center;
background-color:red;
}
<div class="top">so what the hell
<br>
<br>is going on here</div>
<div id="work">
<h2>no margins control this space between divs</h2>
<h4>it's like magic or something</h4>
</div>
<div class="red">any clue what should I do?</div>
Check this link:
How to remove the gap between div in html?
Use
* {
padding: 0px;
margin: 0px
}
at the top of your CSS
H2, H4 {margin: 0;} will fix it, as mentioned previously its the margin on those.
You could also use padding to push the div out the appropriate amount, or a min-height to resolve the issue.
Personnally I would probably just remove the margin from all H1/2/3/4/5/6.
I wouldnt even use a reset since typically this adds more CSS than its worth.
My standard reset would be html, body, H1, h2, h3, h4, h5 {margin:0; padding:0;}
Please DO NOT use: * {margin: 0; padding 0;}
This is generally bad practice and can and will break some forms etc, google it if you want further information.
Heres some of the cons:
http://www.cssreset.com/scripts/universal-selector-css-reset/
•No control over exactly which elements are reset – every element used in the document must now have its margin and padding set explicity, plus any other properties such as border and outline that may be included in the reset (see the extended version below).
•Wave goodbye to inheritance of CSS rules from parent to child elements – the universal selector wins out every time (see note on inheritance, below). So not only must every element be defined after the reset, but child elements now cannot inherit each element’s properties, and so they must also be explicitly defined. The amount of code this requires may even negate the size-savings from the minimal CSS Reset!
•According to the universal declaration, a browser must run through every element on the page and apply the universal rule: elements, their children and great-great-great-grandchildren all alike, and some claim that this may be a huge hit on resources and page load times (this point is debatable for modern browsers.)
•Also – and this might be the deal-breaker for many – Internet Explorer 6 and 7 don’t support the universal selector.
Related
I have this code:
<h1> Something </h1>
<h3> Somethings </h3>
<h3> Some other things </h3>
I think HTML would automatically add 1 line spacing in between them.
I would like them to just go without 1 line of spacing, line by line I mean, not line space line space.
How could I do this without using a <br> element which is definitely unworkable in between the <h1> and <h3>, and probably not a structurally good HTML practice maybe too right?
Is using a CSS bottom margin fix this?
I am sorry to ask such an easy question maybe, I am a still bit new to HTML & CSS, but I am still learning. Thx in advance for all the answers
Default styling, the enemy of consistency
Many elements have default margins applied to them by browsers. The problem is that each browser (Chrome, IE etc) applies different amounts of margin.
This makes it hard to achieve consistency between browsers, and consistency is what we need for effective web-design.
CSS Resets to the rescue
Look into the concept of "CSS Resets". Using this approach, and advisably some prebuilt code like this: https://github.com/murtaugh/HTML5-Reset will RESET these values to zero to give you a level playing field.
Then YOU can decide how much margin your headings will have.
The reset you are looking for here - just for this problem - is:
h1, h2, h3 {
margin: 0px;
}
Then applying your own styles
Then you would build it back up again, like this for example:
h1, h2, h3 {
margin: 0px;
}
h1 {
margin-bottom: 20px;
}
h2, h3 {
margin-bottom: 10px;
}
<h1> Something </h1>
<h3> Somethings </h3>
<h3> Some other things </h3>
The universal reset
A very simple "total" CSS reset would be:
* {
margin:0;
padding:0;
}
But that's not advisable for a number of reasons especially performance: (why) is the CSS star selector considered harmful?
The vertical spacing you see is caused by default top and bottom margins of heading elements. It has nothing to do with line spacing (in the CSS sense). To remove the spacing, set the relevant margins to zero. The details depend on the context and desired effects on margins before and after the sequence of headings (which is questionable structurally here – a 1st level heading immediately followed by two 3rd level headings; maybe you meant the second one to be h2?).
For the given markup, assuming that no other effects are desired, and assuming for simplicity that these are the only h1 and h3 elements, you would set
h1 { margin-bottom: 0; }
h3 { margin-top: 0; }
h1 + h3 { margin-bottom: 0; }
h1, h3 {
margin:0;
}
This will help
Headings have a margin by default, so yes, you should use CSS to remove the margins.
I need to style h1, h2, h3... and p tags inside a div but I only have access to the content area.
If it where possible, this would be what I'd use:
<div style="h1{padding:0;}p{font-size:1.4em;color:#000}">
Is there a solution to do this ? Apart from adding the style to every element.
Thanks
Although HTML syntax restricts style elements to the head part, this requirement is not enforced in practice. It works inside body, too. You just need to take into account that the effects are global to the document. Thus, to limit the effect to elements inside a certain element, you need to use suitable selectors. Example (I have added a color setting because the effect of just padding: 0 as in the question in not noticeable: it equals the default):
<h1>Heading outside the div</h1>
<p>A paragraph outside the div.</p>
<div class=mydiv>
<style>
.mydiv h1 { padding: 0; color: green; }
.mydiv p { font-size: 1.4em; color: #000; }
</style>
<h1>Heading inside the div</h1>
<p>A paragraph inside the div.</p>
</div>
There isn't a good solution.
Style elements may only appear in the head.
Inline style only applies to the element the attribute appears on.
The closest you can come is to use JavaScript to dynamically modify the stylesheet.
You would be better fixing whatever problem is preventing you from modifying the head section.
To avoid unwanted changes inside divs i will be using to divs with 2 unique id's:red and green
If you want different style for specific divs:
<div id="red"><h1>red</h1><p>red</p>
<div id="green"><h1>green</h1><p>green</p>
body #red > h1,body #red >p{
color:red;
}
body #green > h1,body #green > p{
color:green;
}
I have two sections(a header section and main body section) and for whatever reason there is this big gap. I have tried many things such as setting padding to 0, margin to 0, etc but nothing seems to get the job done. Please help!
http://jsfiddle.net/Pfc2Z/2/
Here's the relevant HTML
<section class="header">
<section class="logo">
<img src="logo.png" height="100px" width="200px" alt="Img Not Available">
</section>
<section class="link">
<nav>
<ul>
<li>
<span>Home</span>
</li>
</ul>
</nav>
</section>
</section>
<section class="main">
<p>Hello World</p>
</section>
Here is the CSS
html, body {
margin:0;
padding:0;
background-color:#46a7bb;
}
.header {
background-color:#313145;
}
.logo {
padding-left:20%;
}
.logo, .link {
display:inline-block;
}
nav ul {
list-style:none;
padding:0;
margin:0;
}
.main {
background-color:white;
width:60%;
margin:0 auto;
}
That is because your main section contains a paragraph tag. Paragraph tags have their own default styling (read margins and paddings). If you remove that, you'll be just fine. Just add the following to your CSS classes:
p {
margin:0;
padding: 0;
}
See this here: http://jsfiddle.net/y9deH/1/
UPDATE:
Yes its possible. You can do that by making use of class selectors or id selectors. Class selectors will let you use this kind of styling again whenever you want to use it (re-usability) while not altering the default paragraph styling.
ID selectors on the other hand, will apply only an only to this paragraph.
See the class selector example here: http://jsfiddle.net/y9deH/2/
See the id selector example here: http://jsfiddle.net/y9deH/3/
Hope this helps!!!
There's nothing wrong with the layout and they doesn't have any margin or padding problem. The problem is with the paragraph <p>Hello World</p>. It has a default margin. You can fix this problem by removing its default margin to 0 or sending it down with padding but its not a good idea to override the default paragraph style use padding to the parent instead. Here's the link, i used (20px padding to the main section). Thank you
http://jsfiddle.net/fyfWq/
Just in case someone comes here looking for an annoying gap they cannot explain, and they've gone through all the usual checks on margin, padding, and borders:
If you have made your elements inline-block, then the browser will render white space between your elements at whatever font-size is defined there. I chose to solve this by adding a negative margin, but you can also solve it by removing white-space in your html (either by commenting it out, or... wait for it... using your delete / backspace button).
I came here looking for this answer to this problem, so hope it helps someone else out there.
Update: Just re-looking at the question above, I see an inline-block there, so perhaps this was actually the issue!
Firstly, i'm new to both HTML and css, so don't be too hash. I have a large header div that I wish to place it flush at the top of the screen, however, there appears to be space of about 10px which I can't remove.
HTML
<div class="wrapper"></div>
CSS
.wrapper{width:300px; background-color: red; height: 300px; margin: 0; padding: 0;}
If you're new to CSS and stuff,
you need to know that the browser applies styles to elements by default.
Like for example to Headings the font-size and font-weight,
to context elements like i, b, span other properties like display inline,
and to DIV elements the display:block; etc...
if you take a look at THIS LIST you'll see that 8px are added to the body element.
If you're happy with the styles the browser adds by default to your elements than all you need is
body{
margin:0; /* to remove the 8px default */
}
otherwise if you're not happy at all, and you wish to have full control over the styles being applied to your elements you can use an Ugly Reset (for margin and padding urgency) using the Universal Selector *
*{ margin:0; padding:0; } /* Global reset. "*" is to target all elements. */
or Google for some Stylesheet Reset code like from: http://www.cssreset.com/
that will help you to control/reset the most of all other elements default styles.
You need to add that to the body as well. The DIV is inside the BODY.
Try setting
margin: 0;
padding: 0;
To the body and the html elements
I cannot seem to figure out how to get the space between each element to disappear. I mainly want to remove the white space on top. I have tried using margin-top:0 and it didn't work. Any advice would be greatly appreciated.
This is my html
<body>
<div style="background-color:green;">
<h1>top</h1>
</div>
<div style="background-color:blue;">
<h1>body</h1>
</div>
<div style="background-color:red;">
<h1>footer</h1>
</div>
</body>
This is my css
body, div, header {
padding:0;
margin:0;
margin-top:0px;
}
The space is caused by the User Agent Stylesheet (the default styles applied by your browser).
You need to target the H1 also.
html, body, div, h1 {
margin:0;
}
Alternately, rather than individually targetting each element, you can use the universal CSS selector * to target everything at the start:
* {
margin:0;
padding:0;
}
To avoid trouble, you should start your CSS with a CSS reset. This sets all padding, margins etc on all elements to ensure browsers interpret your styles from the same starting point rather than relying on each browser's individual user agent stylesheet.
If you want to go modern, you can use Normalise.css
Include h1 on your CSS:
Fiddle
body, div, header, h1 {
padding:0;
margin:0;
margin-top:0px;
}