how to have two headings on the same line in html - html

I want to have two headings h2 and h3 on the same horizontal rule one on the left and the other on the right. They have a HR underneath them and I want them to be at the same distance from this HR.
I tried making them both inline and have one float right and the other left. The problem with doing so was with h3 as it is smaller than h2 vertically it was centered at half the h2's length.
h2 was kinda like sitting on the hr and h3 kinda looked like floating in mid air.
I kinda wanted them to be like both sitting on the hr.
h2{
display:inline;
float:left;
}
h3{
display:inline;
float:right;
}
I was talking about visually describing the situation.

You'd need to wrap the two headings in a div tag, and have that div tag use a style that does clear: both. e.g:
<div style="clear: both">
<h2 style="float: left">Heading 1</h2>
<h3 style="float: right">Heading 2</h3>
</div>
<hr />
Having the hr after the div tag will ensure that it is pushed beneath both headers.
Or something very similar to that. Hope this helps.

You should only need to do one of:
Make them both inline (or inline-block)
Set them to float left or right
You should be able to adjust the height, padding, or margin properties of the smaller heading to compensate for its positioning. I recommend setting both headings to have the same height.
See this live jsFiddle for an example.
(code of the jsFiddle):
CSS
h2 {
font-size: 50px;
}
h3 {
font-size: 30px;
}
h2, h3 {
width: 50%;
height: 60px;
margin: 0;
padding: 0;
display: inline;
}​
HTML
<h2>Big Heading</h2>
<h3>Small(er) Heading</h3>
<hr />​

Display property 'inline-block' will place both headers next to each other. You can run this code snippet to see it
<h1 style="display: inline-block" >Text 1</h1>
<h1 style="display: inline-block" >Text 2</h1>

The Css vertical-align property should help you out here:
vertical-align: bottom;
is what you need for your smaller header :)
Vertical-Align

Check my sample solution
<h5 style="float: left; width: 50%;">Employee: Employee Name</h5>
<h5 style="float: right; width: 50%; text-align: right;">Employee: Employee Name</h5>
This will divide your page into two and insert the two header elements to the right and left part equally.

The following code will allow you to have two headings on the same line, the first left-aligned and the second right-aligned, and has the added advantage of keeping both headings on the same baseline.
The HTML Part:
<h1 class="text-left-right">
<span class="left-text">Heading Goes Here</span>
<span class="byline">Byline here</span>
</h1>
And the CSS:
.text-left-right {
text-align: right;
position: relative;
}
.left-text {
left: 0;
position: absolute;
}
.byline {
font-size: 16px;
color: rgba(140, 140, 140, 1);
}

Add a span with the style="float: right" element inside the h1 element. So you can add a "goto top of the page" link, with a unicode arrow link button.
<h1 id="myAnchor">Headline Text
<span style="float: right">↥</span>
</h1>

Related

Set background image using css

Here i upload the picture, i want to put my image to the left side of Food and Travel text
.block-title h3 {
color: #151515;
font-family: 'Montserrat', sans-serif;
font-size: 36px;
font-weight: 700;
line-height: 1.4;
letter-spacing: -0.9px;
margin-bottom: 24px;
margin-top: 50px;
padding-bottom: 13px;
position: relative;
text-align: center;
}
<div class="col-lg-12">
<p>
<center><img src="#">
<div class="block-title">
<h3>Food & Travel</h3>
</div>
</center>
</p>
</div>
You just need to add that line of CSS
div.block-title { display: inline-block; }
<div class="col-lg-12">
<p>
<center><img src="#">
<div class="block-title">
<h3>Food & Travel</h3>
</div>
</center>
</p>
</div>
I would change your HTML a little bit:
<div class="col-lg-12">
<div class="block-title">
<img class="image" src="https://image.flaticon.com/icons/png/512/45/45260.png">
<h3 class="title">Food & Travel</h3>
</div>
</div>
Some observations about your HTML:
Since the creation of CSS, it is considered a bad practice to use styling elements inside HTML, like center. HTML should hold only content and CSS styles. center in HTML can be, in most cases, easily replaced by text-align: center in CSS;
Avoid giving styles to a tag (as you did with H3). It is always better to give a class for each individual element you want to style. For example, you can give a class to your image and to your header, as I did on the example above.
Float, as mentioned by some users here, is barely a good option. I would not recommend it.
I'd go for using Flexbox on the container (block-title). It is the better option and the most accurate.
Your container would be something like
.block-title {
display: flex;
justify-content: center;
align-items: center;
}
... and the magic is done!
Here is an example using flexbox:
https://codepen.io/annabranco/pen/mzEXGv
Another option if you are not comfortable with using Flebox yet, it's to give the H3 a display: inline. By default, all header force a line break (they have display: block). If you change it to display: inline you force the other elements to be displayed in the same line as your header.
In this case you would need to play around with vertical-align to find the exact spot where your text would be centered to the image.
.title {
display: inline;
(..)
}
.image {
vertical-align: -25px; //negative values go up and positive down.
}
Here is an another example, using inline:
https://codepen.io/annabranco/pen/yRJvQa

