I'm having some issues getting an SVG to play well in the layout that I have written. Here is the example I have for the working table-cell layout that I have been working on. The first jsfiddle links to the working example. When I replace the text on the left with an SVG for some reason, the content on the right column gets pushed down. Any idea why?
working example
broken example
Working Example HTML:
<div id="wrapper">
<div id="row">
<div id="left-col">
There are many variations of passages of Lorem Ipsum available
</div>
<div id="right_col">
<aside><strong>ASIDE</strong></aside>
<div id="banner1">Banner 1</div>
</div>
</div>
Broken Example HTML:
<div id="wrapper">
<div id="row">
<div id="left-col">
<div id="divMapContainer" class="embed-responsive embed-responsive-1by1">
<svg width="400" height="100">
<rect width="400" height="100" style="fill:rgb(0,0,255);stroke-width:10;stroke:rgb(0,0,0)"></rect>
</svg>
</div>
</div>
<div id="right_col">
<aside><strong>ASIDE</strong></aside>
<div id="banner1">Banner 1</div>
</div>
</div>
CSS:
body {
background-color: #333;
margin: 1em;
}
#wrapper {
width: 100%;
max-width: 46em;
margin: 0 auto;
}
#media (min-width: 48em) {
/* 768px */
#wrapper {
display: table;
}
header {
display: table-header-group;
}
nav,
#banner2 {
display: block;
}
#row {
/* the rule below is redundant
thanks to SelenlT2
*/
/*display: table-cell;*/
}
#left-col,
#right_col {
display: table-cell;
}
#left-col {
width: 50%;
}
footer {
display: table-footer-group;
}
}
#banner1 {
background-color: #9ed6f9;
height: 50%;
}
#left-col,
#right_col {
background-color: #fff;
}
#right_col {
height: 100%;
}
aside {
background-color: #fbcdfa;
height: 50%;
}
#left-col,
aside {
text-align: left;
padding: .5em;
}
nav,
header,
#banner1,
#banner2,
footer {
text-align: center;
}
.note {
color: #fff;
text-align: center;
}
It's because SVG elements are inline by default. You can set the vertical-align property on your SVG so that the other elements will align to the top of it. See updated fiddle.
svg {
vertical-align: top;
}
Related
I am trying to create a layout without using grid or flexbox, I am using display: inline-block to achieve that but i have a problem with adjusting spaces.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.wrapper {
text-align : justify;
}
.wrapper > div {
display: block;
margin-top: 5px;
}
.header {
background: lightgreen;
margin-top: 0;
}
.footer {
background: #eee;
}
.main > div {
display: inline-block;
width: 49%;
height: 20vh;
background: #eee;
}
<div class="wrapper">
<div class="header">header</div>
<div class="main">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
</div>
<div class="footer">footer</div>
</div>
I am trying to achieve the same effect as justify-content: space-between in flexbox
but i got elements that are not aligned well in the layout.
I can fix the spaces around item4 but using margin-left but i don't like this solution.
Add a hidden element to trigger the justify alignment for the last line but you will need to use a negative margin-bottom to remove the extra line added.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.wrapper {
text-align : justify;
}
.wrapper > div {
display: block;
margin-top: 5px;
}
.header {
background: lightgreen;
margin-top: 0;
}
.footer {
background: #eee;
}
.main > div {
display: inline-block;
width: 49%;
height: 20vh;
background: #eee;
}
.main:after {
content:"";
display:inline-block;
width:5%;
height:50px; /* we consider a bigger value than the line-height*/
}
.main {
margin-bottom:-50px; /*the same value defined in the pseuo element*/
}
<div class="wrapper">
<div class="header">header</div>
<div class="main">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
</div>
<div class="footer">footer</div>
</div>
Or use font-size:0 trick to avoid that extra line:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.wrapper {
text-align : justify;
}
.wrapper > div {
display: block;
margin-top: 5px;
}
.header {
background: lightgreen;
margin-top: 0;
}
.footer {
background: #eee;
}
.main > div {
display: inline-block;
width: 49%;
height: 20vh;
background: #eee;
font-size:initial;
}
.main:after {
content:"";
display:inline-block;
width:5%;
}
.main {
font-size:0;
}
<div class="wrapper">
<div class="header">header</div>
<div class="main">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
</div>
<div class="footer">footer</div>
</div>
I don't think you can achieve this without setting the spacing. As you probably know, you can get better control of inline-block layouts setting negative letter-spacing on the container and resetting on the item: .main {letter-spacing: -4px} .item {letter-spacing: 0} (You need to test if 4px works).
Or (since we're not using flex :), you could float odd and even divs left and right: .item:nth-child(odd) {clear: left; float: left} .item:nth-child(even) {float: right}
I need to perform a dynamic grid system like this:
Each section is an article that contains an image, a title and a link/button to that article.
The problem is that each section is loaded dynamically and i only have the html of the section so i need to put each section on the correct position dynamically from the CSS. The one i know is that there are 5 sections.
The html code of each section and the container of all the sections is this:
<section class="scroll">
<!-- ARTICLES -->
<!-- ARTICLE -->
<div class="article-content">
<img class="article-image" src="${item.imgPath}" />
<div class="article-texts">
<h1 class="article-title">${item.title}</h1>
<a class="article-button" href="${item.link}.html" role="button">Read Article ></a>
</div>
</div>
<div class="clear"></div>
<!-- END ARTICLE -->
<!-- END ARTICLES -->
</section>
If you have control over the dimensions of your sections, you can use a fixed width container and float the sections inside that. Clear the float on the fourth section.
Example Fiddle: http://jsfiddle.net/abhitalks/mbuf9957/3/
Example Snippet:
* { box-sizing: border-box; padding: 0; margin:0; }
div { width: 380px; overflow: hidden; }
section { border: 1px solid #666; float: left; }
section:nth-child(1) { width: 240px; height: 240px; }
section:nth-child(2) { width: 120px; height: 120px; }
section:nth-child(3) { width: 120px; height: 120px; }
section:nth-child(4) { width: 120px; height: 120px; clear: left; }
section:nth-child(5) { width: 240px; height: 120px; }
<div>
<section>1</section>
<section>2</section>
<section>3</section>
<section>4</section>
<section>5</section>
</div>
Since you have tagged this as CSS3, I think Flexbox would be an option. You could set display:flex on the parent and then have percentage widths for each box's flex-basis and set the flex-grow property to the amount of space, relative to other boxes, you want them to take up in the container and set flex-shrink to 0 since you don't need them to shrink.
CSS/HTML:
.grid-system {
/* Uncomment the next line to see the container */
/* border:1px solid black; */
}
.grid-system .box-width-2 {
border:1px solid black;
-webkit-flex:2 0 65%;
flex: 2 0 65%;
}
.grid-system .box-width-1 {
border:1px solid black;
-webkit-flex:1 0 32%;
flex: 1 0 32%;
}
.grid-system .box-height-2 {
-webkit-flex-grow:2;
flex-grow:2;
}
.grid-system .box-height-1 {
-webkit-flex-grow:1;
flex-grow:1;
}
.grid-system .flex-row {
display:-webkit-flex;
display:flex;
-webkit-flex-flow:row nowrap;
flex-flow:row nowrap;
-webkit-justify-content:flext-start;
justify-content:flex-start;
}
.grid-system .flex-column {
display:-webkit-flex;
display:flex;
-webkit-flex-flow:column nowrap;
flex-flow:column nowrap;
width:32%;
}
.grid-system .flex-row > div {
margin:0.5%
}
.grid-system .box-width-1.box-height-1 {
margin-bottom:0.5%;
-webkit-flex-grow:1;
flex-grow:1;
}
.grid-system .box-width-1.box-height-1.end {
margin-bottom:0px;
}
<div class="grid-system">
<div class="flex-row">
<div class="box-width-2 box-height-2">1</div>
<div class="flex-column">
<div class="box-width-1 box-height-1">2</div>
<div class="box-width-1 box-height-1 end">3</div>
</div>
</div>
<div class="flex-row">
<div class="box-width-1">4</div>
<div class="box-width-2">5</div>
</div>
</div>
jsFiddle
A solution only involving floats can reproduce your layout. Compatibility IE8+ (and even below but nobody cares). Pseudo-class :nth-child() (compat. IE9+) is used here to give an arbitrary width and height for demo, you'll have your own layout in real conditions.
* { box-sizing: border-box; }
div { width: 360px; }
section { border: 1px solid #666; }
.left { float: left; }
.right { float: right; }
.clear { clear: both; }
section:nth-child(1) { width: 240px; height: 240px; }
section:nth-child(2) { width: 120px; height: 100px; }
section:nth-child(3) { width: 120px; height: 80px; }
section:nth-child(4) { width: 200px; height: 120px; }
section:nth-child(5) { width: 160px; height: 100px; }
<div>
<section class="left">1</section>
<section class="right">2</section>
<section class="right">3</section>
<section class="left clear">4</section>
<section class="right">5</section>
</div>
I'm trying to put 3 divs in the same row as the following code.
My CSS and HTML:
.row {
display: table;
width: 100%
}
.row > div {
display: table-cell;
height:30px; /*demo purposes */
}
#left-bar {
width: 10%;
background-color: #FF0000;
}
#middle-bar {
width: 70%;
background-color: #6600FF;
}
#right-bar {
width: 20%;
background-color: #99FF99;
}
<div class="row">
<div id="left-bar"> here I have an accordion </div>
<div id="middle-bar"> heve a have my canvas </div>
<div id="right-bar"> and here I have an editor</div>
</div>
Somehow the content of the middle-bar(my canvas) is positioned in the correct place, but the other two divs contents are in the bottom of the page as you can see here see photo. Do you guys know why this is happening?
After discussing the project further with you in the comments, and in chat, I think you should take an approach that uses flexbox instead. The code is fairly straight forward:
.container {
display: flex;
}
.left { flex-basis: 10%; background: #F99; }
.right { flex-basis: 20%; background: #99F; }
.middle { flex-basis: 70%; background: #9F9; }
<div class="container">
<div class="left">L</div>
<div class="middle">C</div>
<div class="right">R</div>
</div>
I only managed width.
There's nothing problematic see this.
.row {
display: table;
width: 100%
}
.row > div {
display: table-cell;
height:30px; /*demo purposes */
}
#left-bar {
width: 20%;
background-color: #FF0000;
}
#middle-bar {
width: 60%;
background-color: #6600FF;
}
#right-bar {
width: 20%;
background-color: #99FF99;
}
<div class="row">
<div id="left-bar"> here I have an accordion </div>
<div id="middle-bar"> heve a have my canvas </div>
<div id="right-bar"> and here I have an editor</div>
</div>
I need a number of elements to line up horizontally in the order in which they appear in the HTML. I need them to move to the right of their container.
If I float the items to the right then the order changes.
If I display as inline-block and make the container's text aligned to the right then there are spaces between them.
I can change the HTML however I cant remove all the white space (which may fix the issue with the inline-blocks). Can this be solved?
http://codepen.io/anon/pen/xbLbLE
<div class="cont">
<div class='itemA'>1</div>
<div class='itemA'>2</div>
<div class='itemA'>3</div>
<div class='itemA'>4</div>
</div>
<div class="cont contB">
<div class='itemB'>1</div>
<div class='itemB'>2</div>
<div class='itemB'>3</div>
<div class='itemB'>4</div>
</div>
.itemA,
.itemB {
width: 50px;
height: 50px;
}
.itemA {
background: green;
float: right;
}
.contB {
text-align: right;
}
.itemB {
background: red;
display: inline-block;
}
Float the elements that need to be in the correct order to the left and float their container to the right.
HTML
<div class="cont">
<div class='itemA'>1</div>
<div class='itemA'>2</div>
<div class='itemA'>3</div>
<div class='itemA'>4</div>
</div>
<div class="cont contB">
<div class='itemB'>1</div>
<div class='itemB'>2</div>
<div class='itemB'>3</div>
<div class='itemB'>4</div>
</div>
CSS
.itemA,
.itemB {
width: 50px;
height: 50px;
}
.itemA {
background: green;
}
.itemB {
background: red;
}
.cont {
float: right;
}
.cont div {
float: left;
}
and the jsFiddle: http://jsfiddle.net/enaeLr60/
The downside, as you have noticed, with using display: inline-block is that any space between each inline-block will be rendered as a single space. I prefer to float items to fix this versus the options like the following as they a not preferred.
<div>1</div><div>2</div> - no spaced between elements
<div>1</div><!-- comment block --><div>2</div> - comment between
elements
use a negative margin
UPDATE
My original answer forgot to include a wrapper around the .cont DIVs. As a result the DIVs with .itemA appear after the .itemB DIVs. See updated code to correct this.
HTML
<div class="list-container">
<div class="cont">
<div class='itemA'>1</div>
<div class='itemA'>2</div>
<div class='itemA'>3</div>
<div class='itemA'>4</div>
</div>
<div class="cont contB">
<div class='itemB'>1</div>
<div class='itemB'>2</div>
<div class='itemB'>3</div>
<div class='itemB'>4</div>
</div>
</div>
CSS
.itemA,
.itemB {
width: 50px;
height: 50px;
}
.itemA {
background: green;
}
.itemB {
background: red;
}
.list-container {
float: right;
}
.cont,
.cont div {
float: left;
}
updated jsFiddle: http://jsfiddle.net/enaeLr60/1
I am not sure if I am understanding your problem correctly, but can you give this CSS and try and see if it is what you are attempting to do.
.cont{
display:inline;
float:right
}
.itemA,
.itemB {
width: 50px;
height: 50px;
display: inline-block;
margin-right:-4px;
}
.itemA:last-child, .itemB:last-child{
margin-right:0;
}
.itemA {
background: green;
}
.contB {
text-align: right;
}
.itemB {
background: red;
}
The margin-right:4px is a hack around the display:inline-block; so, this may not be desirable.
My answer involves adding a wrapper around it all: http://codepen.io/anon/pen/KwvwyL
<div class="wrapper">
<div class="cont contA">
<div class='itemA'>1</div>
<div class='itemA'>2</div>
<div class='itemA'>3</div>
<div class='itemA'>4</div>
</div>
<div class="cont contB">
<div class='itemB'>1</div>
<div class='itemB'>2</div>
<div class='itemB'>3</div>
<div class='itemB'>4</div>
</div>
</div>
Then in the CSS:
.wrapper {
float: right;
}
.contA {
float: left;
background: green;
}
.contB {
float: left;
background: red;
}
.itemA, .itemB {
float: left;
}
.itemA,
.itemB {
width: 50px;
height: 50px;
display:inline-block;
}
.itemA { background: green; }
.cont { display:inline-block; word-spacing: -100%; }
.contB { float:right; }
.itemB { background: red; }
CodeOpen
I am trying to layout a page to look like this: http://jsfiddle.net/LLqwX/1/
But I cannot figure out how to get the divs "logo" and "sometext" to be on the same line as the buttons. I would like it all to stay as it is, with the same colors everywhere, just that the top two lines would become friends and want to stay on the same line together.
HTML
<div id="top">
<div id="logo">logo</div>
<div id="sometext">text text</div>
<div id="menu">
btn 1
btn 2
btn 3
</div>
</div>
<div id="content">
<div id="hello">
hello
</div>
</div>
CSS
#top {
background: #ccc;
}
#logo {
display: inline-block;
background: #ff00ff;
}
#sometext {
display: inline-block;
background: #ffff00;
}
#menu {
text-align: center;
}
#content {
background: #444;
}
#hello {
width: 40%;
margin: 0 auto;
background: #888
}
Simply add display: inline-block to the #menu.
http://jsfiddle.net/LLqwX/3/
You could use the same inline-block method you've used elsewhere:
#menu {
display: inline-block;
text-align: center;
}
http://jsfiddle.net/LLqwX/2/
EDIT:
I realize that you probably want the buttons to remain centered.
There are many methods of structuring this, but I tried to minimize changes to your original code.
<div id="headleft">
<div id="logo">logo</div>
<div id="sometext">text text</div>
</div>
div#headleft {
position:absolute;
}
http://jsfiddle.net/LLqwX/4/
Another way to keep the buttons centered is to use a flex layout.
Here is a FIDDLE showing how.
CSS
#top {
background: #ccc;
display: flex;
}
#logo {
display: inline-block;
background: #ff00ff;
}
#sometext {
display: inline-block;
background: #ffff00;
}
#menu {
display: inline-block;
text-align: center;
flex: 1;
}
#content {
background: #444;
}
#hello {
width: 40%;
margin: 0 auto;
background: #888
}