Opposing skewed rectangeles - html

I am trying to achieve these opposing skewed rectangles with text inside in css:
The first rectangle is set around the text container with the text skewing the opposite way of the rectangle.
HTML
<div class="col-lg-12 col-md-6">
<div class="container-fluid md">
<div class="rectangles rectangle-1">
<div class="text_container">
<?php if ($text) { ?> <div class="text WYSIWYG-styles"> <?= $text ?> </div> <?php } ?>
</div>
</div>
</div>
</div>
SCSS
display: block;
border: 1px solid $primary-color-2;
padding: 60px;
text-align: center;
&.rectangle-1 {
transform:skew(10deg, 10deg);
}
&.rectangle-2 {
transform: skew(-10deg, 10deg);
}
}
.text_container {
.text {
display: block;
transform: skew(-10deg, -10deg);
}
}
}
The problem is getting the second rectangle to skew the opposite way on the same plane. I thought about about using a pseudo element. I don't think that would work. Obviously, another inner div will just create an inner rectangle.
This is what I have so far:
Any help would be greatly appreciated.

I thought about using a pseudo element
Yea, you can use pseudo elements like this:
.double-skew {
position: relative;
text-align: center;
padding: 2rem;
max-width: 80%;
margin: 0 auto;
}
.double-skew:before, .double-skew:after {
content: "";
position: absolute;
border: 1px solid black;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
.double-skew:before {
transform: skew(30deg);
}
.double-skew:after {
transform: skew(-30deg);
}
<div class="double-skew">
Just wow!
</div>

You really only need the first argument in the skew transform to achieve this effect. The second argument will skew it vertically, which isn't what you want if you're trying to match your spec image.
I created this element using a parent div with relative positioning. Inside I have two spans that act as the border elements and a single p element which contains the text. The spans are absolutely positioned so they don't affect the p's position and can be placed on top of each other. Finally the outer div is a table and the inner p is a table-cell so I can easily align the contents of the p to the vertical and horizontal center.
This is done with plain CSS so it can be shown in a snippet, but you could just as well convert it to SASS.
div
{
display: table;
width: 400px;
height: 100px;
position: relative;
margin-left: 50px;
}
div span
{
display: block;
position: absolute;
width: 100%;
height: 100%;
box-sizing: border-box;
border: 2px solid black;
}
div span.left
{
transform: skew(30deg);
}
div span.right
{
transform: skew(-30deg);
}
div p
{
display: table-cell;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
text-align: center;
vertical-align: middle;
}
<div>
<span class="left"></span>
<span class="right"></span>
<p>Lorem ipsum dolor sit amet.</p>
</div>

Related

Offset a border over an image

I'm trying to create exactly like what is shown in the picture below. I have tried other methods using outline and offset method too but I could't figure out how to go on about doing this.
Here's the JSFiddle:
img {
border: 4px solid green;
}
<img src="https://image.ibb.co/cxwWPa/services_gas_oil.jpg" alt="services_gas_oil" border="0">
How do I do to get this offset border over the image?
Wrap the image with an inline block, and set an absolutely positioned pseudo-element as the border:
body {
padding: 50px 0 0 80px;
}
.imageContainer {
display: inline-block;
position: relative;
}
.imageContainer::before {
position: absolute;
top: -5%;
left: -15%;
width: 100%;
height: 100%;
border: 4px solid #77B244;
content: '';
}
<div class="imageContainer">
<img src="https://cdn.pixabay.com/photo/2013/07/31/12/25/cat-169095_960_720.jpg" alt="services_gas_oil" border="0">
</div>
A simpler way would be to use a combination of border, outline and a negative outline-offset. Here is an example :
img{
outline:4px solid #77B244;
outline-offset:-100px;
border:50px solid transparent;
border-width:150px 50px 50px 150px;
margin:-75px 0 0 -75px;
}
<img src="https://image.ibb.co/cxwWPa/services_gas_oil.jpg" alt="services_gas_oil" border="0">
This prevents the use of a parent element and a pseudo element.
Another possibility would be to wrap image inside a div element having a border and move image inside container towards left and bottom.
As from Documentation:
A relatively positioned element is an element whose computed position value is relative. The top and bottom properties specify the vertical offset from its normal position; the left and right properties specify the horizontal offset.
We will need to wrap img inside an element like div i.e:
<div class="image-holder">
<img src="https://image.ibb.co/cxwWPa/services_gas_oil.jpg" >
</div>
We will apply border to the container and move image from its normal position with following CSS:
.image-holder img {
position: relative;
z-index: -1;
left: 40px;
top: 40px;
}
.image-holder {
border: 7px solid #76af46;
display: inline-block;
vertical-align: top;
}
.image-holder img {
position: relative;
z-index: -1;
left: 40px;
top: 40px;
}
<div class="image-holder">
<img src="https://image.ibb.co/cxwWPa/services_gas_oil.jpg" alt="services_gas_oil" border="0">
</div>
Alternatively, we can use CSS3 translate() as well.
.image-holder img {
transform: translate(40px, 40px);
position: relative;
z-index: -1;
}
.image-holder {
border: 7px solid #76af46;
display: inline-block;
vertical-align: top;
}
.image-holder img {
transform: translate(40px, 40px);
position: relative;
z-index: -1;
}
<div class="image-holder">
<img src="https://image.ibb.co/cxwWPa/services_gas_oil.jpg" alt="services_gas_oil" border="0">
</div>

Is there a semantically better way to create this layout of text in circles?

I've come up with what seems like a very hacky, non-semantic way to code a design that I'd like to use. Basically, it's a set of 4 equal-sized circles, distributed so their centers are the same as those of equilateral triangles. I've used a bunch of presentational divs to solve two issues: (1) to get the spacing of the circles right, I need their bounding boxes to overlap; and (2) to vertically space text in the circles without changing their size, it seems like I need to use display:table in my CSS.
It works, but I hate it, and I feel like there has to be a better way. I am new to CSS, and this method is the result of a fair amount of research about how to solve this design problem.
The design is at this codepen: http://codepen.io/bhagerty/pen/rejEPZ
(I put borders on a bunch of the elements just to show the structure.)
Here is the HTML:
<body>
<h1 id="home_title">test</h1>
<div id="container_1">
<div id="picture" class="box">
<div class="circle_outer">
<div class="circle_inner">
<div class="inner-text">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/%22In_Which_We_Serve%22_Advertisement_1943.jpg/1024px-%22In_Which_We_Serve%22_Advertisement_1943.jpg" width=100%; />
</div>
</div>
</div>
</div>
<div id="dog" class="box">
<div class="circle_outer">
<div class="circle_inner">
<div class="inner-text">
dog
</div>
</div>
</div>
</div>
<div id="shoes" class="box">
<div class="circle_outer">
<div class="circle_inner">
<div class="inner-text">
shoes
</div>
</div>
</div>
</div>
<div id="dance" class="box">
<div class="circle_outer">
<div class="circle_inner">
<div class="inner-text">
dance
</div>
</div>
</div>
</div>
<div id="footer_1">
Footer<br>
test
</div>
</div>
</body>
Here is the CSS:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
font-size: 16px;
}
h1#home_title {
text-align: center;
font-size: 3rem;
margin: 0;
padding: .1rem 0 .5rem 0;;
background-color: grey;
}
div#container_1 {
border: green solid 5px;
width: 320px;
margin: auto;
position: relative;
}
div.box {
border: red solid 1px;
position: absolute;
width: 53.6%;
text-align: center;
background-color: transparent;
}
/*pseudo-element to give relative height,
per http://jsfiddle.net/simevidas/PFPDU/
and http://www.mademyday.de/css-height-equals-width-with-pure-css.html */
div.box::before {
content: "";
display: block;
padding-top: 100%;
height: 0;
}
/* if inner text has position relative, it influences the size of the containing box */
/*setting all of the positions to zero forces it inside the circle for some reason */
.circle_outer {
position: absolute;
overflow: hidden;
border: black solid 2px;
border-radius: 50%;
/* to create breathing room all around, set top and left to 1/2 of 100% - width (where width = height) */
top: 5%;
left: 5%;
width: 90%;
height: 90%;
}
.circle_inner {
/* border: grey solid 5px; */
display: table;
width: 100%;
height: 100%;
}
.inner-text {
display: table-cell;
/* border: green solid 2px; */
font-size: 2em;
vertical-align: middle;
}
/*First bounding box is at upper left corner */
div#picture {
overflow: hidden;
left: 0;
margin-top: 0;
}
/*Percent positions all based on W, derived from fact
that bounding boxes circumscribe tangent circles, and
circle centers are connected by equilateral triangles */
div#dog {
left: 46.4%;
margin-top: 26.8%;
}
div#shoes {
left: 0;
margin-top: 53.6%;
}
div#dance {
left: 46.4%;
margin-top: 80.4%;
}
div#footer_1 {
border: red solid 2px;
position: relative;
width: 100%;
left: 0;
margin-top: 137%;
text-align: center;
background-color: blue;
}
I much appreciate any thoughts or help. Thanks!
Well, IMO what you've done is really good. I wouldn't be too concerned about the extra divs.
But, it can be done with fewer divs, making use of float and margins.
Codepen is here
html {
font-size: 16px;
}
h1#home_title {
text-align: center;
font-size: 3rem;
margin: 0;
padding: .1rem 0 .5rem 0;;
background-color: grey;
}
div#container_1 {
border: green solid 5px;
width: 320px;
margin: auto;
position: relative;
box-sizing: border-box;
}
div.box {
border: red solid 1px;
position: relative;
float:left;
width: 53.6%;
text-align: center;
background-color: transparent;
box-sizing:border-box;
margin-bottom:-27%;
}
div.box:nth-child(2n) {
float:right;
}
div.box:nth-child(2n+1) {
float:left;
}
/*pseudo-element to give relative height,
per http://jsfiddle.net/simevidas/PFPDU/
and http://www.mademyday.de/css-height-equals-width-with-pure-css.html */
div.box::before {
content: "";
display: block;
padding-top: 100%;
height: 0;
}
/* if inner text has position relative, it influences the size of the containing box */
/*setting all of the positions to zero forces it inside the circle for some reason */
.featuring {
position: absolute;
overflow: hidden;
border: black solid 2px;
border-radius: 50%;
/* to create breathing room all around, set top and left to 1/2 of 100% - width (where width = height) */
top: 5%;
left: 5%;
width: 90%;
height: 90%;
font-size: 2em;
}
.featuring:before {
content:'';
margin-left:-0.25em;
display:inline-block;
vertical-align:middle;
height:100%;
}
/*Percent positions all based on W, derived from fact
that bounding boxes circumscribe tangent circles, and
circle centers are connected by equilateral triangles */
div#footer_1 {
border: red solid 2px;
position: relative;
width: 100%;
left: 0;
margin-top: 137%;
text-align: center;
background-color: blue;
clear:both;
}
<body>
<h1 id="home_title">test</h1>
<div id="container_1">
<div id="picture" class="box">
<div class="featuring">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/%22In_Which_We_Serve%22_Advertisement_1943.jpg/1024px-%22In_Which_We_Serve%22_Advertisement_1943.jpg" width=100%; />
</div>
</div>
<div id="dog" class="box">
<div class="featuring">
dog
</div>
</div>
<div id="shoes" class="box">
<div class="featuring">
shoes
</div>
</div>
<div id="dance" class="box">
<div class="featuring">
dance
</div>
</div>
<div id="footer_1">
Footer<br>
test
</div>
</div>
</body>

Div float next to non float div

I have a container div for the main content but am trying to have a sidebar float to the left of it. For example (http://www.bureautonic.com/en/) the menu button.
This is the code
.main-wrapper {
position: relative;
top: 50%;
height: 500px;
}
.container {
position: relative;
height: 100%;
}
.body {
height: 100%;
}
.slider {
display: block;
width: 940px;
height: 500px;
margin-right: auto;
margin-left: auto;
float: none;
}
.img {
position: absolute;
width: 100%;
height: 100%;
max-height: 100%;
}
.tagline {
position: absolute;
left: 50%;
top: 50%;
z-index: 1;
display: block;
width: 332px;
margin-right: auto;
margin-left: auto;
padding: 1em 3em;
border: 1px solid white;
-webkit-transform: translate(-50%, 0px) translate(0px, -50%);
-ms-transform: translate(-50%, 0px) translate(0px, -50%);
transform: translate(-50%, 0px) translate(0px, -50%);
font-family: 'Josefin Sans', sans-serif;
color: white;
text-align: center;
text-decoration: none;
text-transform: none;
}
.header {
margin-top: 33px;
margin-bottom: -61px;
}
.brand {
font-family: Cardo, sans-serif;
text-align: center;
}
<body class="body">
<div class="w-section container">
<div class="w-container header">
<h1 class="brand">The One And Only</h1>
</div>
<div class="w-container main-wrapper">
<div data-animation="outin" data-duration="500" data-infinite="1" data-easing="ease-in-cubic" data-hide-arrows="1" class="w-slider slider">
<div class="w-slider-mask">
<div class="w-slide slide">
<div class="tagline">
<h1>Marc Cain</h1>
<h3>F/W 2015-16</h3>
</div>
<img width="846" src="http://uploads.webflow.com/567a26541a69a693654038a1/567b15da06a9675444fc740d_marc_cain_campaign.jpg" class="img">
</div>
</div>
<div class="w-slider-arrow-left">
<div class="w-icon-slider-left"></div>
</div>
<div class="w-slider-arrow-right">
<div class="w-icon-slider-right"></div>
</div>
<div class="w-slider-nav"></div>
</div>
</div>
</div>
</body>
I'm using webflow and uploaded the site for you guys http://the-one-and-only.webflow.io/
I originally tried making another absolute div with a set width and 100% height, but the menu button wasn't relative to the main container. Any help would be appreciated.
Give this a look, it mimics what http://www.bureautonic.com/en/ has for their menu
$(function() {
$('#menu-container').click(
function() {
ToggleMenu();
}
);
});
function ToggleMenu() {
var $menu = $('#menu');
var newleft = +$menu.css('left').replace('px', '') <= -150 ? '0' : '-300px';
$('#menu').css('left', newleft);
}
#menu,
#content {
width: 300px;
height: 200px;
}
#menu-container {
display: inline-block;
height: 200px;
width: 30px;
background-color: yellow;
position: relative;
}
#menu {
display: inline-block;
position: absolute;
}
#content {
display: inline-block;
position: relative;
overflow: hidden;
background-color: red;
}
#menu {
transition: left 1s;
left: -300px;
background-color: orange;
}
#menu-label {
-moz-transform: translateX(-50%) translateY(-50%) rotate(-90deg);
-webkit-transform: translateX(-50%) translateY(-50%) rotate(-90deg);
transform: translateX(-50%) translateY(-50%) rotate(-90deg);
position: absolute;
top: 50%;
text-align: center;
width: 200px;
left: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="menu-container">
<div id="menu-label">
This is my Menu Label
</div>
</div>
<div id="content">
This is the content
<div id="menu">
Menu
</div>
</div>
For the sliding menu
The basic concept is a parent div with position:relative and overflow:hidden , and a child div with position:absolute, beginning with a negative left equal to the width of the div
I used the css transition property for the smooth slide effect
Edit:
For the left aligned & rotated menu label
This effect is created with a combination of several properties.
My code block has been updated with the appropriate css.
See here http://jsfiddle.net/CCMyf/79/ (not my fiddle) for alterations
to the css if you need to have a dynamic height
If you want to float a menu to left of the main content, you need to firstly create the menu element that you want to be the menu (obviously), then float it to the left with float: left. e.g.
HTML
<div class="floated-menu">
Menu
</div>
CSS
.floated-menu {
float: left;
width: 50px;
height: 600px;
background-color: #ccc;
}
Then you have to float the main content container as well. .e.g
.container {
float: left;
position: relative;
width: 800px;
height: 100%;
}
I could be wrong, but I believe if you don't float both the items, the normal (non-floated context) behaviour of the container divs display: block; property kicks in and it will move down the page to the next "line". Which is weird because all items next to something thats floated should lose their display block behaviour and sit next to the floated item - i.e. float was originally intended to make block type headings and paragraphs sit next to pictures like in a magazine or newspaper, but yep, welcome to the world of CSS - you fill find many nonsensical things like this.
Also, the combined width of both floated elements border box (the widest and largest of the boxes that an element is contained in) cannot be wider than their parent element - other wise the second element will drop down to the next line - which actually does make sense. I have reduced the sizes for you in my demo, but you will have to manage that as you build your page.
You also need to remember that, by default the browser uses the
"content-box" box-sizing property. from the docs
content-box
This is the default style as specified by the CSS standard. The width
and height properties are measured including only the content, but not
the padding, border or margin. Note: Padding, border & margin will be
outside of the box e.g. IF .box {width: 350px}; THEN you apply
{border: 10px solid black;} RESULT {rendered in the browser} .box
{width: 370px;}
Here is a demo - http://codepen.io/anon/pen/QyKyVV?editors=110