Center a div or span while between other spans

I have a div container in which there are already 2 spans: one floating left and one floating right. Now I want to add a third span that will be in the center of the div.
I've tried a ton of things, most promising tracks include things like this on the span that is supposed to be the center one:
position: absolute;
overflow: hidden;
display: inline-block;
margin-left: 0px;
margin-right: 0px;
I've even tried using the tags and coupled it with position:absolute which almost gives what I want except the centered text is on another line.
The left floating element has no styles to it as default is left floating
The right floating element has nothing except:
float:right;
The best way is to put text-align: center to the parent div:
div {
text-align: center;
}
div>span:first-child {
float: left;
}
div>span:nth-child(3) {
float: right;
}
<div>
<span>Left</span>
<span>Center</span>
<span>Right</span>
</div>
JSFiddle
Use auto margin left and right on an element you want to be centered:
margin-left:auto;
margin-right:auto;
If I correctly understood your problem, I have created an example fiddle http://jsfiddle.net/8e3au9ay/
Basically, in HTML, do something like following,
<div>
<span style="float: left;">span 1</span>
<span style="float: right;">span 2</span>
<span class="centered-span">span 3</span>
</div>
and in CSS,
div{
position: relative;
}
span.centered-span{
position: absolute;
left: 50%;
margin-left: -20.6px; //half of width
}
If I were you I would do this
Create Three spans with the same class
ex.
<span class="spanNext"> Span 1 </span>
<span class="spanNext"> Span 2 </span>
<span class="spanNext"> Span 3 </span>
Then I would use the following css on the class "spanNext"
.spanNext{
float: left;
width: 30%;
height: 50px;
}
This would set a global Class for all the three spans all using the same class which will always float left until end of page is reached since I am using percentage as a width.
Now If you want to add a margin to each span as a seperator / space between one span and another I would include the following
margin-left: 3px; //in the same css of class '.spanNext'
and to exclude the last div from having a margin left too You should include this in the css
.spanNext:last-child{
margin-left: 0px !important;
}
and if your are using SCSS / SASS {The Best in my Opinion}
.spanNext{
&:last-child{
margin-left: 0px !important;
}
}

Placing a text over an image with HTML and CSS

I have a div containing a title text and an image.
With the code below, the title is showing just above the image.
HTML code:
<div class="thumbnail">
<div class="text">
<center>Heading</center>
</div>
<div class="image">
<img src="sample.png">
</div>
</div>
I would like to align the title so that it will appear on the center (vertically and horizontally) of the image.
How can I achieve that using HTML and CSS?
You could remove the image tag and make the image be the background of the container div.
HTML
<div class="text">
Heading
</div>
CSS
.text {
background-image: url('sample.jpg');
text-align: center;
}
EDIT: I don't want to sell it as my perfect answer, but I realized I missed the vertical alignment, and as similar solutions have already been provided here in comments and answers, let me just provide you with a good source of info below. The point is that you could use vertical-align:middle if you used span or other inline element, but with div, you have to use other tricks like position:absolute and minus margins.
Source: http://phrogz.net/css/vertical-align/index.html
Your markup is mostly correct with the exception of using the center element and you do not need to wrap the img element in a div.
Here is some example markup:
<div class="thumbnail">
<h1>Heading</h1>
<img src="sample.png">
</div>
And its corresponding CSS:
.thumbnail {
position:relative;
}
.thumbnail h1 {
text-align:center;
position:absolute;top:50%;left:0;width:100%;
margin-top:-20px; /*make the negative top margin = half your h1 element height */
}
You could always use an element other than an h1 to hold your title. This just depends on your preference.
The following might help you.
HTML:
<div class="thumbnail">
<div class="text">
Heading
</div>
</div>
CSS:
.text {
background-image: url('http://cs616623.vk.me/v616623331/7991/vPKjXbo-c7o.jpg');
width: 320px;
height: 240px;
line-height: 240px;
vertical-align: middle;
text-align: center;
font-size: 48px;
}
Take into account that in this approach you would have to set manually the height and the width of your text element. Moreover, the height should be duplicated in the line-height in order for vertical alignment to work correctly.
You could test and change the code in the corresponding JSFiddle or just check the full-screen result.
I wouldn't recommend using lineheight to vertically align the text(as some answers suggest) solely because if the header is to long and spans over across two rows it would look terrible.
What I would do is to absolute position the heading and then use display: table-cell to vertical align it.
Note that to be able to use this solution you have to specify an height for the heading.
HTML
<div class="thumbnail">
<div class="text">
<h1>Heading</h1>
</div>
<div class="image">
<img src="http://placehold.it/350x250" />
</div>
</div>
CSS
.thumbnail{
position: relative;
display: inline-block;
}
.text{
position:absolute;
top: 0px;
left: 0px;
width: 350px;
}
.text h1{
height: 250px;
display: table-cell;
vertical-align: middle;
text-align: center;
width: 350px;
color: #fff;
}
JSfiddle here

