Why does flexbox center element properly while positioning properties does not? [duplicate] - css

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 4 years ago.
I am trying to center a div element. It seems in my code that all is right but the element is not being centered properly Where did I wrong? If i try to center it by using flexbox then it is centered properly. Where is the wrong of positioning property?
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 200px;
height: 39px;
border: 1px solid red;
position: relative;
}
.cntr {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: blue;
width: 35px;
height: 35px;
}
<div class='container'>
<div class='cntr'>
</div>
</div>

You can use a flexbox instead of absolute positioning which will keep the document flow intact.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 200px;
height: 39px;
border: 1px solid red;
display: flex;
justify-content: center; /* Horizontal alignment */
align-items: center; /* Vertical alignment */
}
.cntr {
background-color: blue;
width: 35px;
height: 35px;
}
<div class='container'>
<div class='cntr'>
</div>
</div>

Use flexbox
Here's the code
html:
<div class="parent">
<div class="child">
</div>
</div>
css:
.parent{
background: #000;
width:100%;
height:100%;
display:flex;
justify-content:center;
align-items:center;
}
.child{
background: #ff0;
width:100px;
height:100px;
}
Demo:
https://jsbin.com/duxakey/edit?html,css,output

Related

How to center text below image [duplicate]

This question already has answers here:
CSS center display inline block?
(9 answers)
How can I horizontally center an element?
(133 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 1 year ago.
I would like to center my image slightly above the center of the page while having the text centered directly below it.
HTML
<div class="center">
<img src="aboutImages/jay.jpeg" id="art">
<span class="description">caption</span>
</div>
CSS
div.center{
display: inline-block;
text-align: center;
vertical-align: top;
}
div img{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
max-width: 100%;
max-height: 100%;
height: 320px;
width: 250px;
margin-top: 200px;
}
.description{
display: block;
}
Should be able to handle this with some text-align and some margin-top.
div.center {
margin-top: 25%;
text-align: center;
}
.center img{
max-width: 100%;
max-height: 100%;
height: 320px;
width: 250px;
}
<div class="center">
<img src="aboutImages/jay.jpeg" id="art">
<div class="description">caption</div>
</div>
Just put your image and text inside another container. And you create styling center for that container instead of image.
div.center{
display: inline-block;
text-align: center;
vertical-align: top;
}
.image-box{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
height: 320px;
width: 250px;
margin-top: 200px;
}
div img{
max-width: 100%;
max-height: 100%;
}
.description{
display: block;
}
<div class="center">
<div class="image-box">
<img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" id="art">
<span class="description">caption</span>
</div>
</div>
/* in this example I set the body to 100vh to cover entire screen */
body {
display: flex;
height: 100vh;
}
/* center div using margin auto */
div.center {
display: inline-block;
text-align: center;
margin: auto;
}
/* wrap img and desc. in one div */
.wrap {
display: flex;
flex-direction: column;
}
/* use margin to offset image */
.description {
margin-bottom: 100px;
}
div img {
width: 100px;
}
.description {
display: block;
}
<div class="center">
<div class="wrap">
<img src="https://source.unsplash.com/random/150x150" id="art">
<span class="description">caption</span>
</div>
</div>

flex shrink 0 causes scrollbar to disappear and it ignores overflow auto in fire fox edge and ie but not in chrome why [duplicate]

This question already has answers here:
Flexbox column-reverse in Firefox, Edge and IE
(4 answers)
Closed 4 years ago.
This code is designed to show the numbers in column reverse order from 1 to 4 so I suddenly realize I did not like how display flex was setting the .numbers height and it was ignoring my height in 200px in the numbers class name so I added
flex-shrink: 0;
and it prevented display flex from setting it's own height and it suddenly showed the .numbers original height so I was happy :)
so this is how it looks in Chrome
but sadly flex-shrink: 0 gave strange results in Edge, IE and Fire fox I notice in those browsers it removed the scrollbar and it ignored the overflow-y: auto; mentioned in the #numbers-container.
How can I get it to work like the chrome browser in those other browsers that it did not work in ? :(
Code
#container{
background-color: #d6b68d;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
}
#numbers-container{
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
display: flex;
flex-direction: column-reverse;
}
.numbers{
background-color: forestgreen;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
flex-shrink: 0;
}
.numbers h1{
text-align: center;
color: white;
}
<div id='container'>
<div id='numbers-container'>
<div class='numbers'>
<h1>1</h1>
</div><!--</numbers>-->
<div class='numbers'>
<h1>2</h1>
</div><!--</numbers>-->
<div class='numbers'>
<h1>3</h1>
</div><!--</numbers>-->
<div class='numbers'>
<h1>4</h1>
</div><!--</numbers>-->
</div><!--</numbers-container>-->
</div><!--</container>-->
You could move the scrolling onto it's own container:
/* CSS used here will be applied after bootstrap.css */
#container {
background-color: #d6b68d;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
}
#scroll {
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
}
#numbers-container {
background-color: orange;
overflow-x: hidden;
display: flex;
flex-direction: column-reverse;
}
.numbers {
background-color: forestgreen;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
flex-shrink: 0;
}
.numbers h1 {
text-align: center;
color: white;
}
<div id="container">
<div id="scroll">
<div id="numbers-container">
<div class="numbers">
<h1>1</h1>
</div>
<!--</numbers>-->
<div class="numbers">
<h1>2</h1>
</div>
<!--</numbers>-->
<div class="numbers">
<h1>3</h1>
</div>
<!--</numbers>-->
<div class="numbers">
<h1>4</h1>
</div>
<!--</numbers>-->
</div>
<!--</numbers-container>-->
</div>
<!--</scroll-container>-->
</div>
<!--</container>-->
However, if you want to start your scroll from the bottom, you would probably need to use js

