I want to align vertically 3 'title' divs. When the title is one word it must to be in the middle, when is longer must automatically align.
One example of what I want to achieve: http://jsfiddle.net/526TD/4/
My problem is that I have my divs in differents containers and not in a same div.
My HTML
<div class="related_news_filter">
<a href="/news/1">
<div class="round-image"></div>
<div>
<div class="title">title</div>
</div>
</a>
<a href="/news/2">
<div class="round-image">title longeeer</div>
<div>
<div class="title">title 2</div>
</div>
</a>
<a href="/news/3">
<div class="round-image"></div>
<div>
<div class="title">title with more words</div>
</div>
</a> </div>
My css
a {
display: inline-block;
width: 81px;
margin: 0 27px 0 8px;
}
.title {
margin-top: 7px;
width: 96px;
text-transform: uppercase;
line-height: 16px;
}
What I see now:
The word "new" and "inbound" are in the first line and not in the middle. They are not correctly vertical aligned.
Just give vertical-align: middle; to a
a {
display: inline-block;
width: 81px;
margin: 0 27px 0 8px;
vertical-align: middle;
}
.title {
margin-top: 7px;
width: 96px;
text-transform: uppercase;
line-height: 16px;
}
<div class="related_news_filter">
<a href="/news/1">
<div class="round-image"></div>
<div>
<div class="title">title</div>
</div>
</a>
<a href="/news/2">
<div class="round-image">title longeeer</div>
<div>
<div class="title">title 2</div>
</div>
</a>
<a href="/news/3">
<div class="round-image"></div>
<div>
<div class="title">title with more words</div>
</div>
</a> </div>
Flexbox can do that....
Support is IE10 and up.
.related_news_filter {
display: flex;
margin: 1em auto;
width: 80%;
justify-content: center;
}
a {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
flex: 0 0 96px;
margin: 0 27px 0 8px;
text-decoration: none;
}
.round-image {
display: flex;
justify-content: center;
align-items: center;
width: 81px;
flex: 0 0 81px;
margin-bottom: 1em;
background: #000;
border-radius: 50%;
color: white;
}
.title {
flex: 1;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
text-transform: uppercase;
}
<div class="related_news_filter">
<a href="/news/1">
<div class="round-image">title</div>
<div class="title">title</div>
</a>
<a href="/news/2">
<div class="round-image">title longeeer</div>
<div class="title">title 2</div>
</a>
<a href="/news/3">
<div class="round-image">title</div>
<div class="title">title with more words</div>
</a>
</div>
Note: This is not actually aligning the "titles" to one another. It merely lays out all the contents of the link 'containers' in the same way after making them all the same height.
There are many ways of doing this. The best method accroding to me would be to use the display:table method. Check the below fiddle -
http://jsfiddle.net/526TD/29/
.related_news_filter {
display: table;
}
a {
display: table-cell;
position: relative;
width: 81px;
margin: 0 27px 0 8px;
vertical-align: middle;
}
.title {
margin-top: 7px;
width: 96px;
text-transform: uppercase;
line-height: 16px;
text-align: center;
}
for more methods of achieving this go to the link -http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
Related
I am trying to create div which serves as my menu selection of my website. I want it to put into the center of my parent div. I did a lot of experiment but none of it work. Below is my CSS Code
.cell {
color: white;
font-size: .8rem;
padding: 1rem;
}
.lt-main-menu {
grid-area: main-menu;
background: deepskyblue;
height: 80vh;
}
.lt-menu{
background: black;
width: 100px;
height: 100px;
display: inline-block;
margin: 0 auto;
vertical-align:middle;
text-align:center;
/*
display:table-cell;
vertical-align:middle;
text-align:center;
*/
}
and this is my html file
<div class="cell lt-main-menu">
<div class="lt-menu">
menu 1
</div>
<div class="lt-menu">
menu 2
</div>
<div class="lt-menu">
menu 3
</div>
...
</div>
What i want is similar to picture below.
Hi i have created a pen in the CodePen app.
Using Flex you can easily center vertically and horizontally.
.lt-main-menu {
/*...*/
display:flex;
justify-content: center;
align-items: center;
}
This is the pen https://codepen.io/alessandroinfo/pen/JQPrYm
Try something like that, using flex properties:
<style type="text/css">
.cell {
height: 80vh;
display: flex;
align-items: center;
justify-content: center;
}
.lt-menu {
margin: 10px 0;
}
</style>
<div class="cell lt-main-menu">
<div class="cell-wrapper">
<div class="lt-menu">menu 1</div>
<div class="lt-menu">menu 2</div>
<div class="lt-menu">menu 3</div>
</div>
</div>
Try using display: flex to align items. This is a sample code. Hope this helps
HTML
<div class="d-flex flex-row align-items-start m-2">
<div class="container-block">
</div>
<div class="container-block">
</div>
<div class="container-block">
</div>
<div class="container-block">
</div>
</div>
<div class="d-flex flex-row align-items-center justify-content-center m-2">
<div class="container-block">
</div>
<div class="container-block">
</div>
<div class="container-block">
</div>
<div class="container-block">
</div>
</div>
CSS
.d-flex {
display: flex;
}
.flex-row {
flex-direction: row;
}
.align-items-start {
align-items: flex-start;
}
.align-items-center {
align-items: center;
}
.justify-content-center {
justify-content: center;
}
.container-block {
width: 100px;
height: 100px;
background: black;
margin: 0 5px;
}
.m-2 {
margin: 0.5rem;
}
JS Fiddle Link : https://jsfiddle.net/SJ_KIllshot/dbguqy6w/
This will do the job. I always find flexbox to work best for aligning things like this.
.cell {
color: white;
font-size: .8rem;
padding: 1rem;
}
.lt-main-menu {
width: 100%;
background: deepskyblue;
height: 80vh;
display: flex;
align-items: center;
justify-content: center;
}
.lt-menu {
background: black;
width: 100px;
height: 100px;
margin: 0 10px;
text-align: center;
display: inline-block;
}
Demo: https://jsfiddle.net/trethewey/qs1kvuf5/16/
Try this.
.cell {
position: relative;
color: white;
font-size: .8rem;
padding: 1rem;
}
.box-containers {
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
}
.lt-main-menu {
grid-area: main-menu;
background: deepskyblue;
height: 80vh;
}
.lt-menu{
background: black;
width: 100px;
height: 100px;
display: inline-block;
margin: 0 auto;
vertical-align:middle;
text-align:center;
/*
display:table-cell;
vertical-align:middle;
text-align:center;
*/
}
<div class="cell lt-main-menu">
<div class="box-containers">
<div class="lt-menu">
menu 1
</div>
<div class="lt-menu">
menu 2
</div>
<div class="lt-menu">
menu 3
</div>
</div>
</div>
You can also have some other references HERE.
I am trying to achieve the following:
The text inside the square (example text: "1/4") should be centered horizontally
and vertically.
The four squares should be spread with the same
distance over the main container (red).
The text (example text: "Name", "Longer Name" etc.) should be centered over the squares, and not affect the spreading of the squares (point 2)
What I got this far is the code below. I would greatly appreciate if somebody could point me in the correct direction!
.container {
background-color: red;
width: 320px;
height: 320px;
margin: 30px auto 30px auto;
}
.inner {
display: flex;
}
.inner-content {
display: block;
/* width: 25%;*/
margin-left: auto;
margin-right: auto;
background-color: green;
}
.text {
width: 25%;
text-align: center;
}
.square {
margin-top: 5px;
height: 25px;
width: 25px;
vertical-align: top;
text-align: center;
color: #fff;
font-size: 14px;
font-weight: bold;
text-align: center;
background-color: black;
box-shadow: 0 0 0 2px red, 0 0 0 5px black;
}
<div class="container">
<div class="inner">
<div class="inner-content">
<span class="text">Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Longer Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Yo</span>
<div class="square">
1/4
</div>
</div>
</div>
Take a look at the following example:
* {
box-sizing: border-box;
}
.container {
background-color: red;
width: 320px;
height: 320px;
margin: 30px auto 30px auto;
padding: 5px;
}
.inner {
display: grid;
grid-template-columns: repeat(4, 1fr); /* fix spreading */
grid-column-gap: 5px;
}
.inner-content {
background-color: green;
text-align: center;
}
.text {
width: 100%;
}
.square {
margin: 5px auto; /* fix box centering */
height: 25px;
width: 25px;
vertical-align: top;
text-align: center;
color: #fff;
font-size: 14px;
font-weight: bold;
background-color: black;
box-shadow: 0 0 0 2px red, 0 0 0 5px black;
line-height: 25px; /* fix vertical center */
}
<div class="container">
<div class="inner">
<div class="inner-content">
<span class="text">Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Longer Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Yo</span>
<div class="square">
1/4
</div>
</div>
</div>
i need my text elements to be in the middle of the div and beside my canvas element. currently, the texts are beside but aren't in the middle. how can i achieve this effect?
Btw, the canvas contains a chart plugin which i did not include in the code snippets below.
#user-count {
height: 100px;
width: 100px;
}
.card {
background-color: #f4f4f4;
border: none;
padding:10px;
display: inline-block;
}
.card-body {
font-weight: 600;
text-transform: uppercase;
display: inline-block;
}
<div class="row">
<div class="col-md-6">
<div class="card">
<canvas id="user-count"></canvas>
<div class="card-body">
<span>current blood donors</span>
<span>as of 2017</span>
</div>
</div>
</div>
Something like that with Flex :)
#user-count {
height: 100px;
width: 100px;
background-color: red;
}
.card {
background-color: #f4f4f4;
border: none;
padding: 10px;
display: inline-block;
display: flex;
justify-content: center;
align-items: center;
}
.card-body {
font-weight: 600;
text-transform: uppercase;
display: inline-block;
}
<div class="row">
<div class="col-md-6">
<div class="card">
<canvas id="user-count">
</canvas>
<div class="card-body">
<span>current blood donors</span>
<span>as of 2017</span>
</div>
</div>
</div>
You can add this css..
#user-count {
vertical-align: middle;
}
.card {
background-color: #f4f4f4;
border: none;
padding: 10px;
display: flex;
align-items: center;
}
So you can use display:flex instead of inline-block. flex is a bit similar to inline-block but not same and use align-items:center which centers all element to center vertically
I want to align text content and the avatar circle. Here are the classes that I have defined
.user-detail {
display: inline-block;
text-transform: uppercase;
text-align: right;
.user-detail__username {
margin: 18px 16px 0px 10px;
display: block;
float: left;
font-size: $font-size;
font-weight: 500;
}
.user-detail__role {
margin: 18px 16px 0px 10px;
display: block;
font-size: $font-size * 0.9;
font-style: normal;
line-height: 13px;
font-weight: 300;
}
.user-detail__avatar-circle {
display: inline-block;
margin: 8px 11px 0px 0px;
width: 45px;
height: 45px;
border-radius: 50%;
border: 1px solid $lightstroke;
}
.user-detail__avatar {
position: relative;
font-size: 51px;
line-height: 43px;
left:-4px;
}
}
and here is the HTML markup
<div class="user-detail right">
<div style="display:inline-block">
<span class="user-detail__username">
Adminstrator
</span>
<span class="user-detail__role">Adminstrator</span>
</div>
<div class="user-detail__avatar-circle">
<i class="material-icons user-detail__avatar">account_circle</i>
</div>
</div>
it shows like this here
I want to text and avatar circle should be bottom aligned. If I inspect element there is space on the top of text div. If I could remove this space problem will be solved. But I don't know how could I do that? Even changing the margin of text div not working? Any help?
As usual, what is a pain to achieve with traditional CSS (float, margin, inline-block, etc.) is a breeze with Flexbox.
.user-detail {
width: 300px;
height: 100px;
border: blue solid 2px;
display: flex;
align-items: center;
justify-content: center;
}
.user-detail>div {
border: green solid 2px;
}
.user-detail .details {
display: flex;
flex-direction: column;
}
<div class="user-detail right">
<div class="details">
<span class="user-detail__username">
Adminstrator
</span>
<span class="user-detail__role">Adminstrator</span>
</div>
<div class="user-detail__avatar-circle">
<i class="material-icons user-detail__avatar">account_circle</i>
</div>
</div>
You should use Flexbox is less pain.
Try making your
.user-detail {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
More about flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
The vertical-align: top setting normally align and positions your div element to top , in your case the "Administrator" text.
You need to change your html as (check the second <div> tag in the below code for the change),
<div class="user-detail right">
<div style="display:inline-block;vertical-align: top">
<span class="user-detail__username">
Adminstrator
</span>
<span class="user-detail__role">Adminstrator</span>
</div>
<div class="user-detail__avatar-circle">
<i class="material-icons user-detail__avatar">account_circle</i>
</div>
</div>
Hope this helps!
I am trying to display multiple circles on the same horizontal axis but with different width and height. The problem is that the circles are shrinked.
body,
html {
height: 100%;
margin: 0;
padding: 0;
}
.circles-container {
display: table;
border-spacing: 40px;
}
.row {
display: table-row;
}
.circle {
width: 100px;
height: 100px;
border: 1px solid #000;
border-radius: 50%;
text-align: center;
vertical-align: middle;
display: table-cell;
}
.cell {
display: table-cell;
}
.big-circle {
width: 300px;
height: 300px;
}
<div class="circles-container">
<div class="row">
<div class="cell">
<div class="circle">
<span>TEXT</span>
</div>
</div>
<div class="cell">
<div class="circle">
<span>TEXT</span>
</div>
</div>
<div class="cell">
<div class="big-circle circle">
<span>TEXT</span>
</div>
</div>
<div class="cell">
<div class="circle">
<span>TEXT</span>
</div>
</div>
<div class="cell">
<div class="big-circle circle">
<span>TEXT</span>
</div>
</div>
</div>
</div>
JSFIDDLE: https://jsfiddle.net/cxuxgy0u/
You should not use the table layout for this. Your HTML does not semantically represent a table, so table element is worng to use.
What you want to do can be achieved with Flexbox.
article {
display: flex;
align-items: center;
}
article > div + div {
margin-left: 1rem;
}
article > div {
flex-shrink: 0;
height: 4rem;
width: 4rem;
border-radius: 50%;
border: solid 1px black;
display: flex;
justify-content: center;
align-items: center;
}
article > div:nth-child(2) {
height: 6rem;
width: 6rem;
}
<article>
<div><span>TEXT</span></div>
<div><span>TEXT</span></div>
<div><span>TEXT</span></div>
<div><span>TEXT</span></div>
<div><span>TEXT</span></div>
<div><span>TEXT</span></div>
<div><span>TEXT</span></div>
<div><span>TEXT</span></div>
<div><span>TEXT</span></div>
</article>
You might want to read more about Flexbox on MDN.
A simple flexbox solution. Just be sure to set flex-shrink to 0, because the initial value is 1, which allows flex items to shrink when necessary to prevent overflowing the container.
body,
html {
height: 100%;
margin: 0;
padding: 0;
}
.circles-container {
display: flex;
align-items: center;
}
.circle {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
flex: 0 0 100px; /* flex-shrink: 0, to disable shrinking default */
height: 100px;
border-radius: 50%;
margin: 20px;
border: 1px solid #000;
}
.big-circle {
flex-basis: 300px;
height: 300px;
}
<div class="circles-container">
<div class="circle">
<span>TEXT</span>
</div>
<div class="circle">
<span>TEXT</span>
</div>
<div class="big-circle circle">
<span>TEXT</span>
</div>
<div class="circle">
<span>TEXT</span>
</div>
<div class="big-circle circle">
<span>TEXT</span>
</div>
</div>
https://jsfiddle.net/cxuxgy0u/7/
Try this:
HTML
<div class="container">
<div class="circle">Text</div>
<div class="circle">Text</div>
<div class="circle">Text</div>
<div class="circle">Text</div>
</div>
CSS
.container {
display:flex;
justify-content: space-around;
align-items: center;
height: 100vh;
}
.circle {
background: white;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
}
.circle:nth-child(odd) { width: 100px; height: 100px; }
.circle:nth-child(even) { width: 200px; height: 200px; }
Uses flexbox and is the simplest way to achieve what you want.
Here's a fiddle https://jsfiddle.net/itsag/sk3tdo4L/
Hope it helps!
I think your problem is found in the styling.
For each circle, you need to remove the style
display:table-cell
vertical-align: middle;
and then u need to bring in line-height. The line-height should be equal to the height of the circle, for for the smaller circle, you will have
line-height:100px //this brings the text to the middle of the circle vertically.
Then also, you need to increase the border-radius from 50% to 100%
border-radius:100%;
Therefore, your css will not look like this
body, html {
height: 100%;
margin: 0;
padding: 0;
}
.circles-container{
display: table;
border-spacing: 40px;
}
.row {
display: table-row;
}
.circle {
width: 100px;
height: 100px;
border: 1px solid #000;
border-radius: 100%;
text-align: center;
line-height:100px;
}
.cell {
display: table-cell;
}
.big-circle {
width: 300px;
height: 300px;
line-height:300px;
}
This should help you.
Flexbox:
container {
display: flex;
justify-content: center;
}
If you want space between the pictures, use:
margin-left:
or
margin-right:
try this
body, html {
height: 100%;
margin: 0;
padding: 0;
}
.circles-container{
display: table;
border-spacing: 40px;
}
.row {
display: flex;
}
.circle {
padding: 40px 30px;
border: 1px solid #000;
border-radius: 50%;
text-align: center;
vertical-align: middle;
position: relative;
}
.cell {
}
.big-circle {
padding: 150px;
}
<div class="circles-container">
<div class="row">
<div class="cell">
<div class="circle">
<span>TEXT</span>
</div>
</div>
<div class="cell">
<div class="circle">
<span>TEXT</span>
</div>
</div>
<div class="cell">
<div class="big-circle circle">
<span>TEXT</span>
</div>
</div>
<div class="cell">
<div class="circle">
<span>TEXT</span>
</div>
</div>
<div class="cell">
<div class="big-circle circle">
<span>TEXT</span>
</div>
</div>
</div>
</div>