Given this HTML:
<div>foo</div><div>bar</div><div>baz</div>
How do you make them display inline like this:
foo bar baz
not like this:
foo
bar
baz
An inline div is a freak of the web & should be beaten until it becomes a span (at least 9 times out of 10)...
<span>foo</span>
<span>bar</span>
<span>baz</span>
...answers the original question...
That's something else then:
div.inline { float:left; }
.clearBoth { clear:both; }
<div class="inline">1<br />2<br />3</div>
<div class="inline">1<br />2<br />3</div>
<div class="inline">1<br />2<br />3</div>
<br class="clearBoth" /><!-- you may or may not need this -->
Try writing it like this:
div { border: 1px solid #CCC; }
<div style="display: inline">a</div>
<div style="display: inline">b</div>
<div style="display: inline">c</div>
Having read this question and the answers a couple of times, all I can do is assume that there's been quite a bit of editing going on, and my suspicion is that you've been given the incorrect answer based on not providing enough information. My clue comes from the use of br tag.
Apologies to Darryl. I read class="inline" as style="display: inline". You have the right answer, even if you do use semantically questionable class names ;-)
The miss use of br to provide structural layout rather than for textual layout is far too prevalent for my liking.
If you're wanting to put more than inline elements inside those divs then you should be floating those divs rather than making them inline.
Floated divs:
===== ======= == **** ***** ****** +++++ ++++
===== ==== ===== ******** ***** ** ++ +++++++
=== ======== === ******* **** ****
===== ==== ===== +++++++ ++
====== == ======
Inline divs:
====== ==== ===== ===== == ==== *** ******* ***** *****
**** ++++ +++ ++ ++++ ++ +++++++ +++ ++++
If you're after the former, then this is your solution and lose those br tags:
<div style="float: left;" >
<p>block level content or <span>inline content</span>.</p>
<p>block level content or <span>inline content</span>.</p>
</div>
<div style="float: left;" >
<p>block level content or <span>inline content</span>.</p>
<p>block level content or <span>inline content</span>.</p>
</div>
<div style="float: left;" >
<p>block level content or <span>inline content</span>.</p>
<p>block level content or <span>inline content</span>.</p>
</div>
note that the width of these divs is fluid, so feel free to put widths on them if you want to control the behavior.
Thanks,
Steve
Use display:inline-block with a margin and media query for IE6/7:
<html>
<head>
<style>
div { display:inline-block; }
/* IE6-7 */
#media,
{
div { display: inline; margin-right:10px; }
}
</style>
</head>
<div>foo</div>
<div>bar</div>
<div>baz</div>
</html>
You should use <span> instead of <div> for correct way of
inline. because div is a block level element, and your requirement is for inline-block level elements.
Here is html code as per your requirements :
<div class="main-div">
<div>foo</div>
<div>bar</div>
<div>baz</div>`
</div>
You've two way to do this
using simple display:inline-block;
or using float:left;
so you've to change display property display:inline-block; forcefully
Example one
div {
display: inline-block;
}
Example two
div {
float: left;
}
you need to clear float
.main-div:after {
content: "";
clear: both;
display: table;
}
As mentioned, display:inline is probably what you want. Some browsers also support inline-blocks.
http://www.quirksmode.org/css/display.html#inlineblock
Just use a wrapper div with "float: left" and put boxes inside also containing float: left:
CSS:
wrapperline{
width: 300px;
float: left;
height: 60px;
background-color:#CCCCCC;}
.boxinside{
width: 50px;
float: left;
height: 50px;
margin: 5px;
background-color:#9C0;
float:left;}
HTML:
<div class="wrapperline">
<div class="boxinside">Box 1</div>
<div class="boxinside">Box 1</div>
<div class="boxinside">Box 1</div>
<div class="boxinside">Box 1</div>
<div class="boxinside">Box 1</div>
</div>
ok, for me :
<style type="text/css">
div{
position: relative;
display: inline-block;
width:25px;
height:25px;
}
</style>
<div>toto</div>
<div>toto</div>
<div>toto</div>
<span> ?
<style type="text/css">
div.inline { display:inline; }
</style>
<div class="inline">a</div>
<div class="inline">b</div>
<div class="inline">c</div>
I know people say this is a terrible idea, but it can in practice be useful if you want to do something like tile images with comments underneath them. e.g. Picasaweb uses it to display the thumbnails in an album.
See for example/demo http://closure-library.googlecode.com/svn/trunk/closure/goog/demos/inline_block_quirks.html ( class goog-inline-block ; I abbreviate it to ib here )
/* below is a set of hacks to make inline-block work right on divs in IE. */
html > body .ib { display:inline-block; }
.ib {display:inline-block;position:relative;}
* html .ib { display: inline; }
:first-child + html .ib { display:inline; }
Given that CSS, set your div to class ib, and now it's magically an inline block element.
I would use spans or float the div left. The only problem with floating is that you have to clear the float afterwards or the containing div must have the overflow style set to auto
You need to contain the three divs. Here is an example:
CSS
div.contain
{
margin:3%;
border: none;
height: auto;
width: auto;
float: left;
}
div.contain div
{
display:inline;
width:200px;
height:300px;
padding: 15px;
margin: auto;
border:1px solid red;
background-color:#fffff7;
-moz-border-radius:25px; /* Firefox */
border-radius:25px;
}
Note: border-radius attributes are optional and only work in CSS3 compliant browsers.
HTML
<div class="contain">
<div>Foo</div>
</div>
<div class="contain">
<div>Bar</div>
</div>
<div class="contain">
<div>Baz</div>
</div>
Note that the divs 'foo' 'bar' and 'baz' are each held within the 'contain' div.
I think you can use this way without using any CSS -
<table>
<tr>
<td>foo</td>
</tr>
<tr>
<td>bar</td>
</tr>
<tr>
<td>baz</td>
</tr>
</table>
Right now you are using block-level elements that way you are getting an unwanted result. So you can you inline elements like span, small etc.
<span>foo</span><span>bar</span><span>baz</span>
This is what worked for me. I was working with bootstrap and I wanted to have radio buttons inline:
<div class="form-group form-inline-radio">
<div class="form-check form-radio-outline form-radio-primary mb-3">
<input type="radio" name="formRadio4" id="formRadio4" checked="" class="form-check-input">
<label for="formRadio4" class="form-check-label"> Radio Outline Warning </label>
</div>
<div class="form-check form-radio-outline form-radio-primary mb-3">
<input type="radio" name="formRadio4" id="formRadio4" checked="" class="form-check-input">
<label for="formRadio4" class="form-check-label"> Radio Outline Warning </label>
</div>
</div>
And the CSS:
.form-inline-radio {
display: flex;
overflow: hidden;
}
.form-check {
margin-right: 10px;
}
we can do this like
.left {
float:left;
margin:3px;
}
<div class="left">foo</div>
<div class="left">bar</div>
<div class="left">baz</div>
<div class="cdiv">
<div class="inline"><p>para 1</p></div>
<div class="inline">
<p>para 1</p>
<span>para 2</span>
<h1>para 3</h1>
</div>
<div class="inline"><p>para 1</p></div>
http://jsfiddle.net/f8L0y5wx/
<div>foo</div><div>bar</div><div>baz</div>
//solution 1
<style>
#div01, #div02, #div03 {
float:left;
width:2%;
}
</style>
<div id="div01">foo</div><div id="div02">bar</div><div id="div03">baz</div>
//solution 2
<style>
#div01, #div02, #div03 {
display:inline;
padding-left:5px;
}
</style>
<div id="div01">foo</div><div id="div02">bar</div><div id="div03">baz</div>
/* I think this would help but if you have any other thoughts just let me knw kk */
I just tend to make them fixed widths so that they add up to the total width of the page - probably only works if you are using a fixed width page. Also "float".
I want Text Left and Text right should come in same line and I can't restructure HTML.And if I use Absolute position I am not sure how it will behave in different devices and screen size due to yellow area(). Can I give absolute position with reference to parent div(ms-srch-result)
#UpScopeLinkTop{
display: block;
float:right;
}
<div class="ms-srch-result" id="Result" name="Control">
<div id="UpScopeLinkTop" class="ms-srch-upscope-top" style="display:block;width: 700px;">
Text Right
</div>
<div id="ResultCount" class="ms-srch-resultscount">
Text Left
</div>
</div>
Solution 1: (remove inline styles)
#UpScopeLinkTop{
display: block;
float:right;
}
<div class="ms-srch-result" id="Result" name="Control">
<div id="UpScopeLinkTop" class="ms-srch-upscope-top">
Text Right
</div>
<div id="ResultCount" class="ms-srch-resultscount">
Text Left
</div>
</div>
Solution 2: (flex-box)
.ms-srch-result{
display: flex;
flex-direction: row-reverse;
width: 200px;
}
.ms-srch-result div{
margin: 10px;
}
<div class="ms-srch-result" id="Result" name="Control">
<div id="UpScopeLinkTop" class="ms-srch-upscope-top">
Text Right
</div>
<div id="ResultCount" class="ms-srch-resultscount">
Text Left
</div>
</div>
Solution 3: (flex property)
.ms-srch-result{
display: flex;
flex-direction: row-reverse;
}
.ms-srch-result div{
flex: 1;
}
<div class="ms-srch-result" id="Result" name="Control">
<div id="UpScopeLinkTop" class="ms-srch-upscope-top">
Text Right
</div>
<div id="ResultCount" class="ms-srch-resultscount">
Text Left
</div>
</div>
Use something like this in your CSS stylesheet:
#UpScopeLinkTop, #ResultCount {
width: 45%;
}
(Adjust the value as you need it)
P.S.: erase the 700px width from the HTML tag of #UpScopeLinkTop
You don't need floats. Instead of divs inside the big div, use spans.
<div id="thebigdiv">
<span style="display:inline-block";>
Text left
</span>
<span style="display:inline-block";>
Text right
</span>
</div>
I'm trying to align two divs horizontally inside my HTML: the first one contains an image and the second a text, and this is the used code:
<div style="float: left; width: 55px;">
<img src="../img/look.svg" alt="">
</div>
<div style="display: inline-block;">
<span> Look for cases </span>
<span> from people near you. </span>
</div>
I tried many methods, but I've been unable to get the text line in the same level as the image vertical axis, and the text inside the second div gets displayed very far from the image vertically.
So, is there any possibility to fix that?
The problem is with the float. The vertical-align: middle; line-height: 1; will fix the issue:
div {
display: inline-block;
vertical-align: top;
line-height: 1;
}
div:first-child {
width: 55px;
}
<div>
<img src="//placehold.it/50?text=DP" alt="">
</div>
<div style="display: inline-block; vertical-align: middle; line-height: 1;">
<span> Look for cases </span>
<span> from people near you. </span>
</div>
Preview
Top Alignment:
Middle Alignment:
The vertical-align decides the vertical alignment. If you want the image and text to be on the same line, use vertical-align: top.
A few things first:
don't use inline styles
don't mix float with inline-block
Option 1: using flebox
section {
display: flex;
gap: 10px;
}
<section>
<div>
<img src="//dummyimage.com/55x55" alt="">
</div>
<div>
<span> Look for cases </span>
<span> from people near you. </span>
</div>
</section>
Option #2 using inline-block
div {
display:inline-block;
vertical-align:top;
margin-right:10px
}
<div>
<img src="//dummyimage.com/55x55" alt="">
</div>
<div>
<span> Look for cases </span>
<span> from people near you. </span>
</div>
Option #3 using float
div {
float: left;
margin-right:10px
}
<div>
<img src="//dummyimage.com/55x55" alt="">
</div>
<div>
<span> Look for cases </span>
<span> from people near you. </span>
</div>
flexbox to the rescue.
I added your divs inside another one, which will align its items. In my CSS my image has 100px so I changed the width to 100px. Change yours accordingly.
.halign {
display:flex;
align-items: center;
}
<div class="halign">
<div style="width: 100px;">
<img src="http://lorempixel.com/100/100/" alt="">
</div>
<div>
<span> Look for cases </span>
<span> from people near you. </span>
</div>
</div>
Try to seprate the CSS and HTML and do not mix display:inline-block with float:left. Also use clear:both after both div
<style>
.fisrstDiv {
float: left;
display:block;
}
.secondDiv {
float: left;
display:block;
}
.clear {
clear: both;
}
</style>
HTML
<div class="fisrstDiv">
<img src="//placehold.it/50?text=DP" alt="">
</div>
<div class="secondDiv">
<span> Look for cases </span>
<span> from people near you. </span>
</div>
<div class="clear"></div>
I am using the following code for text-align:
<strong>Status:</strong> <span style="color: #01DF3A;">Updated</span>
<span align="right" style="text-align: right;"><strong>TeamSpeak:</strong> ts.abendigo.org</span>
The first text Status: Updated should be on the left but the second part of the text TeamSpeak: ts.abendigo.org should be on the right side but using even both the deprecated align="right" with style="text-align: right;" seems to have no effect with span. They work fine with other tags like div but I want to keep both text on the same line.
<span> is an inline element. From the screenshot below you can see that its width is 188.859px and that's the size of the text in it.
You must wrap the inline elements in a block element. I'd suggest this:
.status {
float: left;
}
.teamspeak {
float: right;
}
<div class="status">
<strong>Status:</strong><span style="color: #01DF3A;">Updated</span>
</div>
<div class="teamspeak">
<strong>TeamSpeak:</strong> ts.abendigo.org</span>
</div>
NB: this answer explains how block level vs inline elements work.
The text-align property only works on block elements. <span> is inline. You should use a <div> or <p>.
<strong>Status:</strong> <span style="color: #01DF3A;">Updated</span>
<div style="text-align: right;">
<span><strong>TeamSpeak:</strong> ts.abendigo.org</span>
</div>
NB: You can set span to be a block element, but unless your HTML is fixed (generated by some other application) and you cannot change it, don't do that. Better keep to what is standard and use div or p.
span { display: block; }
To get a working solution you should use float: right; on the span. I don't see why you would need to use a float:left; on the other text.
HTML
<div class="container"> <strong>Status:</strong>
<span class="left">Updated</span>
<span class="right">
<strong>TeamSpeak:</strong> ts.abendigo.org</span>
</div>
CSS
.left {
text-align:left;
color: #01DF3A
}
.right {
float:right;
}
You can use this
<div>
<strong>Status: </strong><span style="color: #01DF3A;">Updated</span>
<span style="float:right">TeamSpeak: ts.abendigo.org</span>
</div>
The easiest way is to use blocks or a table where is text-align property works on:
<strong>Status:</strong> <span style="color: #01DF3A;display:inline-block;width:45%">Updated</span><span align="right" style="text-align: right;display:inline-block;width:45%"><strong>TeamSpeak:</strong> ts.abendigo.org</span>
<table width="100%">
<tr>
<td><strong>Status:</strong> <span style="color: #01DF3A">Updated</span></td>
<td style="text-align:right"><span align="right" style="text-align:right"><strong>TeamSpeak:</strong> ts.abendigo.org</span></td>
</tr>
</table>
Try the fiddle:
JSFiddle
Try the below link
.status{
float:left
}
.team{
float:right;
}
Fiddle - Link
html:
<div class="status">
<strong>Status:</strong><span style="color: #01DF3A;"> Updated</span>
</div>
<div class="teamspeak">
<strong>TeamSpeak:</strong> ts.abendigo.org</span>
</div>
Css:
.status {
float: left;
}
.teamspeak {
float: right;
}
Demo
I have the following html
<div class="post">
<h2 class="title">TITLE: Kausalya</h2>
<p class="content">CONTENT: Shaunaka Shakha</p>
</div>
<div class="post">
<h2 class="title">TITLE: Kausalya</h2>
<span class="author">AUTHOR: Ayuidasht</span>
<p class="content">CONTENT: Shaunaka Shakha</p>
</div>
I can do this with jQuery, but how can I do this with css?
$('.title').next('.content').css('margin-top','20px');
I just need a margin-top on the content if it comes after a title. i know I can do this with css but i forgot how and I cant figure out what it is called.
You are looking for the adjacent sibling combinator, +.
Example Here
.title + .content {
margin-top:20px;
}
h2 + p{
margin-top: 20px;
}
https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors