I want to do something like this, but i want the Collapse 2 to stay right under Collapse 1 when I show Content 1. Is that possible? I tried to arrange the code differently, but it just went crazy. :D
.collapse{
cursor: pointer;
display: block;
background: #cdf;
}
.collapse + input{
display: none; /* hide the checkboxes */
}
.collapse + input + div{
display:none;
}
.collapse + input:checked + div{
display:block;
}
<label class="collapse" for="_1">Collapse 1</label>
<input id="_1" type="radio" name="c1">
<div>Content 1</div>
<label class="collapse" for="_2">Collapse 2</label>
<input id="_2" type="radio" name="c1">
<div>Content 2</div>
A little bit tricky but its work!
section {
position: relative;
}
section > .content {
position: absolute;
top:100%;
left: 0;
}
.collapse{
cursor: pointer;
display: block;
background: #cdf;
}
.collapse + input{
display: none; /* hide the checkboxes */
}
.collapse + input + div{
display:none;
}
.collapse + input:checked + div{
display:block;
}
<section>
<label class="collapse" for="_1">Collapse 1</label>
<input id="_1" type="radio" name="c1">
<div class="content">Content 1</div>
<label class="collapse" for="_2">Collapse 2</label>
<input id="_2" type="radio" name="c1">
<div class="content">Content 2</div>
</section>
Fiddle demo
Your code actually works great. As far as making the content show up after the labels, that just means we have to change the HTML order, which means we cannot use the plus(adjacent sibling selector). instead we use general sibling selector (~) you can read more about that here http://www.w3schools.com/css/css_combinators.asp
Here is an example of your code using the tilde. note the content divs now need some kind of unique identifier like a class.
.collapse{
cursor: pointer;
display: block;
background: #cdf;
}
input{
display: none; /* hide the checkboxes */
}
input ~ .content{
display:none;
}
input#_1:checked ~ .content1, input#_2:checked ~ .content2{
display:block;
}
<label class="collapse" for="_1">Collapse 1</label>
<input id="_1" type="radio" name="c1">
<label class="collapse" for="_2">Collapse 2</label>
<input id="_2" type="radio" name="c1">
<div class="content content1">Content 1</div>
<div class="content content2">Content 2</div>
Related
Semantically, my data is structured something like the following:
<div class="inputs">
<div class="top">
<h4>Top</h4>
<label for="top-1">Label 1</label>
<input id="top-1"/>
<label for="top-2">Label 2 is longer than the others</label>
<input id="top-2"/>
</div>
<div class="middle">
<h4>Middle</h4>
<label for="middle-1">Label 3</label>
<select id="middle-1">
<option value="middle-value-1">Value 1</option>
<option value="middle-value-2" selected>This is a longer value</option>
</select>
<label for="middle-2">Label 4</label>
<input id="middle-2"/>
</div>
<div class="bottom">
<h4>Bottom</h4>
<label for="bottom-1">Label 5</label>
<input id="bottom-1"/>
<label for="bottom-2">Label 6</label>
<input id="bottom-2"/>
</div>
</div>
I'd like to display these as distinct but related groups. For aesthetic purposes, I'd like to align and equally size all of the inputs and selects. Is it possible to do this without using explicit widths for everything? I greatly prefer to let things size themselves whenever possible.
Below is an implementation using display: grid with fixed widths for the columns and the grid itself. Is there any way to do this with dynamic sizes?
I'm not stuck on display: grid, by the way. It's just the simplest solution I've come up with so far. I like that it helps keep my HTML simple and semantic. I can use display: table just as well if I nest the labels and inputs in a div with display: table-row. But I still can't let things size dynamically, unless I flatten it by removing the top, middle, and bottom divs.
.inputs > div {
display: grid;
grid-template-columns: 13em 10em;
width: 24em;
gap: 0.5em;
padding: 0.5em;
}
.inputs label {
grid-column: 1;
}
.inputs select, .inputs input {
grid-column: 2;
}
.inputs h4 {
grid-column: 1 / span 2;
margin: 0;
}
.top, .bottom {
background-color: #c0c0c0;
}
.middle {
background-color: #e0e0e0;
}
.middle, .bottom {
margin-top: 0.5em;
}
<div class="inputs">
<div class="top">
<h4>Top</h4>
<label for="top-1">Label 1</label>
<input id="top-1"/>
<label for="top-2">Label 2 is longer than the others</label>
<input id="top-2"/>
</div>
<div class="middle">
<h4>Middle</h4>
<label for="middle-1">Label 3</label>
<select id="middle-1">
<option value="middle-value-1">Value 1</option>
<option value="middle-value-2" selected>This is a longer value</option>
</select>
<label for="middle-2">Label 4</label>
<input id="middle-2"/>
</div>
<div class="bottom">
<h4>Bottom</h4>
<label for="bottom-1">Label 5</label>
<input id="bottom-1"/>
<label for="bottom-2">Label 6</label>
<input id="bottom-2"/>
</div>
</div>
I found a solution with display: table. It's a little uglier than the grid solution and not quite as semantic as I'd like, but it's close, and it does what I want.
.inputs {
display: table;
border-collapse: collapse;
}
.inputs > .top, .inputs > .bottom {
display: table-row-group;
background-color: #c0c0c0;
}
.inputs > .middle {
display: table-row-group;
background-color: #e0e0e0;
}
.input {
display: table-row;
}
.input > * {
display: table-cell;
}
.input > h4, .input > label {
padding: 0.5em 0.25em 0 0.5em;
}
.input > select, .input > input {
margin: 0 0.5em 0.25em 0;
box-sizing: border-box;
width: calc(100% - 0.5em);
}
.input:last-child > * {
margin-bottom: 0.5em;
}
.padding {
padding: 0.5em 0;
}
<div class="inputs">
<div class="top">
<div class="input">
<h4>Top</h4>
<div class="empty"></div>
</div>
<div class="input">
<label for="top-1">Label 1</label>
<input id="top-1"/>
</div>
<div class="input">
<label for="top-2">Label 2 is longer than the others</label>
<input id="top-2"/>
</div>
</div>
<div class="input">
<div class="padding"></div>
</div>
<div class="middle">
<div class="input">
<h4>Middle</h4>
<div class="empty"></div>
</div>
<div class="input">
<label for="middle-1">Label 3</label>
<select id="middle-1">
<option value="middle-value-1">Value 1</option>
<option value="middle-value-2" selected="selected">This is a longer value</option>
</select>
</div>
<div class="input">
<label for="middle-2">Label 4</label>
<input id="middle-2"/>
</div>
</div>
<div class="input">
<div class="padding"></div>
</div>
<div class="bottom">
<div class="input">
<h4>Bottom</h4>
<div class="empty"></div>
</div>
<div class="input">
<label for="bottom-1">Label 5</label>
<input id="bottom-1"/>
</div>
<div class="input">
<label for="bottom-2">Label 6</label>
<input id="bottom-2"/>
</div>
</div>
</div>
You can set your grid up more dynamically with something like this:
.inputs {
width: 33%;
min-width: 25em;
}
.inputs > div {
display: grid;
grid-template-columns: 1.3fr 1fr;
gap: 0.5em;
padding: 0.5em;
}
I used your outer container to deal with the width as it relates to the screen. The hard min-width of 25px keeps the long label from wrapping to another line, instead showing a horizontal scrollbar if the screen size causes 33% of the width to go below 25px.
For your inner containers, you can change the unit in grid-template-columns to fr, which means fraction of available space. This will let your columns dynamically size along with your window (until you hit the min-width for the outer container). You can experiment with the width percentage to work out just how much dynamic sizing you would like to do.
This is a good writeup of fr if you would like one.
I have image display that shows individual images when a radio button is clicked. however the image associated with the radio button shifts vertically down in it position. in the example below is 3 images with 3 associated radio button.I tried setting line-height to 0 and also tried white-space:nowrap in css. but didn't work. any help is appreciated.
<style>
#imgflow {
width: 60%;
overflow: hidden;
text-align:center;
margin:0 auto;
}
#imf_holder article img {
width: 100%;
}
#imf_holder .inner {
width: 100%;
line-height: 0;
}
#imf_holder article {
width: 100%;
}
.img-control {
display: block;
text-align: center;
}
input[type="radio"] ~.ps{
display:none;
}
input[type="radio"]:checked ~.ps{
display:block;
}
input[type=radio] {
opacity: 0;
}
</style>
<html>
<div id="imf_holder">
<div id="imgflow">
<div class="inner">
<article>
<input type="radio" checked value="Img1" name="event" id="img-1" ><div class="ps"> <img src="http://www.gettyimages.pt/gi-resources/images/Homepage/Hero/PT/PT_hero_42_153645159.jpg" height="200px" ></div>
</article>
<article>
<input type="radio" value="Img1" name="event" id="img-2" ><div class="ps"> <img src="http://www.hdwallpapery.com/static/images/Winter-Tiger-Wild-Cat-Images_vwV1tOp.jpg" height="200px" ></div>
</article>
<article>
<input type="radio" value="Img1" name="event" id="img-3" ><div class="ps"> <img src="http://eskipaper.com/images/images-4.jpg" height="200px" ></div>
</article>
</div>
</div>
</div>
<div style="margin-left:20px;margin-right:20px;margin-top:15px;">
<li class="img-control">
<label for="img-1" >button1 </label>
<label for="img-2" >button2 </label>
<label for="img-3" >button3 </label>
</li>
</div>
Set your input to display: none;, as when use opacity they are invisible but still occupy space, hence pushing the images down.
input[type=radio] {
display: none;
}
Sample snippet
#imgflow {
width: 60%;
overflow: hidden;
text-align:center;
margin:0 auto;
}
#imf_holder article img {
width: 100%;
}
#imf_holder .inner {
width: 100%;
}
#imf_holder article {
width: 100%;
}
.img-control {
display: block;
text-align: center;
}
input[type="radio"] ~ .ps{
display:none;
}
input[type="radio"]:checked ~ .ps{
display:block;
}
input[type=radio] {
display: none;
}
<div id="imf_holder">
<div id="imgflow">
<div class="inner">
<article>
<input type="radio" checked value="Img1" name="event" id="img-1" ><div class="ps"> <img src="http://www.gettyimages.pt/gi-resources/images/Homepage/Hero/PT/PT_hero_42_153645159.jpg" height="200px" ></div>
</article>
<article>
<input type="radio" value="Img1" name="event" id="img-2" ><div class="ps"> <img src="http://www.hdwallpapery.com/static/images/Winter-Tiger-Wild-Cat-Images_vwV1tOp.jpg" height="200px" ></div>
</article>
<article>
<input type="radio" value="Img1" name="event" id="img-3" ><div class="ps"> <img src="http://eskipaper.com/images/images-4.jpg" height="200px" ></div>
</article>
</div>
</div>
</div>
<div style="margin-left:20px;margin-right:20px;margin-top:15px;">
<li class="img-control">
<label for="img-1" >button1 </label>
<label for="img-2" >button2 </label>
<label for="img-3" >button3 </label>
</li>
</div>
Just make your inputs absolute like this:
input[type=radio] {
opacity: 0;
position:absolute;
}
Trying to create a layout that has two columns, and text between those columns
Something like this:
But I am running into spacing issues with twitter bootstrap to make it actually work. On top of making these items the same width with the text between, they should all be vertically aligned.
You can do that using 3 columns
Live Demo jsfiddle
<div class="container-fluid">
<div class="row">
<div class="col-xs-6">.col-sm-6</div>
<div class="col-xs-6">.col-sm-6</div>
</div>
<br/>
<p>Create an account...............................</p>
<div class="row">
<div class="col-xs-6 test" >
<form class="form-horizontal vmid" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="Enter email"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Enter password"/>
</div>
</div></form>
</div>
<div class="col-xs-1 test" >
<p class="asd"> ~OR~</p>
</div>
<div class="col-xs-5 test" >
<button type="submit" class="asd btn-primary btn-lg">Facebook</button>
</div>
</div>
</div>
Style
.vmid{
position:relative;
top:50%;
transform:translateY(-50%);
}
.asd{
position:relative;
top:50%;
transform:translateY(-35%);
}
This is not a bootstrap answer just a plain simple CSS one. Although you can adapt it to bootstrap easily because the basic underlying principle is the same. Instead of using width percentages that I have used in my example, bootstrap grid system columns can be used instead. Saying all that, you can achieve your desired effect by dividing the wrapper div into 3 columns and then using the display table for parent and table-cell and vertical align middle for the child to place the respective input elements and button elements in its place as needed.
The fiddle can be found here
The code snippet follows...
.wrapper {
height: 300px;
width: 100%;
background: pink;
}
.leftSide,
.rightSide,
.midPart {
box-sizing: border-box;
height: 100%;
display: table;
}
.leftSide {
width: 45%;
background: lightgray;
float: left;
}
.midPart {
width: 10%;
background: aqua;
}
.midPart p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.leftSide div,
.rightSide div {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.rightSide {
width: 45%;
background: lightcyan;
float: right;
}
button {
height: 3em;
width: 100px;
background: blue;
color: white;
}
<div class="wrapper">
<div class="leftSide">
<div>
<input placeholder="Enter Username" />
<br/>
<br/>
<input placeholder="Enter password" />
</div>
</div>
<div class="rightSide">
<div>
<button>Hello</button>
</div>
</div>
<div class="midPart">
<p>-or-</p>
</div>
</div>
Hope this helps. Happy coding :)
Update::
***Another updated Fiddle without colors***
I'm trying to centre a horizontal form in a hero unit, by setting text-align:center; and then display:block-inline;
See jsfiddle for a demo http://jsfiddle.net/bVJZ2/
But this hasn't quite worked in that the checkbox and submit button are not correctly aligned any more. Any suggestions how to fix this?
There are some problems that doesn't allow you to center the checkbox.
<div class="controls"> has a margin-left. You shouldn't put your checkbox inside a that div.
The input[checkbox] has a float: left. You should remove that float with:
.radio input[type="radio"], .checkbox input[type="checkbox"] {
float: none;
}
The checkbox is bad aligned. You should add vertical-align: top to the last selector.
To sum up:
HTML:
<div class="control-group">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn">Sign in</button>
</div>
CSS:
.radio input[type="radio"], .checkbox input[type="checkbox"] {
float: none;
vertical-align: top;
}
You can see the result here: http://jsfiddle.net/GW8zk/
Well This is how you can implement a Hero-unit with form centered in middle .
JSFiddle with centered form http://jsfiddle.net/shail/YmmVS/
First the css :
.hero-unit {
padding:50px 50px 50px 50px;
}
.form-horizontal .control-label {
width: 61px;
}
.form-horizontal .controls {
margin-left: 80px;
}
/* Landscape phones and down */
#media (max-width: 480px) {
.hero-unit{
margin-left:-20px;
margin-right:-20px;
}
.form-horizontal .controls {
margin-left: 0;
}
}
The Html Part :
<div class="container">
<div class="hero-unit">
<div class="row-fluid">
<div class="offset4 span4">
<legend>Sign in to WebApp</legend>
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input type="text" id="inputEmail" placeholder="Email">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox">Remember me</label>
<button type="submit" class="btn">Sign in</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
My site has a header with a ul (with links) for the navigation bar.
One of my pages has a ul for an image gallery. The template for that is from this website: http://www.thecssninja.com/demo/futurebox/v3/
The image gallery does what I want it to: opens an enlarged picture over the page when clicked (overlay), the enlarged picture is centered, and closes when clicking anywhere on the screen. The problem is that the ul messes up my navigation bar. How do I fix that? The nav bar already has a ul class.
Here's the relevant CSS:
ul li{
display: inline-block;
float: left;
list-style: none;
margin:0.3em;
border:0.1em solid black;
-webkit-border-radius:0.25em;
-moz-border-radius:0.25em;
border-radius:0.25em;
}
ul li:hover { border:0.1em solid red; }
ul li img, ul li label {
display: block;
cursor: pointer;
}
ul li input { display: none; }
ul li input:checked + .overlay { display: table; }
.overlay{
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
display: none;
z-index: 999;
background: rgb(0,0,0);
background: rgba(0,0,0,0.7);}
.overlay label
{
display: table-cell;
vertical-align: middle;
text-align: center;
}
.overlay img,
.overlay iframe
{
display: inline;
border:0.25em solid white;
-webkit-border-radius:0.25em;
-moz-border-radius:0.25em;
border-radius:0.25em;
}
Here's the HTML:
<div class="text2col">
<h2>Photo gallery</h2>
<ul>
<li>
<label for="futurebox01"><img src="http://www.thecssninja.com/demo/futurebox/gr_ninja-attack_med.gif" width="100" height="102" alt="The CSS Ninja" /></label>
<input type="radio" id="futurebox01" name="gallery" />
<div class="overlay">
<label for="close" title="Close futurebox"><img src="http://www.thecssninja.com/demo/futurebox/gr_cssninja_lrg.png" alt="The Css Ninja" width="469" height="500" /></label>
</div>
</li>
<li>
<label for="futurebox02"><img src="http://www.thecssninja.com/demo/futurebox/gr_ninja-attack_med.gif" width="100" height="102" alt="The CSS Ninja" /></label>
<input type="radio" id="futurebox02" name="gallery" />
<div class="overlay">
<label for="close" title="Close futurebox"><img src="http://www.thecssninja.com/demo/futurebox/gr_cssninja_lrg2.png" alt="The Css Ninja" width="469" height="500" /></label>
</div>
</li>
<li>
<label for="futurebox03"><img src="http://www.thecssninja.com/demo/futurebox/gr_ninja-attack_med.gif" width="100" height="102" alt="The CSS Ninja" /></label>
<input type="radio" id="futurebox03" name="gallery" />
<div class="overlay">
<label for="close" title="Close futurebox"><img src="http://www.thecssninja.com/demo/futurebox/gr_cssninja_lrg3.png" alt="The Css Ninja" width="469" height="500" /></label>
</div>
</li>
<li>
<label for="futurebox04"><img src="http://www.thecssninja.com/demo/futurebox/gr_ninja-attack_med.gif" width="100" height="102" alt="The CSS Ninja" /></label>
<input type="radio" id="futurebox04" name="gallery" />
<div class="overlay">
<label for="close" title="Close futurebox"><img src="http://www.thecssninja.com/demo/futurebox/gr_cssninja_lrg4.png" alt="The Css Ninja" width="469" height="500" /></label>
</div>
</li>
... (more li's)...
</ul>
<div class="clear"> </div>
<input type="radio" id="close" name="gallery" />
I also tried to add a ul class for the image gallery, by changing all the ul to ul.futurebox and adding that into the class into the webpage, and that made the nav bar go back to normal BUT then it no longer centers the overlay image (the overlay is there, but the image goes to the top, left of the page).
The problem (I think) is that there's an adjacent sibling connector with the overlay: ul li input:checked + .overlay { display: table; } So after adding a class for the image gallery. Or... it might be another problem entirely, but I'm not sure.
I haven't found an answer, but this demo does everything I want it to do: http://www.thecssninja.com/demo/futurebox/
It doesn't have the adjacent sibling connector, so I used this as a template, and put in a ul class called "futurebox" for the image gallery, so that my nav bar wouldn't be affected.
Except the problem with this one is that it goes through all the images you clicked through with the "back" button, whereas the other one didn't.