How do I responsively center text inside of a <div> - html

I have a <div> that contains three <div>s.
In each of those <div> elements is a <p> element with text and 2 nested elements to make a radial progress bar. What I need is to put the text in the middle of the circles, and do it responsively using pure CSS. I need something like this:
The code has flaws, like that <p> inside of a <span> but I am fixing it in the new version with the help you guys provide.
.radius-container div {
-webkit-box-flex: 1;
-ms-flex: 1;
flex: 1;
}
.radius-container div:first-child {
margin-right: 1%;
}
.radius {
padding-top: 11em;
height: 30em;
text-align: center;
border: 1px solid #858280;
display: block;
border-radius: 50%;
width: 100%;
}
.radius3 {
position: relative;
padding-top: 10%;
height: 15em;
width: 50%;
text-align: center;
border: 1px solid #858280;
border-left: 0;
border-bottom: 0;
border-top-right-radius: 100%;
display: block;
margin-left: 15em;
}
.radius3 p {
position: absolute;
right: 50%;
top: 65%;
}
<div class="radius-container">
<div><span class="radius"><p>SERBIAN<br>100%</p></span></div>
<div><span class="radius"><p>ENGLISH<br>100%</p></span></div>
<div><span class="radius3"><p>GERMAN<br>25%</p></span></div>
</div>
See also this jsFiddle

After researching for long time for this issue, I found an generic solution which solves this kind of requeriments. I am proud of it, simple and elegant :)
.center-element{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Hoping it helps, any doubt let me know. Cheers mate :)

This code might help you.
.innerDiv {
height: 100px;
width: 100px;
display: table-cell;
text-align: center;
vertical-align: middle;
border-radius: 50%;
}

You can make a div with a border-radius to 50%. After, you can use the flex display to center verticaly and horizontaly.
html
div {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
div div {
display: inline-flex;
flex-direction: column;
width: 31%;
margin: 1%;
border-radius: 50%;
border: 1px solid #999;
align-items: center;
justify-content: center;
}
div div span {
text-align: center;
color: #999;
}
<div>
<div><span>Serbian</span><span>100%</span>
</div>
<div><span>Serbian</span><span>50%</span>
</div>
<div><span>Serbian</span><span>25%</span>
</div>
</div>

Related

Sticking/layering an image to top-left corner of div container

Been attempting to layer and image over the top left corner of a div container; iv achieved it once but it didn't stick to position if the page was adjusted!
^^ This is what im redesigning
^^ This is what ive managed to design myself
I have come across different posts and answers suggesting that I try to use:
display: block, relative
position: block, relative
margins, float: start
In the end I am stumped an have resorted to removing most displays besides for the p tags an its composed container:
This is the CSS I have been trying to use to make this happen
.roadmap__section__container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
min-height: 100%;
max-height: 100%;
background-color: #383636;
align-items: center;
}
.roadmap__header {
font: 3rem "Nunito";
margin: 3% 50% 3% 50%;
}
.roadmap__phase__one {
width: 100%;
height: 100%;
}
.phase__one__img {
width: 10%;
position: absolute;
margin-left:10%;
}
.phase__one__data {
display: flex;
flex-direction: column;
width: fit-content;
height: fit-content;
align-items: flex-start;
justify-content: center;
text-align: center;
padding: 25px;
margin: 0% 5% 5% 15%;
box-shadow: 1px 1px 40px #ff00e6;
outline-color: #ff00e6;
outline-offset: 0px;
outline-style: solid;
outline-width: 3px;
border-radius: 25px;
}
.phase__one__data p {
font-size: 1.75rem;
color: #fff;
font-family: "Nunito", serif;
padding: 5px 10px;
}
Here is the HTML Code used for my redesign:
<section class="roadmap__section__container">
<h1 class="roadmap__header">Roadmap</h1>
<div class="roadmap__phase__one">
<img class="phase__one__img" src="images/CasinoWRLD__dice1" alt="">
<div class="phase__one__data">
<p>- Working To Perfect The NFT Artwork.</p>
<p>- Finalise The Marketing Plan.</p>
<p>- Plan And Develop Casino WRLD.</p>
</div>
</div>
</section>
.phase__one__img {
grid-column: 3;
width: 11%;
position: absolute;
top: 157.5%;
right: 32%;
}
.phase__zero__img {
grid-column: 1;
width: 11%;
position: absolute;
margin-left: 5%;
top: 119.75%;
}
I've found this to work currently but it is not a 100% answer, if the browser height is adjust the settings regarding ''top:157.5%'' needs to be adjusted accordingly or it will not line up with the top-left corner of the div container
The difference in the two is pertaining to it switches back and forth going down the page

