Responsive CSS Approach - css

The figure below is an illustration of what Im trying to get. The 1st figure represents the longer width and the 2nd figure represents the shorter width.
All the red blocks stays at the right and left position and the yellow block should follow the width of the container.
1: http://i.stack.imgur.com/6bHTo.jpg
heres my current approach
/* the one with black border :) */
.container{
position: relative;
}
/* red blocks have auto heights depends on its content */
.red{
position: absolute;
top: 0;
width: 100px;
}
.red-left{
left:0;
}
.red-right{
right:0;
}
/* yellow block follow the width of the container but should leave spaces for the left and right */
.yellow{
margin: 0 110px;
}
Im almost satisfied with this approach however, I noticed that when the red blocks are higher than the container, the container doesnt auto follow the height of its contents. I understand that its impossible to auto height the container because the children are in position absolute. :)

Have you consider using CSS3 Flex Box? It would go like this:
HTML:
<div class="container">
<div class="red red-left">red-left<br>red-left<br>red-left<br>red-left<br>red-left</div>
<div class="yellow">yellow<br>yellow</div>
<div class="red red-right">red-right</div>
</div>
And Css:
.container{
display: -webkit-box;
-webkit-box-orient: horizontal;
display: -moz-box;
-moz-box-orient: horizontal;
display: box;
box-orient: horizontal;
}
.red{
background-color:red;
width:100px;
}
.yellow{
background-color:yellow;
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
}
​
​Check out this Fiddle:
http://jsfiddle.net/lucaslazaro/sjYNy/
EDIT:
To know more about Flex Box I recommend this quick tutorial: http://www.html5rocks.com/pt/tutorials/flexbox/quick/

Maybe that helps:
HTML
<div class="container">
<div class="red red-left">red-left<br>red-left<br>red-left</div>
<div class="yellow">yellow<br>yellow</div>
<div class="red red-right">red-right</div>
</div>
​
​
CSS
/* the one with black border :) */
.container{
position: relative;
}
/* red blocks have auto heights depends on its content */
.red{
position: absolute;
top: 0;
width: 100px;
background:red;
height:auto
}
.red-left{
left:0;
}
.red-right{
right:0;
}
/* yellow block follow the width of the container but should leave spaces for the left and right */
.yellow{
margin: 0 110px;
background:yellow
}
​
​
​​DEMO

My Own Demo to make it easier.
What we can see here is the content overlapsed the container.

Using:
div {
display: table;
width: 100%;
table-layout: fixed;
}
div > div {
display: table-cell;
}
Review full code:
http://jsfiddle.net/BF6La/

Related

How to exactly fill remaining vertical space with css [duplicate]

