How to set div and text to vertical middle of it parent - html

I have a two divs it's containing in parent div. first div at left side and second at right.
So, i want a right div at middle of parent with out using margin.
HTML
<div class="clearfix" style="background-color: #CCCCCC;">
<div style="padding: 10px; background-color: #0099FF;" class="inline-block">
<div class="inline-block"><div>Post</div><div>139</div></div>
<div class="inline-block"><div>Following</div><div>139</div></div>
<div class="inline-block"><div>Followers</div><div>139</div></div>
</div>
<div class="rfloat" style="background-color: #00FF66; display: table-cell; vertical-align: middle;">
Follow
Message
Report
</div>
</div>
CSS
.inline-block {
display: inline-block;
}
.rfloat {
float: right;
}
.lfloat {
float: left;
}
.clearfix:before,
.clearfix:after {
display: table;
content: " ";
}
.clearfix:after {
clear: both;
}
and here's jsfiddle http://jsfiddle.net/fmr3Y/

This how you can do it
DEMO
HTML
<div class="rfloat" style="background-color: #00FF66; display: table; height: 60px">
CSS
a {
display: table-cell;
vertical-align: middle;
}

Use display:table -- demo
<div class="clearfix" style="background-color: #CCCCCC; display:table;width:100%;">
<div style="padding: 10px; background-color: #0099FF;" class="inline-block">
<div class="inline-block">
<div>Post</div>
<div>139</div>
</div>
<div class="inline-block">
<div>Following</div>
<div>139</div>
</div>
<div class="inline-block">
<div>Followers</div>
<div>139</div>
</div>
</div>
<div class="ss" style="background-color: #00FF66; display: table-cell; vertical-align: middle;"> Report
Message
Follow
</div>
</div>

You could mimic the flex justify-content with display and text-align:justify : DEMO
.inline-block {
display: inline-block;
font-size:16px;/* do not forget to reset font-size to childs*/
font-size:1rem;
text-align:center;/* left, right, .. */
}
.justify:after {
display: inline-table;
content: " ";
width:100%;
height:0;
vertical-align:top;/* no extra gap please */
}
.justify {
text-align:justify;/* will span element from one side to the other */
font-size:0;/* reduce line-height and avoid gap from pseudo element */
}
HTML updated with class name :
<div class="justify" style="background-color: #CCCCCC;">
<div style="padding: 10px; background-color: #0099FF;vertical-align: middle;" class="inline-block">
<div class="inline-block"><div>Post</div><div>139</div></div>
<div class="inline-block"><div>Following</div><div>139</div></div>
<div class="inline-block"><div>Followers</div><div>139</div></div>
</div>
<div class="inline-block" style="background-color: #00FF66; vertical-align: middle;">
Follow
Message
Report
</div>
</div>

I had Changed Your structure to make Right Div at center
<table class="clearfix" style="background-color: #CCCCCC; width: 100%;text-align: center; ">
<colgroup>
<col style="width: 19%"/>
<col style="width: 75%"/>
</colgroup>
<tr>
<td>
<div style="padding: 10px; background-color: #0099FF;" class="inline-block">
<div class="inline-block">
<div>Post</div>
<div>139</div>
</div>
<div class="inline-block">
<div>Following</div>
<div>139</div>
</div>
<div class="inline-block">
<div>Followers</div>
<div>139</div>
</div>
</div>
</td>
<td>
<div style="background-color: #00FF66; vertical-align: middle;">
Follow
Message
Report
</div>
</td>
</tr>
</table>
Here is Working Demo Too.Working Fiddle

Related

Why do my table columns all shift to the left for instead of being evenly spaced?