How to make a split left-right text on two lines?

Need to center this text in this way:
Basically, the top part to the left and the bottom part to the right, but with a bit of overlap.
How can I do that in HTML/CSS?
You can center the text vertically by using the centering with transform technique. Then the text should be separated into two lines and aligned to left/right plus a small negative margin so it overflows outside of the circle.
div {
height: 130px;
width: 130px;
background: lightblue;
position: relative;
border-radius: 50%;
}
p {
position: absolute;
top: 50%;
transform: translateY(-50%);
margin: 0;
width: 100%;
}
span {
display: block;
}
span:first-of-type {
text-align: left;
margin-left: -7px;
}
span:last-of-type {
text-align: right;
margin-right: -7px;
}
<div>
<p>
<span>La crèativitè</span>
<span>est contagieuse</span>
</p>
</div>
Simply use 2 elements for those 2 lines and add a margin-left to the second element:
.logo p {
margin: 0;
}
.logo p:nth-of-type(2) {
margin-left: 3.5em;
}
/* for demonstration purpose only */
html {
font-size: 40px;
font-family: Arial;
}
<div class="logo">
<p>La crèativitè</p>
<p>est contagieuse</p>
</div>
Another entirely different (and more complicated) approach is to use flex-box. It requires you to use containers and to know the principles of flex-box. But once you know what you're doing, it becomes fairly simple to center things inside of other things.
If you need somewhere to practice flex-box with simple games, you can visit https://flexboxfroggy.com/.
.container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: gray;
}
.logo {
width: 200px;
height: 200px;
background-color: maroon;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
p {
font-size: 20px;
}
#p1 {
align-self: flex-start;
}
#p2 {
align-self: flex-end;
}
<div class="container">
<div class="logo">
<p id="p1">La crèativitè</p>
<p id="p2"> est contagieuse</p>
</div>
</div>
Bonne Chance!

Overlapping divs with opaque background [duplicate]

This question already has answers here:
Why the content is not covered by the background of an overlapping element?
(8 answers)
Closed 3 years ago.
I have two divs with a background color, one overlapping the other. The problem is that I can see the content of the underlying div through the top div.
https://jsfiddle.net/jost_s/0dxwtbvn/23/
div {
background-color: lightblue;
width: 100px;
height: 100px;
font-size: 3rem;
display: flex;
align-items: center;
justify-content: center;
}
.overlapping {
margin-left: 50px;
margin-top: -50px;
border: 1px solid white;
}
<div>AB</div>
<div class="overlapping">CD</div>
Use position: relative
div {
background-color: lightblue;
width: 100px;
height: 100px;
font-size: 3rem;
display: flex;
align-items: center;
justify-content: center;
}
.overlapping {
margin-left: 50px;
margin-top: -50px;
border: 1px solid white;
position:relative;
}
<div>
AB
</div>
<div class="overlapping">
CD
</div>
Without a position property, they're not really overlapping in the context of the way the browser renders them.
There's probably a better explanation of why the second block overlaps the first block, but not it's content, but I'm sure it involves a deep understanding of how the rendering engine works. You might even get a different result in different browsers.
To get the desired effect, position the overlapping block instead of using the margin...
div {
background-color: lightblue;
width: 100px;
height: 100px;
font-size: 3rem;
display: flex;
align-items: center;
justify-content: center;
}
.overlapping {
position: relative;
top: -50px;
left: 50px;
border: 1px solid white;
}
<div>
AB
</div>
<div class="overlapping">
CD
</div>
I would use transform: translate(); to position the elements instead of margin.
Since its triggering the stacking context and assure that the elements are "stacket" in the right way.
I cannot really explain why margin behaves in this way but maybe someone wants to educate me.
div {
background-color: lightblue;
width: 100px;
height: 100px;
font-size: 3rem;
display: flex;
align-items: center;
justify-content: center;
}
.overlapping {
border: 1px solid white;
transform: translate(50px, -50px);
}
<div>
<p>
AB
</p>
</div>
<div class="overlapping">
<p>
CD
</p>
</div>

