Vertically Align Divs - html

I am trying to vertically align a div in my code but with no success. This div contains sub divs. The first one
I want this to look like this :
but at the moment it is not aligned. This is my HTML code :
body {
display: block;
margin-left: auto;
margin-right: auto;
background: #f1f1f1;
}
.content {
float: left;
margin: 20px auto;
text-align: center;
width: 300px;
}
.content h1 {
text-transform: uppercase;
font-weight: 700;
margin: 0 0 40px 0;
font-size: 25px;
line-height: 30px;
}
.circle {
width: 100px;
height: 100px;
line-height: 100px;
border-radius: 50%;
/* the magic */
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
text-align: center;
color: white;
font-size: 16px;
text-transform: uppercase;
font-weight: 700;
margin: 0 auto 20px;
}
.blue {
background-color: #052D72;
}
.green {
background-color: #16a085;
}
.red {
background-color: #e74c3c;
}
<div class="content">
<div class="circle blue"></div>
</div>
<div class="content">
<div class="circle blue">Blue</div>
<div class="circle blue"></div>
<div class="circle blue"></div>
<div class="circle blue"></div>
</div>
<div class="content">
<div class="circle blue"></div>
<div class="circle blue"></div>
</div>
So, in the .content I tried adding this :
vertical-align:baseline;
but I saw no difference.

Add display:inline-block & Remove float for #content
.content {
display: inline-block;
margin: 20px auto;
text-align: center;
vertical-align: middle;
width: 200px;
}
https://jsfiddle.net/k0fx384a/1/
EDIT with class: https://jsfiddle.net/k0fx384a/2/

You have used same #id with multiple elements. That is not allowed in HTML across all browsers(seems like IE and FF allow multiple #ids).
So just change all the occurances of id="content" to class="content" and the CSS should start working.
DEMO

change <div id="content"> to <div class="content"> so the styles will be applied.

If you want them both vertically and horizontally aligned, I would recommend using flex. This offers more flexibility and is more forward-facing.
Mozilla Docs on Flex
If you use the rules align-items and justify-content, you'll get magic workings. Check out an example: https://jsfiddle.net/vrad7yuj/
.container {
display: flex;
flex-direction: row;
height: 100%;
width: 100%;
border: 2px solid #f00;
}
.col {
border: 2px solid #00f;
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.ball {
border-radius: 50%;
border: 1px solid #0f0;
height: 60px;
width: 60px;
}
<div class="container">
<div class="col">
<div class="ball"></div>
</div>
<div class="col">
<div class="ball"></div>
<div class="ball"></div>
<div class="ball"></div>
<div class="ball"></div>
</div>
<div class="col">
<div class="ball"></div>
<div class="ball"></div>
</div>
</div>

Alternative, if you want to do this with a little count of codelines, you can use flexbox:
body {
display: flex;
margin-left: auto;
margin-right: auto;
background: #f1f1f1;
}
.content {
display: flex;
flex-direction: column;
margin: 20px auto;
text-align: center;
width: 300px;
}
.content h1 {
text-transform: uppercase;
font-weight: 700;
margin: 0 0 40px 0;
font-size: 25px;
line-height: 30px;
}
.circle {
width: 100px;
height: 100px;
line-height: 100px;
border-radius: 50%;
/* the magic */
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
text-align: center;
color: white;
font-size: 16px;
text-transform: uppercase;
font-weight: 700;
margin: 0 auto 20px;
}
.blue {
background-color: #052D72;
}
.green {
background-color: #16a085;
}
.red {
background-color: #e74c3c;
}
<div class="content">
<div class="circle blue"></div>
</div>
<div class="content">
<div class="circle blue">Blue</div>
<div class="circle blue"></div>
<div class="circle blue"></div>
<div class="circle blue"></div>
</div>
<div class="content">
<div class="circle blue"></div>
<div class="circle blue"></div>
</div>
Take a look on flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Should just be an alternative solution and new knowledge for you. ;-) Cheers.

Related

align div horizontally after screen resize using css

