align center two elements different width - css

How to make two elements aligned so they will be at same distance from line in center which should be in center of wrapper. Also wrapper width is not fixed and may change.
http://jsfiddle.net/x2b2ax37/2/
<div id="block">
<div id="wrapper">
<span id="div1">2222</span>
<span id="div2">2 %</span>
</div>
<div id="wrapper">
<span id="div1">11</span>
<span id="div2">100 %</span>
</div>
<div id="wrapper">
<span id="div1">21</span>
<span id="div2">0 %</span>
</div>
</div>
1 - Initial 2 - What I expect

You could achieve it like this:
(Updated with .classes instead of #IDs)
JSFiddle - DEMO
.wrapper {
position: relative;
}
.div1 {
border: 1px solid #F00;
right: 50%;
position: absolute;
}
.div2 {
border: 1px solid #000;
position: relative;
left: 50%;
display: inline-block;
}
.block {
border: 1px solid green;
width: 200px;
}
<div class="block">
<div class="wrapper">
<span class="div1">2222</span>
<span class="div2">2 %</span>
</div>
<div class="wrapper">
<span class="div1">11</span>
<span class="div2">100 %</span>
</div>
<div class="wrapper">
<span class="div1">21</span>
<span class="div2">0 %</span>
</div>
</div>

The trick as shown earlier by Mary Melody is to use a combination of absolute and relative positioning on the child span elements, .div1 and .div2.
To make sure that the top and bottom border edges line up exactly, apply display: inline-block
to the span child elements.
The trick is to keep .div2 in the flow with a 50% left margin, which provides space for .div1,
which will be absolutely positioned using right: 50%.
To control the spacing between the two span's, add a 1px right-margin to .div1 and to preserve
symmetry, use left: 1px on .div2.
.wrapper {
position: relative;
border: 1px dashed blue;
margin-bottom: 10px;
}
.div1, .div2 {
border: 1px dotted blue;
display: inline-block;
}
.div1 {
position: absolute;
right: 50%;
margin-right: 1px;
}
.div2 {
position: relative;
margin-left: 50%;
left: 1px;
}
<div class="block">
<div class="wrapper">
<span class="div1">2222</span>
<span class="div2">2 %</span>
</div>
<div class="wrapper">
<span class="div1">11</span>
<span class="div2">100 %</span>
</div>
<div class="wrapper">
<span class="div1">21</span>
<span class="div2">0 %</span>
</div>
</div>

i do not advise to use id, u can use classes because u can not repeat the same ids each time, below is the code updated and as well a live demo.
.wrapper_block{
text-align: center;
}
.wrapper_container span{
display: inline-block;
vertical-align: top;
}
.wrapper_container span span{
display: block;
}
.wrapper_left{
text-align: right;
}
.wrapper_right{
text-align: left;
}
.wrapper_left span{
border: 1px solid red;
margin-bottom: -1px;
}
.wrapper_right span{
border: 1px solid black;
margin-bottom: -1px;
}
<div class="wrapper_block">
<div class="wrapper_container">
<span class="wrapper_left">
<span>2222</span>
<span>11</span>
<span>21</span>
</span>
<span class="wrapper_right">
<span>2 %</span>
<span>100 %</span>
<span>0 %</span>
</span>
</div>
</div>

Just need to make the div1 and div2 to inline blocks and set a width for them and also text-align to the different alignments.
Simple example
#div1 {
border: 1px solid red;
display:inline-block;
width: 50px;
text-align:right;
}

Related

Border keeps being under first line

I am trying to add border to a bottom of the text, however whenever the text has two lines, the border keeps under the first sentence. It should take the second sentence and move 15 px from it. How can I fix this?
The second red line should be under word dawdwada
.box {
width: 200px;
border: 1px solid black;
height: 200px;
}
.link {
position: relative;
margin-bottom: 20px;
}
.link a::after {
position: absolute;
content: "";
top: 15px;
left: 0;
height: 2px;
background: red;
width: 100%;
}
<div class="box">
<div class="title">
<div class="link">
<a>waddwaudwahidauidwa</a>
</div>
<div class="link">
<a>waddwaudwahidauidwa dawdwada </a>
</div>
</div>
</div>
instead of using ::after, you can just use border-bottom on link
Use text-decorations instead of borders
<a style="text-decoration: underline;">waddwaudwahidauidwa dawdwada </a>
go to :enter link description here

Keeping Element with Absolute Position inside its container in center-left

