Weird Bug with Border/Outline/Margin/Padding - css

HTML (3x times):
<div class="scream">
<div class="author">
<img src="avatars/dagrevis.png" alt="" title="daGrevis" />
</div>
<div class="data">
<span>Lorem Ipsum is simply dummy text of the printing and typesetting industry...</span>
</div>
<div class="clearer"></div>
</div>
CSS:
.scream {
background: #F4F4F4;
width: 944px; height: 48px;
}
.scream .author {
float: left;
}
.scream .data {
float: left;
}
.clearer { clear: both; }
This is how it looks on my browser:
As you can see, height is set to 48px. Images size is 48px as well. How it comes that I don't see each div.scream in separate line? If I set height to 50px, for example, all works! In my opinion, it's because there are some-kind of border/outline/margin/padding that ruin my life. Unfortunately, with FireBug I don't see anything like that...

The bottom edge of an image is aligned with the baseline (the grey line) of the line box by default. The space below the baseline (also called "descender space") is what's causing your problem. See this article by Eric Meyer for more details.
To fix it, add this rule:
.scream .author img {
display: block;
}
Or this one:
.scream .author img {
vertical-align: bottom;
}

If you play around with a DOM inspector for a bit, you'll find that .author is coming out with a height of 52px or so. The extra 4px appears to be coming from the line-height. Setting line-height to 0:
.scream .author {
float: left;
line-height: 0;
}
Fixes the layout: http://jsfiddle.net/ambiguous/8KzdD/
Or, you can float the image to the left as well:
.scream .author {
float: left;
}
.scream .author img {
float: left;
}
That will remove the image from the local flow and make the line-height irrelevant: http://jsfiddle.net/ambiguous/8KzdD/1/
Yet another option is to ditch the clearing <div> and use overflow:hidden on the outer <div>:
.scream {
background: #F4F4F4;
width: 944px;
height: 48px;
overflow: hidden;
}
This will leave the <div class="author"> with their 52px height but the effect won't be visually noticeable: http://jsfiddle.net/ambiguous/8KzdD/2/

Related

Links not lining up with inline-block

I'm trying to get the three links - LOGO, About, and FAQ to show up on the same line, but I'm unable to do so. It seems that the margin-left on my nav-link class is screwing things up, but I'm not sure why.
body, html {
background-color: #EDEDED;
}
.nav {
position: absolute;
width: 80%;
margin: 5%;
}
.nav-link {
margin-left: 2%;
}
.nav-part {
display: inline-block;
}
.apply {
float: right;
position: relative;
right: 0;
top: 0;
}
<div class="nav">
<div class="nav-part">
LOGO
About
FAQ
</div>
<div class="apply nav-part">
<button>Apply</button>
</div>
</div>
Here's my jsfiddle - https://jsfiddle.net/hcrcba06/1/
the div that contains your links does not have much room to put the links on the same line so it brings the last one to the next line just increase the width of your nav-part class and it will work. JSFiddle here
You could easily solve that by changing it to CSS table layout.
body, html {
background: #EDEDED;
}
.nav {
display: table;
width: 80%;
margin: 5% auto;
}
.nav-part {
display: table-cell;
}
.nav-link {
margin-left: 2%;
}
.apply {
text-align: right;
}
<div class="nav">
<div class="nav-part">
LOGO
About
FAQ
</div>
<div class="apply nav-part">
<button>Apply</button>
</div>
</div>
It's because you are using margin in percentages.
Margins in percentages are always
relative to the width of the containing block.
Remove the margin-left in percentages. If you add a fixed margin it will work.
Use:
.nav-link {
margin-left: 12px;
}
instead of
.nav-link {
margin-left: 2%;
}
DEMO: https://jsfiddle.net/hcrcba06/3/

footer's padding making it expand over its parent div width