Relative position and overflow auto

I have a problem with overflow: auto and position relative? Example of my code is:
div {
border: 3px dashed #ccc;
padding: 20px;
}
div::before { content: attr(class); }
.grandparent { overflow: auto; }
.parent { position: relative; }
.child { position: absolute; height: 60px; background: black; color: white; }
<div class="grandparent">
<div class="parent">
<div class="child"></div>
</div>
</div>
I want the div.child go over div.grandparent but because of div.parent and his position: relative that causes overflowing.
Please note that I need to absolutely position elements inside .parent - the position being relative to .parent - (which is why I need position: relative here), and I also need a scrollable .grandparent.
Expected result:
div, div.child::after {
border: 3px dashed #ccc;
padding: 20px;
}
div::before { content: attr(class); }
.grandparent { overflow: auto; }
.parent { position: relative;}
.child::before{ content: " " }
.child {position: relative; overflow: auto; }
.child::after {content: attr(class); position: absolute; height: 60px; background: black; color: white;
}
<div class="grandparent">
<div class="parent">
<div class="child"></div>
</div>
</div>
this is your answer but It does not seem practical
Because in reality it can not be implemented in this way
Please ask your question clearly or with a more practical example

CSS Layout - Float: ...up? [duplicate]

This question already has answers here:
How do you float elements without a vertical gap?
(9 answers)
Closed 5 years ago.
Is there a technique (codified or hacky) to get floated blocks to fill-in upwards as well as their float direction.
So that something like -
Becomes
I realize this is accomplished by javascript libraries like Masonry.
Just wondering if there are any CSS approaches to accomplish this or something similar.
Related codepen
https://codepen.io/2nj2nu7p9oVLGXKS4tIpu8eILcmoXg/pen/QOdmqw
body * {
box-sizing: border-box;
}
.wrapper {
max-width: 500px;
background: limegreen;
margin-left: auto;
margin-right: auto;
overflow: hidden;
}
.block {
height: 100px;
background: lightblue;
width: 250px;
float: left;
border: solid 2px;
&:nth-child(even) {
background: blue;
height: 150px;
}
}
I simply just made any lightblue (odd) elements float: left and any blue (even) elements float: right
How does this look:
body * {
box-sizing: border-box;
float: left;
}
.wrapper {
max-width: 500px;
background: limegreen;
}
.block {
height: 100px;
background: lightblue;
width: 250px;
border: solid 2px;
vertical-align: top;
}
.block:nth-child(even) {
float: right;
background: blue;
height: 150px;
}
<div class="wrapper">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>

CSS full page background height of scrollable div

there is html code:
<div class="container content">
<div class="wrap-container row">
<div class="col-xs-12 col-md-5 text-center text-white left">
<h2 class="mt-5">Contact</h2>
<h1> 987 123 456</h1>
</div>
<div class="col-xs-12 col-md-6 ml-auto right">
<div class="form">
<form>
<!-- form controls -->
</form>
</div>
</div>
</div>
</div>
now in css
.content.container {
height: 80vh;
overflow: hidden;
box-sizing: border-box;
}
.wrap-container {
position: relative;
height: 80%;
border-radius: 5px;
background: rgba(0,0,0,.3);
overflow: hidden;
box-sizing: border-box;
}
.left {
text-shadow: 0px 1px 2px #808080, 2px 2px 1px rgba(47,171,81, 0.8);
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.right {
background: rgba(0,0,0,.8);
color: #b9b9b9;
margin: 2rem;
height: 86%;
overflow: hidden;
box-sizing: border-box;
}
.form {
overflow: auto;
height:99%;
padding-right: 3rem;
position: relative;
width: 110%;
left: 0;
right: -2rem;
}
#media (max-width: 768px) {
.content.container {
height: 200vh;
display: block;
}
.wrap-container {
display: block;
height: auto;
overflow: auto;
}
.cb-slideshow li div h3 {
line-height: 0;
}
.right, .left {
margin:0;
}
.right {
height: auto;
}
.form {
padding-right: 1.5rem;
/*height: auto;*/
display: block;
}
}
now, when .form div is getting higher than verticah height (100vh) content is cut. In small devices like phone I can scroll .form div content but only vh is visible, there is no visible submit button :/
I added in media query height 200vh of .content.container what solved problem but I think it's not proper way.
How should I extend scrollable div till its content height?
Children adapting to parents height is best resolved using flexbox.
I have set the wrap-container as flex and flex-direction:column as the children are stacked. I would try to avoid setting different heights to all children.
Then just add overflow-y:scroll to the form.
.wrap-container {
display: flex;
flex-direction: column;
}
.form {
overflow-y: scroll;
}
If you want to achive something like: 100vh for the main container and items in overflow:hidden but .form scrollable, for all devices, it should be done like this:
Fiddle

Resources