.container {
display: inline-block;
width: 150px;
border: 1px solid black;
}
.letter {
position: absolute;
top: 0;
left: 0;
font-size: 21px;
}
<div class='container'>
<p class='letter'>A</p>
<p class='word'>A pple</p>
</div>
<div class='container'>
<p class='letter'>B</p>
<p class='word'>B anana</p>
</div>
<div class='container'>
<p class='letter'>C</p>
<p class='word'>C arrot</p>
</div>
I know That this Design is stupid and can be made easily,
But I want to learn using it How I can I make the .letter position same as the First Letter using position: absolute; left:0; top: 0;,
I just want to place it in middle-left with no-padding or spacing or marging at all.
Something like vertical-align: middle; text-align: left; But with the effect of Absolute position of no spacing at all.
But it keeps moving all the letters to left of page above itself instead of the parent element itself after adding left: 0;
How can I do that?
you can add position:relative to .container
.container {
position: relative;
display: inline-block;
width: 150px;
border: 1px solid black;
}
.letter {
position: absolute;
top: 0;
left: 0;
font-size: 21px;
}
<div class='container'>
<p class='letter'>A</p>
<p class='word'>A pple</p>
</div>
<div class='container'>
<p class='letter'>B</p>
<p class='word'>B anana</p>
</div>
<div class='container'>
<p class='letter'>C</p>
<p class='word'>C arrot</p>
</div>