I have a footer in my webpage with some text in it. When i try to add padding-left property to it, the width of the footer expands over the enclosing parent div element and this problem only happens with Opera and not with Chrome or Firefox or IE.. What should be done to correct it ??
CSS for footer :
footer {
color: #FFFFFF;
background-color: #3399CC;
padding-top: 12px;
padding-bottom: 12px;
width: 100%;
float: left;
}
Instead of adding an extra block level element inside of your footer, you should try the CSS3 box-sizing property with value set to border-box. The following footer definition should probably solve the problem for you:
footer
{
color: #FFFFFF;
background-color: #3399CC;
padding-top: 12px;
padding-bottom: 12px;
width: 100%;
float: left;
box-sizing: border-box; /* prefix accordingly */
}
To read a short introduction, visit the following link: http://www.w3schools.com/cssref/css3_pr_box-sizing.asp
you can fix this problem by taking a div inside footer tag and apply padding to that div
<style>
footer {
color: #FFFFFF;
background-color: #3399CC;
width: 100%;
float: left;
}
.footer {
padding-top: 12px;
padding-bottom: 12px;
padding-left:10px;
}
</style>
<footer>
<div class="footer">hello this is footer</div>
</footer>
That's a common sense. If you add "padding" and "width of 100%" you will get that result. Remove the "padding" or the "width of 100%" you will notice the problem no longer exist.
Add div tag between the parent-of-footer and the footer, and add the padding-left to that new div, like this:
<div id="parentOfFooter" style="background-color: #00ff00;">
<div id="paddingLeftProblemSolver" style="background-color: #ff8800; padding-left:50px;">
<div id="myfooter" style="color: #FFFFFF; background-color: #3399CC; padding-top: 12px; padding-bottom: 12px; width: 100%; float: left;">
i'm a footer
</div>
</div>
</div>

Make div size as per contents and horizontally center inside it's parent

I have a div message which basically has a text and a link. I want its size to be changing based on the string inside it. Also I want this message div to be centered inside its container.
I have been playing with it for a while without much luck. Here is the link to JSfiddle: http://jsfiddle.net/pDYJ8/
Also I don't know how make that text and link appear one after other ( not on the new line )
Here is the code:
<div class="container">
<div class="message">
<p>do you want to </p>
<a href="somewhere" target="_blank">
buy this product
</a>
</div>
</div>
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
}
.message {
margin-right: auto;
margin-left: auto;
background: #ddd;
width:100px;
}
Tried display inline block to fit its content but then it wouldn't center it inside its parent.
Keeping width 100px for now just to mock my requirements
Just Tweak Some CSS
See the demo fiddle.
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
text-align: center; /*added*/
}
.container .message {
display: inline-block; /*added*/
text-align: left; /*added*/
background: #ddd;
}
.message p { /*added*/
display: inline-block;
}
Explanation
The text-align center causes the now inline-block display of .message to center, which is then reset to have its own text-align back at left (this is not necessary). To get the a on the same line, the p also needs to be some type of inline display, here I chose inline-block as well..
I think you are over complicating things. All you need is a text-align: centeron the container and a display: inline-block on the message:
.container {
background: #666;
font-size: 15px;
color: #e47911;
text-align: center;
}
.container .message {
background: #ddd;
display: inline-block;
}
http://jsfiddle.net/Pevara/pDYJ8/9/
The inline block makes the div act as a word inside text, and the text-align center makes the 'word' align to the center...
Here is a simplified approach to a couple of the answers given. It reduces the amount of HTML and CSS needed.
CSS
.container {
color: #e47911;
text-align: center;
}
.message {
display: inline;
background: #DDDDDD;
}
HTML
<div class="container">
<p class="message">
Do you want to buy this product?
</p>
</div>
I would definitely put your anchor tag, <a> inside the paragraph tag, <p>.
You could even remove display: inline; from .message if you made it a <span> rather than a <p>.
Check this out:
http://jsfiddle.net/pDYJ8/10/
Changes made to above link
.container .message {
margin: 0 auto;
width:auto;
}
span{
background: #ddd;
display:inline;
}
You can simplify it with display: table; and margin: 0 auto;
.container {
display: table;
margin: 0 auto;
background-color: #DDD;
}
<div class="container">
do you want to buy this product
</div>

Image and text not vertically aligning when using Div tags and CSS

