I was wondering whether there is a better way (more dynamic) way of left aligning the title with the input boxes below it using flexbox. The input boxes need to be displayed inline with the title above them. Currently I've had to wrap a div around both the title and input boxes and set a fixed pixel width. Is there a better way of achieving the same result with flexbox?
I've tried using inline-flex on this wrapper but it's still treating it as a full width element instead of being as wide as its content. I've also tried setting inline-flex on the unordered list and the result is exactly the same.
I would like to not have to set a fixed pixel width. The next time if I need to do the same but with a different element, I would need to keep setting a fixed pixel size which isn't ideal.
Thanks in advance
html {
font-size: 14px;
}
.section {
padding: 1.5rem;
}
.wrapper {
display: flex;
background: lightblue;
}
.warning-percentage-wrapper {
background: lightcoral;
flex: 0 0 30%;
}
.warning-percentage {
display: inline-flex;
margin-left: -1rem;
}
.warning-percentage li {
flex: 0 1 80px;
margin-left: 1rem;
}
.days-of-week-wrapper {
background: lightseagreen;
flex: 1;
display: flex;
justify-content: flex-end;
}
.inline-wrapper {
background: pink;
display: flex;
flex-direction: column;
flex: 0 1 644px;
}
.days-of-week {
display: inline-flex;
margin-left: -1rem;
}
.days-of-week li {
flex: 0 1 80px;
margin-left: 1rem;
}
<section class="section">
<div class="wrapper">
<div class="warning-percentage-wrapper">
<h5 class="title is-5">Warning Percentages</h5>
<ul class="warning-percentage">
<li>
<label class="label">Low</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Medium</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">High</label>
<input class="input" type="text" readonly>
</li>
</ul>
</div>
<div class="days-of-week-wrapper">
<div class="inline-wrapper">
<h5 class="title is-5">Days of Week</h5>
<ul class="days-of-week">
<li>
<label class="label">Monday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Tuesday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Wednesday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Thursday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Friday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Saturday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Sunday</label>
<input class="input" type="text" readonly>
</li>
</ul>
</div>
</div>
</div>
</section>
if you want something as same as the pic you put in your question, you must remove padding/margin from your elements (such as input, label)
html {
font-size: 14px;
}
.wrapper {
display: flex;
background: lightblue;
}
.warning-percentage-wrapper {
background: lightcoral;
}
.warning-percentage {
display: inline-flex;
}
.warning-percentage li {
flex: 0 1 80px;
margin-left: 1rem;
}
.days-of-week-wrapper {
background: lightseagreen;
flex: 1;
display: flex;
justify-content: flex-end;
}
.inline-wrapper {
background: pink;
display: flex;
flex-direction: column;
}
.days-of-week {
display: inline-flex;
}
<section class="section">
<div class="wrapper">
<div class="warning-percentage-wrapper">
<h5 class="title is-5">Warning Percentages</h5>
<ul class="warning-percentage">
<li>
<label class="label">Low</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Medium</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">High</label>
<input class="input" type="text" readonly>
</li>
</ul>
</div>
<div class="days-of-week-wrapper">
<div class="inline-wrapper">
<h5 class="title is-5">Days of Week</h5>
<ul class="days-of-week">
<li>
<label class="label">Monday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Tuesday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Wednesday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Thursday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Friday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Saturday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Sunday</label>
<input class="input" type="text" readonly>
</li>
</ul>
</div>
</div>
</div>
</section>
<!-- end snippet -->
The problem I'm having with inline-flex is caused using a flex basis with a pixel value. I needed the inputs to all be the same width. I forgot to mention I am using Bulma (not by choice) which removes all the margins and padding's from elements by default.
My example below should hopefully provide a better understanding of the problem. Notice I've set the UL - 'days-of-week' to align-self. Now the UL is as wide a its content which is what I wanted. However if you hover over the element with your dev tools there's still quite a bit of space remaining. This is because I'm using a flex-basis with a pixel value. If you set to LI to flex: 1 and inspect the DOM, the elements are nicely fill the space, including the margins.
The solution for this is to remove the flex styles altogether on the LI elements and set a fixed pixel width on the 'inline-wrapper' element. I don't think this is ideal but it allows you to control the size of the LI's without needing additional css styling on the LI's themselves.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="bulma-0.8.0/css/bulma.min.css" rel="stylesheet" />
<style>
html {
font-size: 14px;
}
.section {
padding: 1.5rem;
}
.wrapper {
display: flex;
background: lightblue;
}
.warning-percentage-wrapper {
background: lightcoral;
flex: 0 0 30%;
}
.warning-percentage {
display: inline-flex;
margin-left: -1rem;
}
.warning-percentage li {
flex: 0 1 80px;
margin-left: 1rem;
}
.days-of-week-wrapper {
background: lightseagreen;
flex: 1;
display: flex;
justify-content: flex-end;
}
.inline-wrapper {
background: pink;
display: flex;
flex-direction: column;
flex: 0 1 644px;
}
.days-of-week {
display: inline-flex;
align-self: flex-start;
}
.days-of-week li {
flex: 0 1 80px;
margin-left: 1rem;
}
.days-of-week li:first-child {
margin-left: 0;
}
</style>
</head>
<body>
<section class="section">
<div class="wrapper">
<div class="warning-percentage-wrapper">
<h5 class="title is-5">Warning Percentages</h5>
<ul class="warning-percentage">
<li>
<label class="label">Low</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Medium</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">High</label>
<input class="input" type="text" readonly>
</li>
</ul>
</div>
<div class="days-of-week-wrapper">
<div class="inline-wrapper">
<h5 class="title is-5">Days of Week</h5>
<ul class="days-of-week">
<li>
<label class="label">Monday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Tuesday</label>
<input class="input" type="text" readonly>
</li>
<li>
<label class="label">Wednesday</label>
<input class="input" type="text" readonly>
</li>
</ul>
</div>
</div>
</div>
</section>
</body>
</html>
Related
This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 3 months ago.
Image of my Form
I'd like to move this form to the middle of my web page. I have it center aligned currently, but it is at the top of my webpage. I want it smack dab in the middle. I've been trying to google around but this has been a surprisingly difficult answer to find.
html
<body>
<div class="form">
<form action="./script.js" method="post">
<ul>
<li>
<label for="date">Date:</label>
<input type="date" id="date" name="date" step="0.1" min="0"
placeholder="What's the date today?" size="50">
</li>
<li>
<label for="mileage">Mileage:</label>
<input type="number" id="mileage" name="mileage" step="0.1" min="0"
placeholder="How many miles today?" size="50">
</li>
<li>
<label for="note">Notes:</label>
<textarea id="note" name="user_message" placeholder="How did the run feel?"></textarea>
</li>
<li class="button">
<button type="submit">Submit</button>
</li>
</ul>
</form>
</div>
</body>
</html>
CSS
div.form {
display: block;
text-align: center;
}
form {
margin: 0 auto;
width: 600px;
padding: 1em;
border: 1px solid lightgrey;
border-radius: 1em;
display: inline-block;
margin-left: auto;
margin-right: auto;
text-align: left;
}
form li+li {
margin-top: 1em;
}
This should work:
div.form {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
form {
margin: 0 auto;
width: 600px;
padding: 1em;
border: 1px solid lightgrey;
border-radius: 1em;
text-align: left;
}
form li+li {
margin-top: 1em;
}
<body>
<div class="form">
<form action="./script.js" method="post">
<ul>
<li>
<label for="date">Date:</label>
<input type="date" id="date" name="date" step="0.1" min="0"
placeholder="What's the date today?" size="50">
</li>
<li>
<label for="mileage">Mileage:</label>
<input type="number" id="mileage" name="mileage" step="0.1" min="0"
placeholder="How many miles today?" size="50">
</li>
<li>
<label for="note">Notes:</label>
<textarea id="note" name="user_message" placeholder="How did the run feel?"></textarea>
</li>
<li class="button">
<button type="submit">Submit</button>
</li>
</ul>
</form>
</div>
</body>
</html>
Super easy, give your container all the view height and then super center it with grid.
I forced your form to half of the view width just to show it was centered.
.form-container {
height: 100vh;
display: grid;
place-items: center;
}
/* this is just style */
form {
margin: 0 auto;
width: 50vw;
padding: 1em;
border: 1px solid lightgrey;
border-radius: 1em;
display: inline-block;
margin-left: auto;
margin-right: auto;
text-align: left;
}
form li+li {
margin-top: 1em;
}
<div class="form form-container">
<form action="./script.js" method="post">
<ul>
<li>
<label for="date">Date:</label>
<input type="date" id="date" name="date" step="0.1" min="0" placeholder="What's the date today?" size="50">
</li>
<li>
<label for="mileage">Mileage:</label>
<input type="number" id="mileage" name="mileage" step="0.1" min="0" placeholder="How many miles today?" size="50">
</li>
<li>
<label for="note">Notes:</label>
<textarea id="note" name="user_message" placeholder="How did the run feel?"></textarea>
</li>
<li class="button">
<button type="submit">Submit</button>
</li>
</ul>
</form>
</div>
form {
margin: 0 auto;
width: 600px;
padding: 1em;
border: 1px solid lightgrey;
border-radius: 1em;
text-align: left;
}
form li+li {
margin-top: 1em;
}
.form{
display:flex;
height:calc(100vh - 2px);
justify-content:center;
align-items:center;
border:solid 1px red;}
body,html,div{
margin:0;
padding:0;}
<body>
<div class="form">
<form action="./script.js" method="post">
<ul>
<li>
<label for="date">Date:</label>
<input type="date" id="date" name="date" step="0.1" min="0"
placeholder="What's the date today?" size="50">
</li>
<li>
<label for="mileage">Mileage:</label>
<input type="number" id="mileage" name="mileage" step="0.1" min="0"
placeholder="How many miles today?" size="50">
</li>
<li>
<label for="note">Notes:</label>
<textarea id="note" name="user_message" placeholder="How did the run feel?"></textarea>
</li>
<li class="button">
<button type="submit">Submit</button>
</li>
</ul>
</form>
</div>
</body>
how can I align the labels & inputs to the right
like that all of then appear in the same line
.radioContainer{
width: fit-content;
margin: 5px;
margin-top: 20px;
background-color: aqua;
padding-bottom: 25px;
}
<div class="radioContainer" style="margin-bottom: 40px; margin-right: 0px; ">
<label class="title" for="">fav food</label>
<label for="burger">burger</label>
<input type="checkbox" id="burger">
<br>
<label for="fries">fries</label>
<input type="checkbox" id="fries">
<br>
<label for="onionRings">onion rings</label>
<input type="checkbox" id="onionRings">
<br>
<label for="cackes">cakes</label>
<input type="checkbox" id="cackes" >
<small></small>
</div>
To have them in the same line, I would start by removing <br /> from your code. Then set the css for input and label to be inline-block, something like:
.radioContainer{
width: fit-content;
margin: 5px;
margin-top: 20px;
background-color: aqua;
padding-bottom: 25px;
}
label, input { display: inline-block; }
<div class="radioContainer" style="margin-bottom: 40px; margin-right: 0px; ">
<label class="title" for="">fav food</label>
<label for="burger">burger</label>
<input type="checkbox" id="burger">
<label for="fries">fries</label>
<input type="checkbox" id="fries">
<label for="onionRings">onion rings</label>
<input type="checkbox" id="onionRings">
<label for="cackes">cakes</label>
<input type="checkbox" id="cackes" >
<small></small>
</div>
To make all inputs inline, just remove all the <br /> tags.
Example:
<div class="radioContainer" style="margin-bottom: 40px; margin-right: 0px; ">
<label class="title" for="">fav food</label>
<label for="burger">burger</label>
<input type="checkbox" id="burger">
<label for="fries">fries</label>
<input type="checkbox" id="fries">
<label for="onionRings">onion rings</label>
<input type="checkbox" id="onionRings">
<label for="cackes">cakes</label>
<input type="checkbox" id="cackes">
<small></small>
</div>
Codepen: https://codepen.io/manaskhandelwal1/pen/jOMeqex
I understood your Question that you want them still below each other but aligned to the right side. So i made a solution using flexbox instead of the hard breaks. I commented the Code where i made changes and why, html and css.
Basically i used a colum and put each item-combination (label and checkbox)in a new row-div.
.radioContainer{
width: fit-content;
margin: 5px;
margin-top: 20px;
background-color: aqua;
padding-bottom: 25px;
/*make item a flexbox-container*/
display: flex;
/*combination of flex-direction and flex-wrap*/
flex-flow: column wrap;
}
.row{
/*make item a flexboc container*/
display: flex;
/*flex-flow: row nowrap; this is the default value of flex, which is why you dont need it,
just wanted to leave it in as a comment so you know whats happening*/
/*Align the contents on the end of each row*/
justify-content: flex-end;
}
<div class="radioContainer" style="margin-bottom: 40px; margin-right: 0px; ">
<!--removed the hard breaks and added row-divs around each item combination-->
<div class="row">
<label class="title" for="">fav food</label>
</div>
<div class="row">
<label for="burger">burger</label>
<input type="checkbox" id="burger">
</div>
<div class="row">
<label for="fries">fries</label>
<input type="checkbox" id="fries">
</div>
<div class="row">
<label for="onionRings">onion rings</label>
<input type="checkbox" id="onionRings">
</div>
<div class="row">
<label for="cackes">cakes</label>
<input type="checkbox" id="cackes" >
</div>
<small></small>
</div>
I'm creating a HTML form, and I'm using UL's and LI's to organize my fields.
Inside each LI i have a label and a Input. After some CSS to make LI display:inline-block, I get this result:
I have the example in this fiddle: https://jsfiddle.net/cygv07px/
The question is that I want that the street field to be in a new line, like this:
I achievied that by putting a <br /> element between the <LI>, but that doesn't seems to be a elegant solution, and I keep getting validation messages from Visual Studio, saying that I cant have a BR element inside a LI.
How can i specify a line break before Street Field (or after the name field) in a better way?
Use float:left and clear it for every odd child.
li {
list-style: none;
float: left;
}
label { display: block }
li:nth-child(odd) { clear: left;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
li {
list-style: none;
float: left;
}
label { display: block }
li:nth-child(odd) { clear: left; }
</style>
</head>
<body>
<ul>
<li>
<label for="docNo">Doc Number</label>
<input type="text" id="docNo" />
</li>
<li>
<label for="name">Name</label>
<input type="text" id="name" />
</li>
<li>
<label for="street">Street</label>
<input type="text" id="street" />
</li>
<li>
<label for="houseNumber">House Number</label>
<input type="text" id="houseNumber" />
</li>
</ul>
</body>
</html>
One option is to use display block and floats.
li {
list-style: none;
display: block;
float: left;
margin: 0 1rem 1rem 0;
}
li:nth-child(odd) {
clear: left;
}
label { display: block }
<ul>
<li>
<label for="docNo">Doc Number</label>
<input type="text" id="docNo" />
</li>
<li>
<label for="name">Name</label>
<input type="text" id="name" />
</li>
<li>
<label for="street">Street</label>
<input type="text" id="street" />
</li>
<li>
<label for="houseNumber">House Number</label>
<input type="text" id="houseNumber" />
</li>
</ul>
I have a slightly different solution. When you use an HTML <table> element, you don't even need any CSS to format it. It also avoids float which in my experience can cause issues. My code is below or at https://jsfiddle.net/9myL5rbk/.
label {
display: block;
}
<table>
<tr>
<td>
<label for="docNo">Doc Number</label>
<input type="text" id="docNo" />
</td>
<td>
<label for="name">Name</label>
<input type="text" id="name" />
</td>
</tr>
<tr>
<td>
<label for="street">Street</label>
<input type="text" id="street" />
</td>
<td>
<label for="houseNumber">House Number</label>
<input type="text" id="houseNumber" />
</td>
</tr>
</table>
the previous solutions didn't work for me as I had some classes already set in my framework and I got weird resoluts, so I got a ver easy and simple solution!
If you have and ul displayed horizontaly and want to get a br: just add an extra 'empty' li with a 100% width, so it won't be displayed but it will produce a break line!
ul {
list-style-type: none;
}
li {
float: left;
padding-right: 20px;
}
<ul>
<li>your li element </li>
<li>your li element </li>
<li style="width: 100%;"></li>
<li>your li element </li>
<li>your li element </li>
</ul>
At some point I read that one should avoid float whenever possible as it breaks alot of other things you can potentially do with that element.
I think a flex-box based solution might be better.
Note that the container is not needed. I just used it to simulate the list having some finite space on the page.
label {
display: block;
}
ul {
display:flex;
flex-flow: wrap;
}
li {
list-style: none;
margin: 0;
padding: 0;
width: 50%;
}
.container {
width: 25rem;
margin: 1rem auto;
}
<div class="container">
<ul>
<li>
<label for="docNo">Doc Number</label>
<input type="text" id="docNo" />
</li>
<li>
<label for="name">Name</label>
<input type="text" id="name" />
</li>
<li>
<label for="street">Street</label>
<input type="text" id="street" />
</li>
<li>
<label for="houseNumber">House Number</label>
<input type="text" id="houseNumber" />
</li>
</ul>
</div>
I am having some difficulty with the compatibility of my CSS for the HTML check boxes. For some versions of IE as well as when I place the live html into my PowerPoint, the center div will overlap with the left and the entire page is shifted left. I am not quite sure why this occurs.
html,
body {
height: 90%;
width: 90%;
padding: 5px;
margin: auto;
}
#googleMap {
height: 90%;
width: 90%;
padding: 5px;
margin: auto;
}
.Title {
font-size: 1.5em;
font-weight: bold;
text-align: center;
}
#left_check {
position: absolute;
left: 10%;
}
#padded-left {
position: absolute;
padding-left: 1.4em;
}
#right_check {
float: right;
}
#mid_check {
text-align: center;
margin-left: auto;
margin-right: auto;
}
#left_align {
display: inline-block;
text-align: left;
}
<ul style="list-style-type:none">
<div id="left_check">
<li>
<form>
<input id="united_states1" type="checkbox" name="location" value="united_states">United States (domestic)
<br>
<div id="padded-left">
<input id="west1" type="checkbox" class="location" value="west_coast">West
<br>
<input id="east1" type="checkbox" class="location" value="central_us">East
<br>
<input id="cent1" type="checkbox" class="location" value="east_coast">Central
</div>
</form>
</li>
</div>
<div id="right_check">
<li>
<form>
<input id="euro1" type="checkbox" class="location" value="europe">Europe
<br>
<input id="africa1" type="checkbox" class="location" value="africa">Africa
<br>
<input id="asia1" type="checkbox" class="location" value="asia">Asia
<br>
<input id="aust1" type="checkbox" class="location" value="australia">Australia
</form>
</li>
</div>
<div id="mid_check">
<div id="left_align">
<li>
<form>
<input id="canada1" type="checkbox" class="location" value="canada">Canada
<br>
<input id="centr_am1" type="checkbox" class="location" value="central_america">Central America
<br>
<input id="south_am1" type="checkbox" class="location" value="south_america">South America
<br>
<input id="ocean1" type="checkbox" class="location" value="oceanic">Oceanic
</form>
</li>
</div>
</div>
</ul>
Original CSS (Problems with some pc IE)
With Flex
IE Problems (new)
This is how you can do it, if I got your question right.
ul {
display: flex;
padding: 0px !important;
}
ul > div {
/* width: 30%; */
flex-grow: 1;
}
ul > div#right_check {
order: 3;
}
ul > div#mid_check {
order: 2;
}
ul > div#left_check {
order: 1;
}
div#mid_check li {
text-align: center;
}
div#right_check li form,
div#mid_check li form {
text-align: left;
}
div#right_check li {
text-align: right;
}
form {
height: 39px;
width: auto;
display: inline-block;
}
#map-canvas {
height: 500px;
width: 100%;
background: #AFAFAF;
}
div.frame {
width: 80%;
margin: auto;
}
<div class='frame'>
<div id="map-canvas"></div>
<ul style="list-style-type:none">
<div id="left_check">
<li>
<form>
<input id="united_states1" type="checkbox" name="location" value="united_states">United States (domestic)
<br>
<div id="padded-left">
<input id="west1" type="checkbox" class="location" value="west_coast">West
<br>
<input id="east1" type="checkbox" class="location" value="central_us">East
<br>
<input id="cent1" type="checkbox" class="location" value="east_coast">Central
</div>
</form>
</li>
</div>
<div id="right_check">
<li>
<form>
<input id="euro1" type="checkbox" class="location" value="europe">Europe
<br>
<input id="africa1" type="checkbox" class="location" value="africa">Africa
<br>
<input id="asia1" type="checkbox" class="location" value="asia">Asia
<br>
<input id="aust1" type="checkbox" class="location" value="australia">Australia
</form>
</li>
</div>
<div id="mid_check">
<div id="left_align">
<li>
<form>
<input id="canada1" type="checkbox" class="location" value="canada">Canada
<br>
<input id="centr_am1" type="checkbox" class="location" value="central_america">Central America
<br>
<input id="south_am1" type="checkbox" class="location" value="south_america">South America
<br>
<input id="ocean1" type="checkbox" class="location" value="oceanic">Oceanic
</form>
</li>
</div>
</div>
</ul>
</div>
Is this what you trying to get?
I think i might be missing something basic. Have been on it for few hours now and cannot make it work.
http://jsfiddle.net/x4bLtt7b/
<div class="customerInfo">
<form class="form-style">
<ul>
<li>
<label for="field1">field1</label>
<input type="text" name="field1" maxlength="100"> <span>field1 info</span>
</li>
<li>
<label for="field2">field2</label>
<input type="text" name="field2" maxlength="100"> <span>field2 info</span>
</li>
<li>
<label for="field3">field3</label>
<input type="text" name="field3" maxlength="100"> <span>field3 info</span>
</li>
<li>
<label for="field4">field4</label>
<input type="text" name="field4" maxlength="100"> <span>field4 info</span>
</li>
<li>
<label for="field5">field5</label>
<input type="text" name="field5" maxlength="100"> <span>field5 info</span>
</li>
</ul>
</form>
I just need the layout to be two columned, i.e. The field1/field2 pair should be on the same row (adjacent to each other). Same for field3/field4 pair and so on.
It seemed pretty simple to start with but i just couldn't get it to work yet. Any feedback is welcome.
Thanks
Regular CSS method:
Use this method if you need full browser support or else switch to flexbox.
Set a width for the list and display to inline-block
.form-style li {
border: 1px solid #dddddd;
border-radius: 3px;
display: inline-block;
margin-bottom: 30px;
margin-right: 5px;
padding: 9px;
width: 40%;
}
JSFiddle
Flexbox Method:
This is better but supported only by modern browsers.
.form-style ul {
display: flex;
flex-flow: row wrap;
justify-content: space-between;
}
JSFiddle
Output
You can do it with this:
.form-style li {
display: inline-block;
width: 40%;
}
Adjust the width setting want you want.
FIDDLE: https://jsfiddle.net/lmgonzalves/x4bLtt7b/3/