This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 4 months ago.
I have an inline-block element and would like to centre the text next to it. I had already tried negative margin on the span (box) element but this changed the whole content (box and text).
MY code
fieldset {
max-width: 350px;
margin: 0 auto;
padding:1px;
display: flex;
justify-content: space-around;
align-items: center;
}
.legend-item-box {
display: inline-block;
height: 16px;
width: 30px;
margin-right: 5px;
margin-left: 0;
border: 1px solid #999;
}
.a {
background:#8DD3C7;
}
.b {
background:#FFFFB3;
}
<fieldset>
<legend>Legend:</legend>
<div class="legend-item-container">
<span class="a legend-item-box"></span>Legend a
</div>
<div>
<span class="b legend-item-box"></span> Legend b
</div>
</fieldset>
Many thanks in advance! Max
Is this what you want?
fieldset {
max-width: 350px;
margin: 0 auto;
padding:1px;
display: flex;
justify-content: space-around;
align-items: center;
}
.legend-item-container {
display: flex;
align-items: center;
}
.legend-item-box {
display: inline-block;
height: 16px;
width: 30px;
margin-right: 5px;
margin-left: 0;
border: 1px solid #999;
}
.a {
background:#8DD3C7;
}
.b {
background:#FFFFB3;
}
<fieldset>
<legend>Legend:</legend>
<div class="legend-item-container">
<span class="a legend-item-box"></span>Legend a
</div>
<div>
<span class="b legend-item-box"></span> Legend b
</div>
</fieldset>
You can use vertical-align: middle; to archive this. https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align?retiredLocale=en
fieldset {
max-width: 350px;
margin: 0 auto;
padding:10px;
display: flex;
justify-content: space-around;
align-items: center;
}
.legend-item-box {
vertical-align: middle; /* <--- added */
display: inline-block;
height: 16px;
width: 30px;
margin-right: 5px;
margin-left: 0;
border: 1px solid #999;
}
.legend-item-container {}
.legend-text { /* <--- added */
font-size: 0.6rem;
}
.a {
background:#8DD3C7;
}
.b {
background:#FFFFB3;
}
<fieldset>
<legend>Legend:</legend>
<div class="legend-item-container">
<span class="a legend-item-box"></span><span class="legend-text">Legend a</span>
</div>
<div class="legend-item-container"><!-- added class -->
<span class="b legend-item-box"></span><span class="legend-text">Legend b</span>
</div>
</fieldset>
Related
This question already has answers here:
How does the vertical-align property work?
(2 answers)
Closed 2 years ago.
I can't figure this out, it's suposed to put the boxes in the middle of it's container, but I can't make them move.
The idea is to center the inside the wrapper and to place them horizontally in the middle without having to fuzz around with margins or paddings and using veritcal-align.
#wrapper {
width: 1000px;
height: 1000px;
}
#container {
width: 900px;
height: 900px;
text-align: center;
display: inline-block;
background-color: lightblue;
}
.box {
width: 200px;
height: 200px;
margin: 0 auto;
display: inline-block;
vertical-align: middle;
background-color: lightgreen;
border: 1px solid grey;
margin: 10px;
}
<div id="wrapper">
<div id="container">
<div class="box">BOXES</div>
<div class="box">BOXES</div>
<div class="box">BOXES</div>
</div>
</div>
I think you are looking for flexbox.
I have adapted your jsfiddle to fit
https://jsfiddle.net/ke4w58ra/
The folowing code is what I have changed to your #content element.
display: flex;
justify-content: center;
align-items: center;
gap: 5px;
Essentially, setting the elements to display in the center horisontally (align-items) and vertically (justify-content). With a gap of 5px to space the boxes out.
For more information, look here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Below is the integrated form of the JSFiddle
#container{
width: 100vw;
height: 100vh;
text-align: center;
display: inline-block;
background-color: lightblue;
display: flex;
justify-content: center;
align-items: center;
gap: 5px;
}
.box{
width: 200px;
height: 200px;
margin: 0 auto;
background-color: lightgreen;
border: 1px solid grey;
margin: 10px;
display: flex;
align-items: center;
justify-content: center;
}
body {
margin: 0px;
}
<body>
<div id="container">
<div class="box">BOXES</div>
<div class="box">BOXES</div>
<div class="box">BOXES</div>
</div>
</body>
This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 2 years ago.
I have a flexbox container with 3 flex items
the text of the two outer items should be at the center point of each item
so far the text appears on the top of
using align-items: center;
recommended in How to vertically align text inside a flexbox?
on the container just squished the divs to one line, ignoring the heigth of the container.
how to center the text at the center of each div ?
body {
margin: 0;
}
.file_upload-container {
display: flex;
margin: 20px;
height: 200px;
/*align-items: center;*/
justify-content: center;
}
.drop-zone_main {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
}
.upload_spacer {
background-color: #00ffb7;
flex-basis: 200px;
}
.drop-zone_second {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
flex-basis: 300px;
}
input {
display: none;
}
<div class="file_upload-container">
<div class="drop-zone_main">Drop MainFile here or click to upload</div>
<input class="input_main" type="file" name="upload_mainfile">
<div class="upload_spacer"></div>
<div class="drop-zone_second">Drop second file here or click to upload</div>
<input class="input_second" type="file" name="upload_secondfile">
</div>
You can wrap your input in a div, so it gets aligned with its siblings, and use display: flex to align the inner content:
body {
margin: 0;
}
.file_upload-container {
display: flex;
margin: 20px;
height: 200px;
/*align-items: center;*/
justify-content: center;
}
.drop-zone_main {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
}
.upload_spacer {
background-color: #00ffb7;
flex-basis: 200px;
}
.drop-zone_second {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
flex-basis: 300px;
}
.input-wrap {
display: flex;
align-items: center;
}
<div class="file_upload-container">
<div class="drop-zone_main">Drop MainFile here or click to upload</div>
<div class="input-wrap">
<input class="input_main" type="file" name="upload_mainfile">
</div>
<div class="upload_spacer"></div>
<div class="drop-zone_second">Drop second file here or click to upload</div>
<div class="input-wrap">
<input class="input_second" type="file" name="upload_secondfile">
</div>
</div>
You need to correct the css rules :
.drop-zone_main {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
justify-content: center;
align-items: center;
display: flex;
}
.drop-zone_second {
color: #cccccc;
border: 4px dashed #009578;
border-radius: 10px;
flex-basis: 300px;
justify-content: center;
align-items: center;
display: flex;
}
This question already has answers here:
Why are flex items not wrapping?
(2 answers)
Closed 2 years ago.
I have two boxes I want to be displayed side-by-side. I had them displayed side-by-side fine but the left box was not vertically centered.
I followed a guide online on how to vertically align the boxes and it worked, except now the responsiveness is all messed up, if I resize the window to be very thin, the boxes dont wrap or overflow neatly.
https://jsfiddle.net/martinradio/adqLpxn5/85/
<div>
<style>
#container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
align-items: center;
width:100%;
}
.box {
width: 50%;
box-sizing:border-box;
margin: 5px;
text-align: center;
}
</style>
<div id="container" class='w-100' >
<!-- flex container -->
<div class="box" style='background:red'>
<!-- flex item -->
<div class="form-group">
<div id='taggerErrDisplay'></div>
<input size="22" type="text" placeholder='Discogs URL' name="url" id="urlInput">
<button style="cursor: pointer;" id='urlInputButton' name="data" type="button"
onclick="submitDiscogsURL(document.getElementById('urlInput').value)">Submit</button>
</div>
</div>
<div class=" box" style='background:blue'>
<!-- flex item -->
<div id="drop-area">
<form class="my-form">
<h5>Upload multiple files with the file dialog or by dragging and dropping files here.</h5>
<br>
<input style="cursor: pointer;" type="file" id="file" multiple="multiple" />
</form>
</div>
</div>
</div>
<style>
#drop-area {
/* border: 2px dashed #ccc; */
border-radius: 20px;
width: 480px;
padding: 20px;
}
/*
#drop-area.highlight {
border-color: purple;
}
*/
p {
margin-top: 0;
}
.my-form {
margin-bottom: 10px;
}
#gallery {
margin-top: 10px;
}
#gallery img {
width: 150px;
margin-bottom: 10px;
margin-right: 10px;
vertical-align: middle;
}
.button {
display: inline-block;
padding: 10px;
background: #ccc;
cursor: pointer;
border-radius: 5px;
border: 1px solid #ccc;
}
.button:hover {
background: #ddd;
}
#fileElem {
display: none;
}
</style>
</div>
I tried changing the flex properties but it causes both elements to remain permanently overflowed:
I don't know but you can try this.
Change the css as below:
#container {
display: flex;
flex-direction: row;
align-items: center;
width:100%;
}
.box {
width: 50%;
box-sizing:border-box;
margin: 5px;
text-align: center;
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
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 2 years ago.
I have tried to center my divs like described on this website but something is wrong and I don't get it. Maybe you know what the problem is, because I can't find a proper solution to center the divs in the exact middle. I have added just one css input description because the others are the same.
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
I want to offer two options - flex and table. Wrap in two parent divs (an additional parent div is needed so that the top label is left-aligned and centered at the same time), and add flex rules to the topmost parent div. Did you need it?
flex:
.center_flex {
display: flex;
justify-content: center;
}
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div class="center_flex">
<div class="center_flex_container">
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
</div>
</div>
For a table, it is enough to wrap it in only one parental div, setting the margin rules:
.center_table {
display: table;
margin-left: auto;
margin-right: auto;
}
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div class="center_table">
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
</div>
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 3 years ago.
I'm trying to center the title of an html card within the header div, which has an "x" button.
I've tried centering using margin: 0 auto but it centers within what is left/remainder space of the div. What i want is to center within the full length of the div (true center).
<div class="timer-card">
<div class="head">
<p class="title">Timer title</p>
<button class="close">X</button>
</div>
<div class="body">
<p class="countdown">0:00</p>
<div class="footer"><button>Stop</button><button>Play</button><button>Reset</button></div>
</div>
</div>
.timer-card {
border-color: grey;
border-width: 1px;
border-style: solid;
width: 200px;
background-color: grey;
box-sizing: border-box;
padding: 10px;
}
.timer-card .head {
display: flex;
justify-content: space-between;
}
.timer-card p.title {
text-align: center;
}
https://codepen.io/anon/pen/PvZqZz
position: relative on the .head element;
position: absolute; right: 0; on the .close element;
margin: 0 auto; on the .title element.
Code snippet below:
.timer-card {
border-color: grey;
border-width: 1px;
border-style: solid;
width: 200px;
background-color: grey;
box-sizing: border-box;
padding: 10px;
}
.timer-card .countdown {
font-size: 40px;
}
.timer-card button.close, .timer-card .title {
display: inline-block;
}
.timer-card .head {
display: flex;
justify-content: space-between;
position: relative;
}
.timer-card .title {
margin: 0 auto;
}
.timer-card .close {
position: absolute;
right: 0;
}
.timer-card .body p.countdown {
margin: 0 auto;
text-align: center;
}
.timer-card .footer {
padding-top: 15px;
display: flex;
justify-content: center;
}
.timer-card p.title {
text-align: center;
}
<div class="timer-card">
<div class="head">
<p class="title">Timer title</p>
<button class="close">X</button>
</div>
<div class="body">
<p class="countdown">0:00</p>
<div class="footer"><button>Stop</button><button>Play</button><button>Reset</button></div>
</div>
</div>
A quick dirty hack would be to just add half the size of the X button as margin-left on your CSS
.title {
width: 100%;
margin-left: 25px;
}