Set width to 100 percent of text plus padding - html

I am having trouble getting the code below to style correctly. What I need is to have the labels be 100% width of the text inside (no word wrap) plus a fixed padding on the left and right sides. I can set a manual width using pixels but then all buttons are exactly the same width, which is not what I want. I have searched for the answer and have come up empty handed. All the solutions I tried did not work for me.
Here is a codepen: https://codepen.io/anon/pen/KzYEbv
HTML:
<div id="toolbar-upper">
<div class="sort-by-container">
<span>SORT BY </span>
<ul class="sort-by-radios">
<li>
<input type="radio" id="newest" name="sortby" value="" onchange="setLocation(this.value)">
<label for="newest">Newest Arrivals</label>
</li>
<li>
<input type="radio" id="name" name="sortby" value="" onchange="setLocation(this.value)">
<label for="name">Name</label>
</li>
<li>
<input type="radio" id="price-high" name="sortby" value="" checked="checked" onchange="setLocation(this.value)">
<label for="price-high">Price (High)</label>
</li>
<li>
<input type="radio" id="price-low" name="sortby" value="" checked="checked" onchange="setLocation(this.value)">
<label for="price-low">Price (Low)</label>
</li>
</ul>
</div>
</div>
CSS:
#toolbar-upper .sort-by-radios {
list-style-type: none;
margin: 0;
margin-left: 20px;
vertical-align: middle;
display: inline-block;
padding: 0;
}
#toolbar-upper .sort-by-radios li {
float: left;
margin: 0 15px 0 0;
height: 30px;
position: relative;
width: 130px;
}
#toolbar-upper .sort-by-radios label, #toolbar-upper .sort-by-radios input {
display: block;
position: absolute;
top: 0;
left:0;
right:0;
bottom:0;
}
#toolbar-upper .sort-by-radios input[type="radio"] {
opacity: 0.01;
z-index: 100;
}
#toolbar-upper .sort-by-radios input[type="radio"]:checked + label {
background: #3c8dc5;
color: white;
}
#toolbar-upper .sort-by-radios label {
padding: 0 30px 0 30px;
border: 1px solid #4C4C4C;
cursor: pointer;
display: inline-block;
z-index: 90;
font-weight: 600;
text-align: center;
}
#toolbar-upper .sort-by-radios label:hover {
background:#3c8dc5;
color: white;
}
#toolbar-upper .sort-by-container span {
font-size: 1rem;
display: inline-block;
vertical-align: middle;
font-weight: 600;
}

https://codepen.io/anon/pen/JXVzVx
Here's what you need. The problem was position:absolute; on the label items. I removed it, and added white-space:nowrap; to the label text, and modified the padding a little bit.
If you want to maintain position:absolute; on the rest of the elements:
https://codepen.io/anon/pen/NNmJQj
ProTip: you can go padding: 5px 30px; and it means the same as padding:5px 30px 5px 30px;

Related

How do I center text in this scenario?

