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

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.

Related

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

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.

Center align a span inside a div with an img element [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Vertically align text next to an image?
(26 answers)
Flexbox: center horizontally and vertically
(14 answers)
How do I vertically align text in a div?
(34 answers)
Vertically centering a div inside another div [duplicate]
(24 answers)
Closed 4 years ago.
I am trying to center align a span inside a div, which also contains an img element.
.element {
display: inline-block;
}
.element img {
height: 40px;
width: 40px;
border-radius: 50%;
margin-right: 20px;
}
.element span {
display: inline-block;
}
<div class="element">
<img src="https://images.pexels.com/photos/35646/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500">
<span>hello</span>
</div>
or see this fiddle
However the text wont vertical align. I have looked primary at this question. However vertical-align: middle does nothing here.
I also looked at this question. However I will rather avoid anything position: relative & position: absolute workarounds for this. I also tried messing the line-height with no luck.
I even tried to set height: 100%on the span, as this question suggests, but that does nothing either.
I basically looked at bunch of questions here on SO, it seems like css is so weird about this, that there basically is 12 approaches for a simple thing like this. Yet I can't seem to get 1 of them to work in my occasion.
What is up with this behavior?
EDIT:
Marked as duplicate to How to Vertical align elements in a div? - I have explained that these solutions with line-height and vertical align doesn't work in my case as explained in the original question. The accepted solution did not work in this case. How is it a duplicate?
The answer here is probably to use flexbox. If your flex-direction is row (which is default), you can use align-items to center the elements vertically and justify-content to justify the row to the left (the "start" of the flex container). Let me know if you have any questions!
.element {
align-items: center;
display: flex;
justify-content: flex-start;
}
.element img {
height: 40px;
width: 40px;
border-radius: 50%;
margin-right: 20px;
}
.element span {
display: inline-block;
}
<div class="element">
<img src="https://images.pexels.com/photos/35646/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500">
<span>hello</span>
</div>
Use flexbox for this. Sample:
.element {
display: flex;
align-items: center;
}
Use align-items: center for vertical align and justify-content: center; if you need also horizontal align center.

Center a label vertically inside a div [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
How do I vertically center text with CSS? [duplicate]
(37 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 4 years ago.
I am trying to do something I think is pretty easy but clearly making a pigs ears of it. Here is my fiddle
I have a button and a label on display. I want to centre the label in the middle of the div, so horizontally and vertically centred.
I can centre it horizontally but not vertically. So I have tried a few things but nothing seems to centre the label vertically, even the vertical-align I use below. Not really sure why though?
.headerLbl {
text-align: center;
font-weight: bold;
color: white;
font-size: 14pt;
display: block;
vertical-align: central;
}
You need to add following styles into your code:
.menu {
background-color: #9FACEC; /* Medium blue */
position: fixed;
width: 100%;
margin: 0;
z-index: 1;
display:flex;
justify-content: center;
}
.headerLbl {
align-self: center;
width: 90%;
text-align: center;
}

Make text in the center of a div [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
How do I vertically center text with CSS? [duplicate]
(37 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 4 years ago.
Thats not a div I want I just cant get to post it because it does not meet some requirement. I have never used anything like that before, just need help for a college project that requires programming.
I have this: enter image description here
I just want the text to be centered.
jsfiddle .net /etr1ok47/#&togetherjs=VfasppUOxh
using flex in this-p class:
display: flex;
flex-direction: column;
justify-content: center;
Try applying the following to your containing div (.parent) and the wrapper around your text (.child).
.parent {
height: 300px;
line-height: 300px;
text-align: center;
}
.child {
line-height: normal;
display: inline-block;
vertical-align: middle;
}
You can add padding to the top of your p element in css if you are needing the words in the circle to be vertically better centered.
p.this-p{
vertical-align: middle;
}
If the problem is the p itself not centering in the li element then define the parent with display: table and the element itself with vertical-align: middle and display: table-cell
p.this-p{ vertical-align: middle; align: center}
Above is when you use p element with padding. Same is applicable for other element

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