How can I align the text in the center of the image

I'm trying to center a text inside a div, this div contains an image + another div which contains the text that needs to be centered.
See image the following image:
The problem I'm facing is that the image which is also in the div doesn't allow me to outline the text in the center.
I tried to apply padding and margins(even negatives ones) however with no results
So right now this is the code I have in my HTML & CSS files:
.destinations {
padding: 5px 15px;
}
.destinations img {
max-width: 100%;
max-height: 100%;
border-radius: 10px;
}
.flex-item {
width: 290px;
height: auto;
border-radius: 10px;
margin: auto;
}
.flex-item-title {
text-align: center;
color: white;
background-color: black;
opacity: 0.8;
}
<div class="destinations">
<div class="flex-item">
<img src="assets/img/wassenaar.jpg">
<div class="flex-item-title">Wassenaar</div>
</div>
</div>
I hope you can help me out
Here is one approach to vertically and horizontally center the text over the image:
.destinations {
padding: 5px 15px;
}
.destination {
width: 290px;
height: 290px;
display: flex;
border-radius: 10px;
margin: auto;
background-image: url("https://placekitten.com/500/500");
justify-content: center;
align-items: center;
}
.title {
text-align: center;
color: white;
background-color: black;
opacity: 0.8;
}
<div class="destinations">
<div class="destination">
<div class="title">Wassenaar</div>
</div>
</div>
You can get your porblem solve using following css .
.flex-item{
width:300px;
height:200px;
position: relative;
padding: 0;
margin: 0;
text-align: center;
}
.flex-item-title{
position: absolute;
top: 0; right: 0;
bottom: 0; left: 0;
width: 300px;
height: 200px;
text-align: center;
color: white;
display: inline-table;
vertical-align:middle;
line-height:100%;
}
Try changing your css to this css , it will work .

Best way to arrange elements in div