Calculating hamburger object bar position

I have this hamburger I'm creating. I'm trying to understand how to position the bars with precise calculation without eyeballing it. The hamburger height is 24px
so I would assume in order to position the middle bar it would be half the height so 12px(top: 12px;) or incase of the example 0.5em but that doesn't look centered to the overall menu. (top: 10px looks right or 0.625em).
Overall whats a better way to calculate anything in css?
http://codepen.io/anon/pen/Bjjqez
<div class="hamburger">
<div class="hamburger__top"></div>
<div class="hamburger__middle"></div>
<div class="hamburger__bottom"></div>
</div>
SASS
.hamburger {
position: relative;
width: 3em;
height: 1.5em;
cursor: pointer;
div {
background-color: red;
position: absolute;
width: 100%;
height: .25em;
}
&__top {
top: 0;
}
&__middle {
background-color: green;
top: 0.5em;
}
&__bottom {
bottom: 0;
}
}
Flexbox can do that....no positioning required.
Here's a couple of options.
.hamburger {
width: 3em;
height: 1.5em;
cursor: pointer;
display: flex;
flex-direction: column;
margin: 1em auto;
}
.hamburger div {
background-color: red;
height: .25em;
}
.around {
justify-content: space-around;
background: lightblue;
}
.between {
justify-content: space-between;
background: lightgreen;
}
<div class="hamburger around">
<div class="hamburger__top"></div>
<div class="hamburger__middle"></div>
<div class="hamburger__bottom"></div>
</div>
<div class="hamburger between">
<div class="hamburger__top"></div>
<div class="hamburger__middle"></div>
<div class="hamburger__bottom"></div>
</div>
Replace your top: 0.5em with:
top: 50%;
transform: translateY(-50%);
From my understanding of how this works, the top: 50% will move the top of the element to be half way down. The translateY(-50%) will then move the element up by half it's own height, and therefore centrally position it.
Example: http://codepen.io/anon/pen/RrrqWP

