a vs button vs div vs input type='button
There's a good article that summarizes the differences nicely
In short:
| | General usage | Complex designs | Can be disabled |
|-----------------------|----------------------------|-----------------|-----------------|
| <button> | General button purpose. | Yes | Yes |
| | Used in most cases | | |
| --------------------- | -------------------------- | --------------- | --------------- |
| <input type="button"> | Usually used inside | No | Yes |
| | forms | | |
| --------------------- | -------------------------- | --------------- | --------------- |
| <a> | Used to navigate to | Yes | No |
| | pages and resources | | |
| --------------------- | -------------------------- | --------------- | --------------- |
| <div> | Can be used for clickable | Yes | No |
| | area which is not button | | |
| | or link by definition | | |
Placing buttons in a div
Hope this will solve your problem and help you align your buttons like you wanted.
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}
body {
height: 100vh;
display: grid;
place-items: center;
}
.container {
position: relative;
width: 300px;
height: 300px;
display: grid;
grid-template-rows: auto;
grid-template-columns: repeat(3, 1fr);
}
.container button {
color: white;
display: grid;
font-size: 1rem;
position: relative;
place-items: center;
border: 1px solid white;
}
.container button:nth-child(odd) {
background-color: red;
}
.container button:nth-child(even) {
background-color: green;
}
<div class="container">
<button>Top Left</button>
<button>Top</button>
<button>Top Right</button>
<button>Left</button>
<button>Center</button>
<button>Right</button>
<button>Bottom Left</button>
<button>Bottom</button>
<button>Bottom Right</button>
</div>
Need to make a clickable div button
There are two solutions posted on that page. The one with lower votes I would recommend if possible.
If you are using HTML5 then it is perfectly valid to put a div
inside of a
. As long as the div doesn't also contain some other specific elements like other link tags.
<a href="Music.html">
<div id="music" class="nav">
Music I Like
</div>
</a>
The solution you are confused about actually makes the link as big as its container div. To make it work in your example you just need to add position: relative
to your div. You also have a small syntax error which is that you have given the span a class instead of an id. You also need to put your span inside the link because that is what the user is clicking on. I don't think you need the z-index
at all from that example.
div { position: relative; }
.hyperspan {
position:absolute;
width:100%;
height:100%;
left:0;
top:0;
}
<div id="music" class="nav">Music I Like
<a href="http://www.google.com">
<span class="hyperspan"></span>
</a>
</div>
http://jsfiddle.net/rBKXM/9
When you give absolute
positioning to an element it bases its location and size after the first parent it finds that is relatively positioned. If none, then it uses the document. By adding relative
to the parent div you tell the span to only be as big as that.
Button vs. Clickable Div in HTML
<button>
has some inbuilt things that you may like, but also has some styling restrictions that not apply to <div>
.
<button>
can be focused, where <div>
has to have tabindex
attribute to make focus work.
Pressing it can submit a form if configurated for ex. has built in onclick
handler: <button onclick="submit()">
Usage of <button>
applies to HTML5 document specification which puts divs in place of containers elevating role of contextual elements like <footer>
, <section>
and <button>
.
In short I always use <div>
s as can style it how I need and program them how I like, am not fancying predefined behaviours as they can vary on different browsers :)
Creating a div by pressing a button
Your not off by much. You were selecting the div you already created in the html, and some of your js syntax is off.
Try this jsFiddle
function createDiv ()
{
var boxEle = document.createElement('div');
var container = document.querySelector('.app');
boxEle.style.width = '100px';
boxEle.style.height = '100px';
boxEle.style.backgroundColor = '#f00';
container.appendChild(boxEle);
}
html/css, make a button look like a div
Some CSS styles you should always be able to apply to HTML buttons:
- background-color -> change the background colour of a button.
- font-size -> change the font size of a button label.
- padding -> change the padding of a button.
- border-radius -> add rounded corners to a button.
- border property -> add a coloured border to a button.
- box-shadow property -> add shadows to a button.
Here's a coloured border button example:
<!DOCTYPE html><html><head><style>.button { background-color: white; /* button background */ color: black; /* label colour */ border: 2px solid #0040ff; /* 2px blue line border */ padding: 10px 20px; border-radius: 10px; /* rounded corner style */}</style></head><body>
<h2>A styled button.</h2>
<button class="button">Button label</button>
</body></html>
Adding 100 buttons in a div
The code that creates element is document.createElement
and You call it ones in first example, hence only one element is created.
appendChild
does not creates an element, it appends already created element to some parent.
If it is already appended, then does nothing
Related Topics
How to Start a Skype Chat - Not a Call - with a Link
Stacking Divs from Bottom to Top
Prevent Ligatures in Safari (Mavericks/iOS7) via CSS
How to Display The HTML Code of a Webpage in a Batch File
How to Calc() The Number of Rows in a Grid Template
Edit Line Thickness of CSS 'Underline' Attribute
How to Get Rid of an Element's Offset Using CSS
Unicode Character as Bullet for List-Item in CSS
Make an HTML Element Non-Focusable
Indent Starting from The Second Line of a Paragraph with CSS
How to Avoid a Page Break Immediately After a Heading
Preventing Fixed Footer from Overlapping Content
Reveal and Hide a Div on Checkbox Condition with CSS
Can Data-* Attribute Contain HTML Tags
How to Convert Nunit Output into an HTML Report
How to Have Perfectly Centered Navigation Bar with Equally Wide Links