简体   繁体   English

如何以水平格式对齐div?

[英]how to align divs in a horizontal format?

I'm drawing 2 cirlces and having an img in between those 2 circles. 我正在绘制2个圆,并且在这2个圆之间有一个img。 I want to align all these in one horizontal line. 我想将所有这些对齐在一条水平线上。 All of these are inside a div of span4 width. 所有这些都在span4宽度的div内。

Here is my code. 这是我的代码。

<div id = "divid" span4>

JS code JS代码

var circle_style = "<div style = 'float:left'><canvas id='myCanvas' ></canvas></div><div style = 'float:left'><img src = 'img'></div><div style = 'float:left'><canvas id='myCanvas1'></canvas></div"


$("#divid").append(circle_style);

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var radius = 75;

context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.fill();


var canvas2 = document.getElementById('myCanvas1');
var context2 = canvas2.getContext('2d');
var centerX2= canvas2.width / 2;
var centerY2 = canvas2.height / 2;
var radius2 = 75;

context2.beginPath();
context2.arc(centerX2, centerY2, radius2, 0, 2 * Math.PI, false);
context2.fillStyle = 'green';
context2.fill();

Also, how do I insert text inside the circles? 另外,如何在圈子中插入文字?

You are giving inline style. 您正在提供内联样式。 That is a very bad practice. 这是非常不好的做法。

You have not given src to image. 您尚未将src给image。

Try this: 尝试这个:

JS: JS:

var circle_style = "<div class='left vmiddle'><canvas id='myCanvas' ></canvas></div><div class='left vmiddle'><img src = 'img'></div><div class='left vmiddle'><canvas id='myCanvas1'></canvas></div>";

CSS: CSS:

img{height:50px;width:50px;background:#ccc;}
.vmiddle{vertical-align:middle;}
.left{display:inline-block;}
#divid{min-width:650px;}

Demo here. 演示在这里。

Output here. 在这里输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM