I try to align two kinds of columns, which are interleaved by random content, but without specifying the width.
label::after {
content: ':';
margin-right: 1ch;
}
label.s {
background-color: #AAF;
}
label.l {
background-color: #AFA;
}
<article>
<div>lore ipsum</div>
<form>
<label class="s">M</label>short
</form>
<div>lore ipsum</div>
<form>
<label class="s">MMM</label>short
</form>
<div>lore ipsum</div>
<form>
<label class="l">MMMMMMMMMM M</label>long
</form>
<div>lore ipsum</div>
<form>
<label class="s">MMMMM</label>short
</form>
<div>lore ipsum</div>
<form>
<label class="l">MMMMMMMMMM MMM</label>long
</form>
<div>lore ipsum</div>
<form>
<label class="l">MMMMMMMMMM MMMMM</label>long
</form>
<div>lore ipsum</div>
</article>
My aim is to vertically align the "short" strings in one column and the "long" strings in another column.
Is this possible just with CSS?
Related
When the content is wide enough to have this all on the same row without line breaking (probably need to go full page with the snippet), the right label text is aligned slightly higher than the left label text. I'm trying to get the text of the two labels aligned to the same baseline.
I've tried to use vertical align in various ways, tried to set the height the same as the left on the right group, but I can't get anything to work.
Of course I could just set a fixed margin, but is there a responsive way to do this?
.main {
margin-top: 20px;
}
#left-input {
width: 100px;
}
.vmiddle {
/* does nothing ? */
vertical-align: middle;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container main">
<div class="row">
<div class="col-xs-6 ">
<div class="form-inline">
<div class="form-group">
<label>Left Label:</label>
<input id="left-input" type="text" class="form-control" value="Some Value" />
</div>
</div>
</div>
<div class="col-xs-6 vmiddle">
<div class="form-inline">
<div class="form-group">
<label>Right Label: Higher</label>
</div>
</div>
</div>
</div>
</div>
Since the second one is technically not a label, you can use Bootstrap's Static form control instead. It's designed to align with form-control and .btn elements.
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container main">
<div class="row">
<div class="col-xs-6 ">
<div class="form-inline">
<div class="form-group">
<label>Left Label:</label>
<input id="left-input" type="text" class="form-control" value="Some Value" />
</div>
</div>
</div>
<div class="col-xs-6 vmiddle">
<div class="form-inline">
<div class="form-group">
<p class="form-control-static"><strong>Right Label</strong></p>
</div>
</div>
</div>
</div>
</div>
Add a margin utility class that's half the height of Bootstrap's padding.
<style>
.m-t-7 {
margin-top: 7.5px;
}
</style>
<div class="col-xs-6">
<div class="form-inline">
<div class="form-group">
<label class="m-t-7">Right Label: Higher</label>
</div>
</div>
</div>
Yup.
First, give the right label an id so you can select it individually.
<label id="right">Right Label: Higher</label>
then, in your css add:
#right{
padding-top:2px; /*Insert your own value for paddin*/
}
and to make it dynamic, remove the padding when the left label stacks. Find the pixel width of the browser when this happens, and then create a media query for the event:
#media(max-width:300px){
#right{
padding-top:0;
}
}
Another idea which may not be possible in your layout, would be to limit the width of the content so it never gets wide enough for this.
Yes. Just try to remove the margin bottom of the <label>Right Label: Higher</label>. Please leave a comment if this wont work.
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container main">
<div class="row">
<div class="col-xs-6 ">
<div class="form-inline">
<div class="form-group">
<label>Left Label:</label>
<input id="left-input" type="text" class="form-control" value="Some Value" />
</div>
</div>
</div>
<div class="col-xs-6 vmiddle">
<div class="form-inline">
<div class="form-group">
<label style="margin-bottom: 0;">Right Label: Higher</label>
</div>
</div>
</div>
</div>
</div>
I have set my default font size to be based on 10px via 62.5%, and then set the body to be 1.4rem. This seems to cause issues with the custom checkbox controls and they no longer align correctly.
I'm using Bootstrap 4's SASS files combined with my own. Is there something in the variables file that I'm missing to fix this? Or maybe some overrides I need to do? I've tried messing with various padding, margins, font sizes, etc. but the way they have this working isn't obvious.
Here is what I have set for the default font sizes for the site:
html {
font-size: 62.5%;
}
body {
font-size: 1.4rem;
}
And here is the HTML
<div class="container-fluid">
<div class="row align-items-end">
<div class="col">
<div class="form-group">
<label for="worker">Worker</label>
<select class="form-control" id="worker">
<option>Jane Doe</option>
<option>John Smith</option>
</select>
</div>
</div>
<div class="col">
<div class="form-group">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Check this custom checkbox</span>
</label>
</div>
</div>
</div>
</div>
JSFiddle
To sum up, basically I need the ability to change the body font size and yet still have the checkbox to still work correctly and be the same height as the input fields when they are next to each other.
You can make the parent element flex and use align-items to center vertically. You would also need to use non-absolute position on the checkbox element. That will keep them centered vertically regardless the font size of the page.
html {
font-size: 62.5%;
}
body {
font-size: 1.4rem;
}
.custom-control {
display: flex;
align-items: baseline;
}
.custom-control-indicator {
position: static;
margin: 0 .5rem 0 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row align-items-end">
<div class="col">
<div class="form-group">
<label for="worker">Worker</label>
<select class="form-control" id="worker">
<option>Jane Doe</option>
<option>John Smith</option>
</select>
</div>
</div>
<div class="col">
<div class="form-group">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Check this custom checkbox</span>
</label>
</div>
</div>
</div>
</div>
You could also use top: 50%; transform: translateY(-50%); to center the checkbox vertically, then set the line-height of the text to 1 so it doesn't affect vertical alignment.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<style>
html {
font-size: 62.5%;
}
body {
font-size: 1.4rem;
}
.custom-control-indicator {
top: 50%;
transform: translateY(-50%);
}
.custom-control-description {
line-height: 1;
}
</style>
<div class="container-fluid">
<div class="row align-items-end">
<div class="col">
<div class="form-group">
<label for="worker">Worker</label>
<select class="form-control" id="worker">
<option>Jane Doe</option>
<option>John Smith</option>
</select>
</div>
</div>
<div class="col">
<div class="form-group">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Check this custom checkbox</span>
</label>
</div>
</div>
</div>
</div>
A simple solution would be to change the styles for the checkbox:
.custom-control-indicator {
height: 1.4rem;
width: 1.4rem;
top: calc(1.4rem * 0.25);
}
Change the 1.4rem as required to the same size as the body font-size (you can also set this up to work automatically using SASS variables
I have a 4-column horizontal form in Bootstrap 3 (latest version):
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>Title is here</h1>
</div>
</div>
<div class="row">
<div class="col-xs-12 no-gutter">
<form class="form-horizontal">
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Title">
</div>
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Title">
</div>
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Email Address">
</div>
<div class="col-md-3">
<button type="submit" class="form-control input-lg btn-danger">Check The Price</button>
</div>
</form>
</div>
</div>
And the following CSS (to make the input fields next to each other):
.col-xs-12.no-gutter [class*='col-'] {
padding-right: 0;
padding-left: 0;
}
Here's the issue. I want the button to be smaller (for it to take 2 columns instead of 3). So I changed the last <div> from col-md-3 to col-md-2 and got this:
http://www.bootply.com/JiNeG04Ifl
The code is the same like the pasted code above, just with the last <div> changed to col-md-2. Now, try to resize the viewport from 1194px to 992px (the medium size after the forms are switching to vertical). You will notice that the text overflows the box, although there is more than enough white space in the button itself.
How can I fix this? So far I've tried:
white-space: nowrap;
But the problem with this is that the text does not stay centered in the box, its alignment seems to be going off. Is there some better solution to keep the alignment centered?
Remove the l/r padding from the button...
.input-lg.btn-danger {
padding-left: 0;
padding-right: 0;
}
http://www.bootply.com/QkBiP8yHNE
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
/* CSS used here will be applied after bootstrap.css */
.margin-bottom-30 {
margin-bottom: 30px;
}
.margin-top-30 {
margin-top: 30px;
}
.col-xs-12.no-gutter [class*='col-'] {
padding-right: 0;
padding-left: 0;
}
* {
border-radius: 0 !important;
}
.input-lg.btn-danger {
padding-left: 0;
padding-right: 0;
}
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 class="margin-bottom-30">Title Here</h1>
</div>
</div>
<div class="row no-gutter">
<div class="col-xs-12 form no-gutter">
<form class="form-horizontal">
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Title">
</div>
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Title">
</div>
<div class="col-md-3">
<input type="text" class="form-control input-lg" placeholder="Your Email Address">
</div>
<div class="col-md-2">
<button type="submit" class="form-control input-lg btn-danger">Check The Price</button>
</div>
</form>
</div>
</div>
</div>
<div id="push"></div>
https://jsfiddle.net/pqrbm0o0/
Currently, name/phone #/message/email is on the left side of the box
how can i align it on the top left (not the top center)?
<div class="col-sm-6">
<h4><strong>Your Contact Information</strong></h4>
<span style="color:#cc0000; margin-left:25px">• </span>Required fields.<br/>
<br/>
<div class="contact">
<div class="block">
<label>Name:</label>
<input type="Name" style="width:250px;" />
<span style="color:#cc0000;">• </span> </div>
<div class="block">
<label>Phone Number:</label>
<input type="phone" style="width:250px;"/>
<span style="color:#cc0000;">• </span> </div>
<div class="block">
<label>Email:</label>
<input type="email" style="width:250px;"/>
<span style="color:#cc0000;">• </span> </div>
<div class="block">
<label>Message:</label>
<textarea name="Message" id="Message" style="width:350px;"></textarea>
<span style="color:#cc0000;">• </span>
</div>
</div>
In your CSS, apply the following styles:
.block label { display: block;
text-align: left;
}
This will allow it to be on its own line rather than inline and setting text align left should be fairly straightforward.
In case you don't know the difference between inline-block and block:
https://css-tricks.com/almanac/properties/d/display/
I want to achieve a form using css which have different panels for different groups of input fields.
Similar to this.
![alt text][1]
I know this is a desktop client. But is there any way to achieve such a layout with fieldsets css ?
You can use float:
<form>
<div id="col1">
<fieldset id="fldst1"></fieldset>
<fieldset id="fldst2"></fieldset>
<fieldset id="fldst3"></fieldset>
</div>
<div id="col2">
<fieldset id="fldst4"></fieldset>
<fieldset id="fldst5"></fieldset>
</div>
<div id="col3">
<fieldset id="fldst6"></fieldset>
<fieldset id="fldst7"></fieldset>
<fieldset id="fldst8"></fieldset>
</div>
<div style="clear:both;" />
</form>
In your CSS:
#col1, #col2, #col3 {
float: left;
width: 300px;
}
Then you place the different form elements in your fieldsets.
I'm not quite sure what part of the layout exactly is the problem. You obviously know about fieldset and you layout them exactly the same way you would layout any other HTML element (such as div).
I'm guessing it the columns. You have two possibilities here. A) Either put a number of fieldsets into a div with the width (100% / number of columns) and float those divs, or B) if you only need to support modern browsers use CSS 3 column properties and the browser will calculate the number of fieldsets per column for you automatically:
A)
#columns .column {
float: left;
width: 33%;
}
<div id="columns">
<div class="column">
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
</div>
<div class="column">
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
</div>
<div class="column">
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
</div>
</div>
B)
#columns {
column-count: 3
/* Just covering all bases here. Not sure which browsers actually support this */
-webkit-column-count: 3
-moz-column-count: 3
-o-column-count: 3
-ms-column-count: 3
}
<div id="columns">
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
<fieldset>...</fieldset>
</div>
CSS:
<style type="text/css">
fieldset {
width: 33%;
float: left;
}
</style>
HTML:
<fieldset>
<legend>Anschrift</legend>
<label for="nachname">Nachname:</label>
<input id="nachname" name="nachname" type="text">
</fieldset>
<fieldset>
<legend>Firmenanschrift</legend>
<label for="firma">Firma:</label>
<input id="firma" name="firma" type="text">
</fieldset>
<fieldset>
<legend>Einkommen</legend>
<label for="jaresgehalt">Jaresgehalt:</label>
<input id="jaresgehalt" name="jaresgehalt" type="text">
</fieldset>