clearing after a float left creates a void between two divs - html

I have the following part of my html
<div class="header">
<div class="header-bar">
<div class="pull-left">
<div class="title">Ci models database</div>
</div>
</div>
<div class="clear-both"></div>
<ol class=breadcrumb>
<li class="active">All models</li>
</ol>
</div>
the css(breadcrumb and active classes are bootstrap)
.header-bar {
border: None;
background-color: #66CCFF;
min-height:30px;
}
.title {
padding: 5px 5px 10px 5px;
color: white;
font-size: large;
}
.clear-both{
clear:both;
}
But between header-bar and breadcrumb html added a white space(see bootply). How can I remove this white space, since no padding and margin can be found between to divs.

The problem is that the calculated height of the internal .title div is greater than the calculated height of the container .header-bar. Properties like height, min-height, border, padding can directly effect heights, whereas properties like display, box-sizing and position can all indirectly effect height.
The result is the internal .title div pushes down the next div in the flow by 10px.
CSS has no rules that say a div must contain it's children in height and stop them from effecting other divs, even when height is directly defined. We need to tell it exactly how it should behave when things are rendered.
There are several ways to fix this:
http://www.bootply.com/Qa1ME2M2uk - use overflow: hidden; on the parent. Overflow is a css property which is used how to control what happens when child elements are larger than their parents. It's worth noting that depending on other properties overflow won't necessarily render itself in a way that disrupts layout.
http://www.bootply.com/ssq3EAzeyk - set explicit heights to take strict control over the dimensions of the elements. This might be the best option for a header bar.
http://www.bootply.com/yeodYRLLJk - set a greater min-height on the parent, one which will definitely contain the child. This is useful if your padding is for alignment purposes - setting min-height: 40px; in the example does this.
http://www.bootply.com/GznfJxUWUF - remove the padding that is making the element calculate as taller (as mentioned in another answer).

