How to create a partial border(with a hole)?
Use "nested backgrounds" to generate borders like this:
pane.setStyle("-fx-background-color: blue, -fx-background, -fx-background;"
+ "-fx-background-insets: 0 0 0 0, 0 50 2 100, 2 2 2 2;");
The JavaFX CSS documentation has all the details, but basically the way this works is that it creates three backgrounds, which are laid one over the other. The first is blue, with insets of 0 on every side. I.e.:
pane.setStyle("-fx-background-color: blue; -fx-background-insets 0 0 0 0;");
This creates a blue rectangle which fills the entire pane:
The second background has the default root background from modena (-fx-background
). It has insets of 0
at the top, 50
to the left, 2
at the bottom, and 100
at the right. So the first two nested backgrounds:
pane.setStyle("-fx-background-color: blue, -fx-background;"
+ "-fx-background-insets: 0 0 0 0, 0 50 2 100;");
produce this:
Note the 2-pixel wide blue line at the bottom.
Finally, the third background also has the default background color (-fx-background
), and insets of two pixels on each side. So when this is laid over the previous two backgrounds, the desired effect is seen:
When the values for all four sides are the same, you can replace the four values with a single value. So the CSS can be abbreviated
pane.setStyle("-fx-background-color: blue, -fx-background, -fx-background;"
+ "-fx-background-insets: 0 , 0 50 2 100, 2;");
Just a technical note: the default style sheet is not loaded unless at least one control is created, which is why I add a Label
in the complete example below.
Here is an SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class BorderWithGap extends Application {
@Override
public void start(Stage primaryStage) {
AnchorPane pane = new AnchorPane();
Label label = new Label("Border with gap");
AnchorPane.setTopAnchor(label, 50.0);
AnchorPane.setLeftAnchor(label, 50.0);
pane.getChildren().add(label);
pane.setStyle("-fx-background-color: blue, -fx-background, -fx-background;"
+ "-fx-background-insets: 0, 0 50 2 100, 2;");
Scene scene = new Scene(pane, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Centered partial-width bottom-only borders on divs
To position the border at the bottom, you need to make 2 changes to your CSS rules:
- Specify that the line should be at the
bottom:0
of the absolute-positioned element:
.parent__feature-name:before { bottom:0; }
- Make the parent element
position: relative
so that the absolute position will be relative to this, i.e.:
.parent__feature-name { position: relative; }
Working Demo (using CSS):
.parent {
width: 200px;
}
.parent__feature-name {
font-weight: 700;
background-color: #fafafa;
position: relative;
}
.parent__feature-name:before {
content: "";
width: 70%;
position: absolute;
margin: auto;
right: 0;
left: 0;
bottom:0;
border-bottom: 1px solid #e6e6e6;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<div class="parent">
<div class="parent__feature-name p-2"> example</div>
<div class="parent__feature-name p-2"> example</div>
<div class="parent__feature-name p-2"> example</div>
<div class="parent__feature-name p-2"> example</div>
</div>
UIView border with partial border
If you want to use UIBezierPath
, you could choose the "silly" way:
- stroke the full border of your view
- "cover up" the part where you don't want the border to exist with the view's background color, by drawing a rectangle on top of the border.
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
layer.cornerRadius = 3
backgroundColor = .white
layer.masksToBounds = true
}
override func draw(_ rect: CGRect) {
let borderPath = UIBezierPath(roundedRect: bounds, cornerRadius: 3)
borderPath.lineWidth = 7
UIColor.gray.setStroke()
borderPath.stroke()
let covering = UIBezierPath(rect: CGRect(x: 20, y: -10, width: self.bounds.width - 40, height: 20))
backgroundColor?.setFill()
covering.fill()
}
}
Output:
Other than that, I can't think of an easy way to do this.
However, if you can use CAShapeLayer
...
You should set the strokeEnd
and strokeStart
properties of a CAShapeLayer
added as a sublayer of your view's layer.
Example:
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
layer.cornerRadius = 3
let border = CAShapeLayer()
// make sure this path coincides with the border of the view
border.path = UIBezierPath(roundedRect: bounds, cornerRadius: 3).cgPath
// you should tweak these numbers
border.strokeStart = 0.3
border.strokeEnd = 0.7
border.strokeColor = UIColor.gray.cgColor
border.lineWidth = 3
border.fillColor = nil
layer.addSublayer(border)
backgroundColor = .white
}
}
Output:
HTML5 / CSS3 Circle with Partial Border
Yes, it is possible - see this:
demo
.circle {
position: relative;
margin: 7em auto;
width: 16em;
height: 16em;
border-radius: 50%;
background: lightblue;
}
.arc {
overflow: hidden;
position: absolute;
/* make sure top & left values are - the width of the border */
/* the bottom right corner is the centre of the parent circle */
top: -1em;
right: 50%;
bottom: 50%;
left: -1em;
/* the transform origin is the bottom right corner */
transform-origin: 100% 100%;
/* rotate by any angle */
/* the skew angle is 90deg - the angle you want for the arc */
transform: rotate(45deg) skewX(30deg);
}
.arc:before {
box-sizing: border-box;
display: block;
border: solid 1em navy;
width: 200%;
height: 200%;
border-radius: 50%;
transform: skewX(-30deg);
content: '';
}
<div class='circle'>
<div class='arc'></div>
</div>
Is there a shorter way to write a border on only one side?
You can apply border-width of 2px only to the top edge according to the documentation as following
<div class="border-t-2 border-blue-900">foo</div>
The error made was there is no utility class called border-t-1
in tailwind-CSS. Also applying border
utility class adds the the CSS styling of border-width: 1px;
which adds a border-width to all sides.
Check out the solution at tailwind playground
EDIT: Check out shingo.nakanishi's answer top apply border-top-width
of 1px
if you are using JIT mode
Any way to limit border length?
#mainDiv {
height: 100px;
width: 80px;
position: relative;
border-bottom: 2px solid #f51c40;
background: #3beadc;
}
#borderLeft {
border-left: 2px solid #f51c40;
position: absolute;
top: 50%;
bottom: 0;
}
<div id="mainDiv">
<div id="borderLeft"></div>
</div>
How do I remove a section of the border on a div to make left nav look seamless with main content section?
You can't partially remove borders, you can only cover them up. You need to make the selected item overlap the border (or have a script that places another element over the border).
I want to make border around three sides of AnchorPane, not four
Try:
pane.setStyle("-fx-border-color: green; -fx-border-width: 1px 1px 1px 0px");
Related Topics
When Do Nested Child Elements Expand Their Parent Elment
Chrome Will Increase The Font Size When Zooming Out
Background Images Not Showing in Ie11
How to Align a Button to The Far Right in Mui Appbar
How to Keep CSS Style for Only One Element
Is There an Equivalent to Jquery's: First Selector in CSS3
Setting Linear Gradient Height and Width
CSS Animation Keyframes with Display None Works to Fade In, But Not Fade Out
Viewport-Unit Font-Size and Zooming Bug: Which Browsers Are Affected
Cannot Find Definitions of Editor-Label and Editor-Field in MVC 4, Did Ms Remove It? Why
How to Prevent SASS from Merging Files Included via @Import
Font-Weight:900 Only Working in Firefox
How to Make Header Image Responsive
How to Fix Safari Mix-Blend-Mode: Color-Dodge Bug
Resource Interpreted as Stylesheet But Transferred with Mime Type Text Plain Issue