Code
<div style="width:10px;height:10px;border:1px solid #F00;background:red;"></div>
<p>Red</p>
<div style="width:10px;height:10px;border:1px solid #00F;background:blue;"></div>
<p>Blue</p>
In the above code I expect two square box followed by the color name in different line. But it gives the box in one line and the para in
another line. How to achieve this in the same line like
[] Red
[] Blue
For a quick fix just add this to your CSS:
p,div{
display: inline-block;
}
This way will change properties of all your div and p elements.
Usually you would assaign classes to elements so you can target them from one place and only target them.
Try the following solution:
div {
display:inline-block;
width:10px;
height:10px;
border:1px solid #F00;
margin-right:5px;
}
p {
display:inline;
}
p:after {
content:"\A";
white-space:pre;
}
<div style="background:red;"></div><p>Red</p>
<div style="background:blue;"></div><p>Blue</p>
Hint: I would wrap these items to avoid overwriting the CSS of other <p> and <div> items, like the following:
.legend div {
display:inline-block;
width:10px;
height:10px;
border:1px solid #F00;
margin-right:5px;
}
.legend p {
display:inline;
}
.legend p:after {
content:"\A";
white-space:pre;
}
<div class="legend">
<div style="background:red;"></div><p>Red</p>
<div style="background:blue;"></div><p>Blue</p>
</div>
<div>line #1 (with div).</div>
<p>line #2 (with p).</p>
you can achieve this in so many ways, here is one:
inline-block
div {
width: 10px;
height: 10px;
border: 1px solid #F00;
}
div:first-of-type {
background: red
}
div:nth-of-type(2) {
background: blue
}
div,
p {
display: inline-block
}
<div></div>
<p>Red</p>
<div></div>
<p>Blue</p>
using span (which is an inline element)
div {
width: 10px;
height: 10px;
border: 1px solid #F00;
}
div:first-of-type {
background: red
}
div:nth-of-type(2) {
background: blue
}
div {
display: inline-block
}
<div></div>
<span>Red</span>
<div></div>
<span>Blue</span>
using pseudo-element ::before
span {
position: relative;
padding-left:15px
}
span::before {
width: 10px;
height: 10px;
border: 1px solid #F00;
content: '';
position: absolute;
left: 0;
top:3px
}
span:first-of-type::before {
background: red
}
span:nth-of-type(2)::before {
background: blue
}
<span>Red</span>
<span>Blue</span>
Related
Let's say I have a <div> with 10 floating elements inside:
<div>
<div class=floatme>...</div>
<div class=floatme>...</div>
...(8 more)
</div>
Depending on the horizontal space available, some of them will wrap to consecutive lines.
Q: How can I, say, style the ones on the second line?
<style>
.floatme {
float: left;
}
.floatme:if-wrapped-to-nth-line(2) {
background: url("rainbows.png");
}
</style>
.app {
position: absolute;
top: 0;
bottom: 0;
left: 0;
background: #bbb;
animation: 2s ease-in-out infinite alternate woosh;
}
.app, button {
font-family: sans-serif;
}
.tabs {
border-bottom: 2px solid #039;
}
.tabs button {
background: #039;
color: white;
border: 0;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
.toolbar {
background: white;
}
.toolbar > div {
padding: 3px;
float: left;
border-right: 2px solid #bbb;
}
.toolbar > .right {
float: right;
border-right: 0;
}
.toolbar button {
border: 2px solid #777;
}
.toolbar button:hover {
background: #bbb;
}
.toolbar::after {
display: table;
content: '';
clear: both;
}
#keyframes woosh {
from {
width: 300px;
}
to {
width: 500px;
}
}
<div class=app>
<nav class=tabs>
<button>Tab 1</button>
</nav>
<nav class=toolbar>
<div>
<button>A</button>
<button>B</button>
</div>
<div class=right>
<button>Right</button>
<button>side</button>
</div>
<div>
<button>C</button>
Description
</div>
<div>
<button>D</button>
<button>E</button>
<button>F</button>
</div>
<div>
<strong>Q</strong>: How to style second line?
<button>G</button>
</div>
<div>
<button>H</button>
<button>I</button>
Etc etc...
</div>
</nav>
</div>
You can give the element class name and don't try using nth-child() a lot because it's bad performance.
<div>
<div class=floatme one>...</div>
<div class=floatme two>...</div>
...(8 more)
</div>
Also you can use same idea to create gallery image using CSS Grid layout and BEM methodology.
CSS Grid Layout:
https://www.youtube.com/watch?v=jV8B24rSN5o&t=930s
BEM:
https://www.toptal.com/css/introduction-to-bem-methodology.
How do I get the child div which is inside the parent div, go on the first line of the div?
See the picture for what I mean:
div
{
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
}
.parentDiv
{
position: relative;
/*...*/
}
.childDiv
{
position: absolute;
/*...*/
}
Basicly that's the CSS. Read more
like this?
#parent {
background-color: #aaaaaa;
height:300px;
}
#child {
background-color: #ff0000;
width:220px;
margin-left:10px;
}
#two {
background-color: #00ff00;
width:100px;
}
.kids {
display: inline-block;
padding: 2px;
white-space: normal;
}
<div id="parent" style="width: 250px; display: inline-block; white-space: nowrap">
<div id="child" class="kids">
<span>child</span>
</div>
</div>
IF you want to look like in your img just do this :
.parent {
height:300px;
width:300px;
background-color:orange;
}
.child {
height:50px;
width:80%;
background-color:green;
margin:0 auto;
}
<div class="parent">
<div class="child">
</div>
</div>
i am VERY curious to see why the downvote ? anyone ?
I have 2 div<> that I would like to be next to eachother. They are inside of a form<>. The one I have on the left won't float all the way up. It seems that my First Div keeps blocking it. I have resized it multiple times and It still doesn't work. Here is my Css code and as you can see there is not much to it. I also have no inline styling. My first Div is called ContactInput and my second Div is called invisible
#body {
border: 1px double black;
}
#checkout { //this is just a head at the top
text-align:left;
border-bottom: 1px solid black;
}
#contactInput{
clear:right;
padding:.5em;
}
#invisible{
float:right;
padding:.5em;
}
Like this?
#contact {
width: 50%;
padding:.5em;
background: blue;
float: left;
box-sizing: border-box;
}
#invisible {
width: 50%;
padding:.5em;
background: red;
float: left;
box-sizing: border-box;
}
<div id="contact">
</div>
<div id="invisible">
</div>
I recommend flex instead of float
.wrap {
display: flex;
}
#contact {
flex: 1;
padding:.5em;
background: blue;
}
#invisible {
flex: 1;
padding:.5em;
background: red;
}
<div class="wrap">
<div id="contact">
</div>
<div id="invisible">
</div>
</div>
I want to use the table-cell to make a two-col layout. the content in the inner table has no relationship with the outer table.
My problem is the menu in the left-col will keep in line with the content in the inner- table-cell(the inner-left-head and inner-right-head). How can I stop it?
.wrap {border: 1px solid #ddd;}
.left-col,
.right-col
{
display:table-cell;
}
.left-col {
border-right: 1px solid #ddd;
width:17.5%;
max-width:209px;
}
.right-col {width:2000px;}
.right-col-main {
margin: 30px;
border: 1px solid #ddd;
}
.inner-left-col,
.inner-right-col {
display: table-cell;
height:300px;
}
.inner-left-col {
width:100px;
border-right: 1px solid #ddd;
}
.inner-right-col {
width: 500px;
}
.inner-left-head,
.inner-right-head {
height:48px;
line-height:48px;
border-bottom: 1px solid #ddd;
}
<div class="wrap">
<div class="left-col">
<dl>
<dt>menu<dt>
<dd>sub1<dd>
<dd>sub2<dd>
<dd>sub3<dd>
</dl>
</div>
<div class="right-col">
<div class="right-col-main">
<div class="inner-left-col">
<div class="inner-left-head">left head</div>
<div class="inner-left-body">left body</div>
</div>
<div class="inner-right-col">
<div class="inner-right-head">right head</div>
<div class="inner-right-body">right body</div>
</div>
</div>
</div>
</div>
I tried use table-row to wrap the table-cell but I can't see any difference.
Thanks for editing. It's starting to become clear now. You want to be able to scroll the right column while keeping the left column in sight. Then my question is, why did you make this table layout?
I think you're looking for position: fixed to fixate the left column to the top left corner. You can position the right column by just giving it a left margin.
So the styling for the left and right columns becomes:
.left-col {
position: fixed;
width: 17.5%;
top: 0;
left: 0;
}
.right-col
{
margin-left: 17.5%; /* Same or larger as width of left colum */
height: 2000px; /* Force some height to see the effect.
}
.wrap {border: 1px solid #ddd;}
.left-col {
position: fixed;
width: 17.5%;
top: 0;
left: 0;
}
.right-col
{
margin-left: 17.5%; /* Same or larger as width of left colum */
height: 2000px; /* Force some height to see the effect.
}
.right-col-main {
margin: 30px;
border: 1px solid #ddd;
}
.inner-left-col,
.inner-right-col {
display: table-cell;
height:300px;
}
.inner-left-col {
width:100px;
border-right: 1px solid #ddd;
}
.inner-right-col {
width: 500px;
}
.inner-left-head,
.inner-right-head {
height:48px;
line-height:48px;
border-bottom: 1px solid #ddd;
}
<div class="wrap">
<div class="left-col">
<dl>
<dt>menu<dt>
<dd>sub1<dd>
<dd>sub2<dd>
<dd>sub3<dd>
</dl>
</div>
<div class="right-col">
<div class="right-col-main">
<div class="inner-left-col">
<div class="inner-left-head">left head</div>
<div class="inner-left-body">left body</div>
</div>
<div class="inner-right-col">
<div class="inner-right-head">right head</div>
<div class="inner-right-body">right body</div>
</div>
</div>
</div>
</div>
Instead of using table-cells you could use floats. Combined with relative width settings you will furthermore have a more responsive solution.
This is just one example how it could be working:
.left-col,
.right-col {
float:left;
}
.left-col {
background:#abc;
width:17.5%;
}
.right-col {
background:#aed;
width:82.5%;
}
.right-col-main {
margin-top:40px;
}
.inner-left-col,
.inner-right-col {
float:left;
}
.inner-left-col {
width:20%;
}
.inner-right-col {
width:80%;
}
http://jsfiddle.net/xn0zuo7h/
jsFiddle:
How do I make div2 + Button2 fill the rest of the window width if I use pixel metric on column 1 and 3?
I'll use that to format a form making a textbox to change the size as two other fields are fixed.
Thank you.
CSS
td { border:solid 1px #000; float:left; }
#div1 { width:100px; border:solid 1px #000; float:left; }
#div2 { border:solid 1px #000; float:left; }
#div3 { width:100px; border:solid 1px #000; float:right; }
#Button1 { width:100% }
#Button2 { width:100% }
#Button3 { width:100% }
HTML
<div id="div1">
<button id="Button1">Button 1</button>
</div>
<div id="div2">
<button id="Button2">Button 2</button>
</div>
<div id="div3">
<button id="Button3">Button 3</button>
</div>
Another solution is moving the second DIV to the bottom and applying margins on it without float: http://jsfiddle.net/xC7uZ/6/
As far as I know, there are only two ways of doing this:
Using tables - most people do not like this idea. I for one, think it's fine for overall layout as long as you don't go overboard with nested tables and stuff. Kalle's answer covers this option
Using absolute positioning specifying all four corners. I only recently discovered this method and it works beautifully. It works in all major browsers.
Something like this:
#div1 { position:absolute; left: 0px; width: 100px; border:solid 1px #000; }
#div2 { position:absolute; left: 100px; right: 100px; border:solid 1px #000; }
#div3 { position:absolute; right: 0px; width:100px; border:solid 1px #000; float:right; }
Here is one to make you guys think :)
<div class="maincontainer">
<div class="column01">
<div class="restraint">
<p>Left column</p>
</div>
</div>
<div class="column03">
<div class="restraint">
<p>Right column</p>
</div>
</div>
<div class="column02">
<div class="restraint">
<p>Middle column</p>
</div>
</div>
</div>
and the CSS:
.maincontainer {
position:relative;
overflow:hidden;
}
.maincontainer .column01 {
float:left;
}
.maincontainer .column01 .restraint,.maincontainer .column03 .restraint {
width:200px;
}
.maincontainer .column03 {
float:right;
}
.maincontainer .column02 {
overflow:hidden;
}
.maincontainer .column02 .restraint {
width:100%;
}
* html .maincontainer .column02 {
display:inline-block;
}
I will get hammered for using <table>, but this is the most flexible and crossbrowser method. It works in ie5 ^^
http://jsfiddle.net/hobobne/24urb/
En este ejemplo vemos como poner tres columnas, de las cuales, dos tienen tamaƱo fijo.
Three columns and one column with 100% and two columns with fixed width.
jsfiddle
CSS
div, span, label, li, ul
{
box-sizing: border-box;
-ms-box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
.cabecera
{
top:0px;
left:0px;
width:100%;
height: 100px;
display: table;
position: absolute;
border:1px solid orange;
}
.row
{
width:100%;
display: table-row;
}
.column_izq
{
width:60px;
height:100%;
display: table-cell;
white-space: nowrap;
vertical-align: middle;
border:1px solid black;
}
.column_izq .icono
{
width: 100%;
text-align: center;
border:1px solid red;
}
.column_center
{
width: 100%;
min-width:60px;
text-align:center;
display: table-cell;
vertical-align: middle;
border:1px solid black;
}
.column_der
{
width:60px;
height:100%;
display: table-cell;
white-space: nowrap;
vertical-align: middle;
border:1px solid black;
}
.column_der .logo
{
width: 100%;
text-align: center;
border:1px solid red;
}