Apostolos, the white space is coming from the .titleclass.
The bottom padding of 10px.
Zero this and the white space will go.
.title {
padding: 5px 5px 0px 5px;

you will have to add a float: left to both parent containers (.header-bar and breadcrumb) otherwise the clear won't affect anything. furthermore you will have to give both containers width: 100%
.header-bar {
border: None;
background-color: #66CCFF;
min-height:30px;
width: 100%;
float: left;
}
.breadcrumb {
width: 100%;
float: left;
}
.title {
padding: 5px 5px 10px 5px;
color: white;
font-size: large;
}
.clear-both{
clear:both;
}

Related

horizontal formatting for block-level element

In the "CSS The definitive Guide", the author said "The values of these seven properties must add up to the width of the element’s containing block, which is usually the value of width for a block element’s parent". But In the following, the child element is wider than the parent.
//html
<div class="wrapper">
<div class="content-main">
<div class="main">This is main</div>
</div>
</div>
// style
.wrapper {
width: 500px;
padding: 30px 0;
border: 1px solid #0066cc;
}
.content-main {
padding: 0 20px;
border: 2px solid #00CC33;
}
.main {
width: 500px;
border: 1px solid #f00;
}
So I have two quesions:
What does the author mean for the "seven properties must add up to the width of the element’s containing block".
Why in my example, the element will stick out the parent.
in the edit version, the seven properties add up to the width of the element' containing block seems work well. Why the equation not apply to my example?
EDIT VERSION
p.wide width is 438px, the author calculate as following
10px(left margin) + 1(left border) + 0 + 438px + 0 + 1(right border) – 50px(right margin) = 400px(parent width)
// HTML
<div>
<p class="wide">A paragraph</p>
<p>Another paragraph</p>
</div>
// CSS
div {width: 400px; border: 3px solid black;}
p.wide {
margin-left: 10px; width: auto; margin-right: -50px;
border: 1px solid #f00;
}
What does the author mean for the "seven properties must add up to the width of the element’s containing block".
He is teaching you CSS Box Model, here, you are using div elements which are block level in nature, block level means they take up entire horizontal space by default, unlike span or i or b tags, which are inline elements.
So when you use padding or border they are added outside of the element and not inside. So for example you have an element of say 100x100 in dimension, and you add a padding like
element {
width: 100px;
height: 100px;
padding: 10px;
}
So in the above case, your element will be 120x120 in total, because it will add up 10px of padding on all four size of your element.
Explaining padding syntax
You have two different padding syntax, which are as follows...
padding: 30px 0; in .wrapper and padding: 0 20px; in .content-main so these aren't the same.
Both the above syntax are nothing but short hand syntax of padding ... The complete version looks like...
padding: 5px 10px 15px 20px; /*Nothing to do with your code, this is just a demo */
So in the above example, you have to go clock wise, so 5px is nothing but padding-top: 5px;, then comes 10px which is right, next is bottom and the last 20px is padding-left.
So what when it's just two parameters defined, that means...
padding: 0 20px;
--^---^---
top bottom/left right
So, top and bottom are set to 0 here and right and left to 20px respectively...
Explaining the CSS
Note: None of the element has the height set by you, so the screens
you see ahead which I've attached are computed. So ignore height in them
completely.
.wrapper {
width: 500px;
padding: 30px 0;
border: 1px solid #0066cc;
}
Here, your element is 502px wide, so why? As I said that border will add on all four sides of the element, and hence it will add 1px on all four sides but your padding is applied to top and bottom only. It's better to use tools like Firebug which will show you graphical presentation of what's going on behind the scenes.
Coming to the second snippet which has the following syntax
.content-main {
padding: 0 20px;
border: 2px solid #00CC33;
}
Here, it is now adding 2px border to your element but, the padding is now applied to left and right and nothing for top and bottom so now the computation will be
Coming to the last snippet which is
.main {
width: 500px;
border: 1px solid #f00;
}
Here, just border is applied, but why it goes out? In technical words, why it overflows? Because you have width defined. So since you have padding set for the parent element, which is padding: 0 20px;, so it will nudge the child element by 20px from the left side. I'll attach a screen of Firebug to show you why it is nudged....
Why in my example, the element will stick out the parent.
Because you are defining width of 500px to your .main div
Demo (What happens when you take out the width)
The default box model is known as content-box
This can be altered by defining a new CSS3 introduced property called box-sizing set to border-box which will alter your box-model in such a way that it will count the padding and border inside the element instead of outside

Can't get fields to fit in one row using CSS

EDIT: To clarify, I want all 4 elements in a single row, fit 100%. I know I can change the % but I want them flush with the edge of the div
I'm trying to make my labels and input fields fit in a single row and be % based to resize when required.
Problem is, I can't get them to all fit in one row - I think it has something to do with the padding or margins somewhere but can't figure it out.
I've made a JSfiddle here http://jsfiddle.net/ZxRAu/
And here's the relevant CSS
.generalcontainer {
width:65%;
margin:5%;
height:600px;
float:left;
background-color: #CCCCCC;
border: 0px;
}
.generalcontainer > span {
font-family: Calibri;
font-size: 14px;
padding: 4px;
margin: 0px;
.generalcontainer > span.label {
color: #000000;
font-weight: normal;
display: inline-block;
width:25%;
}
.smallentryfield {
color: #000000;
font-weight: bold;
width: 25%;
padding: 4px;
margin: 0px;
}
select.smallentryfield {
box-sizing: content-box;
}
There can be many reasons why the 25% is not working exactly as you expect. One of the reasons is, you have an element that has its width set in percentile but the padding is in pixels. Another reason could be the input elements disregarding the width applied on them because of their borders, margins and padding. While you can set all of these to 0, you ll still face some challenges when you set the display to inline-block coz that will add some space at the bottom of the element in such a way that it will show invisible margin after the element unless of course you use some combination of vertical-align set to top and font-size or line-height.
A possible solution is to have a container element for each of the form elements and set the width of the container to 25% and set it to display: block along with float: left. Then you can set the inner element's widths to 100% and remove their padding and margins.
For example:
<div class="container">
<label>First name</label>
</div>
<div class="container">
<select>
<option selected="selected" value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
</select>
</div>
<div class="container">
<span>First name</span>
</div>
<div class="container">
<input type="text"></input>
</div>
I m calling it 'container' just as an example. You can change it to whatever. You can set the CSS something for the container something like this:
div.container {
color: #000000;
font-weight: normal;
display: block; /*you need this only if you use a span (as per your example)*/
width:25%;
float: left;
padding: 0px;
margin: 0px;
}
And finally for the CSS for the inner elements, you could do something like:
div.container input[type=text], div.container select {
width: 100%;
padding: 0px;
margin: 0px;
border: 0px;
}
This is just a suggestion as to what direction you can take to solve this issue. Here s a jsfiddle to start playing with this concept in your particular scenario:
http://jsfiddle.net/f8dUC/3/
Hope that helps!
I guess, it just required a <br> tag there, look at the example:
http://jsfiddle.net/afzaal_ahmad_zeeshan/ZxRAu/1/
Or if you want this:
http://jsfiddle.net/afzaal_ahmad_zeeshan/ZxRAu/2/
I just lessen down the width to 20%. And it works :)
There were margins and paddings, which were causing the problem. Due to which 25% for four elements would be 100% and the remaining margins. This way the row was broken and formed 2 rows.
So adding 20% removed the issue. You can have the look in the fiddles.
Started again and used Walmiks guidance for containers within the data. Also set % based padding to get it perfect.
I.E.
form {
width:98%;
line-height:24px;
padding: 1% 1% 0 1%;
}
Check out the JSfiddle below
http://jsfiddle.net/FYdhz/

Wrapping a DIV around content and keeping it centered

I have a problem concerning CSS and HTML.
I'm trying to wrap a DIV around another element (an UL in this case) and having it wrap around it and at the same time keeping both centered. As an added bonus I can't set a specific width since the width of the content inside the wrapping DIV have to be dynamic (since this is basically a template).
I've tried floating, and that works as far as wrapping goes, but then the thing ends up either to the right or to the left.
I'm going a bit crazy over this, and google is no help!
Thanks in advance!
UPDATE:
Sorry about not including code or images. This is what I'm trying to do illustrated with images:
One state of the UL width
Another state of the width
The wrapping DIV can't stretch the full width of the container. It has to wrap around the UL.
The dark grey is the DIV around the UL. I need the DIV to wrap around the UL (which has a horizontal layout) no matter the width of the content, since like I said above, the content of the UL is going to by different from time to time. The text in the LIs are going to change.
I also need it to be centered. I've made it work with float left and float right, but I need it to be centered.
This is the code I'm currently using for the container DIV and the UL and LI elements:
#container{
height: 100px;
width: 500px;
font-size: 14px;
color: #grey;
display: table-cell;
vertical-align: middle;
}
#container ul{
text-transform: uppercase;
text-align: center;
font-weight: bold;
}
#container li{
background: url(checkmark.png) center left no-repeat;
display: inline;
padding-left: 20px;
margin-right: 5px;
}
#container li:last-child{
margin-right: 0;
}
UPDATED
I got it. Is it this you were looking for?? http://jsfiddle.net/vZNLJ/20/
#wrapper {
background: #ccc;
margin: 0 auto; /* to make the div center align to the browser */
padding: 20px;
width: 500px; /* set it to anything */
text-align: center;
}
#wrapper ul {
background: #aaa;
padding: 10px;
display: inline-block;
}
#wrapper ul li {
color: #fff;
display: inline-block;
margin: 0 20px 0 0;
}
#wrapper ul li:last-child {
color: #fff;
display: inline-block;
margin: 0;
}
<div id="wrapper">
<ul>
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</div>
This is an old post, but what you can do now is:
<div style="display: flex; justify-content: center;">
<div style="display: inline-block;">
<input type="button" value="Example Button" />
</div>
</div>
The problem isn't wrapping the DIV around the content, but getting the content to state it's actual size, therefore pushing the DIV boundaries out. There are several things that need to be considered when tackling this issue. Not just from an existing UL or LI tag, but a DIV within a DIV.
I use custom tags to help describe layouts cleaner. Custom tags are DIV tags, thus their properties must be manipulated by CSS in order to get the proper behavior.
<layout-linear horizontal>
<control-label>Label 1</control-label>
<control-label>Label 2</control-label>
<control-label>Label 3</control-label>
<control-label>Label 4</control-label>
<control-label>Label 5</control-label>
</layout-linear>
This layout suggests that the contents .. the control-label(s) tags .. will be display in a horizontal row. To get the border for the layout-linear tag to wrap around the content of the control-label tags, there are several things to do:
layout-linear[horizontal]
{
display : block;
box-sizing: border-box;
border : 1px solid black;
padding : 1px 1px 1px 1px;
white-space: nowrap;
width : 100%;
clear : both;
text-align : center;
}
First, the box-sizing property must be set to border-box. This will force the linear-layout (DIV) tag to wrap around content. Padding, Border, Margin will insure that an empty DIV tag displays. Other tricks to make an empty DIV tag display are to use or :after { content:.; visibility: hidden; }.
If you don't want the control-label tags to wrap, adding white-space : nowrap.
I will discuss text-align when I discuss the float property of the control-label tag.
The next part requires the inner DIV tags (control-labels) to properly specify their box-sizing type and borders.
control-label
{
display : inline-block;
/* float : left; */
box-sizing: border-box;
border : 1px solid black;
margin : 5px 5px 5px 5px;
padding : 5px 5px 5px 5px;
}
Display : inline-block, causes the control-label tags to flow left to right. Display : Block, will cause the tags to stack up vertically.
Float is commented out specifically to draw your attention to the fact that float will cause the layout-linear tag shrink to its smallest box size, based on the margins, padding, and border.
To have the control-labels flow right to left, add text-align : right to the layout-linear tag. Or in this specific case, set text-align : center.
Again, box-sizing is used to tell the control-label (DIV) tag to wrap around it's content completely. Not just the text, but the edges of the box as drawn by the border, padding and margin settings.
This arrangement of CSS properties is what causes the outer and inner boxes to be rendered properly, or as expected.
Happy Coding.
You didn't supply code, but take a look at this fiddle I just setup, which might help:
http://jsfiddle.net/qXDJr/
Please let me know if I'm misunderstanding what you mean. Example code will always help for future reference.
This might help.
If you cant set the width you can just add align='center' in the div wrapping ul
<div align="center">
<ul>
<li>MenuItem</li>
<li>MenuItem</li>
<li>MenuItem</li>
</ul>
</div>

