Solution 1 :

you can just have the <SVG> inside the <button> tag, here is a working snippet:

<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>

Problem :

I want to display an <svg> element in one line with an adjacent <button>.

I tried setting the svg’s display property to inline or inline-block (even though i think thats default for svg), but it didn’t work.
float: left; also doesn’t work.

I still want the button to keep it’s width of 100%, if that is possible.
Am I missing something?

<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>

Comments

Comment posted by not my real name

try using flexbox on the parent

Comment posted by AbgrundLemur

if i wrap the button and svg into a div, the js script for the collapsible doesn’t work anymore.

Comment posted by ROOT

Ok, Let me check.

Comment posted by accepting answer

@AbgrundLemur, I updated my check it now, please consider

Comment posted by AbgrundLemur

Thank You!! I think, placing a inline-block element (svg) inside an inline element (button) is semantically incorrect though. But it works, so I guess this is fine.

Comment posted by AbgrundLemur

ok i found a way. wrapped the svg and adjacent button in a flex div, like you first suggested and changed my js from

By

Leave a Reply

Your email address will not be published. Required fields are marked *