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

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/

Related

clearing after a float left creates a void between two divs

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;
}

Unwanted top padding inside div element

I have such HTML:
<div class="select-wrapper">
<select name="type">
<option value="test">test</option>
</select>
</div>
And css:
#search-box .select-wrapper {
display: inline-block;
padding: 6px;
background: #eaeced;
}
#search-box select {
font-size: 11px;
margin: 0px;
padding: 0px;
padding-left: 8px;
border: 1px solid #a6a6a6;
-webkit-border-radius:2px;
-moz-border-radius:2px;
-border-radius:2px;
}
However somehow wrappers top padding is bigger then bottom one:
When I highlight wrapper in inspector, it looks like that:
As you can see, there is some space between selects top border and top of the inner content of a wrapping div.
Why is it there ? How can I force both top and bottom spacing to be equal ? I need that wrapping element for additional outer border.
CSS outline and box-shadow isn't enough, because I need to specify corner rounding. Shadows rounding is relative to border rounding and its too big for me, so I need to make this outer border with DIV-wrapper with my own, small border-radius.
The height of the select is smaller than the inner height of .select-wrapper.
Add display: block and height: 100% to the select so it will take the full height and width of it's parent.
DEMO.
You could change the font size to 100%, although it changes the font size from what you originally coded, it does sort the padding outside of the select box.
#search-box select { font-size:100%; }
http://jsfiddle.net/r2wpxvw2/
http://jsfiddle.net/sj1aL1o8/
.select-wrapper {
display: inline-block;
padding: 6px;
background: #eaeced;
line-height: 16px;
}

Children divs not being sized equally

I am trying to display a four grid with different items for my web, however now all children divs have the same size:
<div class="container">
<div class="grid4">
<input type="submit" name="input1" value="input1"/>
</div>
<div class="grid4">
<input type="submit" name="input2" value="input2"/>
</div>
<div class="grid4">
<input type="submit" name="input3" value="input3"/>
</div>
<div class="grid4 no-border">
<input type="submit" name="input4" value="input4"/>
</div>
</div>
CSS:
.container {
width: 100%;
margin: 30px 0 30px 0;
}
.grid4 {
width: 25%;
padding: 20px;
border-right: 2px solid rgba(40,40,40,0.8);
display: inline;
}
.no-border {
border: none;
}
I tested it in jsfiddle and they indeed have the same size:
http://jsfiddle.net/ME7k8/
However, you can clearly see that the last chil div is smaller:
Why?! Any help?
edit In case it is too small in the image:
elemento {
}
.grid4 {
width: 25%;
padding: 20px;
border-right: 2px solid rgba(40, 40, 40, 0.8);
display: inline;
}
div {
text-align: left;
}
body, div, td {
font-family: 'Noto Sans',Arial,Helvetica,sans-serif;
font-size: 12px;
color: #666;
}
Inherited from body
body {
text-align: center;
}
edit I checked again with the browser inspector and I can see that the first div is about 50% of the .container div. It has exactly the same css properties than the rest of the divs.
The 3 first divs are wider than the last due to:
1. They have the CSS display:inline (meaning their width gets effected by white-spaces, line breaks etc).
2. The last div has no border unlike the first 3.
Give them identical width
So what you need to do to make sure all 4 divs have the same width is removing all white-space between the submit buttons and their parent divs, and also add padding-right:22px; to the last div (if you want the 4 divs exactly identical wide).
jsFiddle demo.
I use your jdFiddle and put a blue background to see the difference, all divs have the same size, however, I declare a size for the container
.container {
width: 1200px;
background-color: tomato;
}
and re adjust the size of the divs with the grid4 attribute
.grid4 {
display: block;
float: left;
width: 20%;
padding: 2.3%;
border-right: 0.2% solid rgba(40,40,40,0.8);
display: inline;
background-color: blue;
}
when you put padding to each one (20px) that pixels are added to the "25%" of total size.. so this make it a bigger element, and probably that's the difference you couldn't see... with that on mind, may be you could solve your problem... Check This...
Your last element has no border, while the others probably do.
Borders take up space, as do margin and padding.
Check out the box model by pressing ctrl + shift + i in your browser and hovering over an Also,
http://www.w3schools.com/css/css_boxmodel.asp
From inside to outside, there is padding, borderin, margin, outline.
The first three add size to your "box model". Outline does not.
If you specify a width or height, any padding, border, or margin will make your element not that specified width or height anymore. Needless to say, this makes for all kinds of headaches
One solution around this is to use box-sizing: border-box;
This makes specified padding and border actually be your specified width or height. Margin will still add to the dimension, which makes sense if you think about it.
http://css-tricks.com/box-sizing/
Also be sure to take care of prefixes so that it works on all browsers.
You may not want to deal with this at this point, but check out the example in the last link, as well as caniuse.com.
If you don't want to handle cross browser support manually, there is a library to automatically post-process your CSS to add the appropriate prefixes. This uses the caniuse.com database so as long as you update this library, your post-processed css file will have the up to date prefixes without you having to worry about keeping up with browser versions or individual css feature deprecations.
https://github.com/ai/autoprefixer
article on auto prefixing
http://css-tricks.com/autoprefixer/