I'm trying to align div horizontally as the browser resizes, currently, I have 3 divs. As per the requirement, I can add an additional div. My problem is as soon I increase the window size above 2500, the right side of the screen becomes empty & all the divs are floating to left. As I cannot set the div width to 30-33% as per the requirement. Below is my code. kindly help.
div.box-container {
mc-grid-row: true;
margin-left: auto;
margin-right: auto;
float: left;
display: flex;
width: 100%
}
div.box {
float: left;
background-color: #ffffff;
position: relative;
padding: 10px;
box-sizing: border-box;
height: 326px;
margin-left: 20px;
margin-bottom: 0;
top: 55px;
border-top-right-radius: 0px;
width: 30%;
border: 1px solid #cccccc;
}
<div class="box-container">
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
</div>
As #Arman Ebrahimi had already mentioned correctly. Use flex box only. The issue of responsibility can be handled well with media queries.
Working example
* {
box-sizing: border-box;
}
div.box-container {
display: flex;
flex-wrap: wrap;
font-size: 30px;
text-align: center;
gap: 10px;
width: 80%;
margin: 0 auto;
/* or use justify-content: center; */
}
.box {
background-color: #f1f1f1;
padding: 10px;
flex: 30%;
border: 1px solid #cccccc;
word-break: break-word;
height: 326px;
}
#media (max-width: 800px) {
.box {
flex: 100%;
}
}
<div class="box-container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
Remove float and only use flex:
* {
box-sizing: border-box;
}
body,
html {
margin: auto;
}
div.box-container {
mc-grid-row: true;
margin-left: auto;
margin-right: auto;
display: flex;
width: 100%;
}
div.box {
background-color: #ffffff;
padding: 10px;
height: 326px;
margin-left: 20px;
margin-bottom: 0;
top: 55px;
border-top-right-radius: 0px;
width: calc(100vw / 3);
/*calc(100vw / number of div)*/
border: 1px solid #cccccc;
word-break: break-word;
}
<div class="box-container">
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
</div>
Use justify-content: center; when you are using flex. This means the flexed contents will always be centered on all screen types.
div.box-container {
mc-grid-row: true;
margin-left: auto;
margin-right: auto;
display: flex;
gap: 10px;
justify-content: center;
width: 100%
}
div.box {
background-color: #ffffff;
position: relative;
padding: 10px;
box-sizing: border-box;
height: 326px;
margin-bottom: 0;
top: 55px;
border-top-right-radius: 0px;
width: 33.33%;
border: 1px solid #cccccc;
}
body {
margin: 0;
}
<div class="box-container">
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
</div>
Edit ~ add another div, reduce the % the div covers. Demonstrate min-width responsiveness.
div.box-container {
mc-grid-row: true;
margin-left: auto;
margin-right: auto;
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
width: 100%
}
div.box {
background-color: #ffffff;
position: relative;
padding: 10px;
box-sizing: border-box;
height: 326px;
margin-bottom: 0;
top: 55px;
border-top-right-radius: 0px;
width: 24%;
min-width: 300px;
border: 1px solid #cccccc;
}
body {
margin: 0;
}
<div class="box-container">
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
<div class="box">
<p>jfn,mnfngf,mn,mgfnbgnkjdkjgkdg</p>
</div>
</div>

How to equally center a div into another div?

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.

display products inline - special ID's required?

I'm doing front end development for a project between some friends.
He is building the backed in rails and has templates that will generate the products and put the content in but I'm running into a situation where I'm not sure how to make them align properly?
This is a web based platform.
Here is the situation with the products. I have three for example. I created a product container that will hold them. I have the first one showing perfectly, then when I copy pasted another two (just to see them) they don't generate.
<div class="main-bkg">
<div class="card-row">
<div class="product-cont">
<div class="product-holder">
<div class="product-img"> <img src="img/box.jpg"> </div>
<div class="product-name">prod1</div>
<div class="product-info">
<div class="product-price">$99</div>
<div class="sep">-</div>
<div class="product-desc">box</div>
</div>
<div class="product-qty">
<div class="qty-sub">-</div>
<div class="qty-amount">1</div>
<div class="qty-add">+</div>
</div>
</div>
</div>
<div class="product-cont">
<div class="product-holder">
<div class="product-img"> <img src="img/circle.jpg"> </div>
<div class="product-name">prod2</div>
<div class="product-info">
<div class="product-price">$99</div>
<div class="sep">-</div>
<div class="product-desc">circle</div>
</div>
<div class="product-qty">
<div class="qty-sub">-</div>
<div class="qty-amount">1</div>
<div class="qty-add">+</div>
</div>
</div>
</div>
<div class="product-cont">
<div class="product-holder">
<div class="product-img"> <img src="img/tri.jpg"> </div>
<div class="product-name">prod3</div>
<div class="product-info">
<div class="product-price">$99</div>
<div class="sep">-</div>
<div class="product-desc">triangle</div>
</div>
<div class="product-qty">
<div class="qty-sub">-</div>
<div class="qty-amount">1</div>
<div class="qty-add">+</div>
</div>
</div>
</div>
</div>
</div>
The css is build in SASS, and I'll post in that for easy reading. If you want the css export I can show it.
I assume they are overlapping. I'm not totally sure an easy work around this other then giving each product a special ID and then applying styling to it.
.main-bkg {
padding-top: 165px;
height: 100vh;
background-color: #ebf0f1;
.card-row {
padding-top: 15px;
padding-bottom: 15px;
.product-cont {
padding-left: 30px;
padding-right: 30px;
.product-holder {
background-color: white;
height: 350px;
width: 200px;
border-radius: 20x;
.product-img {
img {
display: block;
height: 240px;
width: 170px;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
}
}
.product-name {
text-align: center;
}
.product-info {
display: block;
height: 30px;
width: 100px;
text-align: center;
margin-left: auto;
margin-right: auto;
font-weight: 700;
.product-price {
display: inline-block;
text-align: center;
float: left;
}
.sep {
display: inline-block;
text-align: center;
}
.product-desc {
display: inline-block;
text-align: center;
float: right;
}
}
.product-qty {
display: block;
border-radius: 3px;
border: 1px solid $prime-color;
margin-left: auto;
margin-right: auto;
width: 100px;
.qty-sub {
color: $prime-color;
display: inline-block;
float: left;
width: 30px;
height: 25px;
text-align: center;
}
.qty-amount {
display: inline-block;
color: $prime-color;
font-size: 20px;
text-align: center;
width: 40px;
height: 25px;
}
.qty-add {
color: $prime-color;
display: inline-block;
text-align: center;
float: right;
width: 30px;
height: 25px;
}
}
}
}
}
}
Have you tried to put a float:left in the container to let the container float?
.product-cont {
padding-left: 30px;
padding-right: 30px;
float: left;
}
Check if this Codepen with SASS ready solves your problem