I need to fill the remaining vertical space of #wrapper under #first with #second div.
I need an only CSS solution.
#wrapper {
width: 300px;
height: 100%;
}
#first {
width: 300px;
height: 200px;
background-color: #F5DEB3;
}
#second {
width: 300px;
height: 100%;
background-color: #9ACD32;
}
<div id="wrapper">
<div id="first"></div>
<div id="second"></div>
</div>
You can use CSS Flexbox instead another display value, The Flexbox Layout (Flexible Box) module aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic.
Example
/* CONTAINER */
#wrapper
{
width:300px;
height:300px;
display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */
display: -moz-box; /* OLD - Firefox 19- (buggy but mostly works) */
display: -ms-flexbox; /* TWEENER - IE 10 */
display: -webkit-flex; /* NEW - Chrome */
display: flex; /* NEW, Spec - Opera 12.1, Firefox 20+ */
-ms-flex-direction: column;
-moz-flex-direction: column;
-webkit-flex-direction: column;
flex-direction: column;
}
/* SOME ITEM CHILD ELEMENTS */
#first
{
width:300px;
height: 200px;
background-color:#F5DEB3;
}
#second
{
width:300px;
background-color: #9ACD32;
-webkit-box-flex: 1; /* OLD - iOS 6-, Safari 3.1-6 */
-moz-box-flex: 1; /* OLD - Firefox 19- */
-webkit-flex: 1; /* Chrome */
-ms-flex: 1; /* IE 10 */
flex: 1; /* NEW, */
}
jsfiddle Example
If you want to have full support for old browsers like IE9 or below, you will have to use a polyfills like flexy, this polyfill enable support for Flexbox model but only for 2012 spec of flexbox model.
Recently I found another polyfill to help you with Internet Explorer 8 & 9 or any older browser that not have support for flexbox model, I still have not tried it but I leave the link here
You can find a usefull and complete Guide to Flexbox model by Chris Coyer here
Using CSS Flexbox (MDN Web Docs).
html, body {
height: 100%;
}
.wrapper {
display: flex;
flex-direction: column;
width: 300px;
height: 100%;
}
.first {
height: 50px;
}
.second {
flex-grow: 1;
}
<div class="wrapper">
<div class="first" style="background:#b2efd8">First</div>
<div class="second" style="background:#80c7cd">Second</div>
</div>
You can do this with position:absolute; on the #second div like this :
FIDDLE
CSS :
#wrapper{
position:relative;
}
#second {
position:absolute;
top:200px;
bottom:0;
left:0;
width:300px;
background-color:#9ACD32;
}
EDIT : Alternative solution
Depending on your layout and the content you have in those divs, you could make it much more simple and with less markup like this :
FIDDLE
HTML :
<div id="wrapper">
<div id="first"></div>
</div>
CSS :
#wrapper {
height:100%;
width:300px;
background-color:#9ACD32;
}
#first {
background-color:#F5DEB3;
height: 200px;
}
If you can add an extra couple of divs so your html looks like this:
<div id="wrapper">
<div id="first" class="row">
<div class="cell"></div>
</div>
<div id="second" class="row">
<div class="cell"></div>
</div>
</div>
You can make use of the display:table properties:
#wrapper
{
width:300px;
height:100%;
display:table;
}
.row
{
display:table-row;
}
.cell
{
display:table-cell;
}
#first .cell
{
height:200px;
background-color:#F5DEB3;
}
#second .cell
{
background-color:#9ACD32;
}
Example
Have you tried changing the wrapper height to vh instead of %?
#wrapper {
width:300px;
height:100vh;
}
That worked great for me when I wanted to fill my page with a gradient background for instance...
If you don't want to have fix heights for your main-container (top, bottom, ....), you can simply use this css-file to get a flex-container which uses the remaining space incl. working!!! scrollbars
Fiddler
<!DOCTYPE html>
<html >
<head>
<title>Flex Container</title>
<link rel="stylesheet" href="http://demo.qooxdoo.org/5.0/framework/indigo-5.0.css">
<style>
.cont{
background-color: blue;
position: absolute;
height: 100%;
width: 100%;
}
.headerContainer {
background-color: green;
height: 100px;
width: 100%;
}
.mainContainer {
background-color: white;
width: 100%;
overflow: scroll
}
.footerContainer {
background-color: gray;
height: 100px;
width: 100%;
}
</style>
</head>
<body class="qx-flex-ready" style="height: 100%">
<div class="qx-vbox cont">
<div class="headerContainer">Cell 1: flex1</div>
<div class="mainContainer qx-flex3">
x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>x<br>
</div>
<div class="footerContainer" >Cell 3: flex1</div>
</div>
</body>
</html>
All you need is a bit of improved markup. Wrap the second within the first and it will render under.
<div id="wrapper">
<div id="first">
Here comes the first content
<div id="second">I will render below the first content</div>
</div>
</div>
Demo
You can just add the overflow:auto option:
#second
{
width:300px;
height:100%;
overflow: auto;
background-color:#9ACD32;
}

CSS Stick Footer to Bottom