I'm not that great at CSS. I get how the properties work together but sometimes I don't get the results I want. I have a banner at the top of my page which contains a logo and some text.
They are contained in separate div tags in one larger div tag but my Text aligns to the top of the div tag while my image is centered vertically. How do I get the centers aligned vertically?
<div id="webBanner">
<div id="bannerImage">
<a href="Dashboard.aspx" title="Accreditation Data">
<img src="Images/logo.png" />
</a>
</div>
<div id="bannerText">
Accreditation Database (AD)
</div>
</div>
CSS:
#webBanner
{
height: 60px;
width: 100%;
vertical-align: top;
}
#bannerText
{
font-family: Arial;
font-size: xx-large;
font-style: italic;
color: #fff;
margin: 2px 5px;
}
#bannerImage
{
height: inherit;
float:left;
width: 223px;
vertical-align: middle;
margin: 2px 5px;
}
CSS vertical align does not work the way most people expect it to. It won't actually do anything at all in this particular case.
What you probably want to do is solve this with padding on your bannerText element.
For example, to vertically center 20px text in a 60px wrapper:
#webBanner {
height: 60px;
width: 100%;
}
#bannerText {
font-size: 20px;
height: 20px;
padding: 20px 0;
/* 20px padding on top and bottom plus 20px height = 60px total */
}
Note, the 0 in the padding refers to the left and right padding. You may want to adjust that depending on how your banner is designed.
Also, the "height: 20px" declaration is redundant if the only content in the div is text and the line height is not adjusted. I included it to provide a general solution.
#bannerText {
line-height: 60px;
}
Is one way..
I'd recommend something along the lines of this...
HTML:
<div id="webBanner">
<a id="bannerLink" href="Dashboard.aspx" title="Accreditation Data">
<img src="Images/logo.png" />
</a>
<h1>Accreditation Database (AD)</h1>
</div>
CSS:
#webBanner
{
height: 60px;
}
#webBanner h1
{
color: #fff;
float: left;
font-family: Arial;
font-style: italic;
line-height: 60px;
}
#bannerLink
{
display: block;
float: left;
height: 60px;
width: 223px;
}
You can adjust the CSS to vertically center the logo image by using margin:.
Given your text is inside a div, this may work:
#bannerText {
vertical-align: middle;
}
See this clear tutorial for more information on your options.

text wraps down in rounded corner box

I need to do dinamic rounded box, (dinamic height and dinamic width)
I try using the code offer in the link bellow:
http://home.tiscali.nl/developerscorner/liquidcorners/liquidcorners.htm
but I need it also with images for the middle left and right (I can't use simply background and border as offers in the up code,
I try modified the code, and the box looks great,
but when I enter text in it, the text wrap down.
any idea?
Html code
<div class="RoundCrnr">
<div class="TopLeft"></div>
<div class="TopRight"></div>
<div class="inside">
<div class="MiddleLeft">
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
</div>
<div class="MiddleRight"></div>
</div>
<div class="BottomLeft"></div>
<div class="BottomRight"></div>
</div>
the Css Code
.RoundCrnr {
width:590px;
float:right;
}
.TopLeft {
background-image: url("/Content/Images/Top_left.png");
height: 34px;
font-size: 2px;
margin-right: 34px;
}
.TopRight {
float: right;
margin-top: -34px;
background-image: url("/Content/Images/box_top_right.png");
height:34px;
width: 34px;
font-size: 2px;
}
.gap-saver {
height: 1px;
margin: 0 0 -1px 0;
padding: 0;
font-size: 1px; /* to correct IE */
}
.MiddleLeft {
background-image: url("/Content/Images/Middle_left.png");
height: 7px;
margin-right: 20px;
}
.MiddleRight {
float: right;
margin-top: -7px;
background-image: url("/Content/Images/box_right.png");
height:7px;
width: 20px;
}
.BottomLeft {
background-image: url("/Content/Images/Bottom_left.png");
height: 33px;
font-size: 2px;
margin-right: 33px;
}
.BottomRight {
background-image: url("/Content/Images/box_bottom_right.png");
background-position: 100% 0;
background-repeat: no-repeat;
height: 33px;
font-size: 2px;
margin-top: -33px;
}
.inside {
}
thanks a lot!
I suggest using pure CSS. CSS3 to be precise. And if you are worried about browser compatibility, this is truly excellent:
http://css3pie.com/
The documentation should help you get started with some css3 code to make a box with rounded corners, and how to use their script to be cross-compatible:
http://css3pie.com/documentation/getting-started/
I've used it several times so far, and now not afraid to use CSS3!
BTW in regards to why your text is wrapping down, it is because you have several div tags, which are block elements (display:block). This makes them sit on top of one another (they kinda behave like paragraphs). I don't know the reason why you have several divs, but you could either change the div's display value like this:
.MiddleLeft div {
display: inline;
}
Or change them to span tags, which are inline elements. You can find out more by googling for difference between span & div tags.
Hope this all helps!
Ali.

Resources