How to center <input> horizontally in a <div>? [duplicate] - html

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Place input box at the center of div
(5 answers)
Closed 5 years ago.
I have attempted multiple times, with multiple different possible solutions from around Stack Overflow to accomplish this task, yet frustratingly, I cannot seem to be able to center the input box in the div.
My code essentially looks like this at the moment:
div {
width: 100vw;
position: relative
}
h1 {
text-align: center;
}
<div>
<h1>Title</h1>
<input type='text'>
</div>
As of the moment, the word "Title" is centered on the screen, but the input box is still on the left hand side.
Is there any way to be able to center the horizontally in the div?

Apply flexbox to the container div.
Note: centring the h1 becomes unnecessary after applying flex
div {
width: 100vw;
position: relative;
display: flex;
flex-flow: column wrap;
align-items: center;
}
<div>
<h1>Title</h1>
<input type='text'>
</div>

Try this:
input[type="text"] {
display: block;
margin : 0 auto;
}
Example: Demo

Display block. input elements are inline.
<input type='text' style="display:block; margin : 0 auto;">

Related

Positioning button [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 4 months ago.
I was centering the elements of my page with:
display: block;
margin-left: auto;
margin-right: auto;
but when I try to do this with one div that has two buttons they stay in the left corner, why? and how I place them in the center.
Option 1
If both the buttons are inside the div container you also need to specify the width of the div container, because by default div covers the complete width.
div{
max-width:10rem;
margin :0px auto;
}
<div>
<button>Button1</button>
<button>Button2</button>
</div>
Option 2
You can also flex the div container to center the buttons
div{
display:flex;
align-items:center;
justify-content:center;
}
<div>
<button>Button1</button>
<button>Button2</button>
</div>
Option 3
You can also use the simple text align center property on the div container so it will center the buttons
div{
text-align:center;
}
<div>
<button>Button1</button>
<button>Button2</button>
</div>
because buttons are inline elements.
Not sure about the context but you can use this centering pattern (both horizontal and vertical) with Flexbox as well:
.container {
display: flex;
align-items: center;
justify-content: center;
}
Positioning is very easy with flexbox. Please try following properties on your div
display: flex;
justify-content: center;
align-items: center;
Justify content will place content centrally along horizontal axis and align items will place content centrally along vertical axis (for flex direction row which is default)
The div css:
text-align: center

Float: right property not working in flexbox [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 1 year ago.
So I'm using a flexbox in my webpage , and I have two elements in it.
I want the first element to be aligned center and the second on to be aligned towards the right. I am able to align them to the center with flexbox properties, but I'm not able to align the second one to the left. Float: right doesn't work apparently.
Here's my code:
Html:
<div>
<p> Hello </p>
<span> Hey! </span>
</div>
Css:
div {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
span{
float: right;
}
Here's a fiddle attempt.
since you are using flex,it is seeing you justify-content: center;
So you need to write something which overwrites it and which is:
span{
justify-content: flex-end;
}
to align the right

How to place item center [duplicate]

This question already has answers here:
How do I vertically align text in a div?
(34 answers)
How can I horizontally center an element?
(133 answers)
Closed 2 years ago.
I am trying to put a div in the middle of another div, but always left aligns it.
How I do it easily?
<div>
<div>
<p>This I want center</p>
</div>
</div>
The question is answered with the easiest google search, but the most obvious way is to give them CSS classes and then style them with CSS
<div class="parent">
<div class="child">
<p>Centred</p>
</div>
</div>
And in CSS
.parent{
display: grid;
place-items: center;
background-color: red;
}
.child{
background-color: blue;
}
If it's just text you can use:
text-align: center;
If you want to align the div, give to the div you want to center a margin:auto, but make sure to give it a width first, otherwise it will not work. For example:
width: 500px;
margin: auto;

How to set a DIV block with dynamic size with header DIV block above it at center of the page horizontally and vertically? [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 3 years ago.
I have a DIV block where there are a title and an image into the him. The image is setted by mouse click, so I don't know the size of image and DIV block. It's a structure:
.view-image .header { text-align: center; border: 1px solid black; }
.view-image .image { max-width: 100%; }
.view-image {
position: absolute;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
width: fit-content;
height: fit-content;
max-width: 100%;
text-align: center;
}
<div class = "view-image">
<div class = "header">View of image</div>
<div class = "image"><img src="https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png" /></div>
</div>
I need that view-image block will take place in the centre of page horizontally and vertically. And, it's very important, the width of header and image blocks must be same.
Later I saw that Firefox doesn't support height: fit-content, in the IE and Edge also width: fit-content doesn't work.
How to make this? Of course, I can set a view-image sizes by JavaScript after image selecting, but if is it possible, it will be better do it without JS.
P.S. I want to write why my question isn't a duplicate in my opinion. The main complexity is the width of header block. If I use the flexbox as you suggest, the width of header will be count by width of the its inner text, and header width doesn't equal to image width. But if I use the fit-content as in my code, the header width and the image width are idential. It's very important for me, because in my site the header has a borders and background color, and if these blocks have different width, it won't be good. Maybe I wrote my question incorrect, then I'm sorry and I have edited my question. I have changed the title and added the border for header block in my code
Added: I found the solution by changing the structure to this:
<div class = "view-image">
<div class = "image">
<div class = "header">View of image</div>
<img src="https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png" />
</div>
</div>
I'm very sorry for incorrect question
Here is an example using flexbox:
.view-image {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
<div class="view-image">
<div class="header">View of image</div>
<div class="image">
<img src="https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png"/>
</div>
</div>

Why is this not aligned properly with display: inline-block and some text? [duplicate]

This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Closed 3 years ago.
I'd like to know the reason why it aligns differently when there is text or any other element inside the div with display: inline-block? I know vertical-align fixes it, but I am curious to know how the browser determines to display like that.
div {
width: 100px;
height: 100px;
background: #dd6b4d;
display: inline-block;
/* vertical-align: top; */
}
.inner {
width: 50px;
height: 50px;
background: green;
}
<html>
<body>
<div></div>
<div>aaa</div>
<div>
<div class="inner"></div>
</div>
</body>
</html>
The default value of vertical-align (if you declare nothing), is
baseline
Unless it is overridden, this rule applies. When text is put in the inline-block, that text will create a baseline for the inline-block.
For reference, here is the article on CSS-Tricks