I am trying to achieve this
the way I see it and would be tempted to do it is the following
but this is a lot of div and css
<div class="route-information-container">
<div class="route-information-container__header">
<div class="route-information-container__name">DRONE ID ID </div>
<div class="route-information-container__battery"><mat-icon svgIcon="battery-90"></mat-icon></div>
</div>
<div class="route-information-container__drone-info">
<img class="route-information-container__drone-logo" src="./assets/images/drones/drone_front.jpg" />
<div class="route-information-container__drone-name">DroneID</div>
<div class="route-information-container__drone-camera">CameraID</div>
</div>
</div>
most of my HTML structure are always similar to this. I tend to put boxes everywhere, and I was wondering, is this a bad practice? what would be another way of doing it ?
As per the standard practice the structure is perfect.
You can also achieve the above structure using the below Code:
https://jsfiddle.net/ulric_469/m5dvx2q7/7/
<div class="route-information-container__header">
<div class="route-information-container__name">DRONE ID ID </div>
<div class="route-information-container__battery">
<mat-icon svgIcon="<battery-></battery->90">Right Section</mat-icon>
</div>
</div>
<img class="route-information-container__drone-logo" src="./assets/images/drones/drone_front.jpg" width="50px"/>
<div class= "bottom-section">
<div class="route-information-container__drone-name">DroneID</div>
<div class="route-information-container__drone-camera">CameraID</div>
</div>
.route-information-container__header {
display: flex;
border: 1px solid black;
justify-content: space-between;
}
.bottom-section {
display: inline-block;
vertical-align: top;
}
Your structure is not incorrect and there will never be a problem.
But you can use css selectors to make clean structure in your html tags.
For example you can use this in your css file :
.route-information-container div:first-child {text-decoration: underline;}
.route-information-container div:last-child {color: red;}
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 have in my layout three elements like this:
<div id="container">
<div id="element1"/>
<div id="element2"/>
<div id="element3"/>
</div>
shown this way:
| element1 | element2 | element3 |
I want them to show like this:
element1 | element2
element3 |
The closest thing I've achieved to do is this:
element1 |
element3 | element2
I can't achieve to align element1 and element2
Does anybody knows how to do it only with CSS ?
Here is the working example
<div id="container">
<div id="element1" class="boxes">
This is elem1
</div>
<div id="element2" class="boxes">
This is the elem2
</div>
<div id="element3">
This is elem3
</div>
</div>
<style>
.boxes{
border:1px solid black;
box-sizing:border-box;
width:50%;
float:left;
}
</style>
This is how you use div tags :
<div class="exampleclass">Example Text</div>
An example made by Sören Kuklau can be seen here
Here is an example Fiddle using the float: left css property
Your question is quite open-ended (no context whatsoever), there's a very simple solution, which is to use float: left...
#element1, #element2, #element3 {
float: left;
width: 50%;
box-sizing: border-box; // <- Not necessary for this basic example unless you add padding, etc.
}
<div id="container">
<div id="element1">El 1</div>
<div id="element2">El 2</div>
<div id="element3">El 3</div>
</div>
define them all as float: left and add clear: both to the third one.
<div id="container">
<div id="element1">El 1</div>
<div id="element2">El 2</div>
<div id="element3">El 3</div>
</div>
#container > div {
float: left;
}
#container{
position:relative;
}
#element3{
bottom: -40px;
display: block;
position: absolute;
}
In my explaination the #container #element3 and #container > div are ids of div so please dont read as comment thanks
You can't do it only using CSS because CSS can't do real DOM modifications for that you have to use jQuery.
I hope this link can help you:
What is the easiest way to order a <UL>/<OL> in jQuery?
I'd like to have all surnames on the second line AND maintain the exact same width for test div. What is the best way of achieving this with CSS?
HTML:
<div class="test">
<h1>Mike S</h1>
</div>
<div class="test">
<h1>Mike Smith</h1>
</div>
<div class="test">
<h1>Mike Smiths</h1>
</div>
CSS:
.test {width:25%;float:left;background:red;margin-right:20px}
h1 {text-align:center}
http://jsfiddle.net/zcg9k5xh/
Update your code with this:
.test {width:25%;float:left;background:red;margin-right:20px}
h1 {text-align:center}
h1 span{display: block;}
<div class="test">
<h1>Mike <span>S</span></h1>
</div>
<div class="test">
<h1>Mike <span>Smith</span></h1>
</div>
<div class="test">
<h1>Mike <span>Smiths</span></h1>
</div>
You can also do this by using css, update above css
h1 span{display: list-item;list-style:none;}
jsfiddle with this
http://jsfiddle.net/zcg9k5xh/2/
Given that it seems you are willing to change your HTML, I would recommend you simply add <br> after the first name, instead of wrapping the last name in any other tags. This would be deemed best practice.
The HTML <br> Element (or HTML Line Break Element) produces a line
break in text
This will give more semantic HTML- without the need to adjust native element styling, or clutter your DOM with uneccessary nodes.
.test {
width: 25%;
float: left;
background: red;
margin-right: 20px
}
h1 {
text-align: center
}
<div class="test">
<h1>Mike<br>S</h1>
</div>
<div class="test">
<h1>Mike<br>Smith</h1>
</div>
<div class="test">
<h1>Mike<br>Smiths</h1>
</div>
Use the word-spacing attribute to the child tag:
.test {
width: 25%;
float: left;
background: red;
margin-right: 20px
}
h1 {
background-color: blue;
word-spacing: 100px;
}
<div class="test">
<h1>Mike S</h1>
</div>
<div class="test">
<h1>Mike Smith</h1>
</div>
<div class="test">
<h1>Mike Smiths</h1>
</div>
I don't see what you are asking, it seems like the jsfiddle is what you are asking here.
But you can always set width to 100% so it cover for the text, if you want all that text in the same div then put it all under one Div tag.
Is this what you want?
.test {width:25%;float:left;background:red;margin-right:20px}
h1 {text-align:center}
<div class="test">
<h1>Mike</h1>
<h1>S</h1>
</div>
<div class="test">
<h1>Mike</h1>
<h1>Smith</h1>
</div>
<div class="test">
<h1>Mike</h1>
<h1>Smiths</h1>
</div>
The following code blends both DIVs together instead of displaying them vertically, one after another. Both are set to display: block and I'd like to know which style attribute makes them behave like this?! See demo on jsfiddle.
<div data-role="page">
<div data-role="content">
<div>
Do something
<span href="#" style="float: right; border-radius: 10px; background-color: white; padding: .6em">Something</span>
</div>
<div>
<ul data-role="listview">
<li>Foo list entry</li>
</ul>
</div>
</div>
</div>
After using the floating elements you have to close them with element containing clear:both style, so the part of the code will be:
<div>
Do something
<span href="#" style="float: right; border-radius: 10px; background-color: white; padding: .6em">Something</span>
<div style="clear:both"></div>
</div>
Here is the working JSFiddle (with a little bit of margin modification as well): http://jsfiddle.net/Cw86p/9/