Setting a pivot point for a nested SVG element: A step-by-step guide

Today, I decided to delve into the world of CSS Transforms. With a desire to explore, I set out to create a mobile button using SVG and various transforms.

The challenge at hand is to rotate the mobile button by 90 degrees with a transform origin right at the center of the element.

Behold, my code:

.mobileNav {
    display: block;
    transition: .5s;
}

.mobileNav:hover {
    transform: rotate(90deg);
    transform-origin: 50% 50%;
     /*This is where the problem lies. How do I set the origin to the center of .mobileNav?? */
}
 
ul {    
    float: right;
    margin: 15px 50px 0px 0px;
}
  
li {
    display: inline-block;   
    transition: .5s;
}
<ul class="mobileNav">
<li>
<svg x="0px" y="0px" width="10" height="20">
   <circle cx="5" cy="10" r="3"  stroke-width="4" fill="#333" />
   Sorry, your browser does not support inline SVG.
</svg> 
</li>
<li>
<svg x="0px" y="0px" width="10" height="20">
   <circle cx="5" cy="10" r="3"  stroke-width="4" fill="#333" />
   Sorry, your browser does not support inline SVG.
</svg> 
</li>
<li>
<svg x="0px" y="0px" width="10" height="20">
   <circle cx="5" cy="10" r="3"  stroke-width="4" fill="#333" />
   Sorry, your browser does not support inline SVG.
</svg> 
</li>
</ul>

TASK: To establish the transform origin at the center of .mobileNav.

HICCUP: This objective seems beyond reach.

BROWSER: Running on Firefox version 44.0.2

Answer №1

ul by default has some padding or margin that may require resetting.

To improve the layout, consider resetting the display properties of li and svg to inline-block.

For debugging purposes, let's add a border to visualize the layout structure.

Applying vertical-align along with adjusting the line-height can help define a consistent height for the ul element and properly align its children vertically.

 body {
  margin-top:50px;/* snippet wants this */
 }

.mobileNav {
  display: block;
  transition: .5s;
  border: solid;
  padding: 0
}
.mobileNav:hover {
  transform: rotate(90deg);
}
ul {
  float: right;
  margin: 15px 50px 0px 0px;
  line-height: 1em;
}
li,
svg {
  display: inline-block;
  vertical-align: middle;
  transition: .5s;
}
<ul class="mobileNav">
  <li>
    <svg x="0px" y="0px" width="10" height="20">
      <circle cx="5" cy="10" r="3" stroke-width="4" fill="#333" />Sorry, your browser does not support inline SVG.
    </svg>
  </li>
  <li>
    <svg x="0px" y="0px" width="10" height="20">
      <circle cx="5" cy="10" r="3" stroke-width="4" fill="#333" />Sorry, your browser does not support inline SVG.
    </svg>
  </li>
  <li>
    <svg x="0px" y="0px" width="10" height="20">
      <circle cx="5" cy="10" r="3" stroke-width="4" fill="#333" />Sorry, your browser does not support inline SVG.
    </svg>
  </li>
</ul>

Answer №2

Your issue stems from the default padding. To improve this, consider adjusting the display properties of the SVG and li elements to not be inline or inline-block. Instead, float them so they align vertically, which should result in a smoother appearance.

body {padding: 3em;}
.mobileNav {
    display: block;
    transition: .5s;
    padding: 0;
  }
  
   .mobileNav:hover {
    transform: rotate(90deg);
    transform-origin: 50% 50%;
     /*The centering problem can be resolved here. How do I set the origin to the center of .mobileNav?? */
  }
 
ul {
    
    float: right;
    margin: 15px 50px 0px 0px;
  }
  
  li {
    display: inline-block;   
    transition: .5s;
  }

 .mobileNav svg, .mobileNav li {
      display:block;
      float:left;
 }
<ul class="mobileNav">
<li>
<svg x="0px" y="0px" width="10" height="20">
   <circle cx="5" cy="10" r="3"  stroke-width="4" fill="#333" />
   Sorry, your browser does not support inline SVG.
</svg> 
</li>
<li>
<svg x="0px" y="0px" width="10" height="20">
   <circle cx="5" cy="10" r="3"  stroke-width="4" fill="#333" />
   Sorry, your browser does not support inline SVG.
</svg> 
</li>
<li>
<svg x="0px" y="0px" width="10" height="20">
   <circle cx="5" cy="10" r="3"  stroke-width="4" fill="#333" />
   Sorry, your browser does not support inline SVG.
</svg> 
</li>
</ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Convert an HTML webpage into a JSON format that represents the structure of the DOM tree

I am having an index.html file with multiple div tags inside it. I am attempting to retrieve content from all the div tags on the page, but currently, I am only able to fetch the content from the first div tag. What I actually need is the content from all ...

What is the best way to adjust the content of a Bootstrap Column to be at the bottom of the column

Currently diving into the world of Bootstrap for my personal website, I'm encountering a challenge in aligning the content of my sidebar to the bottom. My quest for a solution led me through numerous threads without success. <!-- wordsmith: < ...

provide a hyperlink to the icon located in front of the navigation bar

I'm struggling to come up with a suitable title for this issue. What I am trying to achieve is placing a small icon in front of the navbar so that visitors can click on it and be directed to another site. Initially, I attempted to place the icon using ...

Safari has a unique feature where margins are not factored into the scroll width calculation

I am facing an issue with a container that scrolls horizontally. Inside this container, there are multiple items and I have added a margin to the last item on the right. However, in all browsers except for Safari, the margin is included in the scroll width ...

Could someone lend a hand in getting the X to align properly on this div? I've been struggling with it for ages and just can't seem to crack

This is a demonstration of the CodePen project: Click here to view <div class="annotation"> <div class="annotext"> <div class=annobar></div> <div class="x">✕</div> Lorem ipsum dolor sit amet, consectetur adipiscing e ...

Creating a mouse hover effect for irregular shapes on an image map

Is it possible to implement a mouse hover effect on irregular shapes within an image map? These irregular shapes are not basic polygons like rectangles, triangles, or circles, but rather complex designs such as lanterns or animals. Can this be achieved? ...

Conditionals in ng-class Syntax

I'm attempting to apply a class conditionally to an element, but I'm struggling with the correct syntax. I've experimented with the code below, but it's not functioning as expected: ng-class="{foo: bar === "true"}" The value of bar i ...

Conceal the element if the offspring is devoid of content

I want to implement a feature where a div is hidden when its child element is empty. To achieve this, I aim to assign the class .no-content to the div if it contains no elements. Below is my existing code with spaces: <div class="ee-posts-list&quo ...

What is the best way to generate a new DIV every time a button is clicked?

I am attempting to make a square DIV that is red and measures 100x100 pixels. Each time a button is clicked, I want the square to be created. However, the code I have written is not functioning properly: <html> <title> Create New Div ...

Stop webpage loading with -webkit-transform

I recently encountered a frustrating issue with the Background Image display problem on iPad. I received advice to add -webkit-transform: translateZ(0); to the id, which initially fixed the problem but then led to a new issue. While it resolved the backgro ...

Achieving Centered Columns in Row with Bootstrap 4

Is there a way to increase the spacing between columns in a row in Bootstrap 4? I've tried adjusting it but can't seem to get the columns centered properly. It's ruining the look of my layout. See the image for reference: https://i.sstatic.n ...

Missing inkbar in Material UI tabs when using ReactJS

For some reason, the small inkbar is not appearing under this tab. I suspect it might be a css issue that I can't seem to figure out. It's possible that I'm missing something I don't know about or maybe the height of the tab is too high ...

Are there any CSS selectors similar to :empty that can match elements with children that have display:none?

Looking for a solution to the issue stated in the title, this code snippet is causing some trouble: .group { width: 100px; height: 100px; background-color: blue; } .group:empty { background-color: red; } .hideme { display: none; } <div cl ...

PHPDocumentator mistakenly generated .cs files instead of .css files

After installing PHPDocumentator, I encountered a minor issue. It seems to be generating CSS files with the extension ".cs" instead of ".css". This discrepancy is causing problems since the HTML files reference these stylesheets as "style.css", "print.css" ...

Substitute the website address using regular expressions

Looking to update the iframe URL by changing autoplay=1 to autoplay=0 using regular expressions. jQuery(document).ready(function($){ $('.playButton').click(function(){ $('.flex-active-slide iframe').contents().f ...

Spin a sphere within a semicircle using html and css

I have been working on a project to animate a ball inside a crescent shape using HTML and CSS. You can check out my codepen here. However, I am facing an issue where the ball is currently stuck at the top of the crescent. I would greatly appreciate any as ...

How can I make a sticky element appear behind a different element?

https://jsfiddle.net/30suam6z/1/ .first { height: 100vh; background-color: red; position: relative; z-index: 2; /* Increase z-index to ensure it covers .test */ } .second { height: 100vh; background-color: yellow; position: relative; z- ...

Can someone help me locate the file using the inspect element feature?

Today, I encountered a small issue on my website that I wanted to fix. Although I was able to make the necessary changes using Inspect Element, I couldn't locate the file where it needed to be changed. The website in question is gesher-jds.org/giving. ...

How can I turn off automatic ellipsis on my IOS device?

Currently, I am working on a web application that involves displaying location descriptions retrieved from an API. The issue I am encountering is that the description is being cut off with an ellipsis after a certain number of lines when viewed on an iPhon ...

What could be causing the lack of access to the prerender.io service in this particular setup?

I am the owner of a Angular application running on an Apache server. To enable prerendering, I added <meta name="fragment" content="!" /> in the <head> section and set $locationProvider.html5Mode(true); In my Apache server's .htaccess fi ...