I have multiple canvases that I want to display adjacent to each other. I'm not the most familiar with CSS but I've tried placing them all in a div that is centered, with each under a specific float (canvas_left set to float left, canvas_center set to canvas center, or canvas_right set to float right). For some reason I can't get them all be to adjacent and centered horizontally on the screen.
CSS:
.left {
display: block;
float: left;
}
.center {
display: block;
margin: auto;
}
.right {
display: block;
float: right;
}
HTML:
<div class="center">
<canvas id="canvas_left" class="left" width="150px" height="400px"></canvas>
<canvas id="canvas_main" class="center"></canvas>
<canvas id="canvas_right" class="right" width="150px" height="400px"></canvas>
</div>
If you know how to fix this, please let me know.
There is no float center. You layout doesn't work because the main canvas is a non-floated block.
Using floats is probably a bad idea anyways. Better use inline-blocks:
.wrapper {
text-align: center;
}
canvas {
border: 1px solid;
width: 25%;
display: inline-block;
}
<div class="wrapper">
<canvas id="canvas_left"></canvas>
<canvas id="canvas_main"></canvas>
<canvas id="canvas_right"></canvas>
</div>
Or flexbox, the modern way to do it:
.wrapper {
display: flex;
justify-content: center;
}
canvas {
border: 1px solid;
width: 25%;
}
<div class="wrapper">
<canvas id="canvas_left"></canvas>
<canvas id="canvas_main"></canvas>
<canvas id="canvas_right"></canvas>
</div>
I suggest using flexboxes, you can do almost anything with that.
Also the container class is the same as the child one, you should change that, like so:
<div id="canvas">
<canvas id="canvas_left" class="left" width="150px" height="400px"></canvas>
<canvas id="canvas_main" class="center"></canvas>
<canvas id="canvas_right" class="right" width="150px" height="400px"></canvas>
</div>
#canvas {
display: flex;
justify-content: center;
}
.left { background: green; flex-shrink: 0; }
.center { background: blue; flex-shrink: 0; }
.right { background: purple; flex-shrink: 0; }
JSfiddle: jsfiddle.net/8uv2zowm
You need to float all canvas tags. Change your CSS:
.left {
display: block;
float: left;
}
.center {
display: block;
margin: auto;
float: left;
}
.right {
display: block;
float: left;
}
But be aware that when changing display width, the canvas tags will again be aligned under each other.
Play around here.
I think this is what you want to achieve.
CSS
.container {
float: left;
}
.left {
display: inline-block;
width: 150px;
height: 400px;
background-color: red;
}
.center {
display: inline-block;
width: 150px;
height: 400px;
background-color: blue;
}
.right {
display: inline-block;
width: 150px;
height: 400px;
background-color: green;
}
HTML
<div class="container">
<canvas id="canvas_left" class="left"></canvas>
<canvas id="canvas_main" class="center"></canvas>
<canvas id="canvas_right" class="right"></canvas>
</div>
Related
My goal: A responsive navbar where the logo is always in the middle and an element
is always on the left. Depending on the context (page dependent), buttons can be
displayed in the right area or not.
My approach: I use a flexbox for the navbar. I have three divs in the flexbox. I have given all divs a fixed width. The middle box is also a flexbox. The div with a logo is located there. I position the logo on the right edge of the middle flexbox. The div with the logo has a fixed width (80px).
The problem: The approach works but I don't find this way very nice. Because the widths are dependent on each other. If you would change the logo and it would be wider or narrower then you would have to adjust the relative width of the middle and right box. The second problem is if the device smaller as 900px then this solution dont work.
Question: What other possibilities are there and what possibilities would resolve this "width" dependency?
#app {
margin: 0 auto;
max-width: 900px;
width:100%;
}
header {
height: 80px;
display: flex;
justify-content:space-between;
}
.header-left {
width:20%;
background: green;
}
.header-middle {
width:34%;
background: gray;
display: flex;
justify-content:flex-end;
}
.header-right {
width:46%;
background: green;
}
.logo {
background-color: red;
width:80px;
height: 80px;
text-align:center;font-size:70px;
}
<div id="app">
<small>width: 900px</small>
<header>
<div class="header-left">Burger Menu</div>
<div class="header-middle">
<div class="logo">
I
</div>
</div>
<div class="header-right">Context Buttons</div>
</header>
<div>
<div style="width:50%; background: black;color:white; text-align:center;">Controller Div 50%</div>
</div>
</div>
You can use flex-grow: 1 on the left and right elements, the middle element will be in center naturally. In this case, you don't need to set widths on elements.
#app {
margin: 0 auto;
max-width: 900px;
width:100%;
}
header {
height: 80px;
display: flex;
justify-content:space-between;
}
.header-left {
flex-grow: 1;
background: green;
}
.header-middle {
background: gray;
display: flex;
justify-content:flex-end;
}
.header-right {
flex-grow: 1;
background: green;
}
.logo {
background-color: red;
width:80px;
height: 80px;
text-align:center;font-size:70px;
}
<div id="app">
<small>width: 900px</small>
<header>
<div class="header-left">Burger Menu</div>
<div class="header-middle">
<div class="logo">
I
</div>
</div>
<div class="header-right">Context Buttons</div>
</header>
<div>
<div style="width:50%; background: black;color:white; text-align:center;">Controller Div 50%</div>
</div>
</div>
Since you're looking for different possibilities i'll suggest you to take the approch used by Tepken Vannkorn :
Centering brand logo in Bootstrap Navbar
Based on your comments, I would suggest the following code as a simple solution.
I have added a max-width value to your .logo CSS class and I have also moved your inline CSS from the front-end code, and created a .controller CSS class for it.
#app {
margin: 0 auto;
max-width: 900px;
width: 100%;
}
header {
height: 80px;
display: flex;
justify-content: space-between;
}
.header-left {
width: 20%;
background: green;
}
.header-middle {
width: 34%;
background: gray;
display: flex;
justify-content: flex-end;
}
.header-right {
width: 46%;
background: green;
}
.logo {
background-color: red;
width: 80px;
height: 80px;
text-align: center;
font-size: 70px;
max-width: 80px;
}
.controller {
width: 50%;
background: black;
color: white;
text-align: center;
}
<div id="app">
<small>width: 900px</small>
<header>
<div class="header-left">Burger Menu</div>
<div class="header-middle">
<div class="logo">
I
</div>
</div>
<div class="header-right">Context Buttons</div>
</header>
<div>
<div class="controller">Controller Div 50%</div>
</div>
</div>
A solution would be to use a mix of flex and position: absolute. Then you need only the left and the right container. the logo you can center with position left: left: calc(50% - calc(80px / 2));. The 80px is the width from your logo.
* {
margin: 0;
padding: 0;
}
#app {
margin: 0 auto;
max-width: 900px;
width:100%;
}
.header {
display: flex;
justify-content: space-between;
height: 80px;
background: yellow;
position: relative;
}
.header-left {
background-color: green;
width: 20%
}
.header-right {
background-color: green;
width: 44%;
}
.logo {
background-color: red;
width:80px;
height: 80px;
text-align:center;
font-size:70px;
position: absolute;
left: calc(50% - calc(80px / 2));
}
<div id="app">
<div class="header">
<div class="header-left">left</div>
<div class="logo">X</div>
<div class="header-right">right</div>
</div>
<div style="width:50%; background: black;">Controller Div 50%</div>
</div>
I have enclosed an image inside a nested div .box. If it is not floated the image can be exactly centered to the .box, but when I float it left or right the center alignment goes away! I have 4 images with various sizes that need te be centered inside their own .box. How can I fix it?
HTML
<div class="con">
<div class="box">
<img src="../_images/icon-test.png">
</div>
</div>
CSS
.con {
width:300px;
height:200px;
background: #996600;
}
.box {
position:relative;
width:150px;
height:150px;
background-color: #333333;
display:table-cell;
text-align:center;
vertical-align:middle;
float:right;
}
You can use display: flex for this.
Change your display: table-cell to display: flex.
Then change text-align:center; and vertical-align:middle; to align-items: center; and justify-content: center; to center it vertically and horizontally.
Edit: Then I have also added a max-width of 150px to the image, to stop it expanding out of the container when the image is bigger than it. Props to #Hkidd for pointing out that this happens.
.con {
width: 300px;
height: 200px;
background: #996600;
}
.box {
position: relative;
width: 150px;
height: 150px;
background-color: #333333;
display: flex;
align-items: center;
justify-content: center;
float: right;
}
img {
max-width: 150px;
}
<div class="con">
<div class="box">
<img src="http://placehold.it/200x100">
</div>
</div>
Explanation
I think you have to position the img absolute, so it will be positioned absolute to its parent .box since .box is positioned relative. Also the display: table-cell; is unnecessary since the img is not inside a table.
This is what I came up with:
.con {
background: #996600;
height: 200px;
width: 300px;
}
.box {
background-color: #333333;
float: right;
height: 150px;
position: relative;
width: 150px;
}
.box img {
bottom: 0;
left: 0;
margin: auto;
position: absolute;
right: 0;
top: 0;
width: 90%;
}
<div class="con">
<div class="box center">
<img src="http://placehold.it/120x100">
</div>
</div>
If you have a single image as in the question, you can use the line-height solution which places the image exactly in center of the .box div & use vertical-align: middle on the image. Works on all browsers & simple to understand.
Refer code:
.con {
width: 300px;
height: 200px;
background: #996600;
}
.box {
position: relative;
width: 150px;
height: 150px;
background-color: #333333;
text-align: center;
vertical-align: middle;
float: right;
line-height: 150px;
}
img {
height: 60px;
vertical-align: middle;
}
<div class="con">
<div class="box">
<img src="http://www.w3schools.com/css/img_fjords.jpg">
</div>
</div>
I have 3 squares, side-by-side of different heights. I want them to be bottom-aligned with each other, but I want the entire group center-aligned within a containing box.
Seems like putting them in a wrapper whose style is vertical-align:middle should do it. But nope, it doesn't work.
Any suggestions on how to do this?
https://plnkr.co/edit/IjqMn5W8xZxdy0XTWrAw?p=preview
#red {
background-color: #FF0000;
height: 20px;
width: 20px;
}
#green {
background-color: #00FF00;
height: 60px;
width: 60px;
}
#blue {
background-color: #0000FF;
height: 40px;
width: 40px;
}
#container {
width: 300px;
height: 100px;
border: thin solid black;
}
#wrapper {
vertical-align: middle;
}
body {
font-size: 0;
}
<body>
<div id="container">
<div id='wrapper'>
<img id="red" src="">
<img id="green" src="">
<img id="blue" src="">
</div>
</div>
</body>
#container {
display: table;
}
#wrapper {
display: table-cell;
}
http://codepen.io/Deka87/pen/oYXOwg
You should use a table layout if not eager to use flexbox.
Try to use display: flex;.
The #container should have an align-items: center; property, and the #wrapper need an align-items: flex-end;
You can change align-items for this following options:
flex-start: cross-start margin edge of the items is placed on the cross-start line
flex-end: cross-end margin edge of the items is placed on the cross-end line
center: items are centered in the cross-axis
baseline: items are aligned such as their baselines align
stretch (default): stretch to fill the container (still respect min-width/max-width)
Here for more about Flex
And back to your question. Look at this example:
#red {
background-color:#FF0000;
height: 20px;
width: 20px;
}
#green {
background-color:#00FF00;
height: 60px;
width: 60px;
}
#blue {
background-color:#0000FF;
height: 40px;
width: 40px;
}
#container {
display: flex;
align-items: center;
width: 300px;
height:100px;
border: thin solid black;
}
#wrapper {
display: flex;
align-items: flex-end;
}
body {
font-size:0;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div id="container">
<div id='wrapper'>
<img id="red" src="">
<img id="green" src="">
<img id="blue" src="">
</div>
</div>
</body>
</html>
Fiddle demo
I have 3 divs (colored squares) and a button.
How can I center the button under the divs?
With my current code the button appears in the same line as #squares and it's floating to the left. Thanks for your answers.
#div1 {
background-color: red;
width: 100px;
height: 100px;
margin-right: 10px;
}
#div2 {
background-color: green;
width: 100px;
height: 100px;
margin-right: 10px;
}
#div3 {
background-color: blue;
width: 100px;
height: 100px;
}
#squares {
display: flex;
position: absolute;
margin-left: 35%;
}
#button {
width: 100px;
height: 50px;
clear: both;
}
<div id="squares">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</div>
<div id="button">
<button>Click me!</button>
</div>
Since you're already using flexbox, there are multiple solutions, all of them relatively simple.
Here's one:
Wrap both containers in a parent container.
Add display: flex and flex-direction: column to this new container.
Now you can easily align the squares and the button both vertically and horizontally.
#container {
display: flex;
flex-direction: column;
}
#div1 {
background-color: red;
width: 100px;
height: 100px;
}
#div2 {
background-color: green;
width: 100px;
height: 100px;
margin: 0 10px;
}
#div3 {
background-color: blue;
width: 100px;
height: 100px;
}
#squares {
display: flex;
align-self: center;
margin-bottom: 10px;
}
#button {
align-self: center;
text-align: center;
width: 100px;
height: 50px;
}
<div id="container">
<div id="squares">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</div>
<div id="button">
<button>Click me!</button>
</div>
</div>
Is there a specific reason you are using position: absolute for the square-container?
If not, you can just make the position relative, recenter the content and center the button.
Important code changes:
#squares {
position: relative;
justify-content: center;
}
#button {
margin: 0 auto;
}
Fiddle: https://jsfiddle.net/q17fa25w/
Other way to solve it, you can set padding left and top for #button div like this:
#button {
width: 100px;
height: 50px;
clear: both;
padding: 120px 35%;
}
html
<div style="display: block; height: 200px; width: 200px; background-color: red; position: relative">
<img src="http://www.kingsailfishmounts.com/Striped_Marlin_Plaque_(hotizontal)-mount-p-543.jpg" style="max-height:100%; max-width: 100%; position: absolute;"/>
</div>
This fits my image according to my div. What I want is no matter how big or small is image it should be displayed according to div.
If the image is vertical there should be blank space on top and bottom and if it is horizontal then left and right blank space. Image should not be stretched and the height and width of the div should be fixed.
I am able to do this but vertical image is aligned top and horizontal is aligned left. How can I make it on center ?
add left:50% & top:50% & transform: translate(-50%, -50%);
<div style="display: block; height: 200px; width: 200px; background-color: red; position: relative">
<img src="http://www.kingsailfishmounts.com/Striped_Marlin_Plaque_(hotizontal)-mount-p-543.jpg" style="max-height:100%; max-width: 100%; position: absolute; left:50%; top:50%; transform: translate(-50%, -50%); "/>
</div>
Putting all together, and naming “wraptocenter” the class of the container, that’s the relevant CSS. Code for IE/Mac is wrapped in a suitable filter. Code for IE7-/Win is put in conditional comments.
<style type="text/css">
.wraptocenter {
display: table-cell;
text-align: center;
vertical-align: middle;
width: ...;
height: ...;
}
.wraptocenter * {
vertical-align: middle;
}
/*\*//*/
.wraptocenter {
display: block;
}
.wraptocenter span {
display: inline-block;
height: 100%;
width: 1px;
}
/**/
</style>
<!--[if lt IE 8]><style>
.wraptocenter span {
display: inline-block;
height: 100%;
}
</style><![endif]-->
And that’s the relevant HTML
<div class="wraptocenter"><span></span><img src="..." alt="..."></div>
<div class="maindiv">
<img src="http://www.kingsailfishmounts.com/Striped_Marlin_Plaque_(hotizontal)-mount-p-543.jpg" alt="tall image" />
</div>
img
{
max-width: 100%;
max-height: 100%;
display: block;
margin: auto auto;
}
.maindiv
{ border: 1px solid #888;
display:table-cell;
height: 100px;
width: 100px;
vertical-align: middle;
}
This should workmargin: 0 auto;
display: block;
max-width: 100%;
Here follow the code:
<div style="display:inline-block; height:200px; width:200px; background-color:red; position:relative; vertical-align:middle; text-align:center">
Great coding for you!
You can use below styles to the div for horizontal and vertical alignment.
display: table-cell;
vertical-align: middle;
text-align: center;
And avoid position for both div and img
Below style added for img for vertical centering.
display: inline-block;
vertical-align: middle;
Also I have separated HTML and style as in below sample.
div{
height: 200px;
width: 200px;
background-color: red;
display: table-cell;
vertical-align: middle;
text-align: center;
}
img {
max-height: 100%;
max-width: 100%;
display: inline-block;
vertical-align: middle;
}
<div>
<img src="http://www.kingsailfishmounts.com/Striped_Marlin_Plaque_(hotizontal)-mount-p-543.jpg" />
</div>
You should remove some of the stylings from div and all stylings from the img and assign the image a fixed width using width attribute as below:
HTML:
<div>
<img src="http://www.kingsailfishmounts.com/Striped_Marlin_Plaque_(hotizontal)-mount-p-543.jpg" width="150" />
</div>
CSS:
div {
height: 200px;
width: 200px;
background-color: red;
}
div img {
display: table;
margin: 0 auto;
}
Refer demo.
flex box is a good solution for this situation. Your code have to be modified a little bit like this:
#container {
/*flex box goes here*/
display: flex;
align-items: center; /* vertical center */
justify-content: center; /* horizontal center */
/* your old code goes here*/
height: 200px;
width: 200px;
background-color: red;
position: relative
}
#img {
/* you can change height and width to test the result */
max-height:50%;
max-width: 50%;
}
<div id="container">
<img id="img" src="http://www.kingsailfishmounts.com/Striped_Marlin_Plaque_(hotizontal)-mount-p-543.jpg"/>
</div>