How do you create consistant vertical spacing while using different line-heights? - html

I am using different line-heights in my css and it is causing issues with my vertical spacing. I would like there to be the same amount above the horizontal rule as there is below.
This is an example of my issue:
My HTML
<div class="intro">
<p>The powered flight took a total of about eight and a half minutes. It seemed to me it had gone by in a lash. We had gone from sitting still on the launch pad at the Kennedy Space Center to traveling at 17,500 miles an hour in that eight and a half minutes. It is still mind-boggling to me. </p>
</div>
<hr />
<div class="normal">
<p>I recall making some statement on the air-to-ground radio for the benefit of my fellow astronauts, who had also been in the program a long time, that it was well worth the wait.</p>
</div>
<hr />
<div class="normal">
<p>The powered flight took a total of about eight and a half minutes. It seemed to me it had gone by in a lash. We had gone from sitting still on the launch pad at the Kennedy Space Center to traveling at 17,500 miles an hour in that eight and a half minutes. It is still mind-boggling to me.</p>
</div>
My CSS
.intro p { margin-bottom: 24px; font-size: 24px; line-height:36px; }
.normal p { margin-bottom: 18px; font-size: 16px; line-height:18px; }
hr { border-top: 1px solid rgb(200,200,200); margin: 10px 0}
You can also see it here:
http://codepen.io/dachan/pen/Csueb
Are there any solutions to my problem, minus having to manually create different margins for the hr tag?
Also, I do intend on having multiple paragraphs so any solution omitting a margin-bottom would not work for me.

If you know where you are in the text flow, just use a different rule style between the intro and the first text unit. The following will give you the same spacing above and below the first <hr/>
hr { border-top: 1px solid rgb(200,200,200); margin: 10px 0 28px 0 }

I do not use the <hr> tag cause it is hard o style properly, defintly in the great browser called IE.
In stead I use an empty div with a class .hr applied to it. My css would look something like this:
.hr {
border-top: 1px solid black;
margin-top: 10px;
height: 10px;
}

If you set the line-height of the text elements equal to the font-size, therefore making the height of the element equal to the height of the text. when you add the hr element the spacing should be the same.
.intro p { font-size: 24px; line-height:24px; }
.normal p { font-size: 16px; line-height:16px; }
hr { border-top: 1px solid rgb(200,200,200); margin: 10px 0}

I don't think your issue here is your line-height. You have defined a bottom margin for each of your paragraphs, but no top margin. Because of this, your second paragraph has default margin above it, making it closer to the hr.
One possible solution is to simply add margin-top equal to your margin-bottom.
.intro p { margin-bottom: 24px; font-size: 24px; line-height:36px; }
.normal p { margin-top: 18px; margin-bottom: 18px; font-size: 16px; line-height:18px; }
hr { border-top: 1px solid rgb(200,200,200); margin: 10px 0; }
Example here: http://jsfiddle.net/3BEkL/1/
The other possibility, which may not be what you want, since it seems you are going for different spacing between the hr's based on the font-size/line-height of each paragraph, is
remove all margin styling from the paragraph elements and only apply margins to the hr elements.
.intro p { font-size: 24px; line-height:36px; }
.normal p { font-size: 16px; line-height:18px; }
hr { border-top: 1px solid rgb(200,200,200); margin: 30px 0; }
Example here: http://jsfiddle.net/3BEkL/2/

Related

CSS: best way to have a text aligned on the top of the container div

I was wondering, what's the best way to achieve this:
What I want
Instead of this, that is the default situation:
How can I align the top of a inline element with the top of it's container? I just saw a lot of answers and methods, and I'm confused. What's the best practice? Until now I just used negative margin, but to me it looks more like a dirty and unstable trick than a clean e reliable solution.
The default behavior you want to 'fix' is because of the way the computer font metrics are calculated. Recently a very good article on this topic has been published.
You need to get the difference between "ascent" metric and the capital height of your font somehow and then move the text up by this value. Unfortunately, it's impossible with CSS currently, so some hardcoded font-specific 'magic numbers' seem inevitable here.
It totally depends on the font how much space there is on top of a normal capital Letter, e.g. to leave space for the dots on top AAÄAA
But what works was to reduce the line-height for the first line or the top margin relative (in em so it will fit for all font-sizes) to the text size. Just play around with it a little:
*::first-line {
line-height: 0.7em;
}
You need to use position property. Below is the sample code
#met {
font-size: 30px;
background-color: green;
float:left;
display:inline-block;
z-index: 1;
}
.na{
position:relative;
}
.na p,
.na a {
position:relative;
top: -5px;
}
a {
vertical-align:top;
}
#tim {
font-size: 20px;
background-color: blue;
}
First part is okay here with line-height:21px; . Couldn't find what worng with p tag even after removing padding and margins
#met {
font-size: 30px;
background-color: green;
float:left;
display:inline-block;
}
a {
line-height:21px;
vertical-align:top;
}
#tim {
font-size: 20px;
background-color: blue;
#met {
font-size: 30px;
background-color: green;
float:left;
display:inline-block;
}
a {
line-height:21px;
vertical-align:top;
}
#tim {
font-size: 20px;
background-color: blue;
margin: 0; padding: 0;
}
p{
margin: 0;
padding: 0;
}
<div>
<div class="na" id="met">
<a>TITLE</a>
</div>
<div class="na" id="tim">
<p class="dummy">
Dummy dummy dummy text<br>
<span id="sp-dum">one two three it's a dummy</span>
</p>
</div>
</div>
Write in style vertical-align:top is good way to display all the content in the top of the line fopr all columns

