Why Do Inline Block Divs Behave Differently From Inline Block Buttons? - html

I tried to change the y-height of an inline button and realized that it doesn't work by setting a negative margin (transform would work though). Choosing a div instead of a button works, also wrapping the div inside a button. Where does this difference come from? I guess it has to do with baseline?
div, button {
box-sizing: border-box;
font-size: 0;
margin: 0;
padding: 0;
border: 0;
background: transparent;
}
.inline-offset {
display: inline-block;
width: 10px;
height: 10px;
margin-bottom: -5px;
border: solid 2px black;
}
Hello,<br />
dear
<div class="inline-offset"></div>
<br />
world!
<br />
<br />
Hello,<br />
dear
<button class="inline-offset"></button>
<br />
world!
<br />
<br />
Hello,<br />
dear
<button><div class="inline-offset"></div></button>
<br />
world!
[EDIT] Just in case it appears differently in some browsers, here's what it looks like to me. Second box is positioned as if no margin was present.

Recently I learned that whenever you set display: inline or inline-block you must be careful about any spaces or newlines that exist in front of the element.
I will be honest in saying that I don't know yet any official documentation about this behavior, but we all would appreciate any help to clarify it.
div, button {
box-sizing: border-box;
font-size: 0;
margin: 0;
padding: 0;
border: 0;
background: transparent;
}
.inline-offset {
display: inline-block;
width: 10px;
height: 10px;
border: solid 2px black;
}
Hello,<br />
dear <!--
--><div class="inline-offset"></div>
<br />
world!
<br />
<br />
Hello,<br />
dear <!--
--><button class="inline-offset"></button>
<br />
world!
<br />
<br />
Hello,<br />
dear <!--
--><button><div class="inline-offset"></div></button>
<br />
world!

Related

Nav bar with position: fixed not displaying properly?

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../css/example.css" rel="stylesheet" type="text/css" />
<title>My Portfolio</title>
</head>
<body>
<div class="nav">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</div>
<div class="content">
<p>Self-studying to become a web developer. Learning HTML, CSS, and JavaScript plus jQuery through Jon Duckett's books and FreeCodeCamp. Once I get my entry level job, I wish to study more and expand on back-end development, so I can become a full-stack developer.</p>
<p>Portfolio:</p>
<p>Contact me here:</p>
<input type="text" />
<br />
<input type="text" />
<br />
<input type="text" />
<br />
<input type="submit" value="Send" />
<p>Here's where you can get in touch with me! Here, you can request for my resume, get more details on my experience, or learn more about my favorite hobbies! I will reply as soon as I am able, thanks!
</div>
</body>
</html>
body {
margin: 0px;
background-color: rgba(195, 246, 255, 0.56);
}
.nav {
background-color: rgba(190, 190, 190, 0.72);
position: fixed;
left: 0px;
right: 0px;
text-align: center;
height: 100px;
border: 1px solid black;
}
li {
display: inline;
font-size: 20px;
}
.content {
border: 1px solid green;
background-color: white;
width: 1100px;
margin-top: 102px;
}
I'm trying to put a nav bar on top, with my content below it so it doesn't interfere. I set my .nav to height=100px and when I try to set my .content below it with margin-top: 102px, it ends up moving the .nav bar as well. However, when I add a border such as border: 1px solid black; to my body, it ends up working. I'm so confused! Can I make it so that without adding a border to my body, the nav displays correctly above my content?
position: fixed makes the element fixed to the page, so that it will always appear in exactly that position (with content either going on top or behind if necessary). If you'd like your navbar to be positioned at the top of the page, you're looking to apply top: 0 to it:
body {
margin: 0px;
background-color: rgba(195, 246, 255, 0.56);
}
.nav {
background-color: rgba(190, 190, 190, 0.72);
position: fixed;
left: 0px;
right: 0px;
top: 0;
text-align: center;
height: 100px;
border: 1px solid black;
}
li {
display: inline;
font-size: 20px;
}
.content {
border: 1px solid green;
background-color: white;
width: 1100px;
margin-top: 102px;
}
<body>
<div class="nav">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</div>
<div class="content">
<p>Self-studying to become a web developer. Learning HTML, CSS, and JavaScript plus jQuery through Jon Duckett's books and FreeCodeCamp. Once I get my entry level job, I wish to study more and expand on back-end development, so I can become a full-stack
developer.</p>
<p>Portfolio:</p>
<p>Contact me here:</p>
<input type="text" />
<br />
<input type="text" />
<br />
<input type="text" />
<br />
<input type="submit" value="Send" />
<p>Here's where you can get in touch with me! Here, you can request for my resume, get more details on my experience, or learn more about my favorite hobbies! I will reply as soon as I am able, thanks!
</div>
</body>
If you don't want it to obscure the content on scroll, you're looking for position: relative. You'll also probably want to remove the margin-top: 102px on .content:
body {
margin: 0px;
background-color: rgba(195, 246, 255, 0.56);
}
.nav {
background-color: rgba(190, 190, 190, 0.72);
position: relative;
left: 0px;
right: 0px;
top: 0;
text-align: center;
height: 100px;
border: 1px solid black;
}
li {
display: inline;
font-size: 20px;
}
.content {
border: 1px solid green;
background-color: white;
width: 1100px;
}
<body>
<div class="nav">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</div>
<div class="content">
<p>Self-studying to become a web developer. Learning HTML, CSS, and JavaScript plus jQuery through Jon Duckett's books and FreeCodeCamp. Once I get my entry level job, I wish to study more and expand on back-end development, so I can become a full-stack
developer.</p>
<p>Portfolio:</p>
<p>Contact me here:</p>
<input type="text" />
<br />
<input type="text" />
<br />
<input type="text" />
<br />
<input type="submit" value="Send" />
<p>Here's where you can get in touch with me! Here, you can request for my resume, get more details on my experience, or learn more about my favorite hobbies! I will reply as soon as I am able, thanks!
</div>
</body>
Hope this helps! :)
You should set top: 0; to your .nav class otherwise it will try to preserve the existing position vertically which could be why the margin is managing to affect it.

html: have form adjust width to contents?

please have a look at this simple jsfiddle:
--> JSFiddle
html:
<form>
<p>
<input id="I1" value="above left" size="40" />
<input id="I2" value="above right" size="10" />
</p>
<p>
<input id="I3" value="below right" size="10" />
<input id="I4" value="below left" size="20" />
</p>
</form>
css:
form {
overflow: hidden;
border: 3px solid red;
padding: 5px;
}
#I1,
#I2 {
background-color: orange;
float: left;
}
#I3,
#I4 {
background-color: yellow;
float: right;
}
what I want is the red border (= form width) auto-adjust to the width of the two top inputs (+ padding, of course).
I would then expect, that the second line inputs appear right-aligned below the first, so "above right" would be exactly above "below right", and "below left" beeing right left of it, like so:
11111111111111111111 2222222
4444444 3333333
Can this be done without tables?
Thx, Armin.
It most certainly can be done without tables :). You could set, for form:
form {
display: inline-block;
padding: ...;
}
This will prevent the form from expanding to the entire width of the screen. It will be as wide as the elements inside it, or, if you manually define its width, as wide as specified.
If you would like no elements to appear on either side of the form within the form's containing element, you can wrap it in a div or other block level element:
<div>
<form>
<!-- Rest of content -->
</form>
</div>
In order to remove the spacing above and below each p, as per your comment, remove their margin-top and margin-bottom, as they have a margin by default:
p {
margin-bottom: 0;
margin-top: 0;
}
I've organized and cleaned-up your code. The way to make the div adjust to its content is by using display: inline-block. I've also fixed the padding issue so now the entire content is padded equally to 5px.
JsFiddle
HTML:
<form>
<p>
<input id="I1" value="above left" size="40" />
<input id="I2" value="above right" size="10" />
</p>
<p>
<input id="I3" value="below right" size="10" />
<input id="I4" value="below left" size="20" />
</p>
</form>
CSS:
.border {
border: 3px solid red;
padding: 5px;
display: inline-block;
}
#I1 {
background-color: orange;
}
#I2 {
background-color: yellow;
}
.top-block,
.bottom-block {
width: 100%;
}
I have update this answer. Probably this is what you need, but there are other options but I don't think is the scope of this question.
CSS Code:
form {
overflow: hidden;
border: 3px solid red;
padding: 5px;
width: 400px; // limites de form size
}
#I1,
#I2 {
position: absolute;
background-color: orange;
}
#I3,
#I4 {
position: absolute;
background-color: yellow;
}
p {
display: block;
border: 1px solid blue;
position: relative;
height: 2em;
}
#I1,
#I4 {
right: 25%;
}
#I2,
#I3 {
right: 0;
}
See here the full code: https://jsfiddle.net/uv8yrrav/18/
hope this helps!

how to get these two div's side by side in this container