So I want to build a simple event box, to replace the default one in a calendar (react-big-calendar for reference, but I don't think it matters)
I would like to make it as responsive as possible, but I have started with a very static box, which corresponds to what I would like to see on a big screen.
Simple fiddle
.container {
width: 200px;
height: 200px;
background-color: #dddddd;
margin: 20px;
padding: 5px;
}
.event-slot-component {
width: 100%;
min-height: 30px;
position: relative;
background-color: #64a7DD;
border: 5px;
border-radius: 3px;
padding: 2px;
}
.event-slot-start-time {
font-size: 0.75em;
vertical-align: top;
position: absolute;
top: 0;
left: 0;
}
.event-slot-end-time {
font-size: 0.75em;
float: left;
vertical-align: bottom;
position: absolute;
bottom: 0;
left: 0;
}
.event-slot-label {
font-size: 1em;
top: 8px;
right: 5px;
position: absolute;
}
<div class="container">
<div class="event-slot-component">
<div class="event-slot-start-time">17h</div>
<div class="event-slot-end-time">21h</div>
<div class="event-slot-label">Occupied Slot</div>
</div>
</div>
My goal is to have a 'centered, eventually slightly to the right' label,
and two small indications on the left that correspond to the start and end of the event.
I have tried using flexbox, coming from other StackOverflow answers, and it does seem to be able to do that somehow, but I have not managed to display the three elements properly. Any insight on a clean solution to achieve this result?
The simplest with the existing markup is to use Flexbox with column direction on the 2 date values and then position the label absolute using transform
.container {
width: 200px;
height:200px;
background-color: #dddddd;
margin: 20px;
padding: 5px;
}
.event-slot-component {
width: 100%;
min-height: 30px;
position: relative;
background-color: #64a7DD;
border: 5px;
border-radius: 3px;
padding:2px;
display: flex;
flex-direction: column;
}
.event-slot-start-time,
.event-slot-end-time {
font-size: 0.75em;
flex-grow: 1; /* share the vertical space equal */
}
.event-slot-label {
position:absolute;
font-size: 1em;
top: 50%;
left: calc(50% + 10px); /* adjust px value for horiz. offset */
transform: translate(-50%,-50%); /* vert./hor. center the label */
}
<div class="container">
<div class="event-slot-component">
<div class="event-slot-start-time">17h</div>
<div class="event-slot-end-time">21h</div>
<div class="event-slot-label">Occupied Slot</div>
</div>
</div>
If you want a good responsive solution, use Flexbox all the way, here with a wrapper for the date's
.container {
width: 200px;
height:200px;
background-color: #dddddd;
margin: 20px;
padding: 5px;
}
.event-slot-component {
width: 100%;
min-height: 30px;
position: relative;
background-color: #64a7DD;
border: 5px;
border-radius: 3px;
padding:2px;
display: flex;
}
.event-slot-time {
display: flex;
flex-direction: column;
}
.event-slot-start-time,
.event-slot-end-time {
font-size: 0.75em;
flex-grow: 1; /* share the vertical space equal */
}
.event-slot-label {
flex-grow: 1; /* fill the remaining horizontal space */
font-size: 1em;
display: flex;
align-items: center; /* vertical center the label text */
justify-content: center; /* horizontal center the label text */
}
<div class="container">
<div class="event-slot-component">
<div class="event-slot-time">
<div class="event-slot-start-time">17h</div>
<div class="event-slot-end-time">21h</div>
</div>
<div class="event-slot-label">Occupied Slot</div>
</div>
</div>
You would need to nest your flexboxes. That's what's so wonderful about it!
To explain, what I did was created three wrappers.
One to hold the entire event.
One to hold your event times.
One to hold the status.
We used flex box to butt the event times and status-wrapper against each other. The event times only take up as much space as the text utilizes (plus a little padding). The status wrapper takes up 100% of its usable space.
Then status wrapper is set to flex box using the justify-content and align-items properties. This centers the status.
The status text container is used in the same way to center the status text itself.
.event-wrapper {
background-color: #eee;
display: flex;
}
.event-times-wrapper {
background-color: skyblue;
padding-left: 0.5rem;
padding-right: 0.5rem;
}
.status-wrapper {
width:100%;
display: flex;
align-items:center;
justify-content: center;
}
.status-text {
height: 100%;
display:flex;
align-items: center;
padding-left: 1rem;
padding-right: 1rem;
background-color: tomato;
}
<article class="event-wrapper">
<div class="event-times-wrapper">
<p class="event-start">9:00a</p>
<p class="event-end">10:00a</p>
</div>
<div class="status-wrapper">
<div class="status-text">Busy</div>
</div>
</article>
As OP requested later, a sample without special containers.
.container {
background-color: #eee;
position: relative;
width: 100vw;
height:6rem;
}
.event-slot-component div {
font-family: sans-serif;
color: white;
background-color: skyblue;
height:3rem;
float:left;
padding-left: 1rem;
padding-right: 1rem;
width:10%;
display:flex;
align-items:center;
justify-content:center;
}
.event-slot-component div:nth-child(2) {
background-color:red;
position:absolute;
bottom:0;
left: 0;
}
.event-slot-component div:last-of-type {
margin-left:25%;
background-color: tomato;
float:left;
height: 6rem;
}
<div class="container">
<div class="event-slot-component">
<div class="event-slot-start-time">17h</div>
<div class="event-slot-end-time">21h</div>
<div class="event-slot-label">Occupied Slot</div>
</div>
</div>