h1 element distance to border differs at top and bottom

CSS:
h1 {
width: 25%;
margin: 11rem auto;
font-family: 'Baron Neue';
font-size: 4vw;
color: white;
text-align: center;
border-top: solid 2px white;
border-bottom: solid 2px white;
}
HTML:
<h1>POTJESMARKT</h1>
The h1 tag has <body> </body> as it's parent.
The problem is that the border on the top isn't attached to h1, while there's no padding there. What I would like eventually is about a 1rem padding between h1 and the border, at the top and bottom. I could solve this by adding padding at the bottom of h1, but if I then resize my browser window, the distance from h1 to border isn't equal anymore on top and bottom.
Solved it in an 'ugly' way:
I added padding-bottom: 1vw;
This way, the padding scales together with h1, which makes it acceptable.
Seeing as (technically), it's working here:
html,body{background:red;}
h1 {
width: 25%;
margin: 11rem auto;
font-family: 'Baron Neue';
font-size: 4vw;
color: white;
text-align: center;
border-top: solid 2px white;
border-bottom: solid 2px white;
}
<h1>POTJESMARKT</h1>
In which since I haven't actually got your font style installed, I believe your problem is with the font itself. In order to 'fix' this you could;
A: Find out the font's baseline margin (in px) and add the extra 'top' padding to your h1 element
B: Use a different/more common font
C: 'Guess' by adding some padding-bottom to your element.
Just some points I would like to note:
Ensure you import your font (so that everyone can 'use' it (i.e. google fonts))
I wouldn't really be that keen on using vw and vh unless actually needed
Again, personal preference would stop me from using 'rem' values, and either stick to em, px, or otherwise.
Here is a semantic answer:Header fiddle
html:
<h1><strong>Potjes market</strong><h1>
css:
h1 {
font-size:4em;
border:3px solid blacK;
}
h1 > strong {
position:relative;
bottom:0.1em;
}`
This pushes the text slightly up so that the border looks even above and below

Eliminate intrinsic padding/margin on <div> element

I'm working on a personal project, but I'm having some difficulty with a div, which has some styling that I can't seem to get around. It's a thin strip at the top of my user interface, where users have a few controls over what's shown on the screen. Pretty important to keep around (so deleting it isn't an option). In case it helps at all, I am using Eric Meyer's CSS Reset as a normalizer.
My problem is that the div element seems to have some intrinsic margin or padding that I can't seem to work around in my css. I've included a photo here for reference; the div is in green.
I need to make that green div element thinner. It would help the layout a lot if I could move it closer to the top of the page. If you have any ideas or see something that I've missed, I would appreciate the help.
I'm also including the html code for that content as follows:
<div class="new_entry_control_container">
<p>You have <span class="credits">33 Credits</span> remaining.
<span class="button">Add More Credits</span>
<span class="button">Add More Items to Study List</span>
<span class="pagination">< 1 | 2 | 3 ></span>
</p>
</div>
As well as the CSS that applies here:
div.new_entry_control_container {
background-color: green;
max-width: 900px;
margin-left: auto;
margin-right: auto;}
div.new_entry_control_container p {
text-align: center;}
.credits {
color: #ffd400;}
.button {
background-color: #ffd400;
color: #3a0091;
border: none;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
padding: 1px 8px 4px 8px;
margin: 10px 0px 0px 3px;}
.pagination {
margin-left: 25px;
font-size: 17px;}
Not sure if it's caused by the padding of parent element of that green bar. A workaround would be using negative "margin-top". And to make it thinner (assuming there would only be one line in that bar), use "height" combined with "line-height".
So the css might look like this
div.new_entry_control_container {
background-color: green;
max-width: 900px;
margin-left: auto;
margin-right: auto;
margin-top: -10px;
height: 18px; line-height: 18px;
}
Hope that helps.
Try:
div.new_entry_control_container{
padding:0;
/* more CSS here */
}
.credits{
padding:0; margin:0;
/* other CSS here */
}

How to prevent a span from breaking into the next line - responsive webdesign?

Following simple list, where in every h4, there is a span at the end.
<ul class="items">
<li>
<h4>Prevent LineBreakOfPlus <span class="goto">o</span>
</h4>
</li>
<li>
<h4>Digital Signage <span class="goto">o</span></h4>
… 
</ul>
Screenshot of the page's source:
The CSS for the span looks like this …
.items .goto {
font-family: 'QuaySans-Icons';
font-size: 1.6em;
position: relative;
float: right;
}
The final thing looks like this:
The problem I have with this is that when decreasing the width of the browser window (I'm working on a responsive webdesign) the span-icon is breaking into the next line.
Do you have any creative solution or idea on how to prevent this from happening?
Kind regards and thank you in advance,
Matt
If you want the icon to keep inline with the last word in your text line, you can simply do:
<ul class="items">
<li>
<h4>Prevent LineBreakOfPlus<span class="goto">o</span></h4>
</li>
<li>
<h4>Digital Signage<span class="goto">o</span></h4>
</li>
</ul>
and the CSS might be:
.items {
list-style: none;
margin: 0;
padding: 0;
}
.items li {
border-bottom: 1px solid gray;
margin: 0;
padding: 0;
}
.items h4 {
margin: 0;
}
.items .goto {
background-color: gray;
font-size: 1.6em;
margin-left: 10px; /* optional */
}
If there is no white space between your work and the span, the motif will simply follow the word if the li element is forced to flow into a second line.
You can use margin-left to create visual spacing or insert a &nbsp entity before the span, quite a few ways to do. The details depend a bit on what effect you want.
Fiddle: http://jsfiddle.net/audetwebdesign/VsBet/ (two examples of how to do it)
Keeping Icon Right Justified
Here is one approach to pinning the icon to the right of the h4 element:
.ex2.items h4 {
position: relative;
line-height: 1.5;
outline: 1px dotted blue;
padding-right: 2.00em;
}
.ex2.items .goto {
background-color: wheat;
line-height: 1.00;
font-size: 1.6em;
position: absolute;
right: 0;
bottom: 0.0em;
height: 1.00em;
width: 1.00em;
outline: 1px dotted red;
}
Use absolute positioning of the span to keep it to the right and bottom of h4. If h4 forms to line, the icon will follow the second line. You may need to adjust the positioning depending on the icon size. If you allow the icon to grow in size, you may get other issue in extreme cases. I might fix the icon to a px height or width (or a max value). Finally, set some padding-right in h4 to prevent the icon from overlapping the text as the window gets smaller.
Note I explicitly specified line-height values to accentuate the issue around not knowing the height of the icon. You may need to adjust these to vertically position the icon.
Decrease your font-size when you have less space. I guess you have the problem in media with max-width:480px. I found decreasing the font-size a good alternative to keep the design consistent in responsive sites
I've mocked it up on the demo, however it is a bit raw.
.items {
padding:0;
margin:0;
/*width:180px;*/
}
.items li {
border: 1px solid red;
list-style-type: none;
position: relative;
}
.items h4 {
margin:0; padding:0; font-size:16px; padding-right:10px;
}
.items .goto {
margin-top: -10px;
position: absolute;
right: 0;
top: 50%;
}
DEMO
Check the following link and decrease the width of browser.
RESULT

Reduce white space between lines of text

I am creating a webpage (first time) and i'm following as much of the CSS rules and tags as I can. However, I ran into a problem with white space. I've underlined the first line of text but now the second line seems to have drifted below. Is there a way to make it a bit more snug, i'd like the second line of text to be just below the above line.
body,td,th {
color: #000000;
}
body {
margin: 0;
padding: 0;
padding-top: 6px;
text-align: center;
background-color: #FFFFFF;
}
#centered
{
width: 800px; /* set to desired width in px or percent */
text-align: left; /* optionally you could use "justified" */
border: 0px; /* Changing this value will add lines around the centered area */
padding: 0;
margin: 0 auto;
}
.style3 {
font-size: 32pt;
color: #666666;
margin-left: 0px;
border-bottom: 3px double;
}
.style5 {
margin-left: 390px;
font-size: 32pt;
color: #CCCCCC;
}
-->
</style></head>
<div id="centered">
<body>
<p class="style3"> FIRST LINE OF TEXT</p>
<p class="style5">INDENTED SECOND LINE</p>
</body>
</div>
</body>
</html>
You need to adjust the line-height. More specifically, add the following declaration:
.style5 {
line-height: 0.72em;
}
If you only want the first line of .style5 to be snug, you need to adjust the top margin. Use this declaration instead:
.style5 {
margin-top: -10px;
}
See fiddle.
Note: You should always validate your markup using the W3C Markup Validation Service and your css using the W3C CSS Validation Service. It will help you a lot when you're starting out.
p.style3, p.style5 {
margin-top: 2px;
margin-bottom: 2px;
}
Play with those two values until you are happy with the result :)
Have you tried the CSS line-height rule?
http://www.w3schools.com/css/pr_dim_line-height.asp
hmm. your code little buggy. first i see that you have div OUTSIDE of body tag.
try to validate your code.
anyway you can change the space weebven lines in the same paragraph with : p {line-height:0.7em} this creates a 7/10 line height of the font size.
if you want to decrease space between paragrapsh you shold change the margin|padding of the paragraphs. p{margin:0 91px 0 37px;padding:0 43px 0 19px}