How to equally center a div into another div? - html

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.

Related

How do I center my div as it always shift to the left side of my body

Aim:
To shift the whole container/division/div to the center of my body.
Problem:
The container is stuck on the left side of the body after I reduced its width.
I had tried putting justify-content: center in the "big_container", but it didn't work
Hope to see some good feedback from dear mentors!
Code snippet:
.main_title {
display: flex;
justify-content: center;
padding-bottom: 20px;
}
.big_container {
width: 500px;
}
.outside {
display: flex;
justify-content: center;
align-items: center;
background-image: linear-gradient(to bottom right, #3d7bcc, #2fedea);
border-top-right-radius: 10px;
border-top-left-radius: 10px;
height: 430px;
}
.paragraphs {
display: flex;
background-color: black;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
justify-content: center;
align-items: center;
}
.container_2 {
padding: 20px;
}
.text {
color: white;
}
<!DOCTYPE html>
<html>
<head>
<title>Sony Hedgehog</title>
<link href="sonic_style.css" rel="stylesheet">
<h1 class="main_title">Sony the Hedgehog</h1>
</head>
<body>
<div class="big_container">
<div class="outside">
<div class="image_container">
<!--useless class-->
<img src="./photo/sonic.jpeg" alt="A photo of sonic">
</div>
</div>
<div class="paragraphs">
<div class="container_2">
<div class="text_1 text">
<p><b>Game:</b>Sonic the Hedgehog</p>
</div>
<div class="text_2 text">
<p><b>Year:</b>1991</p>
</div>
<div class="text_3 text">
<p><b>Platform:</b>SEGA</p>
</div>
</div>
</div>
</div>
</body>
</html>
You only need to just add margin-left and margin-right to auto in .big_container class just like the following.
.big_container{
width: 500px;
margin-left: auto;
margin-right: auto;
}
You can try this:
<div class="big_container">
<div class="">
<div class="outside">
<div class="image_container">
<!--useless class-->
<img src="./photo/sonic.jpeg" alt="A photo of sonic">
</div>
</div>
</div>
<div class="paragraphs">
<div class="container_2">
<div class="text_1 text">
<p><b>Game:</b>Sonic the Hedgehog</p>
</div>
<div class="text_2 text">
<p><b>Year:</b>1991</p>
</div>
<div class="text_3 text">
<p><b>Platform:</b>SEGA</p>
</div>
</div>
</div>
</div>
<style>
.main_title{
text-align: center;
padding-top: 10px;
}
.big_container {
/* width: 500px; */
margin-top: 50px;
margin-bottom: 50px;
margin-right: auto;
margin-left: auto;
max-width: 500px;
}
.outside {
display: flex;
justify-content: center;
align-items: center;
background-image: linear-gradient(to bottom right, #3d7bcc, #2fedea);
border-top-right-radius: 10px;
border-top-left-radius: 10px;
height: 430px;
}
.paragraphs {
display: flex;
background-color: black;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
justify-content: center;
align-items: center;
}
.container_2 {
padding: 20px;
}
.text {
color: white;
}
I edited my answer, because Vishnu Vinod Answer is best way to do it.
also in future I would suggest you to use & learn Bootstrap it has great documentation and is easy to learn how to use rows and cols to get wanted effect. It would be really easy to solve this problem with bootstrap
https://getbootstrap.com/docs/4.0/layout/grid/

How to make child grow and wrap below first element?

I have a parent, that contains 2 child elements, the second contains 3 (grey) other child elements that a set to automatically wrap, only problem is that the second container doesn't wrap under the first, since It's a bit hard to explain I've recreated the exact issue, here's a jsfiddle since the container can be dragged to better understand the issue.
Desired result:
[box] [ 1 ] [ 2 ] [ 3 ]
-----------------------
[box] [ 1 ] [ 2 ]
[ 3 ]
body { background-color: #20262e }
.parent { display: flex; }
.pick { margin-right: 20px; min-width: 307px; min-height: 330px; width: 307px; height: 330px; background-color: #ffffff; display: inherit; justify-content: center; align-items: center; }
.others { display: inherit; flex-wrap: wrap; }
.others > .container { display: inherit; justify-content: center; align-items: center; background-color: #ddd; margin-bottom: 20px; min-width: 307px; min-height: 330px; width: 307px; height: 330px; margin-right: 20px; }
<div class="parent">
<div class="pick">
<h2>first box</h2>
</div>
<div class="others">
<div class="container">
<h2>...</h2>
</div>
<div class="container">
<h2>...</h2>
</div>
<div class="container">
<h2>...</h2>
</div>
</div>
</div>
What you're trying to do isn't possible as long as you have your pick div separate to the others - you would need to combine the three container divs with your pick div (like the snippet below).
body { background-color: #20262e }
.parent { display: flex; flex-wrap:wrap; }
.pick { margin-right: 20px; min-width: 307px; min-height: 330px; width: 307px; height: 330px; background-color: #ffffff; display: inherit; justify-content: center; align-items: center; margin-bottom: 20px; }
.parent > .others { display: inherit; justify-content: center; align-items: center; background-color: #ddd; margin-bottom: 20px; min-width: 307px; min-height: 330px; width: 307px; height: 330px; margin-right: 20px; }
<div class="parent">
<div class="pick">
<h2>first box</h2>
</div>
<div class="others">
<h2>...</h2>
</div>
<div class="others">
<h2>...</h2>
</div>
<div class="others">
<h2>...</h2>
</div>
</div>
Why do you use the flex display on parent element?
What about using directly block display on parent element and define a fix width.
HTML:
<div class="parent">
<div class="pick">
<h2>first box</h2>
</div>
<div class="others">
<div class="container">
<h2>1</h2>
</div>
<div class="container">
<h2>2</h2>
</div>
<div class="container">
<h2>3</h2>
</div>
</div>
</div>
CSS:
body { background-color: #20262e }
.parent { display: block; width: 307px; }
.pick { margin-right: 20px; min-width: 307px; min-height: 330px; width: 307px; height: 330px; background-color: #ffffff; display: inherit; justify-content: center; align-items: center; }
.others { display: inherit; }
.others > .container { display: inherit; justify-content: center; align-items: center; background-color: #ddd; margin-bottom: 20px; min-width: 307px; min-height: 330px; width: 307px; height: 330px; margin-right: 20px; }
You can only wrap the contents that are inside the div. If you want to do so all the components should be under the same div. Check the js fiddle.
enter code herehttp://jsfiddle.net/L91kebox/9/

Aligning alternating items to different places in a container with CSS positioning via flexbox or grid?

Is it possible to layout the following markup to be like the linked screenshot? Of course it would be easy to rearrange the HTML, but how might I start to approach it with only CSS?
<div class="container">
<div class="foo"></div>
<div class="bar"></div>
<div class="foo"></div>
<div class="bar"></div>
<div class="foo"></div>
<div class="bar"></div>
</div>
This CSS doesn't quite get there, but it's close (sort of).
.container{
width: 60%;
margin: 0 auto;
background-color: #ddd;
display: flex;
flex-flow: column nowrap;
}
.foo, .bar {
border: 1px solid black;
}
.foo{
width: 100px;
height: 40px;
background-color: #555;
align-self: flex-start;
}
.bar{
width: 450px;
height: 100px;
background-color: gray;
align-self: flex-end;
order: 2;
}
https://jsfiddle.net/joeashworth/h90nc2qL/3/
I know you mentioned not wanting to change the HTML but this is how I would go about creating this layout.
.container{
width: 60%;
margin: 0 auto;
background-color: #ddd;
display: flex;
flex-flow: column nowrap;
justify-content: space-between;
}
.boxed {
display: flex;
justify-content: space-around;
}
.foo, .bar {
border: 1px solid black;
}
.foo{
width: 100px;
height: 40px;
background-color: #555;
align-self: flex-start;
}
.foo-2 {
margin-top: -60px;
}
.foo-3 {
margin-top: -120px;
}
.bar{
width: 450px;
height: 100px;
background-color: gray;
align-self: flex-end;
order: 2;
margin-bottom: 10px;
}
<div class="container">
<div class="boxed">
<div class="foo foo-1"></div>
<div class="bar"></div>
</div>
<div class="boxed">
<div class="foo foo-2"></div>
<div class="bar"></div>
</div>
<div class="boxed">
<div class="foo foo-3"></div>
<div class="bar"></div>
</div>
</div>

Align elements with different heights on the same row

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>

some issue with margin in flex container

need some help. How to fix bug with .half-img2{ margin-top: 10px; }
http://prntscr.com/94uqok
These 2 imgs height must be equal to main-img
http://plnkr.co/edit/Dvj5HfG6hJqvYPxr0ljJ?p=preview
Html:
<style type="text/css">
.test{
display: flex;
}
.test>div{
flex: 1;
}
.test .main-img{
flex-grow: 2;
}
img{
width: 100%;
}
.half-img{
margin-left: 10px;
}
.half-img2{
margin-top: 10px;
}
</style>
<div class="test">
<div class="main-img">
<img src="http://fakeimg.pl/350x200/00CED1/FFF/?text=img+placeholder">
</div>
<div class="half-img">
<div class="half-img1">
<img src="http://fakeimg.pl/350x200/00CED1/FFF/?text=img+placeholder">
</div>
<div class="half-img2">
<img src="http://fakeimg.pl/350x200/00CED1/FFF/?text=img+placeholder">
</div>
</div>
</div>
I'll ignore the images sizes as these are not really relevant to the div layout issue.
A judicious use of margins and flex-column div layout seems to be required.
Layout would be something like this.
* {
box-sizing: border-box;
}
.test {
display: flex;
width: 80%;
margin: 1em auto;
border:1px solid green;
}
img {
display: block;
}
.test div {
}
.main-img {
flex:2;
margin-right: 10px;
background: lightblue;
}
.half-img {
display: flex;
flex-direction: column;
height: 250px;
}
.half-img {
flex:1;
display: flex;
flex-direction: column;
}
.half-img div {
flex:1;
background: lightblue;
}
.half-img1 {
margin-bottom: 5px;
}
.half-img2 {
margin-top: 5px;
}
<div class="test">
<div class="main-img">
</div>
<div class="half-img">
<div class="half-img1">
</div>
<div class="half-img2">
</div>
</div>
</div>