I'm having a problem with buttons in CSS.
Between the first two I get the right distance, but between the second and the third I get two buttons joined (attached) (as in the picture).
How can I solve this problem?
.buttons {
display: flex;
}
.buttons form {
margin: 0 10px;
}
<div class="buttons">
<form action="createArt.html">
<input type="submit" value="ADD" />
</form>
<input type="button" value="EDIT" onclick="confirmation_edit()" />
<input type="button" value="DELETE" onclick="confirmation_delete()" />
</div>
#container {
display: flex;
//justify-content:space-around;
}
.butts, form {
margin-left: 10px;
}
<div id="container">
<form action="createArt.html">
<input type="submit" value="ADD" >
</form>
<input class='butts'type="button" value="EDIT" >
<input class='butts' type="button" value="DELETE" >
</div>
Change .buttons form by .buttons > *
.buttons > * { margin: 0 10px; }
You selector is faulty, u have selected 'bottom form, INSTEAD of botto. The later would have selected all the 3 buttons n have applied the CSS rules to all the 3.
You would probably be better served with a specific class on the button but here I added the selector the the input based on type (probably not the way to do this; classes should be used to style)
.buttons {
display: flex;
}
.buttons form ,
.buttons input[type="button"] {
margin: 0 10px;
}
<div class="buttons">
<form action="createArt.html">
<input type="submit" value="ADD" />
</form>
<input type="button" value="EDIT" onclick="confirmation_edit()" />
<input type="button" value="DELETE" onclick="confirmation_delete()" />
</div>
As an option it is suitable for small projects, but for a larger project I recommend using classes.
To add styles to an element you can use: element Selector, id Selector, class Selector.
More information can be found here
.buttons {
display: flex;
}
.buttons form {
margin: 0 10px;
}
input:nth-child(2){
margin: 0 10px 0 0;
}
<div class="buttons">
<form action="createArt.html">
<input type="submit" value="ADD" />
</form>
<input type="button" value="EDIT" onclick="confirmation_edit()" />
<input type="button" value="DELETE" onclick="confirmation_delete()" />
</div>
Related
I have a problem applying CSS to <form> element and all nested elements inside. I do it by class .header_navbar_form and .header_navbar_form_input in stylesheet header.css, but only bootstrap takes effect. For some reason, other elements, such as <div>, <p>, <h1> are affected by the same stylesheet
Once again: div with id header_navbar_div is affected by the same stylesheet
cshtml:
<div id="header_navbar_div">
<form class="header_navbar_form" action="/Home/Index">
<input class="header_navbar_form_input" type="submit" value="Uvod">
</form>
<form class="header_navbar_form" action="/Home/Locations">
<input class="header_navbar_form_input" type="submit" value="Lokality">
</form>
<form class="header_navbar_form" action="/Home/Trips">
<input class="header_navbar_form_input" type="submit" value="Nase vandry">
</form>
<form class="header_navbar_form" action="/Home/Equipment">
<input class="header_navbar_form_input" type="submit" value="Vybava">
</form>
<form class="header_navbar_form" action="/Home/Recipes">
<input class="header_navbar_form_input" type="submit" value="Kucharka">
</form>
<form class="header_navbar_form" action="/Home/Tips">
<input class="header_navbar_form_input" type="submit" value="Navody a rady">
</form>
<form class="header_navbar_form" action="/Home/Nature">
<input class="header_navbar_form_input" type="submit" value="O Prirode">
</form>
<form class="header_navbar_form" action="/Home/Songbook">
<input class="header_navbar_form_input" type="submit" value="Zpevnik">
</form>
<form class="header_navbar_form" action="/Home/Partners">
<input class="header_navbar_form_input" type="submit" value="Partneri">
</form>
</div>
CSS:
#header_navbar_div {
padding-top: 2px;
display: flex;
justify-content: center;
}
.header_navbar_form {
width: 50%;
background-color: red;
}
.header_navbar_form_input {
width: 11%;
padding-bottom: 10px;
padding-top: 10px;
border-color: darkolivegreen;
background-color: lightgoldenrodyellow;
color: darkolivegreen;
font-weight: bold;
}
I openned the application in Edge instead of Firefox and the CSS took effect, so i cleared a cache of Mozzila and it works. Kind of noobie but, solved the problem. Thanks for efforts.
Jackob
Busy on a game involving buttons. Having 2 questions about problems I stumbled upon.
Code:
input {
background-color: #e7e7e7;
border: solid;
border-width: 1px;
text-align: center;
height: 50px;
width: 50px;
padding: 0px;
margin: 0px;
}
<html>
<head>
</head>
<body>
<input type="button" value="foo">
<input type="button" value="">
<input type="button" value="">
<br/>
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
<br/>
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
</body>
</html>
Questions:
Why do the button misalign when I put in a value in the the button? Dev tools also says it still the same size (50px 50px) so why does it change position?
How can I style the CSS to have zero distance bewteen the buttons (in other words: the borders of the buttons touch). I already tried to set padding/margin of html/body/input but none of these seems to work.
Why are the buttons with text being pushed down?
So there's a bit at play here. Text and inline elements vertically-align to the baseline by default. The baseline is a value determined by the line-height of the element, though an element without a line-height will determine a "reasonable" value[1] - in the case of an empty element, this will be 0. However when you add text, the element is then given a line-height and moved down by that amount.[2]
A simple solution is to force the inputs to render with the same alignment, text or not, by applying vertical-align: top.
Why is there space between the buttons?
Inline elements (and inline-block elements like your inputs) will naturally align side-by-side, however they behave similarly to text[3]. Much like if you were to put a line-break between two letters in your HTML, a line-break between inline elements will add a single space between them.
Hypothetically, if you were to put all of your inputs on one line (without spaces), it would solve your issue:
<input type="button" value="these" /><input type="button" value="are" /><input type="button" value="touching" />
<br><br>
<input type="button" value="these" />
<input type="button" value="are" />
<input type="button" value="not" />
Though I don't suggest that method - it's merely for demonstration purposes.
So what's the solution?
Well, you have some options. Choose the one that you think would work best for you.
Solution 1: Wrap the inputs in a container and apply font-size: 0 to it. The spaces will still be there, but the font-size: 0 ensures they aren't visible.
input {
background-color: #e7e7e7;
border: solid;
border-width: 1px;
text-align: center;
height: 50px;
width: 50px;
padding: 0px;
margin: 0px;
vertical-align: top;
font-size: 12px;
}
.container {
font-size: 0;
}
<div class="container">
<input type="button" value="foo">
<input type="button" value="">
<input type="button" value="">
<br/>
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
<br/>
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
</div>
Solution 2: Bypass the triviality of inline elements and make use of display: block with float.
input {
background-color: #e7e7e7;
border: solid;
border-width: 1px;
text-align: center;
height: 50px;
width: 50px;
padding: 0px;
margin: 0px;
font-size: 12px;
float: left;
display: block;
}
.row {display: block;}
.row::after {
display: block;
content: '';
clear: both;
}
<div class="row">
<input type="button" value="foo">
<input type="button" value="">
<input type="button" value="">
</div>
<div class="row">
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
</div>
<div class="row">
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
</div>
Solution 3: Use a more modern approach, like flexbox.
input {
background-color: #e7e7e7;
border: solid;
border-width: 1px;
text-align: center;
height: 50px;
width: 50px;
padding: 0px;
margin: 0px;
vertical-align: top;
font-size: 12px;
}
.container {
display: flex;
flex-wrap: wrap;
width: 150px;
}
<div class="container">
<input type="button" value="foo">
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
</div>
Sources
1: "normal: Tells user agents to set the computed value to a "reasonable" value"
2: "For inline non-replaced elements, the box used for alignment is the box whose height is the 'line-height'.
3: "Inline-level elements generate inline-level boxes, which are boxes that participate in an inline formatting context."
"Why do the button misalign when I put in a value in the the button?"
The default value for elements with text content vertical-align is a baseline, so you need to specify it (in my case I use vertical-align: middle).
"How can I style the CSS to have zero distance bewteen the buttons (in other words: the borders of the buttons touch)"
I followed a little hacky way and set a negative margin-left value to get buttons without space between them. I have selected specific items using input:nth-child(2n) and input:nth-child(4n - 1) selectors and gave margin-left: -4px; to them.
Here is my solution:
* {
margin: 0;
padding: 0;
}
input {
background-color: #e7e7e7;
border: solid;
border-width: 1px;
text-align: center;
height:50px;
width:50px;
vertical-align: middle
}
input:nth-child(2n) {
margin-left: -4px;
}
input:nth-child(4n - 1) {
margin-left: -4px;
}
<html>
<head>
</head>
<body>
<input type="button" value="foo">
<input type="button" value="">
<input type="button" value="">
<br/>
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
<br/>
<input type="button" value="">
<input type="button" value="">
<input type="button" value="">
</body>
</html>
Feel free to ask, if anything isn't clear!
For question two, you can try the border-spacing method.
For example:
input {
border-collapse: separate;
border-spacing: 0px 0px;
}
I'm trying to design a TV remote control using flexbox (I would use Grid but it is not supported by webkit).
I'm struggling to align/center the items around the "OK" item.
I was thinking to create an "invisible" item but I can't find any such thing on the flexbox specs (empty space seems to be ignored).
I feel that defining "margins" is not exactly the right way to do this.
It should look as below
Up
|
<left----OK---Right-->
|
Down
But it looks like more like this
Up
|
<left----OK---Right-->
|
Down
Here you can play it.
.grid {
display: flex;
flex-flow: row wrap;
width: 100%;
}
.up {
order: 1;
margin-left: 40%;
margin-right: 50%;
}
.left {
order: 2;
margin-left: 30%;
}
.ok {
order: 3;
}
.right {
order: 4;
margin-right: 40%;
}
.down {
order: 5;
margin-left: 40%;
}
<div class="grid">
<form action="/keyboard/" class="up">
<input type="hidden" name="q" value="126" />
<button type submit class="button-large">Up </button>
</form>
<form action="/keyboard/" class="left">
<input type="hidden" name="q" value="123" />
<button type submit class="button-large">Left</button>
</form>
<form action="/keyboard/" class="ok">
<input type="hidden" name="q" value="36" />
<button type submit class="button-large">OK</button>
</form>
<form action="/keyboard/" class="right">
<input type="hidden" name="q" value="124" />
<button type submit class="button-large">Right</button>
</form>
<form action="/keyboard/" class="down">
<input type="hidden" name="q" value="125" />
<button type submit class="button-large">Down;</button>
</form>
</div>
It's actually not too complicated. Just need some adjustments to your CSS. No changes necessary to your HTML.
.grid {
display: inline-flex; /* 1 */
flex-flow: row wrap;
}
.up, .down {
flex: 0 0 100%; /* 2 */
text-align: center; /* 2 */
}
.left, .right {
flex: 1 0 1%; /* 3 */
display: flex;
}
.left { justify-content: flex-end; }
.right { justify-content: flex-start; }
.ok {}
<div class="grid">
<form action="/keyboard/" class="up">
<input type="hidden" name="q" value="126" />
<button type submit class="button-large">Up </button>
</form>
<form action="/keyboard/" class="left">
<input type="hidden" name="q" value="123" />
<button type submit class="button-large">Left</button>
</form>
<form action="/keyboard/" class="ok">
<input type="hidden" name="q" value="36" />
<button type submit class="button-large">OK</button>
</form>
<form action="/keyboard/" class="right">
<input type="hidden" name="q" value="124" />
<button type submit class="button-large">Right</button>
</form>
<form action="/keyboard/" class="down">
<input type="hidden" name="q" value="125" />
<button type submit class="button-large">Down</button>
</form>
</div>
jsFiddle
Notes:
Size container to content size (not width: 100%).
Occupy all space in the row, then center inline content.
Consume all free space in the row (does not apply to "OK", which takes content width only). The flex-basis: 1% is solely for Safari, which doesn't otherwise break .left to the second row as it should.
Is it possible to change the button css based on values. I want to have different css for Save and different css for Cancel for type button.
Ex:
<input type="button" value="Save">
<input type="button" value="Cancel">
save { /* Some CSS */ }
cancel{ /* Some CSS */ }
NOTE : I cannot use class as all the input is having same class as there are plenty of buttons. Also I dont want to use Jquery.
Yes, with an attribute selector.
input[value="Save"] {
color: red;
}
input[value*="Cancel"] {
color: blue;
}
<input type="submit" value="Save">
<input type="submit" value="Cancel">
<input type="text" value="Cancel-me-too">
To target specific button type you need more than one attribute selector
input[type="submit"][value="Save"] {
color: red;
}
input[type="submit"][value*="Cancel"] {
color: blue;
}
<input type="submit" value="Save">
<input type="submit" value="Cancel">
<input type="text" value="Save">
Use CSS Attribute Selectors :
input[value="Save"] {background:green;}
input[value="Cancel"] {background:grey;}
I think jquery seems to be no need.
Button is large, the class must be used.
Try this code.
<style type="text/css">
input[value="Save"] {
color: green;
}
input[value*="Cancel"] {
color: black;
}
</style>
<input type="submit" value="Save">
<input type="submit" value="Cancel">
I have this code:
.hidden_element {
height: 1px;
width: 1px;
overflow: hidden;
}
<form action="">
<label for="file">
<button type="button" class="button red">Choose File</button>
</label>
<div class="hidden_element">
<input type="file" name="video" id="file" />
</div>
</form>
The problem is when I click choose file nothing happens.
Change your button like <button type="button" onclick="document.getElementById('file').click()" class="button red">Choose File</button>
.hidden_element {
height: 1px;
width: 1px;
overflow: hidden;
}
<form action="">
<label for="file">
<button type="button" onclick="document.getElementById('file').click()" class="button red">Choose File</button>
</label>
<div class="hidden_element">
<input type="file" name="video" id="file" />
</div>
</form>
The label element applies to one input only: since it has one as a child (the button is an input element), it applies to it instead of the file-input as you hoped.
Just remove the button, maybe replace it with a span and style it as you wish, and clicking the label will open the file picker. No javascript needed! :)
.hidden_element {
height: 1px;
width: 1px;
overflow: hidden;
}
.button {
display: inline-block;
box-sizing: border-box;
border: 1px solid #aaa;
background-image: linear-gradient(to bottom, #eee, #ccc);
padding: 0.1em 0.2em;
cursor: pointer;
}
<form action="">
<label for="file">
<span class="button red">Choose File</span>
</label>
<div class="hidden_element">
<input type="file" name="video" id="file" />
</div>
</form>
I do not see the purpose of putting the button as a label. Either use a button or a submit to process your input (file upload). You should not hide your 'input file' tag. You really need two things for the user: the 'input file' tag to allow the user to choose the file he wants to upload and then a way to perform an action by submitting the form or perhaps an ajax call. However, since you are just learning this process, I recommend, just performing a simple submit and for you to write the code on the backend that will handle uploading the file. Here is a sample of my html code:
<form method="post" action="uploadFile.php">
<input type="file" name="video" id="file" />
<br /><br />
<input type="submit" value="upload file">
</form>