I have the following fiddle consisting of this html markup and CSS:
.embers-info{
margin-top: 30px;
border: 1px solid black;
width: 600px;
padding: 20px 0px 30px 0px;
}
.embers-total{
width: 80px;
}
.embers-email{
margin-left: 20px;
}
.embers-phone{
margin: 10px 0px 0px 20px;
}
.embers-notes{
margin: 20px 0px 0px 20px;
}
.embers-pickup{
margin-left: 20px;
}
.embers-actions{
margin-left: 20px;
margin-top: 20px;
}
.af-commerce{
float: right;
}
.embers-checkout-header{
padding: 3px 10px 12px 5px;
}
.embers-user-info{
width: 300px;
border: 4px solid blue;
padding-bottom: 50px;
}
.embers-delivery-info{
width: 300px;
border: 4px solid red;
padding-bottom: 50px;
}
<div class='embers-info'>
<div class='embers-user-info'>
<div class='embers-email'>
email:<br />
<input type="text" size="30" name="email" id="email" />
</div>
<div class='embers-phone'>
phone number:<br />
<input type="text" size="30" name="phone_number" id="phone_number" />
</div>
<div class='embers-notes'>
notes:<br />
<textarea id='notes' cols='30' rows='5'></textarea>
</div>
<div class='embers-pickup'>
<br />pickup times:<br />
November 21 10am:<input type="radio" size="30" class="pickup" name="pickup" value="time1" /> <br />
November 21 noon:<input type="radio" size="30" class="pickup" name="pickup" value="time2" /><br />
November 21 2pm:<input type="radio" size="30" class="pickup" name="pickup" value="time3" /><br />
November 21 4pm:<input type="radio" size="30" class="pickup" name="pickup" value="time4" /><br />
November 22 8am:<input type="radio" size="30" class="pickup" name="pickup" value="time4" /><br />
</div>
<div class='embers-actions'>
<button id='save_order'>save order</button>
<button id="customButton">Purchase</button>
</div>
</div>
<div class='embers-delivery-info'>
here i am
</div>
</div>
and would like to get the blue and red div side-by-side. How would I do this?
I tried to use:
display:inline-block;
but that didn't matter.
Someone has mentioned flexbox, which while a perfectly valid solution may not be what you need depending on your minimum browser requirements as flexbox is still not as well supported as one would like. When aligning elements side by side using traditional methods you have a couple of options, display: inline-block and float.
display: inline-block : http://jsfiddle.net/bzkt2hcx/2/
.embers-info {
display: inline-block;
}
.embers-info > div {
display: inline-block;
vertical-align: top;
}
Notes:
inline-block elements may be rendered with spacing on some browsers, most notably chrome. To fix this set we can set font-size: 0 on the parent container and then reset the font-size on the children.
float: left/right: http://jsfiddle.net/bzkt2hcx/3/
.embers-info {
overflow: auto;
}
.embers-info > div {
float: left;
}
Notes: floated elements are taken out of the flow and do not affect their parents heights, causing sizing and layout issues. To alleviate this we need to apply a clearfix on the parent, the simplest of which is setting overflow: auto on the parent container.
You could use flexbox by adding display: flex to the containing div. This will default in a flex-direction of row, which will put them next to each other:
http://jsfiddle.net/bzkt2hcx/1/
More reading for laying out the children in a flexbox container:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes

CSS absolute position not being put in the correct location

