Center divs under eachother - html

http://jsfiddle.net/jZLW4/702/
I want the center divs to be exactly under eachother, so in this example there would be a bigger empty space between the left and center, center and right. Is this accomplishable?
Something like this (made in mspaint):
-

Bootstrap would make things easy:
<div class="col-md-4">
<!--first column-->
<div class="pull-right">Some content
</div>
<div class="pull-right">Some content
</div>
</div>
<div class="col-md-4">
<!--second column-->
<div class="center">Some content
</div>
<div class="center">Some content
</div>
</div>
<div class="col-md-4">
<!--first column-->
<div class="pull-left">Some content
</div>
<div class="pull-left">Some content
</div>
</div>
More about the Grid-System
CSS for centering div in div:
.center{
width: 50%; /*or every other width...*/
margin: 0 auto;
}
source
If you don't want to use Bootstrap, you can do the Columns and the floating like this:
.col-md-4{
float:left;
width:300px /*or whatever width you want*/
}
.pull-left{
float: left;
}
.pull-right{
float: right;
}

This seems to possible using display: flex which has compatibility with modern browsers. If we go with javascript, then here is the code:
Codepen
function setAlign(parentClass, childCommonClass) {
var childDivs = document.getElementsByClassName(childCommonClass);
var childDivsTotalWidth = 0;
var childDivsLength = childDivs.length;
var parentElement = document.getElementsByClassName(parentClass)[0];
var parentElementWidth = parentElement.offsetWidth;
for (var i = 0; i < childDivsLength; i++) {
childDivsTotalWidth += childDivs[i].offsetWidth;
}
var remainingWidth = parentElementWidth - childDivsTotalWidth;
var gap = remainingWidth / (childDivsLength - 1);
var leftWidth = 0;
for (var j = 0; j < childDivsLength; j++) {
if (j > 0) {
leftWidth += gap + childDivs[j - 1].offsetWidth;
}
childDivs[j].style.left = leftWidth + "px";
}
}
window.onload = setAlign('row', 'box');
window.onresize = function () {
setAlign('row', 'box');
}

First you'd need some type of parent element to add some adjustments to. Then you can simply change the display type of the first level type of child element within and use no extra class names or floats or clears. With a bit more specificity on the first level children you can make it not apply text-align center to the content within the actual divs being center.
Here's a LIVE example.
This example also has a couple more css lines of bullet proofing applied to it. Adjust to hearts content. Also in this example depending browsers width etc... it will responsively center or all be on one row centered together too. Any element that wraps will stay centered.
http://jsfiddle.net/jonnyborg/epebr0cu/1/
HTML
<!-- This style should be a Conditional Stylesheet for IE7 and below. calling it up top in the HEAD area.
.my_parent_element {display:block; margin:0 auto;}
.my_parent_element div {display:inline;}
-->
<div class="my_parent_element">
<div>Content 1</div>
<div>Content 2</div>
<div>Content 3</div>
</div>
CSS
/* other than IE7 and down. All other browsers & versions firefox, chrome etc... render. */
.my_parent_element {
text-align: center;
}
.my_parent_element div {
min-width: 10px;// give it a little substance just in case, but depends on your design especially for responsive layout.
display: inline-block;
float: none;
clear: none;
}
/*
Then see my HEAD Conditional Statement for IE7 and below fix. All other versions of IE will render this perfectly without the fix.
*/

Related

How to have different behavior on element when it gets wrapped to another line

I have two inline-block elements on my header bar. The right side element is floated right, but when screen gets so narrow that it gets wrapped to another line, I'd like to center it instead.
EDIT: The first element content is user-submitted, so I don't know its width in advance.
Below I've included javascript-based solution of the behavior I'm looking for.
Is this possible to achieve by using only CSS?
$(window).bind("resize", function() {
var totalWidth = $('.container').width(),
firstWidth = $('.first').width(),
secondWidth = $('.second').width();
if (totalWidth > firstWidth + secondWidth) {
$(".container").addClass('one-line');
$(".container").removeClass('two-lines');
} else {
$(".container").addClass('two-lines');
$(".container").removeClass('one-line');
}
});
.container div {
display: inline-block;
}
.container.one-line .second-container {
float: right;
}
.container.two-lines .second-container {
display: block;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container one-line">
<div class="first">One and two lines layout: align left</div>
<div class="second-container">
<div class="second">One lne layout: align right. Two line layout: align center.</div>
</div>
</div>
/

Font-size depends on div width and height

Consider the following markup and styles:
<div>
Some text Some text Some text
</div>
div{
width: 100px;
}
How can I do that the text-content of div have a font-size's property value such that there is maximum font-size value in which text-content of div lie on one line entirely? jsFiddle
Try this:
HTML:
<div id="container">
<span id="text_container">whatever text you want</span>
</div>
CSS:
#container {
background:cyan;
width:200px;
}
#text_container {
white-space:nowrap;
}
JS:
var container = $("#container"),
text_container = $("#text_container"),
start_size = 100,
container_width = container.width();
text_container.css('font-size', start_size + 'px');
while (text_container.width() > container_width) {
text_container.css('font-size', start_size--+'px');
}
DEMO
Do this instead:
div{
min-width: 200px;
}
By setting a minimum width you ensure that the div never gets small enough to collapse the text to multiple lines.
Demo: http://jsfiddle.net/96daR/4/
Give a id to the div, say id="myDiv"
Get height using one of this
javascript:
var myDiv = document.getElementById("myDiv");
h=myDiv.clientHeight;
h=myDiv.scrollHeight;
h=myDiv.offsetHeight;
or in jquery:
h=$("#myDiv").height();
h=$("#myDiv").innerHeight();
h=$("#myDiv").outerHeight();
Next set the font-size using this:
document.getElementById("myDiv").style.font-size=h+"px";

How to set height of child div while hovering over a parent div in css?

I'm making a drop-down menu that I want to display a submenu when you hover over one of the main menus. All of the child_menus have an initial height of 0. My HTML is roughly as follows...
<div class = "parent_menu">Parent 1
<div class = "child_menu">Child 1 text</div>
<div class = "child_menu">Child 2 text</div>
</div>
<div class = "parent_menu">Parent 2
<div class = "child_menu">Child 1 text</div>
<div class = "child_menu">Child 2 text</div>
</div>
I know that you can set properties with the :hover selector, but can I do something like .parent_menu:hover THEN FIND .child_menu {height:auto;}
By the way I know I can do it in JavaScript, but there has to be a way in CSS too.
Try this:
http://jsfiddle.net/r83FD/1/
.parent_menu div {
height: 0;
overflow: hidden;
}
.parent_menu:hover div {
height: auto;
}
Try below code it should work
.parent_menu:hover a.child_menu{
height:auto;
}
I think you might mean something like this:
.parent_menu:hover .child_menu {
height: auto;
}
Try a demo of this here: http://jsfiddle.net/M4NUP/

reorder div with css

