Put content in shaped divs - css

I'm trying to make divs that are shaped like slices of a circle, using this code (this is for the top section of the circle) in CSS:
width: 0;
height: 0;
border-left: 250px solid transparent;
border-right: 250px solid transparent;
border-top: 250px solid #FFA8A8;
border-bottom: 250px solid transparent;
position: fixed;
border-radius: 100%;
top: 50%;
left: 50%;
margin-top: -250px;
margin-left: -250px;
and that makes the divs show up the way I want them to on the page, but when I try to put any text into them, it doesn't show up. I think I get why (because the actual height and width of the div are 0 and what shows up on the page is just the border), but how would I make divs that look the same but can contain text or images?

instead border, use overflow to cut off round parts.
flex can also help with centering things
example:
body>div {
width: 80vmin;
height: 80vmin;
border-radius: 50%;
overflow: hidden;
display: flex;
flex-wrap: wrap;
counter-reset: divs;
}
div div {
background: #FFA8A8;
height: 40vmin;
width: 40vmin;
counter-increment: divs;
display: flex;
}
div div:nth-child(odd) {
background: gray;
order: 1
}
div div:last-child {
order: 2
}
div div:before {
content: counter(divs);
transform: rotate(45deg);
margin: auto;
font-size: 10vmin
}
html {
min-height: 100%;
display: flex;
}
body {
margin: auto;
transform: rotate(-45deg)
}
<div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
pen

Using the code you provided, and going off possibly incorrect assumptions, I have come up with some code that will work for you. You can add text inside the div using absolute positioning and the top and left values to get your text where you would like it.
Changes Made:
Added flex to the body to center the slice. Took out width, height, position, top, left, and margin. Reduced the border radius to 50% since that is all that is needed to create the rounded edge. Reduced the border by 100px purely so that the example slice was not so big.
body{
display:flex;
align-items: center;
justify-content:center;
}
.slice{
border-left: 150px solid transparent;
border-right: 150px solid transparent;
border-top: 150px solid #FFA8A8;
border-bottom: 150px solid transparent;
border-radius: 50%;
}
p{
position: absolute;
left:39%;;
top: 30px;
}
<div class="slice">
<p>Some Text That Fits</p>
</div>

Related

center element(with 50px margin) using transform translate

I have an element positioned absolute with margin 50px so after setting left 50% and transform translate (-50%) element is not center because of margin.
Is there an option center element horizontally and keep the margin 50px and transform translate (-50%)
I have a #mixin theme-btn with margin 50px
.btn{
#include theme-btn();
&--uslugi{
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
}
Add a parent div to your div, for example:
HTML
<div class="box">
<div class="box-in">
</div>
</div>
and CSS
.box {
display: flex;
justify-content: center;
}
.box-in {
margin: 50px;
height: 100px;
width: 400px;
border: 1px solid green;
}

Margin auto with additional space

Is there a way to center element horizontally in page with margin auto but also to have 100px left and right if viewport gets smaller, so it would be like this together:
margin: 0 auto;
margin-left:100px;
margin-right:100px;
Or do I need to have parent container for this?
the problem with auto is you can't even use it with calc(auto + 100px)
the most better and accurate way is to use flex
the justify-content property will center your element like margin: 0 auto; and you still have a room to play with margin
.parent{
height: 200px;
background: gray;
display:flex;
justify-content:center; /*center element*/
}
.child{
height: 100px;
width: 200px;
background: yellow;
margin: 0 100px; /*adding margin*/
}
<div class='parent'>
<div class="child"></div>
</div>
You can use border-left and border-right to add a border to the element's left and right hand side. Make sure you set the border color to transparent and add background-clip: padding-box to make sure the border is truly invisible:
.centered {
margin: 0 auto;
width: 100px;
height: 100px;
background: blue;
background-clip: padding-box;
border-left: 100px solid transparent;
border-right: 100px solid transparent;
}
<div class="centered"></div>
Another approach is to use a parent element with padding:
.parent {
padding: 0 100px;
}
.centered {
margin: 0 auto;
width: 100px;
height: 100px;
background: blue;
}
<div class="parent">
<div class="centered"></div>
</div>
Depending on your use case you might be able to use the <body> as the parent, saving you from adding an otherwise superfluous parent element:
body {
padding: 0 100px;
}
.centered {
margin: 0 auto;
width: 100px;
height: 100px;
background: blue;
}
<div class="centered"></div>
You could use padding for this

is it possible to inject background property in the margin of a block element?

demo:https://codepen.io/joondoe/pen/BaBJjqe
I have seen that the css box model include margin as the most outter component of the box model. I am wondering if it is possible to add a background color in the margin of a box element.
div{
display:flex;
text-align:center;
justify-content:center;
align-items:center;
background:orange;
height: 30px;
border: 15px solid green;
margin:50px;
/* to illustrate what I would accomplish */
margin-background:pink;
}
<div> I am a div </div>
I guess you are simply looking for box-shadow:
div{
display:flex;
text-align:center;
justify-content:center;
align-items:center;
background:orange;
height: 30px;
border: 15px solid green;
margin:50px;
box-shadow:0 0 0 50px pink;
}
<div> I am a div </div>
No, you can't do this with a margin, and I see that you've already used border which would be the obvious one to use for what you're asking for.
Other options to achieve the kind of effect you're looking for include:
box-shadow
outline
::before and ::after
Each of these works quite differently, but they could all pull off the effect you've asked for, namely an additional coloured shell around a box, outside of the border.
If you want other background effects such as background images, however, your options are probably limited to using ::before and ::after.
It's not possible (as other answers pointed out), but you could keep what you're doing with use of the ::before (or ::after) pseudo
div {
display: flex;
text-align: center;
justify-content: center;
align-items: center;
background: orange;
height: 30px;
border: 15px solid green;
margin: 50px;
position: relative;
}
div::before {
content: '';
display: block;
position: absolute;
width: calc(100% + 100px);
height: calc(100% + 100px);
left: -50px;
top: -50px;
background: pink;
z-index: -1;
}
<div>
test
</div>
It's not possible to change background color of margin property. I'd prefer to go down the root of wrapping the element in a container that respects your margin instead abusing other properties and pseudo styles. This supports all browsers.
.container {
background-color: red;
display: flex;
}
.container div {
flex: 1 1;
display: flex;
text-align: center;
justify-content: center;
align-items: center;
background: orange;
height: 30px;
border: 15px solid green;
margin: 50px;
}
<div class="container">
<div>I am a div </div>
</div>

CSS background image disappears when float property is set to 'none'

I have a series of three boxes, each approximately one-third of the screen width. The first two boxes have widths of 33.333% and are floated left. I've removed the float on the third box and set the width to auto so that it fills the remaining horizontal space. However, doing so causes the background image to disappear. I've created a Fiddle demonstrating this effect.
The HTML is very simple:
<div></div>
<div></div>
<div></div>
And here's the CSS:
div {
float: left;
width: 33.333%;
height: 120px;
background: #EEE url('http://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Approve_icon.svg/200px-Approve_icon.svg.png') no-repeat center center;
background-size: 100px 100px;
}
div:nth-child(2) {
border-left: 1px solid black;
border-right: 1px solid black;
}
div:nth-child(3) {
float: none;
width: auto;
}
Any ideas?
Use overflow: hidden to solve this issue:
div:nth-child(3) {
float: none;
width: auto;
overflow: hidden;
}
The reason you don't get it is, when you give width: auto, they surely lose their widths for a floated element.
Fiddle: http://jsfiddle.net/praveenscience/aMwk9/1/
You need to add overflow: hidden:
div:nth-child(3) {
float: none;
width: auto;
overflow: hidden;
}
Without this directive the 3rd DIV is not filling the remaining space but overlapping the rest of the DIV's as well.

Problems implementing a carousel

I'm trying to implement a responsive carousel by myself for a webpage I'm designing. I'm having some issues that may be thousends times easier to ilustrate with some screenshots, so here it goes:
So as you see, I have two arrows to slice the items and a horizontall scrollbar.
The arrows are floated to the left and right respectively, and the items are just inline-block divs inside a div.items container, which has a width of 90% (and overflow-x: scroll or course).
SO now, if I append another item to the DOM, I end with this:
Why did the fourth item go below? I'm not floating the items, and as I specified and horizontal scroll, I would expect it to be at the back and to be able to see it with the scrollbar.
What am I missing?
I'll paste relevant code:
HTML:
<div class="grid">
<div class="left-arrow"></div>
<div class="items">
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
<div class="item">...</div>
</div>
<div class="right-arrow"></div>
</div>
CSS:
div.grid {
margin-top: 20px;
padding: 10px 75px;
text-align: center;
z-index: 1000;
}
div.grid .left-arrow, div.grid .right-arrow {
position: relative;
top: 70px;
}
div.grid .left-arrow {
float: left;
width: 0;
height: 0;
margin: 0 30px 0 -50px;
border-top: 50px solid transparent;
border-bottom: 50px solid transparent;
border-right: 35px solid #ddd;
}
div.grid .right-arrow {
float: right;
width: 0;
height: 0;
margin: 0 -50px 0 30px;
border-top: 50px solid transparent;
border-bottom: 50px solid transparent;
border-left: 35px solid #ddd;
}
div.items {
display: inline-block;
z-index: 100;
width: 90%;
overflow-x: scroll;
}
div.item {
margin: 10px;
display: inline-block;
position: relative;
left: 0;
}
EDIT: Oreilly has exactly what I'm looking forward to achieve:
http://shop.oreilly.com/category/browse-subjects/programming.do
The container is growing in height to accommodate the additional items. I believe that you should be able to get the effect you are looking for by setting a specific height on the container element.
Edit: After testing some more, it turns out setting the height won't actually have any impact on this. You need to set white-space: nowrap; to get it to actually work.
Here's the full CSS for the div.items (which is all I changed to get this to work in my tests):
div.items {
display: inline-block;
z-index: 100;
width: 90%;
overflow-x: scroll;
white-space: nowrap;
}

Resources