Here is my code to stick the footer to bottom of the page:
#footer {
background-color: #0F2157;
width: 100%;
bottom: 0px;
min-height: 35px;
padding-top: 5px;
}
When I'm doing it with height it works perfectly fine, but when I'm trying to set the minimum height it leaves a little space under the footer. Any guess how to fix that?
First of all, the height of body, html and container (see element with class 'container') has to have height: 100%;
In this solution I have used flex box. It is supported by all modern browsers and IE11.
It's necessary to add the following properties to container:
display: flex;
flex-direction: column; /*the flex items are placed in column, by default it is in row*/
To move footer to bottom, just add to flex item
margin-top: auto; /* it grabs all free space between flex items and put it before this flex item */
html, body {
height: 100%;
}
.container {
height: 100%;
background-color: green;
display: flex;
flex-direction: column;
}
.header {
height: 20%;
background-color: yellow;
}
.content {
background-color: white;
}
.footer {
min-height: 20%;
background-color: blue;
margin-top: auto;
}
<div class="container">
<div class="header">Header</div>
<div class="content">It's content</div>
<div class="footer">Footer in bottom</div>
</div>
What about using Flexbox? It is supported by IE>=10.
To use that, you have to split your page at least in two separated elements: The "upper"-one (.content) with the whole content of your page and the footer.
The "upper"-one gets the value flex: 1, which is a shorthand for:
flex-grow: 1
flex-shrink: 1
flex-basis: auto
This means, that the "upper"-element could grow to the maximum, while the footer reserves only it's actually required space.
Code snippet
html {
height: 100%;
}
body {
min-height: 100%;
margin: 0;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
}
<!doctype html>
<html>
<head>
</head>
<body>
<div class="content"></div>
<footer class="footer">
Hey footer!
</footer>
</body>
</html>
You used min height 35 px. I think your content's height inside of footer is more than 35px. So check the margin or padding of all footer elements.
It will be better, if you can make a jsfiddle demo.
[SOLVED]
I found this to be working for my example:
#footer {
position: absolute;
bottom: 0;
width: 100%;
}

Absolute position element on equal height columns - how?

Here's what I can't do by any means, using only CSS:
Columns should have equal heights.
And, on a given column, a absolute positioned element should be present relative to that column:
The HTML:
<div class="wrapper">
<div class="inner">
<div class="column">
<img src="http://www.english3.com/wp-content/uploads/2013/05/DFI-Logo-300px-X-200px.png"/>
</div>
<div class="column info">
<p>Some text here yeah :).</p>
<p>Some text here yeah :)</p>
<p>Some text here yeah :)</p>
<a class="link" href="#">I should be absolute.</a>
</div>
</div>
</div>
THE CSS:
.wrapper {
overflow: hidden;
position: relative;
}
/*added */
.inner {
width: 100%;
display: table;
}
.column {
display: table-cell;
width: 50%;
vertical-align: top;
background-color:red;
}
.column img {
max-width:100%;
display: block;
}
.info {
background-color: blue;
padding-bottom: 25px;
}
.link {
display: block;
position: absolute;
left: 50%;
bottom: 3%;
color: yellow;
}
Fiddle to play:
Here's the try with table-cell; and a relative inner container:
http://jsfiddle.net/BuuFv/98/
TRIES AND FRUSTRATIONS:
1) - Display table
For equal heights I can't pull this out, due to FF issues.
Tried with an extra relative parent container
But left image doesn't shrink according to it's container on FF.
Tried giving height: 100%
http://davidwalsh.name/table-cell-position-absolute
But didn't work either, the image doesn't shrink or expands using max-width;
2 - Huge positive padding and negative margin values
Will not work, because the absolute positioned element will not stay in place.
3 - Faux Columns
Seems to be of any help, due to the fact that, we are not playing with solid background colours, and we have an image on the left column instead.
A picture:
Any help, please?
1)
On this case, adding an extra relative container with position relative attribute (.inner), did the trick.
2)
The fact that the image didn't shrink or grow on FF was because it wasn't considering the max-width declaration as it should.
Fix:
.inner {
width: 100%;
display: table;
table-layout:fixed; /* <<-- ADD */
}
the table-layout fixed algorithm made Firefox recognize the max-width in the cell and this let the image shrink.
Credits on this solution should be shared with Paul O'B.
Here is my take on it http://jsfiddle.net/E6UDD/
So basically, uncoment padding and negative margin hack, and add the height of the column to be the same amount.
Then, add relative positioning to the .column.
Then, position of the absolute positioned link should be top: 50% to have it at the very bottom, or 44% to mimic the 3% you originally indended.
DEMO
.wrapper {
position:relative;
overflow: hidden;
white-space:nowrap;
outline:1px solid red;
height:auto;
}
.column {
position:relative;
display:inline-block;
vertical-align:top;
background-color:red;
width:50%;
}
.column img {
max-width:100%;
min-width:30px;
min-height:20px;
}
.info {
position:absolute;
bottom:0;
top:0;
background-color: blue;
}
.link {
clear:left;
display: block;
position: absolute;
bottom: 3%;
color: yellow;
}
http://jsfiddle.net/BuuFv/75/
You can try changing the link from absolute positioned to another table-cell. Not 100% on your use case but it seems to accomplish what you are asking.
.link {
height:100px;
display:table-cell;
vertical-align:bottom;
color: yellow;
}

Can a nested Div ignore the parent Div width?

Basically I have a nested <div> as a footer and the parent div is centered 1000px wide.
I was wondering if it was possible to extend the width of footer div so that it goes out of the parent to fit the browsers width but still keeps its place in the parent?
My solution assumes that .parent element has stretched height. even if it is not the case, then it seems you want the .footer element stick to the bottom of the page. If it is so, then using position:absolute you can bring the child block out of the parent block and then pin it to bottom using bottom: 0px and then to stretch its width use left:0px and right: 0px.
Working Fiddle
UPDATED:
Use this Doctype declaration:
<!DOCTYPE html>
Also, in .footer element mention top:auto css property. Something like this:
.footer{
padding: 0px 15px;
height: 50px;
background-color: #1A1A1A;
position:absolute;
bottom: 0px;
left: 0px;
right: 0px;
top: auto; /* added (IE FIX) */
}
Something that would work for you:
.parent{
width: 300px; /* your parent width */
}
.footer{
height: 50px; /* your footer height */
position:absolute;
left: 0px;
right: 0px;
}
Demo
You could set the footer position to relative and set the left property to -100px and width to 1200px for example.
Better still don't have it in the parent div, have it as it's own div with it's own values set.
Do like this
html
<div id="parent" class="wrap"></div>
<div id="footer"></div>
css
.wrap{width: 1000px;}
#footer{width: 100%;}
Try this CSS.
#footer {
position:absolute;
bottom:0;
width:100%;
}
Try this css this will definitely work as you want
html,body{
height: 100%;
padding: 0px;
margin: 0px;
}
.parent{
width: 400px;/*put your width here*/
height: 100%;
background-color: #ccc;
margin: 0 auto;
}
.footer{
padding: 15px 0px;
height: 30px;
background-color: #000;
position:absolute;
bottom: 0px;
left: 0px;
width:100%
}
If you really want to bypass the parent element, you could look into display:contents
https://css-tricks.com/get-ready-for-display-contents/
It really is a game changer if you need a div to wrap elements for some logic reason, but want the styling of all seperate elements.
Example without:
.main {
align-items: center;
box-sizing: border-box;
display: flex;
flex-direction: row;
}
<div class="main">
<div class="title">
<h2>Foo</h2>
<span>Bar</span>
</div>
<button>shamefull_button</button>
</div>
Example with:
.main {
align-items: center;
box-sizing: border-box;
display: flex;
flex-direction: row;
}
.title {
display: contents;
}
<div class="main">
<div class="title">
<h2>Foo</h2>
<span>Bar</span>
</div>
<button>shameless_button</button>
</div>

CSS: center element within a <div> element

