Padding, list and background - html

Im really confused, it is strangest bug i have seen in my life.
Here is JSFiddle example: http://jsfiddle.net/c92mjkne/1/
As you can see, when our "comment" is hovered, #content get stange margins (but in CSS it have no margins). As i can know, it is ol's margins. But why they are outside of parent div?
Ok, that's strange. BUT! When we changing padding: 0; to padding: 1px; in #content's CSS, we see, that block have no margin! WTF? Help me please :D I really dont know how to google :D
Here is example:
#head, #foot, #content {
padding: 7px;`
}
#content {
padding: 0;
}
#comment:hover div {
background: #eee;
}
#comment {
border: 1px solid rgba(0, 0, 0, 0);
}
#comment:hover {
border: 1px solid gray;
}
ul {
margin: 7px;
}
<div id="comment">
<div id="head">
Efog <span style="color: gray">today, 10:10 pm</span>
</div>
<div id="content">
<ul>
<li>Hello</li>
<li>Stack</li>
<li>Overflow</li>
</ul>
</div>
<div id="foot">
answer
</div>
</div>
And here is code with padding: 1px:
#head, #foot, #content {
padding: 7px;`
}
#content {
padding: 1px;
}
#comment:hover div {
background: #eee;
}
#comment {
border: 1px solid rgba(0, 0, 0, 0);
}
#comment:hover {
border: 1px solid gray;
}
ul {
margin: 7px;
}
<div id="comment">
<div id="head">
Efog <span style="color: gray">today, 10:10 pm</span>
</div>
<div id="content">
<ul>
<li>Hello</li>
<li>Stack</li>
<li>Overflow</li>
</ul>
</div>
<div id="foot">
answer
</div>
</div>
Sorry for english, thanks.

As i can know, it is ol's margins. But why they are outside of parent div?
Because they are supposed to be (the are not “outside” the div, they have become margins of the div) – http://www.w3.org/TR/CSS21/box.html#collapsing-margins:
“In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.
Adjoining vertical margins collapse, […]”
So no bug at all, but specs followed to the point.
BUT! When we changing padding: 0; to padding: 1px; in #content's CSS, we see, that block have no margin!
Read on at the above point,
“Two margins are adjoining if and only if:
[…]
 - no line boxes, no clearance, no padding and no border separate them
[…]”

http://jsfiddle.net/c92mjkne/2/
#comment:hover{
background: gray;
}
It is normal, you set every div inside your comment div a background but you set some a margin. If you don't want any blank area, just set the background color to the div containing them all.
It's not a bug.

Related

Why is margin not working for <p> element?

I want to specify margins for all p elements in my html/php page. It should be straightforward, but it doesn't work for me. Please excuse my noob level.
Here is my css code I added to the head:
<style>
p {
margin-left: 120px;
margin-right: 120px;
}
.code {
color : blue;
background-color: lightgrey
}
.center {
text-align: center;
}
</style>
In the body I have p elements, none of which has a margin.In body Example:
<a name="hardware"></a>
<h4>Required hardware:</h4>
<p>
I tested this procedure on a Raspberry Pi 3B+, having 1 GB of RAM.
</p>
Does anyone know why the left/right margin is not working?
I see that margins added.
Maybe because of added class center to p you don't see. If you provide us with more code, it will be clearer.
To illustrate the idea I made snippet, look inti it.
P.S. useful to know, margin in css between siblings not added but the bigger margin wins. You can see in snippet, that p has default top and bottom margin, but sibling ps have gap between them which equals to one of margin's value, not margin-bottom + marging-top values.
.margins {
margin-left: 120px;
margin-right: 120px;
}
.center {
text-align: center;
}
.p-border p {
border: 1px solid black;
}
.border-dot {
border: 1px dotted black;
}
<div class="border-dot">
<p class="center margins">With margins</p>
<p class="center margins">With margins</p>
<p class="center">Without margins</p>
</div>
<div class="border-dot p-border">
<p class="center margins">With margins</p>
<p class="center margins">With margins</p>
<p class="center">Without margins</p>
</div>

Multiple lines with wrapped background color

I have several different spans all wrapped up in a single div and I'm trying to add background color that wraps close to the text instead of a block (rectangle) around the span. So, I'm using inline, but this then puts all the spans on the same line. How can I get this background effect but putting getting line breaks in between the spans. Note that I can't change the HTML, but I have full control over CSS.
body {
background-color: red;
color: #fff
}
#page {
width: 800px;
}
.header-content {
width: 500px;
}
h1.module_header,
.fullwidth_header_subhead,
.header_content_wrapper {
display: inline;
background: #292d31;
box-shadow: 10px 0 0 #292d31, -10px 0 0 #292d31;
}
<body>
<div id="page">
<div class="header-content">
<h1 class="module_header">
This is the really long main title that can be many lines
</h1>
<span class="fullwidth_header_subhead">
Here is a subhead that can also be multiple lines so this can wrap also
</span>
<div class="header_content_wrapper">
<span>
Here is a shorter line but could be multiple lines
</span>
</div>
</div>
</div>
</body>
You can see the result here: https://codepen.io/jonmrich/pen/gdjBbK
One trick is to use the ::after pseudo-element to insert a line break character. You have to set white-space to pre in order for it to not collapse like other white space. The use of white-space: pre is credited to this answer by Adrift.
To add space between the lines, simply make the ::after pseudo-element display:block. That will add a line below the current line at the same font size as the element it is "after". Set the font-size property to equalize the height.
body {
background-color: red;
color: #fff
}
#page {
width: 800px;
}
.header-content {
width: 500px;
}
h1.module_header,
.fullwidth_header_subhead,
.header_content_wrapper {
display: inline;
background: #292d31;
box-shadow: 10px 0 0 #292d31, -10px 0 0 #292d31;
}
h1.module_header::after,
.fullwidth_header_subhead::after,
.header_content_wrapper::after {
content: '\0A';
white-space: pre;
display: block;
font-size: 10px;
}
<body>
<div id="page">
<div class="header-content">
<h1 class="module_header">
This is the really long main title that can be many lines
</h1>
<span class="fullwidth_header_subhead">
Here is a subhead that can also be multiple lines so this can wrap also
</span>
<div class="header_content_wrapper">
<span>
Here is a shorter line but could be multiple lines
</span>
</div>
</div>
</div>
</body>

CSS overflow not obeying padding

