Center div inside a div vertically - html

I have tried all methods i could find .. and nothing worked for me...
i just can wrap my head around what is the problem of aligning a div (or a block element) inside another div .. what could be so difficult..
I want to align the green block vertically.
here is the fiddle: http://fiddle.jshell.net/795St/1/
<div class="rtl centerwrapper">
<div class="green-block pull-right"></div>
<div class="green-block pull-right"></div>
<div class="green-block pull-right"></div>
<div>Average</div>
</div>
.green-block {
background-color: #02A556;
margin: 0 .25em 0 .25em !important;
width: 1em;
height: 0.5em;
}
.pull-right {
float: right;
}
.rtl {
direction:rtl;
}
.centerwrapper {
position: absolute;
top: 50%;
left: 0px;
width: 100%;
overflow: visible;
}
Please .. can any one help .. and explain what am i dooing wrong ?
Edit:
Let me be more clear...
I need all element in one line.
just the blocks needs to be aligned at the vertical middle of the text.
Edit2: here is an image

New Answer
Here is the new fiddle link http://fiddle.jshell.net/795St/16/
CSS
.green-block {
background-color: #02A556;
margin: 0 .25em 0 .25em !important;
width: 1em;
height: 0.5em;
vertical-align: middle;
position: relative;
}
.pull-right {
display: inline-table;
}
.rtl {
direction:rtl;
}
.centerwrapper {
position: absolute;
top: 50%;
left: 0px;
width: 100%;
overflow: visible;
}
HTML
<div class="rtl centerwrapper">
<div class="green-block pull-right"></div>
<div class="green-block pull-right"></div>
<div class="green-block pull-right"></div>
<div class="pull-right">Average</div>
</div>
Screenshot of output
Old Answer
Here is the required output
http://fiddle.jshell.net/795St/5/
.pull-right {
display: inline-table;
}
.centerwrapper {
position: absolute;
top: 50%;
left: 0px;
width: 100%;
overflow: visible;
text-align: center;
}
For inner divs I added display as inline-table so that they will not be treated as block element and shown in one line. For the outer div I added text-align as center. SO that it will show the contents in center.
Only 2 changes done to your fiddle.
For inner div instead of float: right I added display:inline-table
And for outer div added text-align:center.

http://fiddle.jshell.net/n234A/
Give your green block an appropriate top margin to make them sit in the middle of the wrapper.
In this demo I gave the wrapper div a red background to show centering better

you can use display:flex to do this : http://codepen.io/gc-nomade/pen/wmECy
html,body {
height:100%;
width:100%;
margin:0;
}
body , body > div{
display:flex;
}
div {
margin:auto;
}
.green-block {
background-color: #02A556;
margin: 0 .25em 0 .25em !important;
width: 1em;
height: 0.5em;
order:4;
}
.first {
flex:1;
order:1;
}
or display:table http://jsfiddle.net/MxE8Y/5/ (includes IE8)
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
display:table;
}
body {
display:table-cell;
vertical-align:middle;
}
div {
display:table;
direction:rtl;
margin:auto;
border-spacing:0.25em;
}
.green-block {
background-color: #02A556;
width: 1em;
height: 0.5em;
display:table-cell;
}

.green-block {
background-color: #02A556;
margin: 6px 10px!important;
width: 1em;
height: 0.5em;
}

Demo
Is it ok ?
.centerwrapper div:last-child{
margin-top:-8px;
}

You must display all divs as inline-blocks
.centerwrapper div {
display: inline-block;
}

HTML Code
Average
and CSS
.green-block {
background-color: #02A556;
margin: 0 .25em 0 .25em !important;
width: 1em;
height: 0.5em;
margin:10px!important;
list-style:none;
}
.pull-right {
float: right;
}
.rtl {
direction:rtl;
}
.centerwrapper {
position: absolute;
top: 50%;
left: 0px;
width: 100%;
overflow: visible;
}
.clear{
clear:both;
}
use this.and give margin at what size you want

Related

How to properly align side-by-side blocks in a container

