I'm building a template for headlines that later on will have varying length. Before the headline is an icon. Why does the headline break, according to the icon width?
js fiddle
HTML
<img src="http://www.ecotricity.ch/pix/slideBanner/carLogo.gif" width="50"/>
<div>
<img src="http://png-1.findicons.com/files/icons/1241/twitter_land/256/school_of_fish.png" width="50"/>
<h3> The cars are faster than fish</h3>
</div>
CSS
div{
float: left;
margin-left: 30px;
}
img{
float: left;
}
It's because you're not clearing the float after the img element within the div.
You have two options. You can either use CSS to remove the float from that img element:
div img {
float: none;
}
JSFiddle demo 1.
Or add a clearing element within your div:
<img src="..." width="50"/>
<br style="clear:both"/>
<h3> The cars are faster than fish</h3>
JSFiddle demo 2.
If you want the h3 element to be alongside the img element, you can alter option 1 above to:
div img,
div h3 {
display: inline-block;
float: none;
}
JSFiddle demo 3.
Replace your div style with:
div{
float: left;
margin-left: 30px;
width:300px;
}
//increase the width of div
If your question is "Why does H1 have a line break" the answer is because it is a block level element; however, if you are asking how to accomplish something like having all the elements in a vertical line, you can do this: http://jsfiddle.net/rM6G9/7/
div{
float: left;
clear:both;
}
img{
float: left;
display: block;
}
h3{
float:left;
clear:both;
}
If you want the second image and the headline to be inline do this:
div{
float: left;
clear:both;
}
img{
float: left;
display: block;
}
h3{
float:left;
}
Related
I have a div. It is 100% width and 150 pixels tall. I nested an <h1> tag in it, and it sits under an image instead of next to it.
<body>
<div class='topbar'>
<img src='img source is here'/>
<h1>
GO COSMOS!!!
</h1>
</div>
</body>
CSS:
body {
background-color: #aaffaa;
}
.topbar {
width: 100%;
height: 150px;
background-color: #00bb00;
}
img {
height: 150px;
width: 150px;
}
h1 {
}
A heading (<h1>,<h2>,etc) is a block level element:
A block-level element occupies the entire space of its parent element (container), thereby creating a "block." This article helps to explain what this means.Source: MDN Docs
Simply display the h1 inline-block like:
h1 {
display: inline-block;
vertical-align:top;
/*vertical-align aligns it at the top of the image, instead of the baseline*/
}
JSFiddle Example with your code
All header tags are block by default, meaning it spans the width 100%. If you want it side-by-side another element you need to change the display like so:
h1 {
display: inline;
}
Another option would be to float the two inside elements left. See fiddle: https://jsfiddle.net/DIRTY_SMITH/8gy5oprw/1/
img {
float: left;
height: 150px;
width: 150px;
}
h1 {
float: left;
}
I've tried several properties like 'vertical-align' but I cant align the image with center of paragraph
HTML:
<p><img id="imgPhone" src="phone.png">00244 913 513 199</p>
CSS:
img {
float:left;
width:50px;
height:50px;
}
Demo
vertical-align doesn't work with floats.
Try this:
p,
img {
display: inline-block;
vertical-align: middle;
}
img {
width: 50px;
height: 500px;
}
In this case you can simple add:
p {
line-height: 50px;
}
you should wrap both elements in a div and use line-height. also an img tag inside a p tag is not exactly a good idea
http://jsfiddle.net/omegaiori/6zYeA/7/
I'd like to ask this question again as its previous incarnation was half a decade ago. We need not consider anything pre-IE9 for the purposes of this discussion:
I am trying to float two divs with different font-sizes. I can't find a way to align the text on the same baseline. Here is what I have been trying:
<div id="header">
<div id="left" style="float:left; font-size:40px;">BIG</div>
<div id="right" style="float:left;">SMALL</div>
</div>
I am struggling with this currently and the best solution I've found is magic offsets from inspection, and that's hardly robust. Inline-block has its own issues I'd prefer to avoid.
Edit:
http://jsfiddle.net/crw4r/10/
As you can see, floats align at the top, not at the baseline.
You could use display: table-cell instead of floats?
#header {
display: table;
width: 100%;
}
#header div {
display: table-cell;
}
#left {
font-size: 40px;
}
#right {
text-align: right;
}
Demo
Set the line-height to be the same on both.
http://jsfiddle.net/crw4r/6/
eg.
line-height: 42px;
or if this is not what you want...
you could use absolute positioning.
http://jsfiddle.net/crw4r/7/
or, you could set the line height on both and add margin to the top of the smaller one, so the sum of the line-height and top margin are the same on both text.
http://jsfiddle.net/crw4r/13/
With display: inline-block, the divs are automatically aligned on the baseline. To compensate for the float, you can use text-align
#left {
display: inline-block;
width: 50%;
font-size: 40px;
text-align: left;
}
#right {
display: inline-block;
width: 50%;
text-align: right;
}
See JSFiddle
If you need to account for white space, use width: 49% for one of the divs
JSFiddle
<div id="container">
<div class="left"><span>Big</span></div>
<div class="right"><span>Small</span></div>
</div>
#container{
width:100%;
margin:0px auto;
}
#container div{
position:relative;
height: 42px;
width: 100px;
}
#container div span{
position:absolute;
bottom:0;
right:0;
}
.left{
float:left !important; font-size:40px;
}
.right{
float:right !important;
}
Try below css and html
CSS
.header {
overflow: hidden;
width: 200px;
display:table;
}
.header > div{
display:table-row;
}
.header > div > div{
display:table-cell;
vertical-align:baseline;
width:50%;
}
.big {
text-decoration: underline;
font-size: 40px;
}
.small {
text-decoration: underline;
font-size: 12px;
}
HTML
<div class="header">
<div>
<div class="big">BIG</div>
<div class="small">SMALL</div>
</div>
</div>
Im trying to make a picture display inline with text on it's right, AND at the same time positioned in the middle of the vertical axis to fit in the navigation bar.
At the moment, I can only get it to display inline but it always verges to the bottom of the bar rather than the top.
HTML:
<div id='profileBar'>
<img src='karl.png' width=25 id='profileBarPic'/>
Damen
</div>
CSS:
#profileBar {
float: right;
padding-right: 10px;
}
#profileBarPic {
width: 25px;
display: inline;
float: left;
padding-right: 10px;
}
What it looks like:
Text next to an image? That's how images have always worked. You can get the desired affect with a lot less markup:
<div id='profileBar'>
<img src='http://placekitten.com/50/50' id='profileBarPic'/>
Damen
</div>
And CSS:
#profileBarPic {
vertical-align: middle;
}
Fiddle
A little explanation. img is basically an inline element to begin with, so adding float and display: inline is not necessary.
By default img aligns with the baseline of its container (the bottom edge of an image will align with the bottom of a text line it is next to). vertical-align changes where the img should align itself to (in this case, you wanted the middle).
Try uploading a fiddle next time, I think this should fix it though:
#profilebar {
float: right;
padding-right: 10px;
display: table-cell;
vertical-align: middle;
}
put your text into a span or something so that you can put a display:inline-block on both it and the <img>. This way, you won't need the float:left. Then apply a vertical-align:middle
so:
HTML:
<div id='profileBar'>
<img src='karl.png' width='25' id='profileBarPic'/>
<span>Damen</span>
</div>
CSS:
#profileBar {
float: right;
padding-right: 10px;
}
#profileBar span {display:inline-block; vertical-align:middle; }
#profileBarPic {
width: 25px;
display: inline-block;
vertical-align:middle;
padding-right: 10px;
}
I'm trying to alignt 3 text links - one to the left, one to the center, and one to the right, but they won't align correctly.
Here's the html code:
<div class="navi">
Link 1
Link 2
Link 3
</div>
The relevant CSS is:
.navi {
width: 100%;
}
.text-left {
display: inline
text-align: left;
}
.text-right {
display: inline
text-align: right;
}
.text-center {
display: inline
text-align: center;
}
What am I doing wrong?
Thanks!
That's not how text-alignment works. <a> tags are display:inline by default so you don't need to assign that value. You also need to assign the text-align to the parent container.
This CSS should do the trick for you:
.text-left {
float:left;
}
.text-right {
float:right;
}
.navi {
text-align:center;
}
Working example
You could do it this way...
HTML
<div class="navi">
Link 1
Link 3
Link 2
</div>
CSS
.navi {
width: 100%; /* Probably no need for this */
}
.text-left {
float: left;
}
.text-right {
float: right;
}
.text-center {
display: block;
text-align: center;
}
jsFiddle.
You may also want to use more semantic class names. They should describe the element's content or meaning, not the presentation.
You are using the text align property, which will align the text inside the link and not the link itself.
You can put a div with 100% as the width, and put these three links in that div with the property
float :left
float: right
i.e
<div width="100%" style="float:left">
<a href='' class='float-left'/>
<a href='' class='float-center'/>
<a href='' class='float-right'/>
</div>
.float-left{
float:left;
text-align :left;
}
.float-left{
float:right;
text-align :right;
width: 33%;
}
.float-center {
display: inline
text-align: center;
}
text-align is the text alignment inside the element, not the alignment of the elements, so each of the inlines elements is just the width of the text, and use the alignment of .navi
try changing them to inline block, and specifying widths i.e. 33%
.navi {
width: 100%;
}
.text-left {
display: inline-block;
text-align: left;
width: 33%;
}
.text-right {
display: inline-block;
text-align: right;
width: 33%;
}
.text-center {
display: inline-block;
text-align: center;
width: 33%;
}