How to put bull inside css circle

I need to create a line with a few circles inside and with a dot inside these circles.. They should look like radio buttons - how can I align the dot vertically?
JSfiddle
HTML
<div class="round" id="round-vertically">
<div class="circle img1" id="circle-star"><span>•</span></div>
<div class="circle-line"></div>
<div class="circle img2" id="circle-key"><span>•</span></div>
<div class="circle-line"></div>
<div class="circle img3" id="circle-cursor"><span>•</span></div>
<div class="circle-line"></div>
<div class="circle img4" id="circle-mobile"><span>•</span></div>
</div>
Don't use characters, create it by css:
HTML:
<div class="eye"></div>
CSS:
.eye{
border: 2px solid red;
border-radius: 100%;
position: relative;
width: 2em; height: 2em;
}
.eye::before{
content: "";
display: block;
position: absolute;
width: 1em; height: 1em;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
background: red;
border-radius: 100%;
}
http://codepen.io/anon/pen/ZOaBKg
Flexbox can do that:
.circle {
width: 15px;
height: 15px;
border: 1px #d3d4de solid;
margin: auto;
display: flex;
justify-content: center;
align-items: center;
}
.circle-line {
width: 1px;
background-color: #d3d4de;
height: 191px;
margin: auto;
}
.circle {
width: 15px;
height: 15px;
border: 1px #d3d4de solid;
margin: auto;
display: flex;
justify-content: center;
align-items: center;
}
#round-vertically {
padding-top: 95px;
}
#round-vertically SPAN {
color: #d3d4de;
text-align: center;
font-size: 30px;
}
.circle {
border-radius: 50%;
}
<div class="round" id="round-vertically">
<div class="circle img1" id="circle-star"><span>•</span>
</div>
<div class="circle-line"></div>
<div class="circle img2" id="circle-key"><span>•</span>
</div>
<div class="circle-line"></div>
<div class="circle img3" id="circle-cursor"><span>•</span>
</div>
<div class="circle-line"></div>
<div class="circle img4" id="circle-mobile"><span>•</span>
</div>
</div>
One approach is by using translate css property
#round-vertically SPAN {
color: #d3d4de;
font-size:30px;
left: 50%;
position: absolute;
font-size: 30px;
top: 50%;
transform: translate(-50%,-50%);
}
Updated fiddle
You can try below code:
Working demo
.circle {
width: 15px;
height: 15px;
border:1px #d3d4de solid;
margin: auto;
position:relative;
}
#round-vertically SPAN {
color: #d3d4de;
text-align:center;
font-size:30px;
position:absolute;
top:50%; left:50%;
transform: translate(-50%, -50%);
}
Add text-align:center in class 'circle'. Use line-height property line-height: 50%. See below snippet:
.circle-line {
width: 1px;
background-color: #d3d4de;
height: 191px;
margin: auto;
}
.circle {
width: 15px;
height: 15px;
border:1px #d3d4de solid;
margin: auto;
text-align: center;
}
#round-vertically {
padding-top:95px;
}
#round-vertically SPAN {
color: #d3d4de;
font-size:30px;
line-height: 50%
}
.circle {
border-radius: 50%;
}
<div class="round" id="round-vertically">
<div class="circle img1" id="circle-star"><span>•</span></div>
<div class="circle-line"></div>
<div class="circle img2" id="circle-key"><span>•</span></div>
<div class="circle-line"></div>
<div class="circle img3" id="circle-cursor"><span>•</span></div>
<div class="circle-line"></div>
<div class="circle img4" id="circle-mobile"><span>•</span></div>
</div>

