Custom element is not ignored when child is inline-block - html

I've got custom elements in my code which display some odd behaviour when a child element has a display: inline-block style.
Consider the following two div elements:
<div style="padding: 4px;">
<randomcustomelement style="background-color: yellow">
<div style="display: block; height: 36px; background-color: red; width: 125px;">
<div style="display: block; width: 100%; height: 12px; background-color: green;"></div>
</div>
</randomcustomelement>
</div>
<div style="padding: 4px;">
<randomcustomelement style="background-color: yellow">
<div style="display: inline-block; height: 36px; background-color: red; width: 125px;">
<div style="display: block; width: 100%; height: 12px; background-color: green;"></div>
</div>
</randomcustomelement>
</div>
In the first main div it is clear that the custom element randomcustomelement is ignored by the browser. It does have a proper width and height, but is not rendered, like expected. In the second main div, however, randomcustomelement does get rendered, and what more, it has a very strange height of 17px. I've included an image depicting this through Chrome's element inspector below:
The only difference between both examples is that the child div which is wrapped by randomcustomelement has display: block in the first example, and display: inline-block in the second example. I've given the randomcustomelement a distinct yellow color to also visibly depict that it does get rendered.
This problem is present in all browsers, even though they should ignore the custom element:
User agents must treat elements and attributes that they do not understand as semantically neutral; leaving them in the DOM (for DOM processors), and styling them according to CSS (for CSS processors), but not inferring any meaning from them.
https://www.w3.org/TR/html5/infrastructure.html#extensibility-0
This is really giving me a headache, because I need the inner div to be a display: inline-block. So I would need the second example's code to give the first example's results.

Forcing the style of the randomcustom element to be display: inline-block and height: 0 gives the desired result.
<div style="padding: 4px;">
<randomcustomelement style="background-color: yellow">
<div style="display: block; height: 36px; background-color: red; width: 125px;">
<div style="display: block; width: 100%; height: 12px; background-color: green;"></div>
</div>
</randomcustomelement>
</div>
<div style="padding: 4px;">
<randomcustomelement style="background-color: yellow;height:0;display:inline-block">
<div style="display: inline-block; height: 36px; background-color: red; width: 125px;">
<div style="display: block; width: 100%; height: 12px; background-color: green;"></div>
</div>
</randomcustomelement>
</div>

Related

Center Align Multiple DIV under single main DIV causing overlapping issue

I want to print multiple DIV one after another in single line. Also i need to center the content to the page.
I am able do this with float style but its not Centered to the page. I searched google , it says use display: inline-block; instead of float.
Here is what i tried, but 2 divs are overlapping.
<div style="text-align:center">
<div style="display: inline-block;">
<a style="cursor:pointer;text-decoration:none;color:#000">
<div style="display: inline-block;cursor:pointer;position:absolute;margin-left:0px;margin-top:-1px;margin-right:0px;">
<img src="images/Calendar_blank.png" width="75" height="75">
</div>
<div style="display: inline-block;position:absolute;margin-left:0px;margin-top:-1px;margin-right:0px;width:75px; font-family:Verdana, Geneva, sans-serif; font-size:16px;color:#030">Aug</div>
<div id="date1" style="display: inline-block;position:absolute; margin-left:0px;margin-top:27px;margin-right:0px;width:75px; "><strong>25</strong></div>
</a>
</div>
<div style="display: inline-block;">
<a style="cursor:pointer;text-decoration:none;color:#000">
<div style="display: inline-block;cursor:pointer;position:absolute;margin-left:0px;margin-top:-1px;margin-right:0px;width:75px; ">
<img src="images/Calendar_blank.png" width="75" height="75">
</div>
<div style="display: inline-block;position:absolute;margin-left:0px;margin-top:-1px;margin-right:0px;width:75px; font-family:Verdana, Geneva, sans-serif; font-size:16px;color:#030">Aug</div>
<div id="date2" style="display: inline-block;position:absolute; margin-left:0px;margin-top:27px;margin-right:0px;width:75px; "><strong>25</strong></div>
</a>
</div>
</div>
Out Put is :
Expected is : The Content should be centered to the page & it should not overlap.
Note: I dont want to add hardcoded MARGIN to the style. as it will effective while zooming in and out & it will not be page centered.
Your code is too mired with inline styles for me to debug easily. Below is how you would rebuild your desired layout most basically from scratch.
The key component is adding display: inline-block to the light-gray calendar divs to allow them to sit side-by-side: https://developer.mozilla.org/en-US/docs/Web/CSS/display#Values
Also, setting those divs to position: relative and their child divs to position: absolute allows you to ensure the contained elements' origins are within those parent divs.
Inline styles (styles specified within your HTML) should be kept to an absolute minimum. HTML (markup) is for content primarily. Styles go in your CSS, and behaviors generally belong in JavaScript. This makes your code much easier to read and update (and debug).
A separate CSS file can be linked from your HTML, or it can included in your HTML if wrapped in <style> and </style> tags.
DEMO: http://jsbin.com/lifij/1/
HTML:
<body>
<div id="container">
<div class="calendar">
<div class="stripe">
<h3>Aug</h3>
</div>
<div class="number">
<h1>25</h1>
</div>
</div>
<div class="calendar">
<div class="stripe">
<h3>Aug</h3>
</div>
<div class="number">
<h1>25</h1>
</div>
</div>
</div>
</body>
CSS:
h1, h3 {
font-family: sans-serif;
text-align: center;
margin: 0;
}
h3 {
font-weight: normal;
}
.calendar {
background-color: lightgray;
width: 80px;
height: 80px;
border-radius: 6px;
display: inline-block;
position: relative;
}
.stripe {
background-color: red;
width: 100%;
height: 24px;
border-radius: 6px 6px 0 0;
position: absolute;
top: 0;
}
.number {
position: absolute;
top: 30px;
width: 100%;
}

