Set property to current width of element with CSS? [duplicate] - css

This question already has answers here:
Element will not stay centered, especially when re-sizing screen
(2 answers)
Closed 5 years ago.
I'm looking for some syntax like this:
.centered{
left: calc(50% - current-width/2)
}
Basically, a way to reference the current-width of the target element so that I can have a flexible-width item centered. Does this exist?

One answer is to move it left and then translate it:
.centered {
left: 50%;
transform: translate(-50%);
}
But a better way is to use Flexbox on the parent element:
.centeredParent {
display: flex;
justify-content: center;
}

Related

After changing the image, the CSS style of the image is invalid [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 4 months ago.
At first, my picture is like this, the avatar can be displayed in the center。
And the CSS code for class .avator is:
.idx_box .search_box .avator {
width: 0.64rem;
height: 0.64rem;
border-radius: 50%;
background: #feb5b4;
}
The CSS code for img is:
.idx_box .search_box .avator img {
width: 0.3rem;
height: 0.3rem;
margin: 0 auto;
-webkit-transform: translateY(45%);
-ms-transform: translateY(45%);
transform: translateY(45%);
}
where 1rem is equal to 100px;
However, when I changed the picture, the horizontal centering effect didn't work. The CSS code is still the same as above and has not changed. I can't find the reason. Picture as shown:
Thanks!
Just add this 'text-align:center;' to .avator class.
I think the reason it didnt work, was because the image wasnt centrally aligned in the first instance. This showed in the second instance when you put a smaller image.
By the way, this 'margin: 0 auto;' inside the img, does nothing.

How to vertically center a div inside another div that does not have pre determined height? (has height auto) [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 3 years ago.
So I already have done this once in this page: link to the website's page
In this page I was able to vertically center the text displayed on the image:
That column has 2 codes that make the text centered:
This one is added to the mother div
.academy-pc .one-third.mcb-wrap {
float: none;
display: table-cell;
position: relative;
}
and this one is on the child div that has the text:
.academy-pc .vertic-center {
position: absolute;
top: 50%;
transform: translate(0, -50%);
}
I've been trying to apply the same logic on this page but without success: page link
I wanted to vertically center the text in this image bellow but I just can't find a way.
Any help? thanks in advance
Into the .section_wrapper .mcb-section-inner add
display: flex;
justify-content: center;
align-items: center;
then everything inside will be vertically centered.
Of course you don't need to add padding: 200px 80px 0px 80px;

Sticky position not working in float-based layout, but works outside float-based layout [duplicate]

This question already has answers here:
Why position:sticky is not working when the element is wrapped inside another one?
(1 answer)
'position: sticky' not working when 'height' is defined
(3 answers)
Why bottom:0 doesn't work with position:sticky?
(2 answers)
Closed 3 years ago.
I'm wondering if anyone has any ideas why I'm having problems with position:sticky inside a float-based layout, but not outside of it.
You can see a fiddle at: https://jsfiddle.net/pjt0kmd7/1/
I'm using the following css for the sticky position:
.sticky {
min-width: 200px;
height: 300px;
background-color: red;
color: #fff;
position: sticky;
top: 1em;
z-index: 10000;
}
I've also tried removing overflow and heights on the first sticky's parents via:
overflow:none;
height:auto;
But that has not made any difference. Anyone know why the first red box isn't sticky like the second?
Edit Looks like I figured it out, the sticky parent needs a height defined? https://jsfiddle.net/pjt0kmd7/4/

CSS make height of divs equal (Adjusted to the div with most content) [duplicate]

This question already has answers here:
CSS - Equal Height Columns?
(11 answers)
Closed 5 years ago.
I am creating a quiz game for my website. It sometimes happens that the content of the answers is different in length, which means that the divs containing the content have different height.
I want them to have the same height. They must somehow get the height of the div with the most content
Here is a Fiddle with the problem.
The live site is https://www.path-of-exile-builds.com/quiz
The problem:
I tried using
display: flex;
But it never worked. Right now I have no ideas.
I will probably try to play around with JS and set the elements height on displaying the next question, but I feel like there should be an easier solution with CSS.
Yes, flexbox is the way, but you have to set it properly. This few lines of code did the magic:
.quiz-container {
display: flex;
flex-flow: row wrap;
}
.quiz-container .question {
flex: 1 0 100%;
}
.quiz-container .helper {
flex: 0 0 50%;
}
.quiz-container .helper .possible-answers {
height: 100%;
}
Try live version here: https://jsfiddle.net/xpdxyaz6/79/

How to center a div vertically from a unknown dynamic height? [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 5 years ago.
In my code there could be hundreds of elements depending on how much images is added to the pages, meaning the height of the page could be between 0 to 1000s or even more.
How would I center a overlay div in the middle of the page. The webpage is more or less trying to mimic Instagram, you can have hundreds of images but once you click on one of them the overlay is still set in the middle of the page.
This is the main div for the overlay of the images. this div works fine as long as the page is no longer than the view height of the page, if the height is too large that you have to scroll down, then it will mess with the position of the vertical. I know this problem is because im setting the bottom to 15% so it takes in consideration the entire height value. But I dont know how I can fix that.
.overlayImgContainer{
position: absolute;
height: 70%;
width: 120%;// (page is set to 960px this add a bit more room)
z-index: 1;
bottom: 15%;
left: -10%;
background-color: rgba(0,0,0,0.5);
overflow-x: hidden;
border-radius: 5px;
text-align: center;
}
You can use flexboxes for that.
CSS
.wrapper {
display: flex;
align-items: center;
justify-content: center;
}
HTML
<div class="wrapper">
<div>...</div>
</div>

Resources