How to vertically center a floating element [duplicate] - css

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
How can I vertically align elements in a div?
(28 answers)
Center one and right/left align other flexbox element
(11 answers)
Closed 3 years ago.
* {
margin: 0px;
}
.container {
background: #ccc;
line-height: 50px;
verticle-algn: middle;
clear: both;
}
.float {
overflow: hidden;
float: right;
}
<div class="container">
<button class="inside">inside</button>
<button class="float">float</button>
</div>
Please check this example.I want the .float element to be vertically centered of the .container element as the .inside element.But no matter I trigger BFC or add a pseudo element, I can not clear the impact of the float element.I don't think margin is a best way to solve this.Can you tell me how to fix it, thanks for your help.

Try this its work what you expected.
* {
margin: 0px;
}
.container{
display:flex;
justify-content: space-between;
background: #ccc;
line-height: 50px;
}
.float{
margin: 0 auto;
}
<div class="container">
<div class="inside"><button>inside</button></div>
<div class="float"><button>float</button></div>
</div>

Related

How to centralize a div with pictures [duplicate]

This question already has answers here:
How do I center floated elements?
(12 answers)
CSS center display inline block?
(9 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I am totally a newbie when it comes to coding.
I have a div with 4 pictures in it. I want the div to be centralize but I can´t make it happen. If I manage to centralize, they are not aligned any more (horizontal). How do I do this?
I´ve tried everything, justify-content, display, position, align-items and so on. Where do I go wrong?
What code do I use to put the boxes in the center, just as they are, horizontal?
Thanks for you´re help!
.wrapper {border: 1px solid #678596; max-width: 350px; margin: 30px auto;}
.parentClass { display: flex; justify-content:center;}
.parentClass div {margin: 5px; background: #678596; width: 50px; height: 50px; line-height: 50px; text-align: center; font-size: 30px; color: #fff;}
<div class="wrapper">
<div class="parentClass">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>

Align children flex container bottom not working [duplicate]

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Align an element to bottom with flexbox
(10 answers)
Closed 2 years ago.
I have 3 elements (1 image, 2 texts) inside a flex container. I am not able to align them on the bottom line. I tried with margin-bottom: 0 margin-bottom: auto and it is not working.
.distance {
display: flex;
justify-content: space-between;
}
.flex {
display: flex;
}
.icon {
width: auto;
max-height: 100px;
margin-right: 8px;
}
.text1 {
font-size: 15px;
}
.text2 {
font-size: 12px;
margin-bottom: 0px;
}
<div class="distance">
<div class="flex">
<img src="https://r00t4bl3.com/uploads/pp-840x830-pad-1000x1000-f8f8f8-c3edd4cbfc0d7bc9d7b02b27c6f6bac5.jpg" class="icon">
<div class="text1">
Golang
</div>
<div class="text2">
Made by Google
</div>
</div>
</div>
for vertical align in a flex display use the property align-items. in your case add align-items: flex-end at flex class. css-tricks is a good reference for flexbox.

CSS responsiveness, inline-divs with unequal heights [duplicate]

This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Closed 2 years ago.
If we style multiple divs with property display set to "inline-block" and one of them has a height longer than the others, all the smaller divs align at the bottom creating extra space at the top. But all I wanted is that the smaller divs align at the top and leave any extra space at the bottom. Is there any way I can achieve the required effect?
vertical-align: top for inline-block divs does that. vertical-align
.b{
display: block;
background-color: red;
}
.ib {
display: inline-block;
background-color: blue;
width: 50px;
vertical-align: top; /*this line*/
}
.d1 {
height: 100px;
}
.d2, .d3 {
height: 50px;
}
<div class="b">
<div class="ib d1"></div>
<div class="ib d2"></div>
<div class="ib d3"></div>
</div>

Centering an image in div [duplicate]

This question already has answers here:
center image in a div
(3 answers)
Closed 6 years ago.
I want to put the image in the center of the div.
My div:
width: 95%;
padding: 2.5%;
max-width: 1200px;
height:100%;
but I can not put image in the center and responsive.
I had 50% of img and putting 25% of padding.
But I want to use picture and recommended not to use text-align and
display: block;
margin: 0 auto;
not function , does anyone know what to do?
Use text-align: center for parent <div> and display: inline-block for image
#parent {
text-align: center;
}
img {
display: inline-block;
}
#parent {
text-align: center;
}
img {
display: inline-block;
}
<div id="parent">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR2kHE1B9lVRicrWWdui_B309y9cd-YOmwgAxxWmNy27b6ArKvWXA">
</div>

Vertically align a child div element with parent div having dynamic height [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 6 years ago.
How do I vertically align a child element with the parent having a dynamic height.
CSS code:
parent{
height: 400px;
}
.child{
position:relative;
top:50%;
transform: translateY(-50%;);
}
This sets the child once, then doesn't change. How do I change it so that it changes with the dynamic height of the parent?
You can use display: flex.
.parent {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
width: 100px;
margin: 1em;
background-color: tomato;
}
.child {
width: 10px;
height: 10px;
background-color: yellow;
}
You can use `displa: flex`.
The following doesn't rely on parent's height.
<div class="parent">
<div class="child">
</div>
</div>

Resources