To center an HTML element I can use the CSS left: 50%;. However, this centers the element with respect to the whole window.
I have an element which is a child of a <div> element and I want to center the child with respect to this parent <div>, not the whole window.
I do not want the container <div> to have all its content centered, just the one specific child.
Set text-align:center; to the parent div, and margin:auto; to the child div.
#parent {
text-align:center;
background-color:blue;
height:400px;
width:600px;
}
.block {
height:100px;
width:200px;
text-align:left;
}
.center {
margin:auto;
background-color:green;
}
.left {
margin:auto auto auto 0;
background-color:red;
}
.right {
margin:auto 0 auto auto;
background-color:yellow;
}
<div id="parent">
<div id="child1" class="block center">
a block to align center and with text aligned left
</div>
<div id="child2" class="block left">
a block to align left and with text aligned left
</div>
<div id="child3" class="block right">
a block to align right and with text aligned left
</div>
</div>
This a good resource to center mostly anything.
http://howtocenterincss.com/
Actually this is very straightforward with CSS3 flex boxes.
.flex-container{
display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6, BB7 */
display: -ms-flexbox; /* TWEENER - IE 10 */
display: -webkit-flex; /* NEW - Safari 6.1+. iOS 7.1+, BB10 */
display: flex; /* NEW, Spec - Firefox, Chrome, Opera */
justify-content: center;
align-items: center;
width: 400px;
height: 200px;
background-color: #3498db;
}
.inner-element{
width: 100px;
height: 100px;
background-color: #f1c40f;
}
<div class="flex-container">
<div class="inner-element"></div>
</div>
UPDATE:
It seems that I didn't read the OP edit at the time I wrote this answer. The above code will center all inner elements (without overlapping between them), but the OP wants just an specific element to be centered, not the other inner elements. So #Warface answer second method is more appropiate, but it still requires vertical centering:
.flex-container{
position: relative;
/* Other styling stuff */
width: 400px;
height: 200px;
background-color: #3498db;
}
.inner-element{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
/* or 3d alternative if you will add animations (smoother transitions) */
transform: translate3d(-50%,-50%,0);
/* Other styling stuff */
width: 100px;
height: 100px;
background-color: #f1c40f;
}
<div class="flex-container">
<p>Other inner elements like this follows the normal flow.</p>
<div class="inner-element"></div>
</div>
CSS
body{
text-align:center;
}
.divWrapper{
width:960px //Change it the to width of the parent you want
margin: 0 auto;
text-align:left;
}
HTML
<div class="divWrapper">Tada!!</div>
This should center the div
2016 - HTML5 + CSS3 method
CSS
div#relative{
position:relative;
}
div#thisDiv{
position:absolute;
left:50%;
transform: translateX(-50%);
-webkit-transform: translateX(-50%);
}
HTML
<div id="relative">
<div id="thisDiv">Bla bla bla</div>
</div>
Fiddledlidle
https://jsfiddle.net/1z7m83dx/
You can use bootstrap flex class name like that:
<div class="d-flex justify-content-center">
// the elements you want to center
</div>
That will work even with number of elements inside.
To center only the specific child :
.parent {
height: 100px;
background-color: gray;
position: relative;
}
.child {
background-color: white;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 20px;
height:20px;
margin: auto;
}
<div class="parent">
<span class="child">hi</span>
</div>
OR, you can use flex too, but that would center all children
.parent {
height: 100px;
background-color: gray;
display: flex;
justify-content: center;
align-items: center;
}
.child {
background-color: white;
}
<div class="parent">
<span class="child">hi</span>
</div>
text-align:center; on the parent div Should do the trick
Is the div a fixed width or a fluid width? Either way, for fluid width you could use:
#element { /* this is the child div */
margin-left:auto;
margin-right:auto;
/* Add remaining styling here */
}
Or you could set the parent div to text-align:center; and the child div to text-align:left;.
And left:50%; only centers it according to the whole page when the div is set to position:absolute;. If yous set the div to left:50%; it should do it relative to the parent div's width. For fixed width, do this:
#parent {
width:500px;
}
#child {
left:50%;
margin-left:-100px;
width:200px;
}
just give the parent div position: relative
I believe the modern way to go is place-items: center in the parent container
An example can be found here: https://1linelayouts.glitch.me
If you want to use CSS3:
position:absolute;
left:calc(50% - PutTheSizeOfTheHalfOfYourElementpx);
You might want to do further searches to figure out how to get the percentage to fit your element's width.
after flex it show essay
.container{
display:flex;
color:blue;
background-color: yellow;
justify-content:center;
}
<div class="container">
<div>Element</div>
</div>
and if you set element Center elements horizontally and vertically.
.container{
display:flex;
color:blue;
height:100px;
background-color: yellow;
justify-content:center;
align-items:center;
}
<div class="container">
<div>Element</div>
</div>
First of all you can do it with left:50% to center it relative to parent div but for that you need to learn CSS positioning.
One possible solution from many is to do something like
div.parent { text-align:center; } //will center align every thing in this div as well as in the children element
div.parent div { text-align:left;} //to restore so every thing would start from left
if your your div to be centered is positioned relatively, you can just do
.mydiv { position:relative; margin:0 auto; }
left: 50% works resectively to the nearest parent with static width assigned. Try width: 500px or something on parent div. Alternatively, make the content you need to center display:block and set left and right margins to auto.
If the child element is inline (e.g not a div, table etc) I would wrap it up inside a div or a p and make the wrapper's text align css property equal to center.
<div id="container">
This text is aligned to the left.<br>
So is this text.<br>
<div style="text-align: center">
This <button>button</button> is centered.
</div>
This text is still aligned left.
</div>
Otherwise, if the element is a block (display: block, e.g a div or a p) with a fixed width, I'd set its margin left and right css properties to auto.
<div id="container">
This text is aligned to the left.<br>
So is this text.<br>
<div style="margin: 0 auto; width: 200px; background: red; color: white;">
My parent is centered.
</div>
This text is still aligned left.
</div>
You could of course add a text-align: center to the wrapper element to make its contents centered as well.
I won't bother with positioning because IMHO its not the way to go for the OPs problem but be sure to check this link out, very helpful.
Create a new div element for your element to center, then add a class specifically for centering that element like this
<div id="myNewElement">
<div class="centered">
<input type="button" value="My Centered Button"/>
</div>
</div>
Css
.centered{
text-align:center;
}
I have found another solution
<style type="text/css">
.container {
width:600px; //set how much you want
overflow: hidden;
position: relative;
}
.containerSecond{
position: absolute;
top:0px;
left:-500%;
width:1100%;
}
.content{
width: 800px; //your content size
margin:0 auto;
}
</style>
and in body
<div class="container">
<div class="containerSecond">
<div class="content"></div>
</div>
</div>
This will center your content div whenever your container is bigger or smaller. In this case your content should be bigger than 1100% of container to not be centered, but in that case you can make with of containerSecond bigger, and it will work
Assign text-align: center; to the parent and display: inline-block; to the div.
for example, i have an article div which is inside a main content div..
Inside the article theres an image and under that image is a button, style like this:
.article {
width: whatever;
text-align: center;
float: whatever (in case you got more articles in a row);
margin: give it whatever margin you want;
}
.article {
}
/* inside the article i want the image centered */
.article img {
float: none;
margin: 0 auto;
padding: give it a padded ridge if you want;
}
/* Now under this image in the same article element there should be a button like read more Of course you need to work with em or % when its inside a responsive design*/
.button {
background: #whatever color:
padding: 4.3567%; /*Instead of fixed width*/
text-align: cente;
font: whatever;
max-width: 41.4166%;
float: none;
margin: 0 auto; /* You could make the zero into any margin you want on the top and bottom of this button.. Just notice the float: none property.. this wil solve most your issues, also check if maybe position and displaay might mess up your code..*/
}
Good luck
If you want to center elements inside a div and don't care about division size you could use Flex power. I prefer using flex and don't use exact size for elements.
<div class="outer flex">
<div class="inner">
This is the inner Content
</div>
</div>
As you can see Outer div is Flex container and Inner must be Flex item that specified with flex: 1 1 auto; for better understand you could see this simple sample. http://jsfiddle.net/QMaster/hC223/
Hope this help.
mine would like magic.
html, body {
height: 100%;
}
.flex-container{
display: -ms-flexbox;
display: -webkit-box;
display: flex;
-ms-flex-align: center;
-ms-flex-pack: center;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
}
<div class="flex-container">
<div>Content</div>
</div>
<html>
<head>
</head>
<style>
.out{
width:200px;
height:200px;
background-color:yellow;
}
.in{
width:100px;
height:100px;
background-color:green;
margin-top:50%;
margin-left:50%;
transform:translate(-50%,50%);
}
</style>
<body>
<div class="out">
<div class="in">
</div>
</div>
</body>
</html>
There can be a lot of ways on how you can center an element, it can be either using display properties or position or floats or margins. But normally I prefer using flexbox as it is easy and simple. I know that different people have different preferences but it depends entirely on the developer's preference and relation of the elements.
.parent{
display: block;
}
.sub-parent-1{
display: flex;
justify-content: center; //horizontal centering
align-items: center; //vertical centering
}
<body>
<div class="parent">
<div class="sub-parent-1">
<div class="child-1">...</div>
</div>
<div class="child-2">...</div>
<div class="child-3">...</div>
<div class="child-4">...</div>
<div class="child-5">...</div>
</div>
</body>
If none of those answers hit's it. Try this one.
Horizontally and vertically aligned child element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="content">
<img width="300px" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Building92microsoft.jpg/800px-Building92microsoft.jpg" alt="microsoft" />
</div>
</div>
</body>
<style>
.container {
display: flex; /* can also use grid */
background-color: #47472d;
width: 500px;
height: 400px;
overflow: auto;
}
.content { margin: auto; }
</style>
</html>

Resources