I have a div with display block and in it I have others in inline-block. This is what I have.
display:inline-block case
When I put div inside the first in display: block, with float left, I have this:
display:block with floating case
#container {
display: block;
width: 50%; /*Of its parent*/
}
#container > div {
display: block;
width: 22%;
padding: 5px 1.2%;
float: left;
}
/* or
#container > div {
display: inline-block;
width: 22%;
padding: 5px 1.2%;
}
or
#container > div {
display: inline-block;
width: 22%;
padding: 5px 1.2%;
float: left;
}
*/
Please, can someone tells me what it is wrong and help to fix it?
Thanks
5px 1.2% and 22% won't work really well, try
#container > div {
display: block;
width: 25%;
padding: 5px 1%;
margin: 0;
float: left;
}
#container>div {
display: block;
width: 23%;
padding: 5px 1%;
margin: 0;
float: left;
height: 100px;
}
.red{
background: red;
}
.green{
background: green;
}
<div id="container">
<div class="red"></div>
<div class="green"></div>
<div class="red"></div>
<div class="green"></div>
</div>
Try using display: flex. Floats tend to create issues. Flex looks to fix some of those issues. A great guide can be found here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

How to center an element in a div, while keeping it on the same horizontal level as another element?

Having trouble getting my image to center, while remaining on the same horizontal level as my icon.
HTML
<div class="smalltop w3-top">
<span class="w3-opennav w3-xlarge" onclick="w3_open()"><i id="menui" class="material-icons w3-xxlarge">menu</i></span>
<img id="smalllogo" src="img/navlogo-invert.jpg">
</div>
CSS
#smalllogo {
max-height: 50px;
width: auto;
display: block;
margin: 0 auto;
border: 5px solid;
margin-top: 5px;
}
#menui {
left: 0;
}
.smalltop {
background-color: #FFF;
list-style-type: none;
margin: 0 auto;
}
https://jsfiddle.net/pzLbruhx/
Add relative positioning to the container and absolute positioning to the icon
#menui {
left: 0;
position:absolute;
}
.smalltop {
background-color: #FFF;
list-style-type: none;
margin: 0 auto;
position:relative;
}
jsFiddle example
Just give property float:left to #menui, and see the result
Use display:table\table-row\table-cell and vertical-align on icon
#smalllogo {
max-height: 50px;
width: auto;
display: table-cell;
margin: 0 auto;
border: 5px solid black;
margin-top: 5px;
}
.w3-opennav {
display: table-cell;
vertical-align: middle;
}
.smalltop {
background-color: #FFF;
list-style-type: none;
margin: 0 auto;
display: table-row;
}
Fiddle

inline 2 spans inside div

html
<div id="container">
<div>
<span>Visit website</span>
<span>View project</span>
</div>
</div>
css
#container {
width: 100%;
padding:0;
background-color: green;
}
div { padding: 0 20px; width: 0px; background:red;overflow:visible; text-align: center;}
span {
background:#222;
color:#fff;
display:inline-block;
margin:10px 10px 0 0;
padding:5px 10px
}
Demo: http://jsfiddle.net/cePe3/445/
How to make the 2 span to be inline with each other in the middle of the container DIV!
Note: code structure must be as its.
thank you
You can use a more modern solution: flexbox
Add display: flex; justify-content: center; to #container and to #container div. It's magic.
Working fiddle: http://jsfiddle.net/cePe3/448/
You have to set your div width to auto
div { padding: 0 20px; width: auto; background:red;overflow:visible; text-align: center;}
https://jsfiddle.net/cePe3/446/
you have to set div width for inline span
div
{ padding: 0 20px;
width: 350px;
background:red;
overflow:visible;
text-align: center;
}
DEMO
make the position absolute for the spans, then add jquery to center the spans, even on resizing of the window:
$( window ).resize(function() {
shuffle();
});
function shuffle() {
$('span').each(function(){
$(this).css('left',($('#container').width()-$(this).width()) / 2);
});
$('#container').find('div').css('height',$('span:first').height()*6);
}
shuffle();
#container {
width: 100%;
padding:0;
background-color: green;
}
div { padding: 0 20px; width: 0px; background:red;overflow:visible; text-align: center;}
span {
position: absolute;
background:#222;
color:#fff;
display:inline-block;
margin:10px 10px 0 0;
padding:5px 10px
}
span:nth-child(2) {
top: 60px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div>
<span>Visit website</span>
<span>View project</span>
</div>
</div>

Stretch DIV to full page width but also make it auto resize according to browsers borders

How can I make this layout to be fully responsive?
Basically I need both boxes:
chatEntries and #chatUsers to be sctretched across whole page.
They must share page width in ratio of: 85% [chatEntries] - 15% [chatUsers].
So how would I do this?
Accounting for the borders in your illustration I would recommend something like this:
jsFiddle
HTML
<div class="clearfix container">
<div class="chatEntries"></div>
<div class="chatUsers">
<h4>Online Users</h4>
</div>
</div>
CSS
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block; }
.container {
background: #000;
padding-top: 30px;
}
h4 {
color: #05E9FF;
text-align: right;
margin: 0;
font-size: 16px;
font-family: arial, sans-serf;
position: absolute;
top: -20px;
left: 0;
z-index: 1;
}
.chatEntries,
.chatUsers {
min-height: 500px;
}
.chatEntries {
width: 84.8%;
background: #ccc;
float: left;
}
.chatUsers {
position: relative;
width: 14.8%;
background: #999;
float: right;
margin-right: 0.2%;
}
Something like this?
JSFiddle
#chatEntries {
display:inline-block;
width:85%;
float:left; }
#chatUsers {
display:inline-block;
width:15%;
float:left; }

Layout problems with CSS

I got some problems with layouting in CSS. Here is the code I am talking about: Fiddle.
The <div id="header"> should have the height of the <div id="menubuttons"> which I marked red.
I always thought that if you don't state the height of a div it will get the height of it's children.
The <div class="contentLine> is stuck to the <div id="theme"> although I defined margin-top: 20px;.
The right column always has greater margin than the left column. I want both to have the same margin to the browser window.
CSS
body {
margin: 0 auto;
padding: 0;
font-family:'Share', cursive;
}
#header {
width: 100%;
vertical-align: middle;
}
#header_logo {
width:;
float: left;
margin: 11px 20px 20px 20px;
background-color:;
}
#menubuttons {
margin-right: 0;
margin-top: 0;
height: 2.5em;
line-height: 2.5em;
display: inline-block;
background-color: red;
}
#menubuttons ul {
list-style-type: none;
margin:0;
padding:0;
}
#menubuttons li {
float: left;
margin-right: 20px;
}
a {
font-family:'Share', cursive;
}
a:link {
text-decoration:none;
}
a:visited {
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
a:active {
text-decoration:underline;
}
#theme {
width: 100%;
height: 400px;
background-color: green;
margin-top: 0;
float: left;
}
.contentLine {
margin: 0 auto;
margin-top: 20px;
width: 96%;
}
.contentLine .column {
float: left;
margin: 0;
width: 30%;
margin-right: 1%;
padding: 1%;
position: inherit;
/* shadow for seeing div boundaries */
box-shadow: 0 0 1px black inset;
}
.contentLine #last {
margin-right: 0;
}
Let me go 1 by 1
1) Your <div id="header"> contains floated elements, you need to clear that, so use overflow: hidden; on parent element i.e #header
2) Again, you've floated #theme but you've set it to width: 100%; so you don't need float there.
3) About the last you need to set the margins accordingly, right now it's 1% so you need to calculate this correctly, I would like to suggest you to use box-sizing: border-box; and set 33% width for each element and than apply padding-right
Demo
Also make sure you clear your floating elements which are nested inside contentLine.
If you are not one of those IE fans, than you can use the snippet below, which will self clear the parent element in a better way.
.clear:after { /* Much much better than overflow: hidden; */
content: "";
display: table;
clear: both;
}
Update your html
</ul>
<!--Menu ends here -->
</div>
<!--menubuttons ends here -->
<!--Add following div to your code -->
<div class="clear"></div>
</div>
<div id="theme">
Update your CSS
.clear{
clear:both;
}
This should help.
- will be reusable also.