Issue aligning this with CSS

My problem is quite simple, but I couldn't figure out how to do it.
I have a div and inside it, I display some information . basically, something like this:
title1: 20
title2: 30
I want the title to be aligned to the left, and the number to the right.
Here is how I did http://jsfiddle.net/MmLQL/34/ . As you can see, I have a line break between the number and the title (which I believe comes from the use of h tag). But the thing is even if I use a span tag which is supposed to display elements inline and does not force line break, I lose the text-align right/left option. Here is an exmaple : http://jsfiddle.net/MmLQL/35/
You should try this way with "float:":
.container {
width: 100%;
clear: both;
}
.title {
float:left ;
display: inline;
}
.number {
float: right;
}
<div >
<div class="container">
<div class="title">title:</div>
<div class="number">number </div>
</div>
<div class="container">
<div class="title">title:</div>
<div class="number">number </div>
</div>
</div>
I'd do this woth float param. Like this: http://jsfiddle.net/dan1410/MmLQL/38/
Try this, http://jsfiddle.net/MmLQL/36/,
HTML
<div >
<h3>number </h3>
<h2 >title: </h2>
</div>
<div >
<h3>number </h3>
<h2 >title: </h2>
</div>
<div >
<h3>number </h3>
<h2 >title: </h2>
</div>
<div >
<h3>number </h3>
<h2 >title: </h2>
</div>
CSS
h2 {text-align:left}
h3 {
text-align: right;
float:right;
}
You might have to use float-clear on the divs though, this should help, http://www.positioniseverything.net/easyclearing.html,
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
..and modify the divs as class="clearfix".
I think the following should work, using inline-block to adjust the layout of the headers, then a float on the left-aligned one to ensure it's nestled against the right one.
div { width: 100%; }
h2 {
width: 50%;
display: inline-block;
float: left;
}
h3 {
display: inline-block;
width: 50%;
text-align: right;
}
You can also use CSS table/table-cells
<div class="container">
<h2>title: The Title</h2>
<h3>number</h3>
</div>
CSS:
.container {
border: 1px solid gray;
display: table;
width: 400px; /* set to 100% if full width */
}
h2 {
text-align:left;
display: table-cell;
}
h3 {
text-align: right;
display: table-cell;
}
See demo http://jsfiddle.net/audetwebdesign/pcZaq/
This approach is useful if you need some control over vertical alignment.
In addition, the table-cells will always remain on a single line, unlike floats or inline-blocks that could wrap to a second line for small screen sizes.
The choice depends in part on how you want the layout to behave in a responsive manner.
Instead of all the hacky solutions provided in other answers, it looks like you want to align tabular data. In which case, you should use a table for that.
Display:table-cell actually only exists in CSS to give the actual element and it's children their styles. It should not be used to let non-table elements behave like table elements. At least, imho.
Float:left seems like an ok alternative, if you're only looking for aligning the lay-out of the elements.
If your data actually is tabular data, then use a table. It solves your problem and is more semantic at the same time.

Set inline-block divs to ignore line breaks

I have three divs that are side-by-side using display:inline-block.
If the divs are empty, they are at the same horizontal level.
As soon as I add <p> tags and some line breaks (<br/>) to the leftmost/first div, the rest are moved down.
If I put enough content in the second box, the third is moved down even more.
My HTML for the boxes:
<div class="main-box" id="about">
<h1>About</h1>
<p>This box has one paragraph of text, with line breaks</p>
</div>
<div id="login-container">
<div class="main-box" id="login">
<h1>Login</h1>
<p>Already a member? Sign in and see your stuff!</p>
</div>
<div class="main-box" id="signup">
<h1>Signup</h1>
<p>Create an account by filling out this form.</p>
</div>
</div>
The last two boxes are grouped in a div so that they "float" together.
My CSS:
div.main-box {
text-align: left;
display: inline-block;
border: 10px solid red;
margin: 20px;
padding: 25px;
border-radius: 50px;
width: 400px;
height: 400px;
}
div#login-container {
display: inline-block;
}
Just add:
vertical-align: top;
You can read about inline-block and some more details like IE7 fix and spacing in html code here.
Are you willing to use float: left instead of display: inline-block for the inside divs?
Another solution that I can think of is to create a class with float: left or display: table and applying it to the paragraph tags.