What is the correct technique for splitting an svg shape into two separate parts?

I am working with an svg logo that I want to split into two halves, where each half redirects to a different page when clicked. After cutting the logo in illustrator and attempting to match the pieces together, I encountered an issue.

<div id="lobby">
    <div id="jekyll">
        <a href="#" >
        <img src="lobby/jekyll.svg" alt="dr.Jekyll">
        </a>
    </div>      
    <div id="hyde">
        <a href="#" >
        <img src="lobby/hyde.svg" alt="mr.Hyde">
        </a>
    </div>
</div>

CSS
#jekyll{
    float:left;
    width:50%;

}

#hyde{
    float:right;
    width:50%;

}

Despite matching perfectly at the top, the images start to mismatch towards the bottom. I've even tested this technique on a simpler image with the same result. Is there a correct way to achieve this or perhaps an alternative approach? http://codepen.io/anon/pen/YXdymZ

Answer №1

The SVG images are displaying at different heights within their img containers, despite having the same viewport dimensions.

Although I am unsure of the cause for this discrepancy, setting the heights of the img elements to the same values resolves the issue:

#jekyll img, #hyde img { height: 421px; }

Update:

Using absolute measurements may not be ideal. Another solution involves using additional CSS properties only (see demo here on CodePen):

#jekyll{
    float:left;
    width:50%;
    margin-left: 1px; /* added */
}

#hyde{
    float:right;
    width:calc(50% - 1px); /* added */
}
#hyde img, #jekyll img { margin-left: -1px; } /* added */

Answer №2

The main issue causing the misalignment is due to the discrepancy in dimensions between the two SVG viewBoxes.

Since only the widths of the parent divs were provided, the browser had to calculate the final heights using the aspect ratios from the viewBox.

In your codepen example, assigning a width of 50% to both images (essentially half of #lobby) resulted in an actual width of 148.188px, rounded to 148px for simplicity.

To determine the height of each image, the browser scales the width based on the aspect ratio specified in the viewBox.

For 'Jekyll,' the viewBox dimensions are 61.08 x 163.904.

For 'Hyde,' it is 60.699 x 163.904.

Therefore, the calculated heights for each will be:

Jekyll: 148 * 163.904/61.08 = 397px
  Hide: 148 * 163.904/60.699 = 400px

This difference leads to the alignment problem.

Since 'Hyde' is narrower and left-aligned in the viewbox, adjusting the width value in the Hyde viewBox to match Jekyll's could resolve the issue. Modify the viewBox as follows:

viewBox="481.73 57.015 61.08 163.904"

Although untested, this adjustment should potentially fix the alignment.

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

What is the best way to resize an image back to its original dimensions using CSS?

Is there a way to maintain the original size and aspect ratio of an image in CSS? HTML <img src="https://www.gstatic.com/webp/gallery/1.jpg" /> CSS img { width: 320px; height: 320px; position: relative; transition: 0.2s ease; } i ...

What could be the reason that setting document.body.style.backgroundImage does not apply to the body element itself?

Consider an HTML document with the given CSS: body { background: url("http://via.placeholder.com/200x200"); width: 200px; height: 200px; } Why does the following code not display the background image URL when executed: console.log(document.body. ...

Is there a way to transform HTML special characters within files back to regular characters?

I received some source code files in an HTML output format which makes them difficult to use. For example, I have content like this: %include &quot;macros.mac&quot; It should actually look like this: %include "macros.mac" Is there a way (using ...

Tips for changing form field values using jQuery

Is there a way to use jQuery to retrieve the values of multiple checkboxes and insert them into a hidden form field? Here is how my checkboxes are structured: <form> <input type="checkbox" name="chicken" id="chicken" />Chicken <in ...

Ways to center a vertically aligned SVG in relation to text using Bootstrap

After experimenting with bootstrap, I have encountered an issue where I cannot vertically center an inline SVG relative to text. Below is a simplified example showcasing this problem: <!DOCTYPE html> <html> <head> <link rel=& ...

Making the child element expand to the full width of its parent while maintaining an overflow-x:auto property

Is there a way to stretch a child div without specifying fixed widths? Even when trying to wrap items and child in one wrapper div, the child div always takes up 100% width. Not full horizontal width. Thank you. .parent { background: skyblue; widt ...

How can I use Videogular to play a video file when the user clicks on a div element?

I have a list of items and I want to be able to click on a selected item to play a video file with a specific URL. This means that the player instance should be hidden initially, and when an item is clicked, the video should play in fullscreen mode, loop ...

Resolving Div Alignment Issues in Internet Explorer with jQuery CSS AddClass and RemoveClass

I've set up an HTML table with a hover highlight feature using jQuery and CSS. Here's how my hover event is structured: $('#' + element.id + ' tr:not(:first,[class="summary_row"])').die('mouseover mouseout').live(&a ...

Query database using two dropdown menus and display the results in a table using PHP

I am facing an issue with two dropdowns on my webpage. The second dropdown's options are based on the selection made in the first dropdown. Now, I need to use the selected values from both dropdowns to query a database and display the results in an HT ...

Using the jQuery each loop with Waypoints

Having trouble with the $.each() function in Waypoints and could use some assistance. I've checked other Stack Overflow posts using the same method but still can't seem to get it working properly. You can find my code on JSFiddle: https://jsfidd ...

Tips for automatically disabling cloudzoom based on image width

I'm currently utilizing cloudzoom with the given markup: <div id="container"> <img id="main-image" class="cloudzoom" src="/img/image1.jpg" data-cloudzoom="variableMagnification: false, zoomOffsetX: 0, zoomPosition: 'inside', zoom ...

Converting numerical indexes in an array to an associative array: A step-by-step guide

Struggling with posting dynamic forms currently. Using simple post, looking for a method to retrieve data from entire table. This is the HTML table structure: <form id="importForm" method="post" action="/modules/import/test.php"> <table class="t ...

The height of the navigation bar adjusts to fit the image

I'm currently struggling with my html and css learning project. The height of the navigation bar is not adjusting properly to the size of the image. I have tried various solutions, but nothing seems to work. Any guidance would be appreciated! htm ...

Tips for configuring a Vue component as the background

I am working on a Vue component that utilizes the tsparticles library. I want to set it as a background because it is covering all my other components. Can someone provide guidance on how to achieve this? Thanks! ...

Having a top margin in CSS is resulting in an unnecessary scrollbar appearing on the body

I am faced with a scenario where I need to maintain html, body {height:100%;} but also add margin or position relative to the .content so that it is pushed down 30px from the top. The issue arises when scrollbars appear on the right due to this adjustmen ...

The ngx-material-timepicker lacks proper design when the minutesGap is set to 1

https://i.sstatic.net/J4z5H.png Check out this example of HTML code I have written: <mat-form-field> <mat-label>StartTime</mat-label> <input matInput readonly [ngxTimepicker]="timeStart" [formControlName]="'sta ...

What is the process for creating a default button in Ionic framework?

Recently, I developed an app that includes a survey page. Each question in the survey has two buttons for the user to select: Yes or No. However, as a newcomer to using Ionic, I encountered an issue after building the code and checking the output. One of ...

Is it possible to include two functions within a single HTML script?

On my webpage, there are multiple tabs that display different sets of data. Below is the code for handling the functionality of these tabs: function openTab(event, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassNa ...

PHP - Dynamically populating form questions based on user input

Currently, I am in the process of developing a fuel calculator form that utilizes PHP and HTML. The form is taking shape nicely, but there is one particular aspect that has me stuck. I want to incorporate a fuel perks feature that requires user input. Spe ...

"Divs are now linked and drag as one cohesive unit instead of

As I was experimenting with making images draggable and resizable, I encountered a small issue. When I attempted to drag two or more images, they would merge into one large draggable object instead of remaining separate. My goal was to make each image drag ...