How to keep a specific element in the middle?

I have a row of elements, but one of them, which is in the middle, should be centered. Try to run this simple snippet, the "Must be in the middle" thing is not in the middle, but I would like it to be, despite the sizes of things around it. The "text-align:center" won't help, because it puts the entire list of elements in the middle and it's not aware that I want the "the-middle" thing to be in the middle:
.the-whole {
width: 100%;
text-align: center;
}
.side-thing {
display: inline-block;
margin: 8px;
}
.the-center {
display: inline-block;
font-weight: bold;
}
<div class="the-whole">
<div class="side-thing">
long thing at left
</div>
<div class="side-thing">
a
</div>
<div class="side-thing">
b
</div>
<div class="the-center">
(Must be in the Middle)
</div>
<div class="side-thing">
c
</div>
<div class="side-thing">
d
</div>
</div>
If you can change HTML, than you can move left and right elements inside centered one:
.the-whole {
width: 100%;
text-align: center;
}
.side-thing {
display: inline-block;
}
.the-center {
display: inline-block;
position: relative;
margin: 8px;
padding: 0 10px;
}
.the-center span {
font-weight: bold;
}
.left {
position: absolute;
white-space: nowrap;
right: 100%;
top: 0;
}
.right {
position: absolute;
white-space: nowrap;
left: 100%;
top: 0;
}
<div class="the-whole">
<div class="the-center">
<div class="left">
<div class="side-thing">
long thing at left
</div>
<div class="side-thing">
a
</div>
<div class="side-thing">
b
</div>
</div>
<span>(Must be in the Middle)</span>
<div class="right">
<div class="side-thing">
c
</div>
<div class="side-thing">
d
</div>
</div>
</div>
</div>
Solution using display:flex.
.the-whole {
display: flex;
}
.the-whole div {
display: inline;
}
.the-whole > div {
flex: 1;
border: 1px solid green;
}
.the-whole > div.center {
text-align: center;
}
<div class="the-whole">
<div class="left">
<div>
long thing at left
</div>
<div>
a
</div>
<div>
b
</div>
</div>
<div class="center">
<div>
(Must be in the Middle)
</div>
</div>
<div class="right">
<div>
c
</div>
<div>
d
</div>
</div>
</div>
You can use flexbox:
.the-whole {
display: flex;
}
.side {
flex: 1; /* Distribute remaining width equally among the left and right parts */
}
.the-left {
text-align: right;
}
.the-right {
text-align: left;
}
.side-thing {
display: inline-block;
margin: 0 8px;
}
.the-center {
font-weight: bold;
}
<div class="the-whole">
<div class="the-left side">
<div class="side-thing">long thing at left</div>
<div class="side-thing">a</div>
<div class="side-thing">b</div>
</div>
<div class="the-center">(Must be in the Middle)</div>
<div class="the-right side">
<div class="side-thing">c</div>
<div class="side-thing">d</div>
</div>
</div>
I think it's better to insert a new div. So you will have seven. only One in the center.
#page{
width: 100%;
text-align: center;
}
.the-whole {
width: 39%;
display: inline-block;
text-align: center;
}
.side-thing {
min-width: 10%;
display: inline-block;
text-align: center;
margin: 0px;
border: 0.1em solid red;
}
.side-thing-left{
min-width: 40%;
display: inline-block;
text-align: center;
margin: 8px;
border: 0.1em solid red;
}
.the-center {
width: 20%;
display: inline-block;
font-weight: bold;
border: 0.1em solid green;
}
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<id id="page">
<div class="the-whole">
<div class="side-thing-left">left</div>
<div class="side-thing">a</div>
<div class="side-thing">b</div>
</div>
<div class="the-center">(Must be in the Middle)</div>
<div class="the-whole">
<div class="side-thing-left">left</div>
<div class="side-thing">c</div>
<div class="side-thing">d</div>
</div>
</div>
</body>
</html>
Is this a solution?
.the-whole {
width: 100%;
text-align: center;
}
.side-thing {
float: left;
margin: 8px;
}
.the-center {
float: left;
font-weight: bold;
margin: 0 auto;
}