How to make 1 div centre align and other float right using CSS [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 5 years ago.
I want to make my div2 to centre align and div3 to be at right.
I tried doing that with text align: center for main div and making float right to div3 but it is making it center align by considering main div's remaining part. I have given display: inline-flex to main div
<div style="height: 40px;width:120px;background-color: yellow;align-items: center;">
<div style="height: 20px;width:20px;background-color: red;">
Hello
</div>
<div style="height: 20px;float: right;width:20px;background-color: red;">
</div>
</div>
Please try with this code:
<div style="height: 40px;width:120px;background-color: yellow;align-items: center; position:relative;">
<div style="height: 20px;width:40px;background-color: red; overflow:auto; margin:0 auto">
Hello
</div>
<div style="height: 20px;position:absolute; right:0px; top:0px; width:20px;background-color: red;">
</div>
</div>
.main {
display: block;
position: relative;
width:100%;
text-align: center;
border: 1px solid red;
}
.main .div1 {
display: inline-block;
border: 1px solid;
}
.main .div2 {
float: right;
border: 1px solid;
display: inline-block;
}
<div class="main">
<div class="div1">
div1
</div>
<div class="div2">
div2
</div>
</div>
Divs are block level elements, so you can use a margin of auto on the left and right to place it in the middle.
.center {
margin: 0 auto;
}
.right {
float: right;
}
In the HTML you will need to adjust the ordering of the divs. Put div 3 before div 2 so that when you float it, they appear on the same line:
<div class="outer">
<div class="right"></div>
<div class="center"></div>
</div>
https://jsfiddle.net/dcqpw12u/1/
You can use position:relative for the main, and position:absolute to the other div, and it also centers it vertically
.main {
text-align: center;
background-color: red;
height: 50px;
position: relative;
}
.div2 {
background-color: blue;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.div3 {
background-color: green;
position: absolute;
right: 0;
top: 50%;
transform: translate(0, -50%);
}
<div class="main">
<div class="div2">SOME DIV 2</div>
<div class="div3">SOME DIV 3</div>
</div>
Add style="margin: auto;" to your div2 element. And
style="margin-left: auto;" to your div3 element.
<div style="height: 40px;width:120px;background-color: yellow;align-items: center;">
<div style="margin:auto; height: 20px;width:20px;background-color: red;">
Hello
</div>
<div style="margin-left:auto; height: 20px;float: right;width:20px;background-color: red;">
</div>
</div>
.contentmain{
background: white none repeat scroll 0 0;
color: black;
height: auto;
width: 35%;
float: left;
background:red;
}
.contentCenter{
background: white none repeat scroll 0 0;
color: black;
height: auto;
width: 30%;
float: left;
background:yellow;
}
.contentRight{
background: white none repeat scroll 0 0;
color: black;
height: auto;
width: 35%;
float: right;
background:red;
}
<div class="contentmain">
Main<br/>
Content<br/>
</div>
<div class="contentCenter">
Center<br/>
Content<br/>
</div>
<div class="contentRight">
Right<br/>
Content<br/>
</div>
This might be fulfill your requirement.
<!DOCTYPE html>
<head>
<style>
.div0 {
text-align: center;
border-style: solid;
border-width: 5px;
height: 50px;
border-color: red;
position: relative ;
}
.div1 {
border-style: solid;
border-width: 4px;
right: 0%;
height: 40px;
width:40px;
border-color: green;
position: absolute;
}
.div2 {
left: 50%;
right:50%;
width:40px;
position: absolute;
border-style: solid;
height: 40px;
border-width: 4px;
border-color: green;
}
</style>
</head>
<body>
<div class="div0">
<div class="div1"><p>div1</p></div>
<div class="div2"><p>div2</p></div>
</div>
</body>
</html>
basically you can achieve this by using the position property and the right and left properties of CSS which you can refer to more on
Position and right property left property could be found on the site.
what i've done in my answer is set the main div as position relative and the other sub divs(div2 and div3) as absoulute
To get one div to the right most corner you set the right property to 0%
and to center a div i used 50% on both right and left properties.

'Float' right a column when using display: table?

Im using display table for a 3 column section of my site. Im doing this as I need to vertically center the content of the first column.
How can I move the third column to the right while still keeping its text left aligned?
<span class="cont">
<span class="one">One</span>
<span class="two">One</span>
<span class="three">
<div class="a">Some text and A</div>
<div class="b">Some more text and B</div>
</span>
</span>
.cont {
display: table;
border: 1px solid black;
width: 100%;
}
.one,
.two,
.three {
display: table-cell;
}
.one {
background: grey;
vertical-align: middle;
}
.two {
background: green;
}
http://codepen.io/anon/pen/azbmrP
One simple solution is to use float: right to .three class and vertical-align: top to .two class:
.cont {
display: table;
border: 1px solid black;
width: 100%;
}
.one,
.two,
.three {
display: table-cell;
}
.one {
background: grey;
vertical-align: middle;
}
.two {
background: green;
vertical-align: top;
}
.three {
float: right;
}
<span class="cont">
<span class="one">One</span>
<span class="two">One</span>
<span class="three">
<div class="a">Some text and A</div>
<div class="b">Some more text and B</div>
</span>
</span>
Use
.three
{
float:right;
}
http://codepen.io/anon/pen/raNMXr
.table { display: table; margin-left: auto;}
DEMO
<span class="cont">
<span class="one">One</span>
<span class="two">One</span>
<span class="three">
<div class="set">
<div class="a">Some text and A</div>
<div class="b">Some more text and B</div>
</div>
</span>
</span>
.set{ float:right;}
Here is a solution:
.three > div {
float: right;
}
and you can put a <br>tag after first div.
Link: http://jsfiddle.net/7081odd1/

How to center a fontawesome icon inside a div element

I have a div with width: 180px; and height: 180px;.
How can I center a fontawesome icon horizontally and vertically inside the container?
Note: i may also add text below the icon
<div style="width:180px; height:180px; float:left;">
<i class="fa fa-search fa-4x"></i>
</div>
should look something like this:
Try this:
.centered{
position:relative;
top:50%;
left:50%;
transform:translate(-50%,-50%)
}
.container{
width:100px;
height:100px;
}
On mobile.
You can do it like this
#search{
background: gray;
width:180px;
height:180px;
float:left;
line-height: 180px;
text-align: center;
vertical-align: bottom;
}
#search > p {
margin-top: -155px;
}
Working example http://jsfiddle.net/kasperFranz/h91p8w4e/3/ (using icon instead of fa in the example, but shouldn't affect the result.)
I'm probably too late here, but it's a common question, so here's a simple working idea, the interesting side of it is that it adapts to the container size:
span{
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
position: absolute;
text-align: center;
width: 15px;
height: 15px;
display: block;
}
Asuming to my comment, here is a JSFIDDLE
Without setting relative or absolute
html
<div class="myDiv">
<div class="content_wrapper">
<!--i class="fa fa-search fa-4x">test</i-->
<i class="icon-search icon-4x"></i><br />
<span class="myText">Search</span>
</div>
</div>
css
.myDiv{
width: 180px;
height: 180px;
float: left;
background: #ccc;
text-align: center;
display: table;
}
.content_wrapper{
display: table-cell;
vertical-align: middle;
}
.myText{
font-weight: bold;
font-size: 20px;
}
CSS
/* This parent can be any width and height */
.block {
text-align: center;
background:#ccc;
margin-bottom:10px;
}
/* The ghost, nudged to maintain perfect centering */
.block:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can
also be of any width and height */
.centered {
display: inline-block;
vertical-align: middle;
width: 300px;
}
HTML
<div class="block" style="height: 300px;">
<div class="centered">
<h1>Some text</h1>
<p>p1</p>
</div>
</div>
<div class="block" style="height: 200px;">
<div class="centered">
<h1>Some text</h1>
<p>p2</p>
</div>
</div>
<div class="block" style="height: 600px;">
<div class="centered">
<h1>Some text</h1>
<p>p3</p>
</div>
</div>
DEMO
Update 1:
DEMO
I know it's a bit late and you probably already figured this out.
I was able to achieve what you were looking for by wrapping the icon and text in a div then adding padding to that.
See the attached pen.
http://codepen.io/ForTheJim/pen/YPxveR
<p data-height="268" data-theme-id="0" data-slug-hash="YPxveR" data-default-tab="result" data-user="ForTheJim" class='codepen'>See the Pen <a href='http://codepen.io/ForTheJim/pen/YPxveR/'>Centering Icon Question</a> by Jim (<a href='http://codepen.io/ForTheJim'>#ForTheJim</a>) on <a href='http://codepen.io'>CodePen</a>.</p>
<script async src="//assets.codepen.io/assets/embed/ei.js"></script>
Short way just add display: grid; place-items: center; to your div and your ready to go.
like this:
<div style="width:180px; height:180px; display: grid; place-items: center;">
<i class="las la-user-circle"></i>
</div>

Resources