How to make a h3 tag occupy less space within a div?

Here is the HTML code:
<div id="homepage_boxes_holder">
<div class="homepage_boxes">
<h3 class="box_heading">Test</h3>
</div>
<div class="homepage_boxes">
<h3 class="box_heading">Test</h3>
</div>
<div class="homepage_boxes">
<h3 class="box_heading">Test</h3>
</div>
</div>
Here is the CSS:
.homepage_boxes{
width: 300px;
height: 200px;
float: left;
margin-top: 100px;
margin-right: 80px;
margin-left: 150px;
line-height: 10;
}
.box_heading{
text-align:center;
font-family: BebasNeue;
font-size: 30px;
padding: 0;
margin: 0;
border: 1px solid black;
}
For some reason, the h3 is occupying a huge amount of space in the div (it looks as though the padding is huge to me but that can't be since I have set it to 0). I have put a border on the .box_heading for demonstrative purposes. Image is here:
Link to image: http://imgur.com/vDs1KYs -The blue is the div border, and the black is the H3 border.
EDIT: If possible, I would also like the heading to be centred on the div, rather than pushing outside the boundaries.
That's because you added a line-height: 10; to the parent element, which increases the height of each line 10 times.
Just remove that And it works.
Working fiddle
Update
To keep the h3 element at the middle of its parent, you could set a line-height as the parent's height to that element (in this case you could apply this CSS declaration to both parent and h3 element).
.homepage_boxes {
/* ... */
line-height: 200px; /* as the same of the parent's height */
}
Udpated Fiddle
Another option is setting the same padding to the top and bottom of the parent element without setting a fixed height, which makes the children at the middle.
JSFiddle Demo
just add line-height: 1em; to the .box-heading class description.
The line-height attribute alters the 'ascent' or 'ascender height' of the font, making it that high.
Just add this to the h3:
line-height: 1em;
http://jsfiddle.net/B7ZTw/
To center the text both ways please add the following to h3:
position: relative;
top: 50%;
margin-top: -0.5em;
What you do here is: set the h3 to be 50% from the top (so the middle), but then substract half of the element height so it doesn't start at 50% but is centered there.
See here: http://jsfiddle.net/B7ZTw/2/
You can put static height for if it possible

Fieldset height 100% of its parent

I have 2 fieldsets wrapped in a <div>, let's call them blah1 and blah2.
blah2 can grow as the max of its <div> parent, but I need blah1 to have the same % height of his sibling blah2
This is my demo
The answer was kinda weird, but I make it work, looks like the margin was making a mess here, if I set it to margin: 0 5px; then I can make both fieldsets to have the same height as their parent
Part of the css markup:
fieldset
{
border: 1px solid #ddd;
padding: 0 1.4em 1.4em 1.4em;
margin: 0 0 1.5em 0;
border-radius: 8px;
margin: 0 5px;
height: 100%;
}
legend
{
font-size: 1.2em;
font-weight: bold;
}
.blahContainer{
width: 100%;
height: 100%;
}
.blahColumna1{
width: 70%;
height: 100%;
}
If you wanna see the final demo, click here.
Hope it can help somebody else...
Try wrapping both <fieldset>s in a single container <div> that isn't fixed height -- it will neatly enclose blah2, and will grow up to the max size of your <div> parent. Set blah1's height to be 100% of the containing <div>.
I was able to get it working by adding padding-bottom to the fieldset for Chrome only. This balances out some of the extra height %. It's nice in that it works (relatively consistently) even when resizing.
if (navigator.userAgent.toLowerCase().indexOf('chrome')) { // Replace this with your preferred way of checking userAgent
$('fieldset').css('padding-bottom', '4%'); // Exact percent may vary
}
Just as a note, I found this to be an issue in at least IE8 - IE11 as well, so the fix can be applied to IE.