I have divs sorted 1, 2, 3 ... as html layout
HTML:
<div class="bigdiv">
<div class="div1">div 1</div>
<div class="div2">div 2</div>
<div class="div3">div 3</div>
</div>
CSS:
.div1 {
float: left;
}
.div2 {
float: left;
}
.div3 {
float: left;
}
How can I reorder divs to this layout with CSS
<div class="bigdiv">
<div class="div3">div 3</div>
<br>
<div class="div1">div 1</div>
<div class="div2">div 2</div>
</div>
by CSS (float, :after, :before)
My try:
.div3 {
float: right;
}
.div3:before {
content: '\A\A';
white-space: pre;
}
Thank you in advance.
You can do this with the CSS Flexible Box Layout Module
The ‘order’ property controls the order in which flex items appear
within their flex container, by assigning them to ordinal groups.
A flex container will lay out its content starting from the lowest
numbered ordinal group and going up. Items with the same ordinal group
are laid out in the order they appear in the source document. This
also affects the painting order [CSS21], exactly as if the elements
were reordered in the document. (W3.org)
FIDDLE
CSS (Without browser specifics)
.bigdiv {
display: flex;
flex-direction: row;
}
.div1 {
order: 2;
}
.div2 {
order: 3;
}
.div3 {
order: 1;
}
You could try to play around the display property and use this style:
CSS
.bigdiv { display:table; }
.div1, .div2 { float: left; }
.div3 { display: table-header-group; }
Live example: http://jsbin.com/axekof/2/edit
Note the display:table; applied to the main wrapper and the display property applied to the .div3. The example should work even on IE8 (didn't tested)
As a sidenote, for .div3 you may use both display: table-caption and display: table-header-group, but there's a slight difference: with the first property, the parent element wraps only .div1 and .div2 while, with the latter, the container wraps all the children elements (try to apply a border to the .bigdiv so you can clearly see the difference)
HTML:
<div class="bigdiv">
<div class="div1">div 1</div>
<div class="div2">div 2</div>
<div class="div3">div 3</div>
</div>
CSS:
.bigdiv {
display:table;
width:100%;
}
.div1 {
display: table-row-group;
}
.div2 {
display: table-footer-group;
}
.div3 {
display: table-header-group;
}
The result will be:
div 3
div 1
div 2
And for those pesky early versions of Internet Explorer... Source
Old browsers IE6 and IE7 do not support CSS properties of display: table family.
Also, IE8 has a dynamic rendering bug that appears in some cases: if a block to move contains preudo-table elements (display: table*) (this is the only buggy case noticed currently), some pseudo-table cells (such cells as well as cells count are different each time the page is reloaded) may disappear randomly when the page is initially rendered.
So, for IE8 and lower, we can override CSS rules that make blocks tably, and additionally move blocks to required positions in DOM tree of HTML document with JavaScript instead of CSS:
/**
* Reorders sibling elements in DOM tree according to specified order.
* #param {Array} elems Sibling elements in desired block order.
*/
function reorderElements(elems) {
var count = elems.length;
if (!count) {
return;
}
var parent = elems[0].parentNode;
for (var i = count - 1; i >= 0; i--) {
parent.insertBefore(elems[i], parent.firstChild);
}
}
// If IE8 or lower
if (document.all && !document.addEventListener) {
var blocks = [
document.getElementById('div3'),
document.getElementById('div2'),
document.getElementById('div1')
];
reorderElements(blocks);
}
You can't do that with just CSS as I see you are manipulating the DOM. However if you would still want to achieve that you would need to use position:relative for bigdiv and position:absolute for .div1 .div2 and .div3, which are actually the children of .bigdiv. You would then need to use left, top, right, and bottom properties to style them as needed.

CSS of a slide in swipe view

I am working in Phone Gap using java script,html and css. I have implemented a swipeview using this.
my doubt is how should the CSS be.The CSS i implemented is as follows.
.swiper-threshold {
width:100%;
height:350px;
margin-top:40px;
text-align:left;
line-height:20px;
font-size: 15px;
text-align:justify;
text-justify:inter-word;
}
.swiper-threshold .swiper-slide{
width:auto;
height:350px;
margin-top:2%;
margin-left:0;
padding-right:0%;
text-align:left;
line-height:20px;
font-size: 15px;
text-align:justify;
text-justify:inter-word;
}
the sliding movement is from left to right.when it reaches the last slide.the movement reverses.
when,I change margin-left:0; from 0 to say 5...as and when I slide the left space gets on adding up the last slide is seen for half of the screen.
but,when i give margin-left:0; all are stuck to the left margin.
html coding:
<div id="swipe_body">
<div class="swiper-container swiper-threshold">
<div class="swiper-wrapper" id="swiper-wrapper">
</div>
</div>
</div>
SWIPER-SLIDE creation
var val = k+1;
var superdiv = document.getElementById('swiper-wrapper');
var newdiv = mySwiper.createSlide('div');
newdiv.append();
var divIdName = 'swiper-slide'+val;
console.log("div name: "+divIdName);
newdiv.setAttribute('id',divIdName);
newdiv.className="swiper-slide";
superdiv.appendChild(newdiv);
var cnt = '<div id="container'+val+'" class="container">values</div>';
document.getElementById(divIdName).innerHTML=cnt;
console.log("processsing parameter loop ");
what should i give such that n-number of swipes the slides should be the same position-middle of the screen.
please,guide me!
If I understand correctly, you're trying to get your slider to sit in the middle of the screen, but in doing so you're adding margin to the slider elements itself. Is that correct?
If so, the reason you're having trouble is that the slider implementation itself uses the margins to position the slides during movement left/right.
In order to control the spacing around the slider, the easiest bet is to wrap it in another div which you can traget directly.
So, taking the dimensions/etc that you've set up, your HTML would instead look like this:
<div class="slider-positioner">
<div id="swipe_body">
<div class="swiper-container swiper-threshold">
<div class="swiper-wrapper" id="swiper-wrapper">
</div>
</div>
</div>
</div>
You can then drop this CSS in to target that new div and push it into the middle (either using amrgin: auto as I have done below, or using set margin amounts as you seem to suggest you would like):
.slider-positioner{
display: block;
width: 550px; //set this to the width of your slider
height: 350px;
margin: 0px auto;
}