How to fix my problems when it comes to centering elements? [duplicate] - html

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How to center a "position: absolute" element
(31 answers)
How can I horizontally center an element?
(133 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 3 months ago.
For some reason, I can't seem to get my elements centered on both pages I'm working on. I have the elements to step into the page by 50% the viewport and then translate back 50%, among a few other things.
The notable html :
<body>
<h1><ku>ク</ku><ri>リ</ri><su>ス</su></h1>
</body>
The notable css:
body {
background-color: rgb(0, 0, 0);
margin: 0;
}
h1 {
color: white;
width: fit-content;
height: fit-content;
margin: 0;
position: relative;
top: 50vh;
left: 50vw;
transform: translate(-50%,-50%);
}
P.S:
How do I best position elements on a page freely so page size doesn't affect them, in relation to both centering and other.
I've tried many things I've looked up and sketched it out to see if the math worked out, and I just can't seem to figure it out, but it doesn't seem to work.

Related

CSS Left : 50% puts element to the edge of the screen [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
How to center a "position: absolute" element
(31 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed last year.
I'm trying to center a button on my website, so I tried left : 50% to get it to the middle of the screen, but for some reason it brings it all the way to the right side of the screen. Here's the code:
#ContactButton {
font-size: 20px;
border: none;
width : 120px;
position : relative;
height: 30px;
font-weight: 500;
border-radius: 30px;
left : 50%;
}
There are many ways. On the button itself, you can just use margin: 0 auto;.
Or you could maybe just use text-align: center on the button's container.
Or, for example, you could use flexbox on the buttons container:
.container{
display: flex;
justify-content: center;
}
And there are several other possibilities. Check the link #cloned gave you on his comment.

Is there a way to position a html element absolute but still place it in the center of the page? [duplicate]

This question already has answers here:
Why magin:auto is not enough to center position absolute or fixed?
(3 answers)
How to center a "position: absolute" element
(31 answers)
Closed 3 years ago.
.container{
position: absolute;
margin: 0 auto;// this doesn't position it in the centre anymore.
}
<div class="container">
this is a container
</div>
"This is supposed to be placed in the center... but when i positioned it as absolute, it went back to the top of the webpage. Is there anyway to bypass this."
This will do it:
.container{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}

How do I center inline-blocks? [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
CSS center display inline block?
(9 answers)
Closed 4 years ago.
I was wondering how I can center inline-blocks. Any help?
I tried doing margin-top: x em; but all it did was have the image go down. This inline-block is deliberately inside an image block. If you have links that show me how to position inline-blocks, I will gladly take them into consideration, as I am still learning how to do CSS and HTML.
#img-banner article {
border: solid thick white;
box-sizing: border-box;
display: inline-block;
width: 14em;
position: static;
}
#img-banner h1 {
font-family: 'Lobster', cursive;
text-align: center;
color: white;
font-size: 27px;
}
<span id="img-banner">
<article>
<h1>Introduction</h1>
</article>
</span>
It would be important that you include the properties of the containing span as this is the parent you need to align on. Assuming you want to vertically center your element, you need to ask yourself if the height of your containing element is known and that will guide you towards the proper centering technique.
https://css-tricks.com/centering-css-complete-guide/
You will find alot of common centering techniques on this website. You can consider your inline-block element as a block-level element.
Personally, I like making sure my container is position:relative and then add top:50%; transform: translateY(-50%) to my unknown height positioned elements.
Edit : As noted in your comments, this HTML is invalid. Only inline elements are allowed in a span. I don't know why I overlooked this.

How to vertically and horizontally center div [duplicate]

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 6 years ago.
I am facing a problem, how to center text (h1) inside div. I want the web page to be full screen and i achieved that with vh units.
.page { height: 100vh;
width: 100%;}
On that page i want div to be centered horizontally and vertically. I tried with
.div { top: 50%;
left: 50%;}
but that just didnt do the trick. I also want this to be "resposive" si that it is centered on any screen.
I am looking to build something like this: http://www.anthonygoodisondesign.com/
use css code as below;
div{height:100vh;
display:flex;
align-items:center;
justify-content:center;
}
<div> <h1>center me</h1></div>
Use this:
text-align: center;
position: relative;
top: 40vh;
transform: translateY(-50%);
https://jsfiddle.net/yak613/pLb8aysd/

Center div horizontally and vertically with container of 100% width and height [duplicate]

This question already has answers here:
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
How can I horizontally center an element?
(133 answers)
Closed 6 years ago.
What is the best way to center div in the middle of another element?
I have an container with with set to 100% and height to 130% of the screen but i fail to centr it int the middle ( of the container ).
i tried using
div {
margin: auto;
top: 0;
bottom: 0;
right: 0;
left: 0;
position: absolute;
text-align: center;
vertical-align: middle;
}
But this will just set the the content of div in the middle horizontally only , and set the width and height of the inner element to container element.
What is the best trick to achieve such thing? Here is demo