Should the spacing between parent and child containers belong to parent or child?

I'm struggling to determine where in my CSS the padding/margins should go to keep seperation between parent and child containers. For example, if you have have a parent div with two nested children and you need to have even 10px spacing between the children and also have the children be spaced 10px from the parent; then would you add padding to parent div {padding:10px} and then just add 10px of margin between the children? Or would you leave the parent at 0 padding and have the children define what separation they need from each other and also their parents?
Here's the original fiddle showing both examples, and a snippet showing the same thing
.parent1 { /*spaces itself from its children*/
display: inline-block;
padding: 10px;
font-size: 0;
border: 1px solid red;
}
.child1 {
display: inline-block;
margin-right: 10px;
font-size: 12pt;
border: 1px solid blue;
}
.child2 {
display: inline-block;
font-size: 12pt;
border: 1px solid green;
}
.parent2 { /*has not spacing from its children*/
display: inline-block;
font-size: 0;
border: 1px solid red;
}
.child3 {
display: inline-block;
margin: 10px;
font-size: 12pt;
border: 1px solid blue;
}
.child4 {
display: inline-block;
margin-right: 10px;
font-size: 12pt;
border: 1px solid green;
}
<div class="parent1">
<div class="child1">child1</div>
<div class="child2">child2</div>
</div>
<br/>
<br/>
<div class="parent2">
<div class="child3">child3</div>
<div class="child4">child4</div>
</div>
For me it depends entirely on the semantic meaning of the nodes in question -- what is the parent, what are the children?
Does the parent "want" space within it to keep its content away from its borders (padding)? Or do the children "want" distance between and amongst themselves and their parent (margin)?
The answers to those questions are based on the purpose each node serves (I'm big into semantic markup and avoid presentational markup)
In your case, without knowing further meaning, it sounds like the children want to be 10px from everything, so I would give them margin: 10px; on all sides. The margin collapse between the two children would leave only the 10px gap there, and they'd be 10px away from everything else surrounding them.
I usually go for padding on the parent container. Then margin between the children. If I had an ul with 10 li children, for example, I'd add 10px padding to the ul, 10px margin-bottom to the li, and 0px margin-bottom to the li:last-of-type...
I don't know, it varies with the situation, and your interpretation of it, I guess. In my example, since I believe the space between the ul and the li belongs to the ul, I make it the ul's padding. The space between the lis belongs to them, so I make it their margins.
Not much of a help, huh?
The CSS margin attribute is on the outside whereas padding
is within the DIV block. I am not sure if that is what you want -
documented here:
When to use margin vs padding in CSS
I just verified and the margin attribute does work inside a nested DIV also.
So that is an alternative to padding.
IF, however, the position in the class is absolute, the padding shouldn't matter. But
if you use absolute positions, all the DIV's would have to have
absolute positions to avoid a potential conflict, and you would have
to know the client's minimum screen size (absolute may not be a good idea).
NOTE: If any of your clients are still using IE5, that browser
has a padding bug that was supposed to be fixed in IE6.
http://forums.devshed.com/css-help-116/css-padding-inside-absolute-div-70789.html
Hi all i am not a HTML expert i am learner so what i have learnt from the web industry so i am sharing on the behalf of that.
According of me Padding is the inner space of an element, and margin is the outer space of an element.
Paddings :- If you will use padding in parent container than you will have to adjust width and height of the parent container otherwise parent will add the padding in his width & height.
Margins :- If you will use margins in child container than you don't need to touch the parent container width & height because child container will start from the required area.
or see your updated fiddle example i have played with margins in child container because when i used padding in parent container i adjusted the width & height of that but when i used margins don't need to adjust the parent container see the demo :- http://jsfiddle.net/WSTv6/1/
& can read more about margin & padding

How can I force overflow: hidden to not use up my padding-right space

I have the following code:
<div style="width: 100px;
overflow: hidden;
border: 1px solid red;
background-color: #c0c0c0;
padding-right: 20px;
">
2222222222222222222222111111111111111111111111113333333333333333333</div>
(XHTML 1.0 transitional)
What happens is that the padding-right doesn't appear, it's occupied by the content, which means the overflow uses up the padding right space and only "cuts off" after the padding.
Is there any way to force the browser to overflow before the padding-right, which means my div will show with the padding right?
What I get is the first div in the following image, what i want is something like the 2nd div:
image
I have the same problem with the overflow:hidden; obeying all the padding rules, except for the right hand side. This solution works for browsers that support independent opacity.
I just changed my CSS from:
padding: 20px;
overflow: hidden;
to
padding: 20px 0 20px 20px;
border-right: solid 20px rgba(0, 0, 0, 0);
Having container divs works fine, but that effectively doubles the amount of divs on a page, which feels unnecessary.
Unfortunately, in your case this won't work so well, as you need a real border on the div.
Your best bet is to use a wrapping div and set the padding on that.
I had a similar problem that I solved by using clip instead of overflow. This allows you to specify the rectangular dimensions of the visible area of your div (W3C Recommendation). In this case, you should specify only the area within the padding to be visible.
This may not be a perfect solution for this exact case: as the div's border is outside the clipping area, that will become invisible too. I got around that by adding a wrapper div and setting the border on that, but since the inner div must be absolutely positioned for clip to apply, you would need to know and specify the height on the wrapper div.
<div style="border: 1px solid red;
height: 40px;">
<div style="position: absolute;
width: 100px;
background-color: #c0c0c0;
padding-right: 20px;
clip: rect(auto, 80px, auto, auto);">
2222222222222222222222111111111111111111111111113333333333333333333</div>
</div>
Wrap the div and apply padding to the parent
.c1 {
width: 200px;
border: 1px solid red;
background-color: #c0c0c0;
padding-right: 50px;
}
.c1 > .c1-inner {
overflow: hidden;
}
<div class="c1">
<div class="c1-inner">2222222222222222222222111111111111111111111111113333333333333333333
</div>
</div>
If you have a right-adjacent element to the one in question, put padding on its left. That way the content from the left element will flow up to but not past its margin, and the left padding on the right-adjacent element will create the desired separation. You can use this trick for a series of horizontal elements which may have content that needs to be cut off because it is too long.