Margin and positioning of input elements [duplicate]

This question already has answers here:
Why is there an unexplainable gap between these inline-block div elements? [duplicate]
(6 answers)
Closed 8 years ago.
I want to partition a block into two separate smaller blocks. For that, I am using the following HTML:
<div style="width: 300px; height: 200px; background-color: red; ">
<div style="display: inline-block; width: 100px; height: 200px; background-color: lightblue;">
</div>
<div style="display: inline-block; width: 196px; height: 200px; background-color: green;">
</div>
</div>
Result:
Here's the first problem: As you can see, there is still some of the (red) background visible between the two boxes (grey & green). I don't know how to get rid of that space - both div elements already have a margin, border and padding of 0. When I increase the width of the green div element to 200px (as it should be), the element jumps out of its parent since it grew too large.
Is there any default padding, or a rule that browsers must add some space between simple elements? If so, how can I get rid of it?
The second issue arises when I add a input tag to the green div:
<div style="width: 300px; height: 200px; background-color: red; ">
<div style="display: inline-block; width: 100px; height: 200px; background-color: lightblue;">
</div>
<div style="display: inline-block; width: 196px; height: 200px; background-color: green;">
<input type='submit' value='Details'/> <!-- new -->
</div>
</div>
Now, for some reason, the green div is forced down again:
The input element (and the containing div by extension) is moved down to the bottom of the red div. I found out I can stop that by using position: absolute but I'm confused as to why it behaves like this at all. It seems like there's something more subtle going wrong, but I don't know what.
Thanks for your help.
Instead of display:inline-block use float
<div style="width: 300px; height: 200px; background-color: red; ">
<div style="float: left; width: 100px; height: 200px; background-color: lightblue;">
</div>
<div style="float: right; width: 200px; height: 200px; background-color: green;">
<input type='submit' value='Details'/> <!-- new -->
</div>
</div>
DEMO
<div style="width: 300px; height: 200px; background-color: red; ">
<div style="display: inline-block; width: 100px; height: 200px; background-color: lightblue;">
</div>
<div style="display: inline-block;position:fixed; width: 200px; height: 200px; background-color: green;">
<input type='submit' value='Details'/>
</div>
</div>
Demo: http://jsfiddle.net/hsakapandit/c6AUF/

How to I get a div that shows on hover, not to push content down on the page?