How to put text over img on hover - width and height are variable

I know how to put text on hover on an image if the height and the width is fixed. but I have a responsive slider (owl-slider) and want to add link (easy - yeah.) and a blue overlay with white text in it and a simple fading/sliding transition from the overlay.
The problem is: every item changes its height and width on resizing. I could write several media queries, but I'm quite sure there must be a simpler solution to that problem.
I have a very simple markup:
<div>
<a href="#">
<img src="http://placehold.it/360x100">
<div class="overlay">Click here for more Infomartion</div>
</a>
</div>
Normally I would go for pure css method with setting height and width from .overlay to the image size and set visibility on hover. But.. that won't work, because the width & height will differ from viewport to viewport. So, what would you suggest?
The trick involves setting position: relative to the parent container .image-container which contains the image. Using display: inline-block will force the parent container to shrink-to-fit the image.
You then apply position:absolute to the child container (overlay) .hover-text and set all the offsets (left, right, top and bottom) to zero, which will force the overlay to match the size of the image.
If you want to vertically center the text, you need to add two nested blocks.
One way of doing it is to repurpose the a element using display: table with width and height of 100%, and then apply display: table-cell to the nested div with vertical-align: middle. This will center the text vertically if so desired.
I added a transition effect to demonstrate how to set it up. You can
adjust the details as you like for duration and transition type.
Ref: http://www.w3.org/TR/css3-transitions/
You could also do a translation using a CSS transform, which is also feasible since the modern browsers support transforms (especially in 2D).
.image-container {
position: relative;
display: inline-block;
}
.image-container .hover-text {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: rgba(0, 0, 255, 0.5);
opacity: 0;
transition: opacity;
}
.hover-text a {
display: table;
height: 100%;
width: 100%;
text-decoration: none;
}
.hover-text a div {
display: table-cell;
vertical-align: middle;
text-align: center;
font-size: 3.0em;
color: white;
}
.image-container img {
vertical-align: top; /* fixes white space due to baseline alignment */
}
.image-container:hover .hover-text {
opacity: 1;
transition-duration: 1s;
transition-timing-function: linear;
}
<div class="image-container">
<img src="http://placehold.it/360x100">
<div class="hover-text">
<a href="#">
<div>Text on hover</div>
</a>
</div>
</div>
Try this, it doesn't care about the image size
.image-container{
position: relative;
display: inline-block;
}
.image-container .hover-text{
position: absolute;
top: 33%;
width: 100%;
text-align: center;
visibility: hidden;
}
.image-container:hover .hover-text{
visibility: visible;
}
/* styling */
.hover-text{
font-family: sans-serif;
color: white;
background: rgba(0,0,0,0.3);
text-shadow: 1px 1px 1px black;
padding-top: 0.5em;
padding-bottom: 0.5em;
}
.hover-text a{
color: white;
}
<div class="image-container">
<img src="http://placehold.it/360x100">
<div class="hover-text">
Text on hover Link
</div>
</div>
Skipped the transition stuff, but is this what you're requesting?
div {
position: relative;
}
img {
width: 100%;
}
.overlay {
background: blue;
color: white;
display: none;
position: absolute;
left: 0;
top: 50%;
width: 100%;
text-align: center;
}
a:hover .overlay {
display: block;
}
JSFIDDLE: http://jsfiddle.net/volzy/hLpLabaz/1/
For full size overlay do:
.overlay {
height: 100%;
top: 0;
}