Reputation: 1198
If I hover over the rectangle, the border size is exceed the rectangle size. I know because parent node width is 100
that's why the rectangle border is shown.
If I set the parent node width as 40
, it's working fine but I need that same behavior without changing parent node's width
and stroke-width
.
How can I achieve this?
// In mouse hover, I have set the style:
var links = $('#ch');
links.hover(function() {
$('#ch').attr('class', 'st');
}, function() { //mouseout
$('#ch').attr('class', '');
});
.st {
stroke-width: 60px;
stroke: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<svg id='par' width="100" height="40">
<rect id='ch' width="40" height="40" style="fill:rgb(0,0,255)" />
</svg>
Upvotes: 2
Views: 225
Reputation: 1198
found the answer,
<svg width="100" height="100">
<defs>
<clipPath id="clipRect">
<rect width="40" height="40" />
</clipPath>
</defs>
<rect id ='ch' width="40" height="40" clip-path=url(#clipRect) style="fill:rgb(0,0,255)" stroke-width="8px" stroke="red" />
</svg>
Upvotes: 0
Reputation: 123985
You mean like this? Changing the fill colour would be simpler.
Other ways would be creating a clipPath or using the clip CSS property.
// In mouse hover, I have set the style:
var links = $('#ch');
links.hover(function() {
$('#ch').attr('class', 'st');
}, function() { //mouseout
$('#ch').attr('class', '');
});
.st {
stroke-width: 60px;
stroke: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<svg id='par' width="100" height="40">
<svg width="40" height="40">
<rect id='ch' width="40" height="40" style="fill:rgb(0,0,255)" />
</svg>
</svg>
Upvotes: 0