I have a set of HTML elements that I need to style, which I can't change the structure of in any way (yeah, I know).
The HTML has a div that contains two nested spans. The div has padding and the overflow is hidden. The width of the div is set programatically and applied as an inline style.
I would like the text contained within the inner span to be clipped, but still retain the right hand padding as specified on the containing div.
After some research, it appears that the standard approach to this is to use a second nested div but, as I mentioned, I can't change the structure of the HTML.
Currently I have:
<!-- This is what I have to work with (I can't change the structure of this HTML!) -->
<div class="c1" style="width: 100px;">
<span class="c1-inner">
<span class="c1-inner-2">
122333444455555666666777777788888888999999999
</span>
</span>
</div>
<!-- This is how I want the HTML above to display -->
<div class="c2" style="width: 100px;">
<div class="c2-inner">
122333444455555666666777777788888888999999999
</div>
</div>
Styled by the following CSS:
.c1 {
border: 1px solid red;
border-radius: 4px;
background-color: #c0c0c0;
padding: 0 13px 0 13px;
overflow: hidden;
}
.c1-inner {
// No relevant styles here yet
}
.c1-inner-2 {
// No relevant styles here yet
}
.c2 {
border: 1px solid red;
border-radius: 4px;
background-color: #c0c0c0;
padding: 0 13px 0 13px;
}
.c2-inner {
overflow: hidden;
}
A jsFiddle is available here
I need to style the top "button" so that it looks like the second one only using CSS. I have reached the limits of my CSS skills and any help would be very much appreciated.
A simple fix. Most important bit: you can make a span have a display value of block rather than inline, which is its default.
Here's the relevant CSS you need and a working example:
.c1 {
border: 1px solid red;
background-color: #c0c0c0;
padding: 0 13px 0 13px;
}
.c1-inner {
overflow: hidden;
display: block;
}
.c2 {
border: 1px solid red;
background-color: #c0c0c0;
padding: 0 13px 0 13px;
}
.c2-inner {
overflow: hidden;
}
We want this<br>
<!-- This is what i Have to work with -->
<div class="c1" style="width: 100px;">
<span class="c1-inner">
<span class="c1-inner-2">
122333444455555666666777777788888888999999999
</span>
</span>
</div>
<!-- This displays how i want the html above to display -->
<br>
to look like this<br>
<div class="c2" style="width: 100px;">
<div class="c2-inner">
122333444455555666666777777788888888999999999
</div>
</div>
<br>
but cannot change the structure of the html!

Vertically Centering anchor tag To Right Floating Element

I have been looking for a way to center an anchor tag vertically according to a span tag, which are both encased within div tag.
My HTML
<div id="project_list">
<div class="title">
Example Project
<span class="show_details">Show Details</span>
<div style="clear: both"></div>
</div>
</div>
My CSS
div#project_list {
border: 2px solid #000000;
}
div#project_list div.title {
background: grey;
padding : 10px;
}
div#project_list div.title a {
font-size: 1.231rem;
}
div#project_list span.show_details {
background: orange;
float : right;
padding : 13px 5px;
}
I have also create a JSFiddle here, so you may see what I am speaking about.
Thank you to everyone in advance as I have been racking my brain on how to do this for a couple days now.
You could set the line height to match the button height:
a { line-height:46px; }
Note: I just used a but you will probably want to add a class so the style doesn't get applied to all anchor tags.
http://jsfiddle.net/GxqTh/2/
#OpenNoxdiv- try adding padding to your a tag; 20px seemed to center nicely for me. - see below
#project_list div.title a {
padding-top:20px;
}

Put a line beside my text by css

I want to put a line beside my text Like THIS
----- Hello WOrld -----
the line should be continued and BOLD and align side middle wise And RED
Have you tried :before and :after selectors?
<span class="dashes">Hello WOrld</span>
<style type="text/css">
.dashes { font-weight: bold; }
.dashes:before, .dashes:after { content:"----"; color:#f00; }
</style>
This is how it comes out: image sample
UPDATE
Based on your updates and comments, I think this fits your description:
<h4 class="sidelines"><span>Hello WOrld</span></h4>
<style type="text/css">
h4.sidelines { text-align: center; border-bottom: 1px solid #f00; height: 0.5em; }
h4.sidelines span { display: inline-block; background: #fff; padding:0 0.25em; }
</style>
This will give you a centered, bolded title with continuous lines on each side.
Here's an example of the update: http://o7.no/PVXvaH
No css required
&boxh;&boxh;&boxh; Hello World &boxh;&boxh;&boxh;
Looks like this
&boxh;&boxh;&boxh;&boxh; Hello World &boxh;&boxh;&boxh;&boxh;
try this. but not compatible for all browser versions.
p:before,p:after {
content: "---";
}
<p>Hello WOrld</p>
I achieved this using div's. check this link to see the result.
HTML
<div class="line"></div>
<div class="text">Hello WOrld</div>
<div class="line"></div>
CSS
.line
{
width:100px;background-color:black;
border: 0.1em solid black; /* dashed, groove, inset */
margin-top:0.45em;margin-bottom:0.45em;
}
.line, .text
{
float:left;
}
.text
{
padding-left:10px;padding-right:10px;
}
Remember that when you add the scales(height) of margin-top, margin-bottom and border, it should be equal to one. Like 0.45 + 0.45 + 0.1 = 1 in my example. This will keep layout clean.
If you want to make the lines more bold then just increase the scale of border keeping in mind about the scales of margin-top and margin-bottom.
Hello World
p {
text-align:center;
border-left: 50px solid #363454;
border-right: 50px solid #363454;
width:150px;
line-height: 2px;
}
<p>Hello World</p>