I'm attempting to produce mailing list labels from a database connected to a website (yes, I could grab the list in some text format and import to something like Word... but ultimately I'm not going to be preparing the mailing so I want as few steps as possible in the process!). I've managed to get it working except for one part: the labels are not positioned correctly vertically.
I've looked at a number of questions on SO over the last few hours but none of the questions (or solutions) quite match what I've got.
I've defined 3 different paragraph classes for the 3 labels across the page (there's wierd gaps in the labels we've got for printing), with dimensions based on the label positions and size. Each label is a single paragraph with one of the 3 classes, and banks of labels sit inside a div representing the pages (these take into account the top and bottom margins of the pages).
Horizontally, it's working fine; vertically is another matter. Firebug is showing the paragraphs overlapping. I'm stumped as to what's going on.
Here's the CSS:
body
{
color: Black;
background-color: White;
font-family: "Times New Roman", Times, serif;
height: 100%;
margin: 0;
padding: 0;
font-size: 11pt;
}
p.print_label_left, p.print_label_right, p.print_label_middle
{
background: white;
position: absolute;
width: 5.4cm;
height: 1.43cm;
max-height: 1.43cm;
min-height: 1.43cm;
margin-left: 0.5cm;
margin-top: 1.48cm;
margin-right: 0.5cm;
margin-bottom: 0.5cm;
padding: 0.5cm;
top: 0;
border: none;
outline: solid 1px black;
}
p.print_label_left
{
left: 0.65cm;
}
p.print_label_middle
{
left: 7.3cm;
}
p.print_label_right
{
left: 13.95cm;
}
div.print_page
{
background: white;
position: relative;
padding: 0;
margin: 0;
left: 0;
top: 0;
width: 21cm;
height: 29.7cm;
border: none;
}
And here's a representative HTML sample (I can't give the real stuff due to privacy):
<body>
<div class="print_page" style="top:-1.33333333333cm;">
<p class="print_label_left" style="top:0cm;">A<br />
B<br />
C<br />
<br />
<br />
<br />
</p>
<p class="print_label_middle" style="top:0cm;">D<br />
E<br />
F<br />
<br />
<br />
<br />
</p>
<p class="print_label_right" style="top:0cm;">G<br />
H<br />
I<br />
<br />
<br />
<br />
</p>
<p class="print_label_left" style="top:2.43cm;">J<br />
K<br />
L<br />
<br />
<br />
<br />
</p>
<p class="print_label_middle" style="top:2.43cm;">M<br />
N<br />
O<br />
<br />
<br />
<br />
</p>
<p class="print_label_right" style="top:2.43cm;">P<br />
Q<br />
R<br />
<br />
<br />
<br />
</p>
</div>
</body>
The header defines a doctype (xhtml transitional 1.0) and I've played around with the max-height and min-height and putting extra lines in each paragraph to try to force it to have sufficient content to fill out the text, but no such luck.
When I print out the list, the outlines indicate the boxes are spaced at 2cm intervals not the required 2.43cm. And I'm fresh out of ideas of things to search for!
If it helps, I'm using Firefox 36 on a Windows 7 x64 machine but the server I'm running this on is a typical LAMP setup.
It's should work if i understand your question?
body
{
color: Black;
background-color: White;
font-family: "Times New Roman", Times, serif;
height: 100%;
margin: 0;
padding: 0;
font-size: 11pt;
}
p.print_label_left, p.print_label_right, p.print_label_middle
{
background: white;
width: 300px;
height: 100px;
margin-top: 10px;
padding: 0.5px;
top: 10px;
border: none;
outline: solid 1px black;
}
p.print_label_left
{
margin-left: 10px;
}
p.print_label_middle
{
margin-left: 150px;
}
p.print_label_right
{
margin-left: 300px;
}
div.print_page
{
background: white;
padding: 0;
margin: 5px;
left: 0;
top: 0;
width: 500px;
height: 100px;
border: none;
outline: solid 5px Red;
}
<div class="print_page">
<p class="print_label_left">A<br />
B<br />
C<br />
</p>
<p class="print_label_middle">D<br />
E<br />
F<br />
</p>
<p class="print_label_right">G<br />
H<br />
I<br />
</p>
<p class="print_label_left">J<br />
K<br />
L<br />
</p>
<p class="print_label_middle">M<br />
N<br />
O<br />
</p>

Slide-to-the-right submenu after clicking a button

I want to make a control panel for the admin part of my website. The control panel I developed consists of several buttons. What I need is whenever I click one of the buttons, that option's sub-menu will appear right next to it. For example, I have "My Account" as one of the main options. If I click on the "My Account" button, its sub-menu (with Update Profile and Change Password etc.) will appear.
Here's the code for the control panel:
<div class="main-area">
<div class="control-panel">
<h1>Admin Control Panel</h1>
<button class="categories">My Account ►</button><br /><br />
<button class="categories">System Users ►</button><br /><br />
<button class="categories">Applicants ►</button><br /><br />
<button class="categories">Blacklist ►</button>
<br /><br />
<button class="categories">Jobs ►</button><br /><br />
<button class="categories">Requirements ►</button><br />
<br />
<button class="categories">Reports ►</button>
Here's the CSS part:
body {
font-family: arial;
background-color: #0F8DC7;
}
.control-panel {
border: solid 1px #000;
padding: 15px;
border-radius: 15px;
box-shadow: 5px 5px 5px;
width: 300px;
text-align: center;
display: inline-block;
height: 370px;
background-color: #FFE400;
}
.main-area {
width: 500px;
margin: auto;
}
.categories{
width: 200px;
}
Hoping for some answers not involving JavaScript.
It's just a demo, I haven't styled your code for a better view.
You have to use css :hover to achieve this functionality. And put your button(menuItem) and submenuItems(li) inside "ul" tag like this,
<ul>
<button class="categories">My Account ►</button><br /><br />
<li>First</li>
<li>Second</li>
</ul>
Add this css code ,
.main-area ul li{
display : none;
}
.main-area ul:hover li{
display : block;
background:blue;
height:auto;
width:8em;
}
Style your submenu items as you want.
jsFiddle