Square div with border. Only show the corners
we can do this with pseudo
elements , check the demo
.box { width: 200px; height: 200px; margin: 15px auto; background: #999; position: relative;}.box:before { position: absolute; content: ""; width: 30px; height: 30px; top: -2px; left: -2px; z-index:-1; border-left: 2px solid red; border-top: 2px solid red;}.box:after { position: absolute; content: ""; width: 30px; height: 30px; right: -2px; bottom: -2px; z-index:-1; border-right: 2px solid red; border-bottom: 2px solid red;}
<div class="box"></div>
Create corner border in CSS
You can use :before
and :after
pseudo-classes like this:
div { position: relative; width: 100px; height: 100px; margin: 20px; border: 1px solid #000;}
div:after { display: block; content: ""; width: 20px; height: 20px; position: absolute; top: -5px; right: -5px; border-top: 3px solid blue; border-right: 3px solid blue;}span:before { display: block; content: ""; width: 20px; height: 20px; position: absolute; bottom: -5px; left: -5px; border-bottom: 3px solid orange; border-left: 3px solid orange;}
<div><span></span></div>
Border at corner only in flutter
This can be done with CustomPaint
widget with CustomPainter set as foregroundPainter
:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CustomPaint spotlight',
home: Scaffold(
appBar: AppBar(
title: Text('CustomPaint spotlight'),
),
body: Center(
child: CustomPaint(
foregroundPainter: BorderPainter(),
child: Container(
width: 200,
height: 100,
color: Colors.deepOrange[50],
),
),
),
),
);
}
}
class BorderPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
double sh = size.height; // for convenient shortage
double sw = size.width; // for convenient shortage
double cornerSide = sh * 0.1; // desirable value for corners side
Paint paint = Paint()
..color = Colors.black
..strokeWidth = 1.5
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
Path path = Path()
..moveTo(cornerSide, 0)
..quadraticBezierTo(0, 0, 0, cornerSide)
..moveTo(0, sh - cornerSide)
..quadraticBezierTo(0, sh, cornerSide, sh)
..moveTo(sw - cornerSide, sh)
..quadraticBezierTo(sw, sh, sw, sh - cornerSide)
..moveTo(sw, cornerSide)
..quadraticBezierTo(sw, 0, sw - cornerSide, 0);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(BorderPainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(BorderPainter oldDelegate) => false;
}
This produces the result like:
You need to know that CustomPaint does not crop its child corners and in some cases that might be discouraging. You have two ways of solving this:
Easy one - to cast rounded borders on its child and make them be visibly equal to CustomPaint borders.
Robust one - to wrap child with
ClipPath
with a path that copies CustomPaint path
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CustomPaint spotlight',
home: Scaffold(
appBar: AppBar(
title: Text('CustomPaint spotlight'),
),
body: Center(
child: CustomPaint(
foregroundPainter: BorderPainter(),
child: ClipPath(
clipper: BorderClipper(),
child: Container(
width: 200,
height: 100,
color: Colors.deepOrange[50],
),
),
),
),
),
);
}
}
class BorderPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
double sh = size.height; // for convenient shortage
double sw = size.width; // for convenient shortage
double cornerSide = sh * 0.1; // desirable value for corners side
Paint paint = Paint()
..color = Colors.black
..strokeWidth = 1.5
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
Path path = Path()
..moveTo(cornerSide, 0)
..quadraticBezierTo(0, 0, 0, cornerSide)
..moveTo(0, sh - cornerSide)
..quadraticBezierTo(0, sh, cornerSide, sh)
..moveTo(sw - cornerSide, sh)
..quadraticBezierTo(sw, sh, sw, sh - cornerSide)
..moveTo(sw, cornerSide)
..quadraticBezierTo(sw, 0, sw - cornerSide, 0);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(BorderPainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(BorderPainter oldDelegate) => false;
}
class BorderClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
double sh = size.height; // for convenient shortage
double sw = size.width; // for convenient shortage
double cornerSide = sh * 0.1;
Path path = Path()
..moveTo(cornerSide, 0)
..quadraticBezierTo(0, 0, 0, cornerSide)
..moveTo(0, sh - cornerSide)
..quadraticBezierTo(0, sh, cornerSide, sh)
..moveTo(sw - cornerSide, sh)
..quadraticBezierTo(sw, sh, sw, sh - cornerSide)
..moveTo(sw, cornerSide)
..quadraticBezierTo(sw, 0, sw - cornerSide, 0);
return path;
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
How to create CSS border corners around text
h1 { display: inline-block; position: relative; padding: 10px;}
h1:before,h1:after { height: 14px; width: 14px; position: absolute; content: '';}
h1:before { right: 0; top: 0; border-right: 3px solid #9b59b6; border-top: 3px solid #9b59b6;}
h1:after { left: 0; bottom: 0; border-left: 3px solid black; border-bottom: 3px solid black;}
<h1>TEXT</h1>
How to do a partial border for a button in css?
Use the :before
as well but instead of creating a single line, create the corner with each element.
.special_button{ position: relative; background-color: white; height: 60px; line-height:60px; font-size: 32px; font-family: dosis_light; border:none; padding:0 15px;}
.special_button::before,.special_button::after{ position: absolute; content: ''; width: 33px; height: 20px;}
.special_button::before{ left:0; top:0; border-left:3px solid black; border-top:3px solid black;}.special_button::after{ right:0; bottom:0; border-right:3px solid black; border-bottom:3px solid black;}
<button class="special_button">ME CONTACTER</button>
Display only the corners of a UIView
Try with this class
, here I use a custom view drawing using CoreGraphics
, added some Inspectable variables to help with customization
//
// CornerView.swift
// CornersViewSO
//
// Created by Reinier Melian on 5/31/17.
// Copyright © 2017 Reinier Melian. All rights reserved.
//
import UIKit
import CoreGraphics
@IBDesignable
class CornerView: UIView {
@IBInspectable
var sizeMultiplier : CGFloat = 0.2{
didSet{
self.draw(self.bounds)
}
}
@IBInspectable
var lineWidth : CGFloat = 2{
didSet{
self.draw(self.bounds)
}
}
@IBInspectable
var lineColor : UIColor = UIColor.black{
didSet{
self.draw(self.bounds)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.clear
}
func drawCorners()
{
let currentContext = UIGraphicsGetCurrentContext()
currentContext?.setLineWidth(lineWidth)
currentContext?.setStrokeColor(lineColor.cgColor)
//first part of top left corner
currentContext?.beginPath()
currentContext?.move(to: CGPoint(x: 0, y: 0))
currentContext?.addLine(to: CGPoint(x: self.bounds.size.width*sizeMultiplier, y: 0))
currentContext?.strokePath()
//top rigth corner
currentContext?.beginPath()
currentContext?.move(to: CGPoint(x: self.bounds.size.width - self.bounds.size.width*sizeMultiplier, y: 0))
currentContext?.addLine(to: CGPoint(x: self.bounds.size.width, y: 0))
currentContext?.addLine(to: CGPoint(x: self.bounds.size.width, y: self.bounds.size.height*sizeMultiplier))
currentContext?.strokePath()
//bottom rigth corner
currentContext?.beginPath()
currentContext?.move(to: CGPoint(x: self.bounds.size.width, y: self.bounds.size.height - self.bounds.size.height*sizeMultiplier))
currentContext?.addLine(to: CGPoint(x: self.bounds.size.width, y: self.bounds.size.height))
currentContext?.addLine(to: CGPoint(x: self.bounds.size.width - self.bounds.size.width*sizeMultiplier, y: self.bounds.size.height))
currentContext?.strokePath()
//bottom left corner
currentContext?.beginPath()
currentContext?.move(to: CGPoint(x: self.bounds.size.width*sizeMultiplier, y: self.bounds.size.height))
currentContext?.addLine(to: CGPoint(x: 0, y: self.bounds.size.height))
currentContext?.addLine(to: CGPoint(x: 0, y: self.bounds.size.height - self.bounds.size.height*sizeMultiplier))
currentContext?.strokePath()
//second part of top left corner
currentContext?.beginPath()
currentContext?.move(to: CGPoint(x: 0, y: self.bounds.size.height*sizeMultiplier))
currentContext?.addLine(to: CGPoint(x: 0, y: 0))
currentContext?.strokePath()
}
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
super.draw(rect)
self.drawCorners()
}
}
EDITED
Example Code of Use
import UIKit
class ViewController: UIViewController {
var cornerViewCode : CornerView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.cornerViewCode = CornerView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(self.cornerViewCode!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
this is how it looks
Hope this helps
Related Topics
See :Hover State in Chrome Developer Tools
Left Aligned Last Row in Centered Grid of Elements
How to Use HTML to Print Header and Footer on Every Printed Page of a Document
Bootstrap Take Full Width for Select Drop Down
How to Set the Size of a Column in a Bootstrap Responsive Table
Footer At Bottom of Page or Content, Whichever Is Lower
Font Awesome Not Working, Icons Showing as Squares
How to Apply a CSS Filter to a Background Image
Margin on Child Element Moves Parent Element
Line Before and After Title Over Image
Is There a CSS Hack to Affect Safari Only Not Chrome
What's the Difference Between Align-Content and Align-Items
Difference Between Margin and Padding
How to Make Flexbox Items the Same Size
Css3'S Attr() Doesn't Work in Major Browsers