I have created the following
http://jsfiddle.net/fcW66/1/
CSS
.div_wrapper {
float: left;
width: 100px;
background: 3333;
margin: 15px;
background: #cacaca;
z-index: 1;
}
.div_two {
display: none;
height: 120px;
background: #444;
z-index: 999;
}
.div_one:hover .div_two {
display: block;
}
HTML
<div class="div_wrapper">
<div class="div_one">
<img src="#" style="width: 100px; height: 100px;">
<div class="div_two">description</div>
</div>
</div>
<div class="div_wrapper">
<div class="div_one">
<img src="#" style="width: 100px; height: 100px;">
<div class="div_two">description</div>
</div>
</div>
<div class="div_wrapper">
<div class="div_one">
<img src="#" style="width: 100px; height: 100px;">
<div class="div_two">description</div>
</div>
</div>
<br style="clear:both;" />
<div class="div_wrapper">
<div class="div_one">
<img src="#" style="width: 100px; height: 100px;">
<div class="div_two">description</div>
</div>
</div>
<div class="div_wrapper">
<div class="div_one">
<img src="#" style="width: 100px; height: 100px;">
<div class="div_two">description</div>
</div>
</div>
<div class="div_wrapper">
<div class="div_one">
<img src="#" style="width: 100px; height: 100px;">
<div class="div_two">description</div>
</div>
</div>
When you hover over it shows the description, but it pushes the other divs further. I have tried giving the div_wrapper a fixed width, which fixes that problem but when the div2 appears it shows under the next div that is under it. I tried adding a z-index and it did not change anything. I need the div_two to show over the top of the divs that are below it and not change the layout.
If you want white-space in the location of the object BEFORE hover, you would use visibility, not display.
visibility:hidden, instead of display:none
and visibility:visible, instead of dislpay:block
You should use position: absolute to position this div. Absolutely positioned elements do not take up space within their container preventing them from pushing other elements around.
In most cases such as this, you will want to set the parent element to position: relative as well, so that the absolute element can be positioned relative to its parent instead of the whole document.
http://jsfiddle.net/fcW66/7/
.div_one{
position: relative;
}
.div_two {
/* ... */
position: absolute;
width: 100%;
}
You can use position: absolute; to accomplish this.
Here's a fiddle http://jsfiddle.net/QbAzY/
Add position:absolute; and width: 100px; to your .div_two rules
.div_two {
display: none;
height: 120px;
background: #444;
z-index: 999;
position:absolute;
width: 100px;
}
jsFiddle example
z-index only applies to positioned elements, so by setting position:absolute on your .div_two elements it not only allows the z-index to work, but it takes those elements out of the normal flow of the document and won't push the other divs down. Note that you also have two background rules on your .div_wrapper element, and a z-index rule that isn't doing anything.

How to measure div size including children

I have a div which has display: block. Inside of it, I have another div with display: inline-block. When I measure the size of the containing div, children bounds aren't taken into account. It's readily visible in a browser's inspector. Is there a way to stretch the containing div so that its bounds contain bounds on inline block?
Here is code which demonstrates the problem:
<div style="display: inline">
<div style="display: inline">aaa</div>
<div style="display: inline-block; width: 100px; height: 100px; background: red"></div>
</div>
P.S. I can't change inline on the container to the inline block. The only thing I can change is inline-block div's attributes.
You can apply display: table; on the container div. Here's a working fiddle.
Set height and width to auto
<div style="display: inline-block; background-color:green;width:auto; height:auto;">
<div>aaa</div> ^^^^^^^^^^^^^^^^^^^^^^^^
<div style="width: 100px; height: 100px; background: red;opacity:0.5;"></div>
</div>
You could wrap the contents within a div and give it a value of display: block;
DEMO http://jsfiddle.net/kevinPHPkevin/649EB/
CSS
.container {
background: #ccc;
}
.inner {
display: block;
background: #000;
}
HTML
<div class="container" style="display: inline">
<div class="inner">
<div style="display: inline">aaa</div>
<div style="display: inline-block; width: 100px; height: 100px; background: red"></div>
</div>
</div>
EDITED
If you can use display: block; then I would set it to height: auto;
[updated] DEMO http://jsfiddle.net/kevinPHPkevin/649EB/1/

inline-block messes up with relative positioning

I have the following code:
<HTML>
<head>
<style>div{border:dashed 1px silver}</style>
</head>
<BODY style="background: #fff;">
<div style="position: absolute; background: #0f0; width: 256px; height: 96px; overflow: scroll;">
<DIV style=" display: inline-block;position: relative;top: 64px; left: 32px;">
<DIV style="width: 18px; height: 14px; float: left; background: #f00;"></DIV>
<DIV style="float: left">First</DIV>
<div style="clear: both;"></div></DIV>
<DIV style=" display: inline-block;position: relative;top: 96px; left: 32px;">
<DIV style="width: 18px; height: 14px; float: left; background: #0f0;"></DIV>
<DIV style="float: left">Second</DIV><div style="clear: both;"></div></DIV>
</div>
</BODY>
</HTML>
The second div isn't positioned on the 32 x position unless I remove the display: inline-block property, which I need. Is there a way around this?
Edit: it seems to work if I remove display: inline-block, but then the scrollbars will appear horizontally (as the div takes some invisible space).
You should use the way position:absolute elements are displayed when inside position:relative.
In addition, to avoid the horizontal scroll bar, use overflow-y.
Working example: http://jsbin.com/uveni3
When using "inline-block" properties, you should always start your HTML opening tag in a DTD format. placing one there should resolve this.