Search code examples
htmlcsssvginlinedisplay

Display SVG element inline with Button element


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>


Solution

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