centering two divs within one - html

I'm building a sort header for a table that's built entirely with divs (ie. no table tags). Inside the sort header, I have two divs, the sort title and the sort direction. I want to have the header horizontally centered. I have a jsfiddle that looks like this:
<div class="SomeHeader">
<div class="HeaderTitle">sort1</div>
<div class="HeaderSort"> ▾</div>
</div>
<div class="SomeOtherHeader">
<div class="HeaderTitle">sort2</div>
<div class="HeaderSort"> ▾</div>
</div>​
.SomeHeader{
width:90px;
float:left;
background:red;
color:white;
line-height:20px;}
.SomeOtherHeader{
width:120px;
float:left;
background:black;
color:white;
line-height:20px;}
.HeaderTitle{float:left;}
.HeaderSort{float:left;}
What's the way to do this cleanly? I know I can specify the margin and width of each element to get the desired effect but when the value of these header will change then the alignment will not be centered. I know I can also do this with jQuery by looping over the header and calculating the width of headers and titles and programmatically setting the margins of each header. I'm looking to see if there's a more general CSS-based approach that will center both the HeaderTitle and the HeaderSort inside the SomeHeader classes.
Thanks for your suggestions.

http://jsfiddle.net/7R9WW/13/
Same markup, but display: inline-block on the inner divs and text-align: center on the field containers.
.SomeHeader{
width:90px;
float:left;
background:red;
color:white;
line-height:20px;
text-align:center;}
.SomeOtherHeader{
width:120px;
float:left;
background:black;
color:white;
line-height:20px;
text-align:center;}
.HeaderTitle{display:inline-block;}
.HeaderSort{display:inline-block;}

The HeaderSort div seems unnecessary. Could it not look like this http://jsfiddle.net/7R9WW/7/
Updated answer: http://jsfiddle.net/7R9WW/14/

Use a wrapper!
<div class="wrap">
<div class="SomeHeader">
<div class="HeaderTitle">sort1</div>
<div class="HeaderSort"> ▾</div>
</div>
<div class="SomeOtherHeader">
<div class="HeaderTitle">sort2</div>
<div class="HeaderSort"> ▾</div>
</div>​
</div>
And the CSS:
.wrap {width: 220px; margin: auto;}

wrap these two header divs with another div and then giv that div a margin:0 auto.
<div class="SomeHeader">
<div id="wrapper">
<div class="HeaderTitle">sort1</div>
<div class="HeaderSort"> ▾</div>
</div>
</div>
<div class="SomeOtherHeader">
<div id="wrapper">
<div class="HeaderTitle">sort2</div>
<div class="HeaderSort"> ▾</div>
</div>​
</div>
CSS:
#wrapper{
width: 100px;
margin: 0 auto;
overflow: hidden;
}
The width should be the sum of the width of the two contained divs , so you'll need to define a width to them.

Related

Align three divs side by side with css

I had three divs inside a main div with id main_div and has css already as below
<div id="main_div" style="height:10px; line-height:50px; margin-top:1px; position:relative;>
</div>
I just want to insert three divs in the main div as below
<div id="main_div" style="height:10px; line-height:50px; margin-top:1px; position:relative;>
<div>
Div One should be Left(breadcrumb_text)
</div>
<div>
Div Two should be Center(dropdownlist)
</div>
<div>
Div Three should be Right(Pagination)
</div>
</div>
So i want the div format to display the text like
breadcrumb_text dropdownlist Pagination
I tried with different css by using position attribute and various css options but could n't able to align them in a horizontal line with one div as left , one div as center and other div to right.
So can anyone let me know me know the css to place them in an horizontal line ?
This maybe help you Fiddle
#main_div > div {
width: 33%;
float: left;
}
I have modified your code little bit with spacing equally for each div and removed Position in the Main div.
Sometimes position will overlap with other div (position) based on z-index value. so if you really need use position unless not required.
#main_div{
height:10px; line-height:50px; margin-top:1px;
box-sizing:border-box;
}
#main_div > div{
width:31.1%;
float:left;
box-sizing:border-box;
border:1px solid grey;
margin-right: 10px;
display:inline-block;
}
#main_div > div:first-child{
margin-left:10px;}
<div id="main_div">
<div>
Div One should be Left(breadcrumb_text)
</div>
<div>
Div Two should be Center(dropdownlist)
</div>
<div>
Div Three should be Right(Pagination)
</div>
</div>
I think this is what you are asking for
JSFiddle
CSS
body
{
margin:0%;
}
.main_div
{
display:block;
margin:0% 5%;
width:90%;/*Just random, modify as per your requirement*/
height:300px; /*Just random, modify as per your requirement*/
background:#eee;
position:relatve;
}
.left-div, .center-div, .right-div
{
display:inline-block;
height:100%;
position:relative;
float:left;
width:33%;
border:1px solid #000;
text-align:center;
padding-top:5px;
}
HTML
<div class="main_div">
<div class="left-div">
Div One should be Left(breadcrumb_text)
</div>
<div class="center-div">
Div Two should be Center(dropdownlist)
</div>
<div class="right-div">
Div Three should be Right(Pagination)
</div>
</div>

