显示与Button元素内联的SVG元素

时间:2020-05-22 01:55:44

标签: html css svg inline display

我想与相邻的<svg>一起显示<button>元素。

我尝试将svg的display属性设置为inline或inline-block(即使我认为这是svg的默认设置),但该方法无效。
float: left;也无效。

如果可能的话,我仍然希望按钮的宽度保持100%。 我想念什么吗?

<html>
<head>
<style>
* {
	box-sizing: border-box;
	font-family: sans-serif;
	color: #4e4e4e;
}
.collapsible {
	background-color: #ffffff;
	cursor: pointer;
	padding: 18px;
	width: 100%;
	border: none;
	text-align: left;
	outline: none;
	font-size: 18px;
	font-weight: bold;
	line-height: 24px;
}

.active, .collapsible:hover, .collapsible:active {
	background-color: #027bcb;
	color: #ffffff;
}

.content {
	margin: 10px 18px;
	display: none;
	overflow-x: scroll;
}

svg {
	width: 60px;
	height: 60px;
	padding: 18px;
}

line {
	stroke:#027bcb;
	stroke-width:1
}
</style>
</head>
<body>
<svg> <!-- this svg should be in line with the following button -->
	<line x1="12" y1="0" x2="12" y2="24" />
	<line x1="0" y1="12" x2="24" y2="12" />
</svg>

<button type="button" class="collapsible">Button here</button>

<div class="content">
	<p>some content</p>
</div>

<script>
	var coll = document.getElementsByClassName("collapsible");
	var line = document.getElementsByTagName("line");
	coll[0].addEventListener("click", function() {
		this.classList.toggle("active");
		var content = this.nextElementSibling;
		if (content.style.display === "block") {
			content.style.display = "none";
			line[0].style.stroke = "#027bcb";
			line[1].style.stroke = "#027bcb";
		} else {
			content.style.display = "block";
			line[0].style.stroke = "#ffffff";
			line[1].style.stroke = "#ffffff";
		}
	});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以将<SVG>放在<button>标记内,这是一个有效的代码段:

<html>
<head>
<style>
* {
	box-sizing: border-box;
	font-family: sans-serif;
	color: #4e4e4e;
}
.collapsible {
	background-color: #ffffff;
	cursor: pointer;
	padding: 18px;
	width: 100%;
	border: none;
	text-align: left;
	outline: none;
	font-size: 18px;
	font-weight: bold;
	line-height: 24px;
}

.active, .collapsible:hover, .collapsible:active {
	background-color: #027bcb;
	color: #ffffff;
}

.content {
	margin: 10px 18px;
	display: none;
	overflow-x: scroll;
}

svg {
    width: 60px;
    height: 30px;
}

.collapsible:hover line {
  stroke: #fff;
}

line {
	stroke:#027bcb;
	stroke-width:1
}
</style>
</head>
<body>


<button type="button" class="collapsible">

<svg> <!-- this svg should be in line with the following button -->
	<line x1="12" y1="0" x2="12" y2="24" />
	<line x1="0" y1="12" x2="24" y2="12" />
</svg>
Button here</button>

<div class="content">
	<p>some content</p>
</div>

<script>
	var coll = document.getElementsByClassName("collapsible");
	var line = document.getElementsByTagName("line");
	coll[0].addEventListener("click", function() {
		this.classList.toggle("active");
		var content = this.nextElementSibling;
		if (content.style.display === "block") {
			content.style.display = "none";
			line[0].style.stroke = "#027bcb";
			line[1].style.stroke = "#027bcb";
		} else {
			content.style.display = "block";
			line[0].style.stroke = "#ffffff";
			line[1].style.stroke = "#ffffff";
		}
	});
</script>
</body>
</html>