How can I line up my CSS vertically within an outer DIV? - html

I have the following:
<div class="outer" style="margin-bottom: 10px">
<div class="dialog-float">
<select name="AccountID" id="AccountID">
...
</select>
</div>
<div class="dialog-float">
Create
</div>
<div class="dialog-float">
<label for="htmlEdit">Html Editor</label>
</div>
</div>
What I would like to do is:
a) Have a 10px margin below the class "outer". Right now it seems like that DIV does not have any height and the margin doesn't appear properly.
b) Have all the "dialog-float" classes line up vertically.
Can someone give me some suggestions on what I am doing wrong.

add the following to your css file
.clear_cont, .cc {
min-height: 1px;
}
.clear_cont:after, .cc:after {
clear: both !important;
content: ".";
display: block;
font-size: 0;
height: 0;
visibility: hidden;
}
//ok now IE6 doen't have min-height property here is a hack
* html .clear_cont, * html .cc{
height: 1px;
}
Whenever you need to fix this kind of problems add "cc" or "clear_cont" class to the container DIV
<div class="outer cc" style="margin-bottom: 10px">

Add a wrapping element for this solution to work:
<div class="outer" style="margin-bottom: 10px">
<div class="inner_wrapper">
<!-- ... -->
</div>
</div>
Sample
http://jsfiddle.net/dbjPV/2/
CSS
.outer {
border: 1px solid #ff0000; /* Show element size */
margin-bottom: 10px;
height: 200px;
display: table;
overflow: hidden
/* Include in separate IE CSS-file */
/*position: relative;*/
}
.inner_wrapper {
display: table-cell;
vertical-align: middle;
/* Include in separate IE CSS-file */
/*position: absolute;
top: 50%*/
}
.dialog-float {
border: 1px solid #00aa00; /* Show element size */
/* Include in separate IE CSS-file */
/*position: relative;
top: -50%*/
}

If it was me i would use the following css:
.outer{
margin-bottom: 10px;
min-height: 100px;
width: [some width];
}
this should do it.
The outer box would be a minimal width of 100px and grow with the content.
and always have 10px margin at the bottom.
Also the width is in my opinion always best to set.
Then you always know how big it is.
And dont forget that a div is a block element, so when placed like you did it will always be shown verticle.
A tip:
I never (at least try to never) use a clear div.
I think it is better to declare a width for a floating div so that it will fit the container div perfectly and other divs wont get both sides.
if you have them as floats than you could use the display: block css for it, but i'm not sure if it overules the float.

Related

Displaying three elements inside a <div> in a same line

This is the html code:
<div class="produto_title">
<h2 th:text="${produto.name}"></h2>
Baixar
Comprar <span th:text="${produto.preco}"></span>
</div>
Could anyone give me a hint how to place the three items inside .produto_title in a same line (h2 floating at left and the two a floating at right).
Also, h2 has a border around item and the a is displayed like a button; I want add a line behind crossing all the "line" formed by this three elements, like this:
jsfiddle: https://jsfiddle.net/klebermo/sf7a6fnj/5/
ps.: also, how let the content of tag <span> inside the button, like the text?
An hr is a block element that's essentially just a line with a border.
I'd recommend sticking one of those at the top of the container and giving it a negative margin that vertically centers it in the parent. position: absolute is more trouble than it's worth.
https://jsfiddle.net/JackHasaKeyboard/0juqg4j7/
As for aligning the elements to the left and the right, I'll let you figure that out. There's many ways to accomplish it, the simplest way being with float.
I would look at twitter's bootstrap, specifically the row and col components.
You could do
<div class="row">
<div class="col-md-4">
// something here
</div>
<div class="col-md-4">
// something here
</div>
<div class="col-md-4">
// something here
</div>
</div>
This will all be displayed on the same line, splitting the row into equal thirds
btns{
height: auto; //Fix the span not being in the element
margin-top: 20px; //line everything up with the top of the heading element.
}
As for the line you can make a div and give it a absolute position (remember to give parent a relative position) and then position it accordingly.
.parent{
position: relative;
width: 100%;
}
.line{
height: 4px;
background-color: #000;
position: absolute;
z-index: -1;
top: 50%;
width: 100%;
}
This is a very bare-bones answer but it will be a start for you to go off.
For the first question, you can do that easily by manipulating margin or vertical-align properties. For example, if you put margin: 30px 5px; on your btn elements, it would be on the same line-ish.
Secondly, the <span> problem: if you set fixed width: 60px; of element (in your case .btn_comprar), text would either overflow from button to the right or bottom. Try setting width: 90px; or more on button elements, or height: auto; if you need it to be fixed.
Updated fiddle
First of all, you can't set a fixed width on a button if you want the text to not wrap. I recommend leaving the buttons at a width: auto and using padding to control the spacing around the text. I'd also bundle the styles for both button selectors, as they're exactly the same
Secondly, the only way (I know of) to get items to vertically align while they're float: right is by manually pushing them down, so I recommend making your buttons position: relative and setting a top: 25px;
/* Bundled both buttons together as they share the same styles */
.btn_free,
.btn_comprar {
float: right;
/* width: 60px; Removing this to allow the text to set the button width */
/* height: 20px; Removing this to let the line-height set the button height */
background: silver;
border-radius: 5px;
padding: 1px 15px;
box-shadow: 1px 1px 1px #000;
/* display: block; Removing this as floats are automatically display: block; */
/* text-align: center; Removing this since the text is already setting width */
background-image: linear-gradient(to bottom, #f4f5f5, #dfdddd);
font-family: arial;
font-size: 12px;
line-height:20px;
position: relative; /* Pushing buttons down a bit */
top: 25px;
margin: 0 10px; /* Spacing buttons out */
}
.btn_free:hover,
.btn_comprar:hover{
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
}
Thirdly, remember to use a clearfix so the .produto_title container maintains height!
.produto_title:after {
content: "";
display: table;
clear: both;
}
Lastly, rather than using another div to make the line, I'd use the :before psuedo-element on .produto-title (can't use :after if you're also doing a clearfix).
.produto_title:before {
content: '';
height: 1px;
background: #000;
width: 100%;
position: absolute;
left: 0;
top: 50%;
display: block;
}
Here's a working demo:
https://jsfiddle.net/zcqLbg4h/1/

CSS: Setting styles on form or main content not working (but inside)

I am new to CSS and hope someone can help me with this.
I have an HTML page that contains an HTML form with the below structure.
Everything works fine so far but for some reason I am unable to apply certain styles to the "main" section or the form, esp. a border or a background-color style.
I tried adding some of the following to both the section and the form and both using IDs and classes, with no effect - even if I add !important:
border: 1px solid #000;
background-color: #ccc;
The only thing I am able to set on the above is a margin which works.
However, if I set border or background styles to the inner divs (e.g. for class "frmLine") it works just fine so my guess is that for some reason the inner styles are overlapping any styles applied to the parenting form and section.
Can someone tell me how I can resolve this ?
Could the reason be that there is no margin or padding between the parenting section / form and the inner divs ?
My HTML:
<div class="wrapper">
<?php require_once("includes/menu.php"); ?>
<section id="main">
<form id="frmRequest">
<div class="col-12">
<span class="frmTitle">Some title</span>
</div>
<div class="col-12 frmLine">
<div class="col-3 frmCaption">Some caption</div>
<div class="col-9">Some form field</div>
</div>
<div class="col-12 frmLine">
<!-- ... -->
My CSS (relevant styles):
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
}
.wrapper {
height: auto;
margin: 0 auto -140px;
min-height: 100%;
overflow: auto;
padding: 0 0 140px;
}
#main {
border: 1px solid #000; /* This is what is not working ! */
margin-left: 8.33%;
margin-right: 8.33%;
}
.frmCaption {
font-weight: bold;
}
.frmField {
display: block;
width: 100%;
}
.frmLine {
margin: 0;
padding: 0;
}
Update:
I did some further testing which showed this is caused by the CSS. I then removed all styles and re-added them step by step which showed that the issue is caused by my grid styles (see below), specifically by the floating styles on them. As soon as I add these styles the border around the main section disappears resp. gets moved above the section - can someone tell me how I can fix this ?
/* ... */
.col-12 {
width: 100%;
}
[class*="col-"] {
float: left;
padding: 15px;
}
.row:after {
clear: both;
content: '';
display: block;
}
Many thanks in advance,
Mike
Somehow, the default value for the overflow of (most of the, if not all) block level elements (being visible) causes the overflowing elements to be visible, but the element itself to not adjust its own size. By setting the overflow to hidden the element does adjust its own size.
#main {
border: 1px solid #000;
margin-left: 8.33%;
margin-right: 8.33%;
overflow: hidden;
}

Firefox removes background when floating elements

I have a wrapper div in my css that covers most of my viewport. I have a background color and inside that div, I have two more divs floated right and left respectively.
When I'm with Chrome, I can see the background code perfectly, but when I'm with Firefox, the background color is not seen. Here are some screenshots:
On Chrome:
On Firefox
And here's the code.
My HTML:
<div id="wrapper">
<div id="asd"></div>
<div id="perejavi">
<p id="pere">Pere</p>
<p id="and">&</p>
<p id="javi">Javi</p>
</div>
<div id="web">
<p id="programmer">Programación</p>
<p id="programmer2">diseño web</p>
</div>
My CSS:
#wrapper {
background-color: #00CE6B;
height: auto;
min-height: 93%;
width: 85%;
color: #ffffff;
margin: 0 auto;
}
#perejavi {
margin: 0;
font-size: 9.1em;
float: left;
padding-bottom: 0;
height: 60%;
width: 50%;
}
#web {
margin: 0;
width: 50%;
height: 60%;
float: right;
}
Why is this happening? Hope you can help!;)
Floats remove the HTML element from the normal document flow and can cause issues like this. There are a couple ways to handle floats but i'll just give you the way I prefer to do it:
in your css:
.clearfix:after {
visibility:hidden;
display:block;
font-size:0;
content:" ";
clear:both;
height:0;
}
Then in your HTML:
<div id="wrapper" class="clearfix>
This creates a pseudo element as the last child of your .clearfix div which clears the floated elements contained within the .clearfix div
note: Also, you seem to be missing your closing </div> for your wrapper div. Make sure you add that in
Further reading on floats and the strangeness they bring with them:
http://css-tricks.com/all-about-floats/

Bottom margin of last child gets hidden when overflow applies

I have a container div which has children anchored to the bottom. The problem is that when the div's overflow scrollbar appears, the bottom margin of the last child gets hidden.
Please see http://jsfiddle.net/TxEAP/3/. At first, there's a correct margin underneath the 1 div. Clicking "append one" so that the scrollbar eventually appears makes the last div not have a bottom margin anymore. Opening DevTools shows that the margin of that last child is there, but it is outside of the container's viewport, even when scrolling completely to the bottom.
How can this be solved? It would suffice to get this working in Google Chrome.
HTML:
<div class="main">
<div class="container">
<div class="item">1</div>
<!-- several of these .item divs -->
</div>
</div>
CSS:
.main {
height: 200px;
overflow-y: scroll;
position: relative;
border: 1px solid black;
}
.container {
width: 100%;
max-height: 100%;
position: absolute;
bottom: 0;
}
.item {
padding: 20px;
margin: 15px;
border: 1px solid black;
}​
Here's my final solution using flexbox. It's supported well enough on Chrome despite all -webkit- prefixes. Basically, the idea is to have a dummy element that, in case of no overflow, fills up the space of the container starting from the top (so that the real children are anchored to the bottom); in case of overflow, it is hidden automatically because of height: 0. It does not suffer from the margin issue, and it does not collapse margins.
http://jsfiddle.net/mCYLm/1/
HTML:
<div class="main">
<div class="gap-filler"></div>
<div class="item">foo</div>
<!-- more `div.item`s -->
</div>
CSS:
div.main {
display: -webkit-box;
-webkit-box-orient: vertical;
height: 200px;
overflow-y: scroll;
}
div.main div.gap-filler {
-webkit-box-flex: 1;
height: 0;
}
div.main div.item {
border: 1px solid black;
margin: 20px;
padding: 20px;
}​
Edit: This was a solution without flexbox, but it had selection issues.
A solution that eventually worked was the following: http://jsfiddle.net/TxEAP/7/. This appends hidden "content" which makes Chrome not hide the margin of the last .item div.
.container:after {
content: "";
font-size: 0;
display: block;
height: 1px;
}
Edit: The following only works if display: inline-block is possible.
Finally I found a solution. If all .items have display: inline-block except the first one, then the margin does not get hidden.
http://jsfiddle.net/TxEAP/5/
.item:not(:first-child) {
display: inline-block;
/* attempt at getting `width: auto` like `display: block` has */
width: -webkit-calc(100% - 2 * 15px);
box-sizing: border-box;
}
If you just move the overflow-y: scroll; from .main. to .container class then the margin is preserved. The only drawback is for less than 3 items (for the given container height) you get a small scrollbar placeholder, instead of a full height one.
Removing max-height:100% on the container seems to fix it for my test in Chrome 21.
Moving the properties so that the overflow is on the container, preserves the margin/padding for an element added to the end that results in the scrollbar appearing.
.main {
height: 200px;
border: 1px solid black;
}
.container {
width: 100%;
height: 100%;
overflow-y: scroll;
}

IE 11: Element with min-height: 100vh will cause scrollbar

I already read a lot of posts about IE 11 with display: flex and min-height, but didn't find my answer.
I have a normal <div> with a min-height: 100vh;. In that <div> I have another element with a margin-bottom: 5px;. Now the whole outer <div> has a scrollbar and a transparent border at the bottom of 5px.
When I increase the margin, the gap at the bottom will increase the same.
Example:
<div class="layout">
<div class="panel">
Some content
</div>
</div>
body {
margin: 0;
}
.layout {
min-height: 100vh;
background: orange;
}
.panel {
margin-bottom: 40px;
background: white;
border-radius: 5px;
padding: 5px;
}
<div class="layout">
<div class="panel">
Panel
</div>
</div>
Now I made the code snipper, I see it also going wrong in Chrome.
I hope you understand me, but if you need more info please ask. I hope to find an answer!
Thank you,
Ronald.
Your issue is because of margin collapsing and it could be fixed in different ways.
Depending on your case, easiest is to use overflow: hidden for .layout:
.layout {
min-height: 100vh;
background: orange;
overflow: hidden;
}
You could also use padding-bottom on .layout instead of margin-bottom on .panel to avoid the issue with margins.
Another option could be clearfixing the .layout like so:
.layout:before,
.layout:after {
content: ' ';
display: table;
}