I'm having some trouble with this not running properly. It won't align the text correctly and it also won't distribute the cells across the available space. Not sure what I'm doing wrong here, so any help would be appreciated.
While we're at it, I'm also trying to figure out how to get it to show 2 columns instead of 1 when it switches to mobile sized. The way it's setup now, it just transitions to 1 column.
This is for a website on Shopify that uses an html block to insert custom code. I have other blocks that I've inserted into it and they all seem to be working fine, it's just this one that's giving me issues.
.table2 {
display: table;
}
.table-row2 {
display: table-row;
}
.table-cell2 {
display: table-cell;
vertical-align: top;
width: 20%;
padding: 2.5%;
}
.icontext {
display: text;
vertical-align: middle;
width: 20%;
padding: 2.5%;
float: middle;
}
#media screen and (max-width: 768px) {
.table2,
.table-row2,
.table-cell2 {
display: block;
width: 100%;
float: middle;
}
}
<h1><center>Materials</center></h1>
<div class="table2">
<div class="table-row2">
<div class="table-cell2" width="20%" align="center">
<img src="https://ucarecdn.com/0ce94db3-8b05-4d84-a64c-dc6de006151c/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Durable
</div>
</div>
<div class="table-cell2" width="20%" align="center">
<img src="https://ucarecdn.com/b6ff1bd5-4c2e-4db0-bacc-3aa76678e92c/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Quick Dry
</div>
</div>
<div class="table-cell2" width="20%" align="center">
<img src="https://ucarecdn.com/e09a4c7e-c262-415f-b491-599bd0bc3a66/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Lightweight
</div>
</div>
<div class="table-cell2" width="20%" align="center">
<img src="https://ucarecdn.com/03df04db-7851-4f80-ad8f-0438cc7e0397/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Breathable
</div>
</div>
<div class="table-cell2" width="20%" align="center">
<img src="https://ucarecdn.com/8034c6ad-6c30-406d-8865-a4bdb4060bca/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Moisture Wicking
</div>
</div>
</div>
</div>
I would recommend using flex something like below will fix your issue. There was a few issues with your previous code being out of date. This will also flex on mobiles to show 2 columns as you stated.
.table-row2 {
width: 100%;
height:auto ;
display: flex;
flex-direction: row;
flex-wrap:wrap;
justify-content:space-around;
}
.table-cell2 {
vertical-align: top;
padding: 2.5%;
}
.icontext {
padding: 2.5%;
text-align: center;
}
#media screen and (max-width: 768px) {
.table2,
.table-row2,
.table-cell2 {
// removed all
}
}
<meta name="viewport" content="width=device-width" />
<h1><center>Materials</center></h1>
<div class="table2">
<div class="table-row2">
<div class="table-cell2">
<img src="https://ucarecdn.com/0ce94db3-8b05-4d84-a64c-dc6de006151c/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Durable
</div>
</div>
<div class="table-cell2" >
<img src="https://ucarecdn.com/b6ff1bd5-4c2e-4db0-bacc-3aa76678e92c/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Quick Dry
</div>
</div>
<div class="table-cell2" >
<img src="https://ucarecdn.com/e09a4c7e-c262-415f-b491-599bd0bc3a66/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Lightweight
</div>
</div>
<div class="table-cell2" >
<img src="https://ucarecdn.com/03df04db-7851-4f80-ad8f-0438cc7e0397/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Breathable
</div>
</div>
<div class="table-cell2">
<img src="https://ucarecdn.com/8034c6ad-6c30-406d-8865-a4bdb4060bca/-/format/auto/-/preview/3000x3000/-/quality/lighter/">
<div class="icontext">
Moisture Wicking
</div>
</div>
</div></div>

display 4 lines of first column to left of the page

we have 4 lines displaying like this in page
we want to move all texts of first column to left & keep all 4 colon[:] in same line
<div class="all-link">
<p>Delivery :</p>
<span>Delivery.com</span>
<div style="clear: both; height: 0px;"> </div>
<p>Parcelled.in:</p>
<span>Parcelled.in</span>
<div style="clear: both; height: 0px;"> </div>
<p>DTDC:</p>
<span>DTDC</span>
<div style="clear: both; height: 0px;"> </div>
<p>Speed Post:</p>
<span>Speed Post</span>
<div style="clear: both; height: 0px;"> </div>
</div>
i am trying with `position : relative & left , right pixels but i need to use lot of code for that.
Check below html, it might help you.
<div class="all-link">
<p style="display: inline-block;margin: 0;text-align: left !important;width: 90px;">Delivery <b style="float:right">:</b></p>
<span>Delivery.com</span>
<div style="clear: both; height: 0px;"> </div>
<p style="display: inline-block;margin: 0;text-align: left !important;width: 90px;">Parcelled.in <b style="float:right">:</b></p>
<span>Parcelled.in</span>
<div style="clear: both; height: 0px;"> </div>
<p style="display: inline-block;margin: 0;text-align: left !important;width: 90px;">DTDC <b style="float:right">:</b></p>
<span>DTDC</span>
<div style="clear: both; height: 0px;"> </div>
<p style="display: inline-block;margin: 0;text-align: left !important;width: 90px;">Speed Post <b style="float:right">:</b></p>
<span>Speed Post</span>
<div style="clear: both; height: 0px;"> </div>
</div>
Ths is really tabular data so an actual table would be best but CSS Tables can manage this with a slight HTML structure change.
Wrap each paragraph and associates span in a div and these wrapper can act as table rows.
.all-link {
display: table;
margin: 1em;
}
.all-link .row {
display: table-row;
}
.all-link .row p,
.all-link .row span {
display: table-cell;
padding: .25em;
border:1px solid lightgrey;
}
.all-link .row p {
text-align: right;
}
<div class="all-link">
<div class="row">
<p>Delivery :</p>
<span>Delivery.com</span>
</div>
<div class="row"><p>Parcelled.in:</p>
<span>Parcelled.in</span>
</div>
<div class="row">
<p>DTDC:</p>
<span>DTDC</span>
</div>
<div class="row">
<p>Speed Post:</p>
<span>Speed Post</span>
</div>
</div>

using a div inside a td tag

i want a code which displays as
Account No:
But in my code its displayed as
and my code is as below
<td colspan="3">Account No:<div class="boxed">
</div>
<div class="boxed">
</div>
<div class="boxed">
</div>
<div class="boxed">
</div>
<div class="boxed">
</div>
</td>
Enclose the Account No. text with a div and add
CSS "float:left"
To that div.
Working code:
<table>
<tr>
<td colspan="3" style="float: left;">
<div style="float: left">Account No:</div>
<div class="boxed"></div>
<div class="boxed"></div>
<div class="boxed"></div>
<div class="boxed"></div>
<div class="boxed"></div>
</td>
</tr>
</table>
<style>
.boxed {
border: 1px solid;
width: 50px;
height: 50px;
float: left;
}
</style>
You can use div to wrap them and float.
<td colspan="3">
<div style="float:left;padding:15px 5px 0 0;">Account No:</div>
<div style="float:right;">
<div class="boxed"></div>
<div class="boxed"></div>
<div class="boxed"></div>
<div class="boxed"></div>
<div class="boxed"></div>
</div>
</td>
Fiddle
You can try this html structure and see fiddle link as below:
<td colspan="3">
<div class="title">
Account No:
</div>
<div class="boxed">
1
</div>
<div class="boxed">
2
</div>
<div class="boxed">
3
</div>
<div class="boxed">
4
</div>
<div class="boxed">
5
</div>
CSS:
.title {
float: left;
width: 20%;
}
.boxed {
border: 1px solid;
float: left;
padding: 10px 0;
text-align: center;
width: 7%;
}
https://jsfiddle.net/wwta4e8h/
.box{
border: 1px solid;
width: 30px;
height: 30px;
float: left;
}
<div style="float: left;">
<div style="float: left;padding-top:10px;padding-right:10px">Account No:</div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
You can replace float: left; with display: inline-block; And in case if you want the boxes to ne top aligned add vertical-align: top;
<style>
.boxed {
border: 1px solid black;
display: inline-block;
vertical-align: top;
width:30px;
height:30px;
}
</style>

How to use "display: table" to merge two rows, css?

I try to show following object with 3 images:
...........
| | |
| 1 | |
|----| 2 |
| 3 | |
| | |
|____|____|
I use display style:
.table {
display: table;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
}
So far I got this:
<div style="display: inline-block; height: 100%; overflow: hidden;">
<div style="display: table;">
<div class="row">
<div class="cell" style="height: 100%;">
<img style="height: 38.5%; display:block;" src="img/group_6.png"/>
</div>
<div class="cell" style="width: 37px;overflow: hidden;">
<img style="height: 74px; display: block; margin-left: -18.5px;"
src="img/group_6.png" />
</div>
</div>
<div class="row">
<div class="cell" style="height: 100%;">
<img style="height: 38.5%; display:block;" src="img/group_6.png" />
</div>
</div>
</div>
</div>
How to achieve the view I posted?
Thank you,
Please change the name of images. For the image available in the right side it might be possible that you've to adjust: align or top or bottom margins. Adjust them according to the requirement. I've created the borders which will help you to adjust images appropriately. Once done please remove them. Kindly check and verify....
<html>
<style>
.table {
display: table;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
}
</style>
<body>
<div class="table">
<div class="cell" style="border: 10px solid;">
<div class="row" style="border: 10px solid;">
<img style="height: 38.5%; display: block;" src="xyz.jpg" />
</div>
<div class="row" style="border: 10px solid;">
<img style="height: 38.5%; display: block;" src="pqrs.jpg" />
</div>
</div>
<div class="cell" style="border: 10px solid; height:auto; width:100%">
<img align="middle" style="width:20%; height: auto; display: inline-block;" src="abcd.jpg" />
</div>
</div>
</body>
</html>

Colspan alternative in <div> based structure

I want to use div elements to create a table-like layout.
What I want
.tableStyle td {
background: red;
color: #fff
}
.contentBox {
min-height: 200px;
}
<table class="tableStyle" width="100%" cellspacing="10">
<tr>
<td width="25%">
<div class="contentBox">One</div>
</td>
<td width="25%">Two</td>
<td width="50%" colspan="2">Three</td>
</tr>
<tr>
<td colspan="2">
<div class="contentBox">Four</div>
</td>
<td colspan="2">Five</td>
</tr>
</table>
What I have
.table {
display: table;
width: 100%;
border-collapse: separate;
border-spacing: 10px;
color: #fff
}
.table .row {
display: table-row;
}
.table .table-cell {
display: table-cell;
background: red;
width: 50%;
}
.contentBox {
min-height: 200px;
}
.table .smlCell {
width: 25%;
}
.table .table {
border-spacing: 0;
}
<div class="table">
<div class="row">
<div class="table-cell">
<div class="table">
<div class="row">
<div class="table-cell smlCell">
<div class="contentBox">One</div>
</div>
<div class="table-cell smlCell">
Two
</div>
</div>
</div>
</div>
<div class="table-cell">Three</div>
</div>
<div class="row">
<div class="table-cell">
<div class="contentBox">Four</div>
</div>
<div class="table-cell">Five</div>
</div>
</div>
I want to have equal spacing between the cells marked "One" and "Two".
I also want all cells in a row to be of same height.
After searching on net, I know that there are some limitations or issues
for display: table such as a lack of colspan/rowspan equivalents which may help what I'm trying to accomplish.
Is there anyway (apart form <table>) to create this?
Sure there is!
Demo Fiddle
HTML
<div class='table'>
<div class='row'>
<div class='table'>
<div class='cell'>One</div>
<div class='cell'>Two</div>
</div>
<div class='cell'>Three</div>
</div>
<div class='row'>
<div class='cell'>Four</div>
<div class='cell'>Five</div>
</div>
</div>
CSS
html, body{
width:100%;
height:100%;
margin:0;
padding:0;
}
.table {
display:table;
width:100%;
height:100%;
}
.cell {
display:table-cell;
width:50%;
}
.row {
display:table-row;
}
.cell {
background:red;
color:white;
border:5px solid white;
}
Try this. I have used inline instead of CSS class. You had placed one div in wrong location.
<div class="table">
<div class="row">
<div class="table-cell">
<div class="table">
<div class="row">
<div class="table-cell smlCell">
<div style="width:50% float: left" class="contentBox">One
</div>
<div style="width:50% float: right" class="contentBox">
Two
</div>
</div>
</div>
</div>
</div>
<div class="table-cell">Three</div>
</div>
<div class="row">
<div class="table-cell">
<div class="contentBox">Four</div>
</div>
<div class="table-cell">Five</div>
</div>
</div>
with support for older (ms-explorer?). But if your content is table by it's nature, use table and not those tricks :-)
<!DOCTYPE HTML>
<html>
<head>
<title>Bla!</title>
<style type='text/css'>
/* reset sizing model ... */
* { box-sizing: border-box; -webkit-box-sizing: border-box;-moz-box-sizing: border-box; }
/* basic div definitions */
div { display:inline-block; min-height:50px; background-color:red; border:solid 3px white; }
/* 1/2 line div */
div.half { width:50%; }
/* 1/4 line div */
div.quater { width:25%; }
</style>
</head>
<body class='body' id='body' >
<div class='quater'> One </div><div class='quater'> Two </div><div class='half'> Three </div><div class='half'> Four </div><div class='half'> Five </div>
</body>
</html>