Scrollable div below non-fixed height div

I'm trying to place two divs one above the other. The top div should stay always visible (not scrolling). The div below contains a list, and if the list is too long, and overflows the window/containing div, the div should be scrollable. When defining the height of the top div, it's good, but the content of the top div may change, so the height should not be fixed (like in this question).
My attempt on Plunker.
Is it possible with pure CSS, without JavaScript calculation?
Edit:
The list should strech to the bottom of the screen/container div.
You need to use some not too obvious CSS trickery to get the behaviour you're after, importantly any scrollable content needs to be within a separate container in a CSS table's cell, with overflow-y set, and a height of 100%. The top cell then needs a height of 1% to auto expand as appropriate.
Then all you need to do is set the tables height and max-height as appropriate.
By using CSS tables, you get a lot more flexibility when it comes to layout calculation/manipulation in terms of relating the sizes of elements
Demo Fiddle
CSS
html, body {
height:100%;
width:100%;
margin:0;
padding:0;
}
.table {
display:table;
table-layout:fixed;
height:100%;
width:100%;
max-height:100%;
}
.row {
display:table-row;
}
.cell {
display:table-cell;
}
.row:first-of-type >.cell {
background:lightgreen;
height:1%;
}
.row:last-of-type >.cell {
background:pink;
}
#list {
height:100%;
overflow-y:auto;
}
HTML
<div class='table'>
<div class='row'>
<div class='cell'>This is text in the <strong>list-head</strong>, it's content may change, so the height of the div shouldn't be fixed, but should stay always visible (not scrolling).</div>
</div>
<div class='row'>
<div class='cell'>
<div id="list">
<div class="list-element">These are list elements.</div>
<div class="list-element">If the list is too long</div>
<div class="list-element">and reaches the bottom</div>
<div class="list-element">the list should be scrollable.</div>
<div class="list-element">(And only the list</div>
<div class="list-element">not together with the <strong>list-head</strong>.)</div>
</div>
</div>
</div>
</div>
Will this work for you ?
<div id="top" >
</div>
<div id="bottom">
</div>
<style>
#top{
display:block;
width:100%;
}
#bottom{
overflow:scroll;
display:block;
height:500px;
width:100%;
}
</style>
use this structure
<div class="main">
<div class="header">
</div>
<div class="content">
</div>
</div>
.main{
height:100%;
}
.header{
height:50px;
position:fixed;
top:0;
background:#454546;
width:100%;
}
.content{
margin-top:53px;
background:#ffffff;
}
Demo

How to div start in center like float left or right?

i'm trying to div start in center like float right or left but i didn't do this.
Please help me?
HTML
<div class="conta">
<div class="box">
<p>some text</p>
</div>
<div class="box">
<p>some text</p>
</div>
<div class="box">
<p>some text</p>
</div>
<div class="box">
<p>some text</p>
</div>
</div>
CSS
.conta{width:100%; position:relative; text-align:center}
.conta .box{float:left; width:100px; height:100px; border:1px solid #000; margin:5px;}
I want to start box in center and inline.
there is many possible ways.
One
simplest way this is: margin:0 auto but you need to have fixed width.
DEMO
Two
you can set display:inline-block; to inner element and set text-align:center to the container.
DEMO
HTML
<div>
<div></div>
</div>
CSS
div{
text-align:center;
}
div div{
display:inline-block;
background:gray;
width:200px;
height:100px;
}
Three
set position:relative to the container and position:absolute to the inner element, then set left:50% and margin-left:-100px (margin-left must be half of the width, in this case 100px).
DEMO
CSS
div{
position:relative;
}
div div{
position:absolute;
left:50%;
margin-left:-100px;
background:gray;
width:200px;
height:100px;
}
For block-level items, you can use auto margins to do this (when the item is within the document flow. When you float it, position it other than static, you are taking it out of the document flow.)
div.my-container { margin: 0 auto; }
Will do the trick :)
You can do like this also,
<div id="main" style="text-align:center">
<div id="inner" style="text-align:left;">
I want to be center left.......
</div>
</div>
Now main(outer div) aligned to center and the content div(inner) is aligned left in center. This works fine in IE. You should add margin:0 auto; to make this work in all other browsers.
.myclass{
margin:0px auto;
text-align:center
}

Inline display of divs with child overlapped divs

I'm a noob, trying everyday to learn more about CSS (even though I'm clumsy and "not a natural", my brain steams out like an old computer). I want to find out how to code a set of independent modules which share the same style and are displayed inline in two rows of two columns. I want to recreate a paper stack for each module, using z-index and absolute positioning for it.
I made this image to show what I'm looking for:
I tried display:inline for the top div - but this way, I had to code individual overlapped divs for each module, using position:absolute and coordinates. What would be most desirable is that a single module+overlap could be repeated, using the same class (without having to change each module's overlapped div coordinates).
Does anyone have an idea about how to do this using CSS? Thank you in advance :)
Yes, you could do the following. You'll want to resize to whichever dimensions serve you best. JSfiddle here: http://jsfiddle.net/CNPJ9/2/
<div class="contain">
<div class="box">
<h1>A</h1>
</div>
<div class="behind"></div>
</div>
<div class="contain">
<div class="box">
<h1>B</h1>
</div>
<div class="behind"></div>
</div>
<div class="contain">
<div class="box">
<h1>C</h1>
</div>
<div class="behind"></div>
</div>
<div class="contain">
<div class="box">
<h1>D</h1>
</div>
<div class="behind"></div>
</div>
.contain {
margin:25px 25px;
float:left;
}
.box h1 {
font: 8em normal Futura, sans-serif;
text-align:center;
color:#f1f1f1;
}
.box {
width:400px;
height:300px;
background:#000;
z-index:1;
float:left;
position:absbolute;
}
.behind {
width:350px;
height:325px;
margin-left:25px;
background:#333;
z-index:2;
}
Something like this may help you.
Fiddle
.abc{
width=300px;
height:200px;
margin:50px;
border:solid 2px black;
border-bottom: 5px double blue;
}

carousel like with css, overflow-x (horizontal only)

I have an overflow-x:scroll and overflow-y:hidden. I have images, but when I'm trying to make it only scroll horizontally, it doesn't work? When I highlight the images and drag the highlight downwards, its scrolls down vertically.
#contents
{
margin:0 auto;
text-align:center;
width:1200px;
clear:both;
}
#image_contents
{
float:left;
height: 208px;
overflow-x:scroll;
overflow-y:hidden;
margin:0 auto;
}
.images
{
float:left;
margin:2px;
background:#000;
overflow:hidden;
position:relative;
display:inline-block;
}
<div id="contents">
<div id="image_contents">
<div class="images">
<img src="1.jpg"/>
</div>
<div class="images">
<img src="2.jpg"/>
</div>
<div class="images">
<img src="3.jpg"/>
</div>
<!-- and so forth !->
</div>
</div>
The best way to do this, to my knowledge, would be to create another div inside the image_contents div. Then give this div the width of all the images (with JavaScript if necessary).
Another solution would be to use display:table-cell on the images inside another div with display:table-row.
Or (dare I say it) use a table.