When nesting <div>'s their margin's and padding's start to overlap. What I understood from the spec's the margin, border and padding are just part of the space an element takes and each element should stay contained in the content-area of parent <div>.
Clearly I understand wrong, but I hope somebody can explain me right.
Bottom-line: please help me let the thingy's stay inside the top. Preferably in a natural (not too hacky) way.
Thanks for reading and any insight you can share in advance!
The Fiddle:
http://jsfiddle.net/WrasM/2/
The HTML (making sure newlines in HTML don't have any influence):
<!--
--><div class="parent"><!--
--><div class="top"><!--
--><div class="thingy">label 1</div><!--
--><div class="thingy">label 2</div><!--
--><div class="thingy">label 3</div><!--
--></div><!--
--><div class="bottom"><!--
-->some test content<!--
--></div><!--
--></div><!--
-->
The CSS:
.parent { display: block;
margin: 0px; padding: 0px;
border: 3px solid Yellow; }
.top { display: block;
margin: 0px; padding: 0px; }
.thingy { display: inline;
margin: 20px; padding: 10px;
border: 1px solid DarkBlue; }
.bottom { display: block;
margin: 0px; padding: 0px;
border: 1px solid DarkBlue; }
The result:
Change .thingy to display:inline-block.
Section 8.3 on this spec page http://www.w3.org/TR/CSS2/box.html says
These properties have no effect on non-replaced inline elements. for margin-top and margin-bottom.
Can someone tell me why the contents of page-view-count & num-of-days div spills into top-header in my implementation?
Markup:
<div id="top-stats">
<div id="page-view-count">count</div>
<div id="num-of-days">num of days</div>
</div>
<div id="top-header"This is a test</div>
CSS
#top-stats{
width: 100%;
}
#page-view-count, #num-of-days{
color: #666;
text-shadow:1px 1px #FFFFFF;
font-size:13px;
font-weight: bold;
padding-bottom: 5px;
}
#page-view-count{
float:left;
}
#num-of-days{
float:right;
}
#top-header{
width:100%;
display:block;
background-color:#DBDB70;
border-radius:3px;
}
If I were to remove the float property it stacks up nicely inside top-stats.
See fiddle
I'm probably missing some simple rule but I'm probably looking too closely and need another pair of eyes..
#top-header{
clear: both;
width: 100%;
display:block;
background-color:#DBDB70;
border-radius:3px;
}
Fiddle
I'm having a problem with positioning my navigation element.
CSS:
#wrapper { width: 600px; margin: 0 auto; height: 300px; background: #f9f9f9; border: 1px solid #f0f0f0; }
#navigation { margin: 0 auto; text-align: center; }
.mylink { background: #666; color: #ccc; padding: 5px 10px; display: inline-block; }
.mylink:first-child { -webkit-border-top-left-radius: 10px; -moz-border-radius-topleft: 10px; border-top-left-radius: 10px; }
.mylink:last-child { -webkit-border-top-right-radius: 10px; -moz-border-radius-topright: 10px; border-top-right-radius: 10px; }
HTML:
<div id="wrapper">
<div id="navigation">
<a class="mylink">Homepage</a>
<a class="mylink">Second Page</a>
<a class="mylink">Third Page</a>
</div>
</div>
I want to remove the margins on the inline elements.
I tried a "float:left" variation too. But than I can't center the elems without a width value.
Any ideas how i can solve it?
That's a common problem with 'inline-block'. You're not dealing with a margin, but an actual space character.
This should help: http://css-tricks.com/fighting-the-space-between-inline-block-elements/
You don't have a margin, inline elements just take whitespace (spaces, tabs, newline, whatever other invisible separators might be there) into consideration, either use block display and floats or type it without whitespace, updated your fiddle http://jsfiddle.net/Rnmbx/1/
What am I doing wrong here?
I have a .social div, but on the first one I want zero padding on the top, and on the second one I want no bottom border.
I have attempted to create classes for this first and last but I think I've got it wrong somewhere:
.social {
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
border-bottom: dotted 1px #6d6d6d;
}
.social .first{padding-top:0;}
.social .last{border:0;}
And the HTML
<div class="social" class="first">
<div class="socialIcon"><img src="images/facebook.png" alt="Facebook" /></div>
<div class="socialText">Find me on Facebook</div>
</div>
I'm guessing it's not possible to have two different classes? If so how can I do this?
If you want two classes on one element, do it this way:
<div class="social first"></div>
Reference it in css like so:
.social.first {}
Example:
https://jsfiddle.net/tybro0103/covbtpaq/
You can try this:
HTML
<div class="social">
<div class="socialIcon"><img src="images/facebook.png" alt="Facebook" /></div>
<div class="socialText">Find me on Facebook</div>
</div>
CSS CODE
.social {
width:330px;
height:75px;
float:right;
text-align:left;
padding:10px 0;
border-bottom:dotted 1px #6d6d6d;
}
.social .socialIcon{
padding-top:0;
}
.social .socialText{
border:0;
}
To add multiple class in the same element you can use the following format:
<div class="class1 class2 class3"></div>
DEMO
Remember that you can apply multiple classes to an element by separating each class with a space within its class attribute. For example:
<img class="class1 class2">
If you have 2 classes i.e. .indent and .font, class="indent font" works.
You dont have to have a .indent.font{} in css.
You can have the classes separate in css and still call both just using the class="class1 class2" in the html. You just need a space between one or more class names.
If you only have two items, you can do this:
.social {
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
border: none;
}
.social:first-child {
padding-top:0;
border-bottom: dotted 1px #6d6d6d;
}
I know this post is getting outdated, but here's what they asked.
In your style sheet:
.social {
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
border-bottom: dotted 1px #6d6d6d;
}
[class~="first"] {
padding-top:0;
}
[class~="last"] {
border:0;
}
But it may be a bad way to use selectors. Also, if you need multiple "first" extension, you'll have to be sure to set different name, or to refine your selector.
[class="social first"] {...}
I hope this will help someone, it can be pretty handy in some situation.
For exemple, if you have a tiny piece of css that has to be linked to many different components, and you don't want to write a hundred time the same code.
div.myClass1 {font-weight:bold;}
div.myClass2 {font-style:italic;}
...
div.myClassN {text-shadow:silver 1px 1px 1px;}
div.myClass1.red {color:red;}
div.myClass2.red {color:red;}
...
div.myClassN.red {color:red;}
Becomes:
div.myClass1 {font-weight:bold;}
div.myClass2 {font-style:italic;}
...
div.myClassN {text-shadow:silver 1px 1px 1px;}
[class~=red] {color:red;}
If you want to apply styles only to an element which is its parents' first child, is it better to use :first-child pseudo-class
.social:first-child{
border-bottom: dotted 1px #6d6d6d;
padding-top: 0;
}
.social{
border: 0;
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
}
Then, the rule .social has both common styles and the last element's styles.
And .social:first-child overrides them with first element's styles.
You could also use :last-child selector, but :first-childis more supported by old browsers: see
https://developer.mozilla.org/en-US/docs/CSS/:first-child#Browser_compatibility and https://developer.mozilla.org/es/docs/CSS/:last-child#Browser_compatibility.
Another option is to use Descendant selectors
HTML:
<div class="social">
<p class="first">burrito</p>
<p class="last">chimichanga</p>
</div>
Reference first one in CSS: .social .first { color: blue; }
Reference last one in CSS: .social .last { color: green; }
Jsfiddle: https://jsfiddle.net/covbtpaq/153/
Instead of using multiple CSS classes, to address your underlying problem you can use the :focus pseudo-selector:
input[type="text"] {
border: 1px solid grey;
width: 40%;
height: 30px;
border-radius: 0;
}
input[type="text"]:focus {
border: 1px solid #5acdff;
}
I have a "bubble" with content, which is working fine. Now, I want to display a count (2 lines) which should always be in the bottom right corner of that div, INSIDE it. I tried many things but for some reason it always overlaps the div and shows outside. What am I doing wrong?
<style type="text/css">
body{
background-color:#f3f3f3;
}
.commentbox{
background-color: #ffffff;
width: 200px;
border-color: #D1D1D1;
border-radius: 4px;
border-style: solid;
border-width: 1px;
padding-bottom: 9px;
padding-left: 9px;
padding-right: 9px;
padding-top: 9px;
position:relative;
}
.count{
float:right;
text-align:right;
}
</style>
<div class="commentbox">
<div class="title">Some several lines long long long long content text goes here
</div>
<div class="count">123<br>456</div>
</div>
You are floating .count so it doesn't influence it's parent container's height.
Set overflow: hidden on the parent (.commentbox) or use one of the other float containing techniques so that it does.
Do you really need float: right; for .count? I think text-align should be enough for the desired layout.
Since you're already using position:relative on the parent div. Try this instead:
.count {
position:absolute;
right:0;
bottom:10px;
}
Probably you have to add a clear after the "count" div.
<style type="text/css">
body{
background-color:#f3f3f3;
}
.commentbox{
background-color: #ffffff;
width: 200px;
border-color: #D1D1D1;
border-radius: 4px;
border-style: solid;
border-width: 1px;
padding-bottom: 9px;
padding-left: 9px;
padding-right: 9px;
padding-top: 9px;
position:relative;
}
.count{
float:right;
text-align:right;
}
</style>
<div class="commentbox">
<div class="title">Some several lines long long long long content text goes here
</div>
<div class="count">123<br>456</div>
<div style="clear: both"></div>
</div>