I want to center the label vertically within the <li>, and add some padding to the label. How can I do this? Sorry if this is a dumb question, this is for a school project due tommorow.
TBH this is mostly stolen from this JSFiddle.
In context. In JSFiddle
:root{
--cream: #ECD9BA;
--wood: #824936;
--slate: #222C2A;
--white: #FFF;
}
*{
margin: 0;
padding: 0;
box-sizing: border-box;
background-clip: border-box;
}
.q-wrap{
display: flex;
justify-content: center;
}
.question {
width: 80%;
display: flex;
flex-direction: column;
list-style-type: none;
}
.question li {
width: 100%;
min-height: 5rem;
margin: .5rem 0;
padding: 2rem 0;
position: relative;
}
.question label,
.question input {
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.question input[type="radio"] {
opacity: 0;
}
.question input[type="radio"]:checked+label {
background-color: #E62222;
}
.question input[type="radio"].correct:checked+label {
background-color: #559905;
}
.question label {
font-size: 2rem;
font-weight: 900;
padding: 5px;
background-color: var(--wood);
color: var(--cream);
border: 2px solid var(--cream);
border-radius: 1rem;
cursor: pointer;
z-index: 90;
}
.question label:hover {
filter: brightness(110%)
}
.question input[type="radio"]:checked+label:hover{
filter: brightness(105%);
}
<div class="q-wrap">
<ul class="question">
<li>
<input type="radio" id="b2b" name="amount" />
<label for="b2b">B2B</label>
</li>
<li>
<input type="radio" id="p2p" class="correct" name="amount"/>
<label for="p2p">P2P</label>
</li>
<li>
<input type="radio" id="b2a" name="amount" />
<label for="b2a">B2A</label>
</li>
<li>
<input type="radio" id="c2c" name="amount" />
<label for="c2c">C2C</label>
</li>
</ul>
</div>
I tried adding padding-left: 2rem to .question label, but it doesn't work
Also, how do I make code snippets in Stack Overflow? I don't see the button for it, even though there are screenshots with it there.

How to instigate action when input is checked in SCSS?

Its very simply and I have looked at all these examples but still am not able to figure out what I am doing wrong!
I have created a custom checkbox, increased the size and style, and when it is clicked I want the letter "A" to appear in the box, but it simply will not respond, maybe a second pair of eyes will help me identify the problem.
below is my html and css:
.container {
position: relative;
cursor: pointer;
display: block;
input,
label {
display: inline-block;
}
input {
// opacity: 0;
position: absolute;
height: unset;
width: unset;
cursor: pointer;
}
label::before {
border: 1px solid #333;
content: "";
display: inline-block;
font: 16px/1em sans-serif;
height: 50px;
margin: 0 .25em 0 0;
padding: 0;
vertical-align: top;
width: 50px;
border-radius: 5px;
color: red;
font-size: 50px;
padding: 10px;
}
input[type="checkbox"]:checked + label::before {
content: "A"; //code for checked
}
}
<div class="container">
<div>
<label for="form_agreeTerms" class="required">Agree terms</label>
<input type="checkbox" id="form_agreeTerms" name="form[agreeTerms]" required="required" value="1">
</div>
</div>
Here it is in codepen
You using the + operator, which means "The next sibling element".
You must move the label to be after the checkbox.
<div class="container">
<div>
<input type="checkbox" id="form_agreeTerms" name="form[agreeTerms]" required="required" value="1">
<label for="form_agreeTerms" class="required">Agree terms</label>
</div>
</div>

CSS Radio-button and text outline

I'm having a problem with setting up box & shadow around my radio button. My CSS sets box only around radio button and shows nasty white square box around it. How to set border or outline around whole Radio-button + text to make selection more distinctive.
enrgy-form {
width: 50%;
float: right;
}
.label-width {
margin-left: 22px;
white-space: nowrap;
}
.label-nowrapp {
white-space: nowrap;
}
.selected-item input:checked {
/*border: 1px solid dodgerblue;*/
box-shadow: 3px 3px 11px 1px dodgerblue;
}
<div class="form-check enrgy-form">
<label class="form-check-label label-nowrapp selected-item">
<input class="form-check-input selected-item" type="radio" name="energy" formControlName="energy" value="Energy" (change)="setOptions()">Fuel-fired</label>
</div>
I think your best bet is to simulate the radio button with css so you can have the behavior you want.
You should first set the input to display: none and give it an id in your HTML so you can link it with the label, by giving the label a for attribute, this way you can control the check/uncheck of your radio button from the label.
Next you want to simulate the appearance of the radio button, i'll do this by adding two spans, one inside the other, so we can have a checked/unchecked status.
try this:
enrgy-form {
width: 50%;
float: right;
}
.label-width {
margin-left: 22px;
white-space: nowrap;
}
.label-nowrapp {
white-space: nowrap;
}
.selected-item {
display: none;
}
.selected-item:checked + label {
box-shadow: 0px 0px 11px 2px dodgerblue;
}
label{
padding: 3px;
}
label .bullet{
border-radius: 50%;
border: 1px solid gray;
background-color: lightgray;
margin-right: 3px;
display: inline-block;
width: 10px;
height: 10px;
position: relative;
}
.selected-item:checked + label .bullet .bullet-selected{
position: absolute;
top: 50%;
left: 50%;
border-radius: 50%;
transform: translate(-50%, -50%);
display: inline-block;
width: 5px;
height: 5px;
background-color: gray;
}
<div class="form-check enrgy-form">
<input class="form-check-input selected-item" type="radio" name="energy" formControlName="energy" value="Energy" (change)="setOptions()" id="someUniqueId"/>
<label class="form-check-label label-nowrapp" for="someUniqueId">
<span class="bullet">
<span class="bullet-selected"></span>
</span>
Fuel-fired
</label>
</div>
You could go the route where you style the whole radio button using :before and :after in CSS. That way you could even go nuts with animations and stuff...
It would require you to change the HTML a bit as well....
There's plenty of examples to be found if you search for "css custom radio".
[type="radio"]{
position: absolute;
left: -9999px;
}
[type="radio"] + label
{
position: relative;
padding: 0 20px;
cursor: pointer;
}
[type="radio"] + label:before{
content: '';
position: absolute;
left: 0;
top: 0;
width: 14px;
height: 14px;
border: 1px solid #ddd;
border-radius: 100%;
background: #fff;
}
[type="radio"]:checked + label:before{
box-shadow: 0px 1px 11px 1px dodgerblue;
}
[type="radio"] + label:after{
content: '';
display: none;
width: 10px;
height: 10px;
background: gray;
position: absolute;
top: 3px;
left: 3px;
border-radius: 100%;
}
[type="radio"]:checked + label:after {
display: block;
}
<div class="form-check enrgy-form">
<input type="radio" name="energy" id="one">
<label for="one">Fuel-fired</label>
</input>
<input type="radio" name="energy" id="two">
<label for="two">Something else</label>
</input>
</div>
Update
Here is a possible solution, you could modify it as you want!
.form-check {
position: relative;
display: inline-flex;
align-items: center;
font-size: 1rem;
}
.form-check-label {
font-size: 0.9em;
margin-right: 0.25em;
white-space: nowrap;
}
.form-check-input {
margin: 0;
margin-right: 0.5em;
}
.form-check-input:checked + .form-check-label:after {
content: '';
width: 100%;
height: 100%;
top: 0;
left: 0;
position: absolute;
border-radius: 1.5em 8px 8px 1.5em;
box-shadow: 3px 3px 11px 1px dodgerblue;
}
.medium { font-size: 2rem; }
.medium input[type=radio] { zoom: 2 }
.big { font-size: 3rem; }
.big input[type=radio] { zoom: 3 }
<div class="form-check">
<input id="inputcheck" class="form-check-input" type="radio" name="energy" formControlName="energy" value="Energy">
<label for="inputcheck" class="form-check-label">Fuel-fired normal</label>
</div>
<br><br>
<div class="form-check medium">
<input id="inputcheck1" class="form-check-input" type="radio" name="energy" formControlName="energy" value="Energy">
<label for="inputcheck1" class="form-check-label">Fuel-fired medium</label>
</div>
<br><br>
<div class="form-check big">
<input id="inputcheck2" class="form-check-input" type="radio" name="energy" formControlName="energy" value="Energy">
<label for="inputcheck2" class="form-check-label">Fuel-fired big</label>
</div>

Tabbed Nav Not showing Content

Can someone help me, I'm at my wits end. I've created a CSS3 tabbed nav set up. When the tab is clicked it should be showing the base content -- at this point it's only one line. I'm missing something and I just can't see it. I'm not looking to implement JS or Jquery this should work but it's not.
Here's the code:
.tabs
{
position: relative;
display: block;
height: 40px;
width: 600px;
margin: 75px auto;
list-style: none;
float: none;
}
.tabs input[type="radio"]
{
display: none;
}
.tabs label
{
float: left;
display: block;
position: relative;
height: 40px;
width: 160px;
display:block;
padding: 10px 20px;
font-weight: normal;
font-family: 'Lucida Sans';
font-size: 17px;
background: #f2f2f2;
line-height: 20px;
text-align: center;
cursor: pointer;
color: #bfbfbf;
box-shadow: 0.5px -2px 2px rgba(0,0,0,0.1), 0.5px -1px 2px rgba(0,0,0,0.1);
transition: all 0.1s ease-in-out;
top: 2px;
}
.tabs label:hover
{
background: rgba(255,255,255,0.5);
top:0;
}
.tab-content
{
position: absolute;
top: 100px;
left: 0;
display: none;
}
[id^=tab]:checked+label
{
background: rgba(255,255,255,0.5);
top:0;
}
[id^=tab]:checked~[id^=tab-content]
{
display: block;
}
<ul class="tabs">
<li>
<input type="radio" name="tab" id="tab1" checked />
<label for="tab1">Personal Information</label>
<div class="tab-content" id="tab1-content">Here is the content for tab 1</div>
</li>
<li>
<input type="radio" name="tab" id="tab2" />
<label for="tab2">Academic Information</label>
<div class="tab-content" id="tab2-content">Here is the content for tab 2</div>
</li>
<li>
<input type="radio" name="tab" id="tab3" />
<label for="tab3">OECTA Involvement</label>
<div class="tab-content" id="tab3-content">Here is the content for tab 3</div>
</li>
</ul>
It's only your last css selector
[id^=tab]:checked~.tab-content{
display: block;
}
[id^=tab-content] selects all elements with an attribute ID starting with tab-content which is not what you want.

Display inline or float without breaking - without using media queries

When I try to add float left or display inline, things break. Currently, I have a max-width of 1000px for the form. What I was hoping is somehow, the first, and last name will automatically float side by side if it is wide enough. So perhaps a min-width for inputs First and Last name?
Important note: I wrote this to test out writing CSS DRY code. You notice if you change the font size, the whole project changes size, So this is important to me. Also, I do not want to use media queries.
I am aware that I may need to change my approach, and I am open to that as well. Not so much looking for an exact code answer.
form {
text-align: center;
}
form ul, form li, form input, form label {
box-sizing: border-box;
margin: 0; padding: 0;
}
form ul {
font-size: 100%;
border: 3px solid #000;
border-radius: .3em;
max-width: 1000px;
margin: 50px auto;
list-style: none;
overflow: hidden;
}
form li {
position: relative;
border-bottom: inherit;
border-bottom: 3px solid;
}
form label {
position: absolute;
border-bottom: 1px dotted;
border-bottom-color: inherit;
width: 100%;
padding: .3em .3em;
padding-bottom: .1em;;
top: 0; left: 0;
font-size: .6em;
text-transform: uppercase;
}
form input, form input:focus {
text-transform: capitalize;
text-align: inherit;
background: transparent;
border: none;
width: 100%;
font-size: 2em;
padding: .7em .1em;
padding-bottom: .2em;;
}
form input:focus {
background-color: rgba(255, 255, 0, .2);
}
form input[type="submit"] {
text-transform: uppercase;
padding-bottom: 1.8em;
font-size: .6em;
height: 1.5em;
background-color: #ddd;
}
<form action="">
<ul>
<li>
<input id="first-name" type="text" autofocus>
<label for="first-name">First Name</label>
</li>
<li>
<input id="last-name" type="text">
<label for="last-name">Last Name</label>
</li>
<li>
<input id="username" type="text">
<label for="username">Username</label>
</li>
<li>
<input type="submit" name="submit">
</li>
</ul>
</form>
Flexbox is the most modern solution to this problem. However, remember to add the necessary prefixes for some browsers. If IE9 support is necessary, see the float solution below:
HTML
<form action="">
<ul>
<li class="split">
<input id="first-name" type="text" autofocus>
<label for="first-name">First Name</label>
</li>
<li class="split">
<input id="last-name" type="text">
<label for="last-name">Last Name</label>
</li>
<li class="fill">
<input id="username" type="text">
<label for="username">Username</label>
</li>
<input type="submit" name="submit">
</ul>
</form>
CSS
form {
text-align: center;
}
form ul, form li, form input, form label {
box-sizing: border-box;
margin: 0; padding: 0;
}
form ul {
font-size: 100%;
border: 3px solid #000;
border-radius: .3em;
max-width: 1000px;
margin: 50px auto;
list-style: none;
overflow: hidden;
}
form li {
position: relative;
border-bottom: inherit;
border-bottom: 3px solid;
}
form label {
position: absolute;
border-bottom: 1px dotted;
border-bottom-color: inherit;
width: 100%;
padding: .3em .3em;
padding-bottom: .1em;;
top: 0; left: 0;
font-size: .6em;
text-transform: uppercase;
}
form input, form input:focus {
text-transform: capitalize;
text-align: inherit;
background: transparent;
border: none;
width: 100%;
font-size: 2em;
padding: .7em .1em;
padding-bottom: .2em;;
}
form input:focus {
background-color: rgba(255, 255, 0, .2);
}
form input[type="submit"] {
text-transform: uppercase;
padding-bottom: 1.8em;
font-size: .6em;
height: 1.5em;
background-color: #ddd;
}
#media only screen and (min-width: 768px) {
li {
clear: both;
}
li.split {
width: 50%;
float: left;
clear: none;
}
}
https://jsfiddle.net/qefo9eLr/
.fl-name {
display: flex;
flex-direction: row;
}
you can try to use bootstrap grid system
this way u can have the inputs into columns
bootstrap grid system
look at this fiddle:
gri system sample
<div class='row'>
<div class="col-xs-2">Hi</div>
<div class="col-xs-2">Hi</div>
in your case col-xs-6 will give you 2 columns fullwidth
Not exactly sure if this is what you're going for, but it seems to fit your criteria.
form {
text-align: center;
}
form ul,
form li,
form input,
form label {
box-sizing: border-box;
margin: 0;
padding: 0;
}
form ul {
font-size: 100%;
border: 3px solid #000;
border-radius: .3em;
max-width: 1000px;
margin: 50px auto;
list-style: none;
overflow: hidden;
}
form li {
position: relative;
border-bottom: inherit;
border-bottom: 3px solid;
}
form label {
position: absolute;
border-bottom: 1px dotted;
border-bottom-color: inherit;
width: 100%;
padding: .3em .3em;
padding-bottom: .1em;
;
top: 0;
left: 0;
font-size: .6em;
text-transform: uppercase;
}
form input,
form input:focus {
text-transform: capitalize;
}
form #fl-name {
display: inline-block;
}
form .floatMe {
float: left;
}
form .clearMe {
clear: right;
}
<form action="">
<ul>
<div class="fl-name">
<li class="floatMe">
<input id="first-name" type="text" autofocus>
<label for="first-name">First Name</label>
</li>
<li class="floatMe clearMe">
<input id="last-name" type="text">
<label for="last-name">Last Name</label>
</li>
</div>
<li>
<input id="username" type="text">
<label for="username">Username</label>
</li>
<input type="submit" name="submit">
</ul>
</form>
Here is another alternative using our old faithful floats: https://jsfiddle.net/mvpu6s5o/3/
The main difference is basically here:
form li {
width: 33.33%;
float: left;
}
form li:nth-child(3) {
float: right;
}
form li:last-child {
width: 100%;
clear: both;
}
I used a width with percentage to keep it fluid, so it'll adjust to different screen sizes. The li:nth-child(3) float the last input to the right, so we can get rid of a small gap at the end due to the 33.33% width. form li:last-child is used to clear both floats to the last input (since this too is an li).
I just change the semantic and apply flexbox. This is the result:
*, *:before, *:after {
box-sizing: inherit;
margin: 0;
padding: 0;
}
body {
align-items: center;
/background-color: #EB6361;
display: flex;
height: 100vh;
justify-content: center;
}
form {
box-shadow: 0 0 0 8px rgba(204,204,204,.85);
border-radius: 5px;
width: 500px;
}
form header {
background-color: #1ABC9C;
}
form header p {
color: #FFF;
font-family: 'ubuntu';
font-size: 15px;
padding: 15px 10px;
text-align: center;
}
form .body {
background-color: #EEE;
padding: 15px 20px;
}
form .body .block {
border: 2px solid #333;
border-radius: 4px;
overflow: hidden;
}
form .body .block:not(first-of-type) {
margin-top: 10px;
}
form .body .block:first-of-type > .group {
width: 98%;
}
form .body .block:first-of-type {
display: flex;
}
form .body .block .group {
display: flex;
flex-flow: column-reverse nowrap;
overflow: hidden;
}
form .body .block:first-of-type .group:first-of-type {
border-right: 2px solid #333;
}
form input {
background-color: transparent;
border: none;
color: #555;
font-size: 22pt;
padding: 6px 10px;
text-align: center;
}
form input:focus, form input:focus + label {
background-color: #F7F8E0;
}
form label {
border-bottom: 1px dotted #bbb;
color: #555;
font-family: 'ubuntu';
font-size: 11px;
padding: 2px;
text-align: center;
text-transform: uppercase;
}
form footer {
overflow: hidden;
}
form footer button {
background-color: #F39C12;
color: #FFF;
cursor: pointer;
width: 100%;
border: none;
padding: 4px;
}
<form action="">
<header>
<p>Submit Query Form</p>
</header>
<section class="body">
<div class="block">
<div class="group">
<input type="text" />
<label for="">First Name</label>
</div>
<div class="group">
<input type="text" />
<label for="">Last Name</label>
</div>
</div>
<div class="block">
<div class="group">
<input type="text" />
<label for="">Username</label>
</div>
</div>
</section>
<footer>
<button>Submit query</button>
</footer>
</form>
A very simple solution is with Flexbox.
Set the parent element to display type 'flex'.
Also set up flex wrap: wrap // This way the children will wrap if needed.
The children become flex objects. Since I want them to be even, I set them both to flex grow: 1
Set the children to flex-basis as 300px. // This is almost like a minimum width. This triggers the wrap.
body {
padding: 50px;
}
.main {
background-color: #e9e9e9;
display: flex;
flex-wrap: wrap;
}
.main input {
background-color: #e9e9e9;
}
.one {
flex-grow: 1;
flex-basis: 300px
}
.two {
flex-grow: 1;
flex-basis: 300px;
}
<head>
<link rel="stylesheet" href="inline.css">
</head>
<body>
<form class="main">
<input type="text" class="one">
<input type="text" class="two">
</form>
</body>