I want to give an explanation based on color of each dot below.
The whole page is:
My HTML code for the spesific area:
<div class="dot-expl">
<div class ="dot dot-red">Ended</div>
<div class ="dot dot-green">Running</div>
<div class ="dot dot-yellow">Ready to start</div>
</div>
My whole CSS code:
.dot-expl{
display: flex;
align-items: center;
overflow:visible;
}
.dot {
height: 20px;
width: 20px;
border-radius: 50%;
display: inline-block;
}
.dot-red {
background-color: red;
}
.dot-green {
background-color: green;
}
.dot-yellow {
background-color: yellow;
}
But the result is:
I cannot find how to give more space between the dots as so as the text can be visible
You could do it with this approach by using the pseudo class ::before
I set a margin to each .dot on both sides (left & right) to have some space between the individual elements.
You could also give your wrapper a fixed width and use justify-content on the wrapper. But I assume this will make you run into other problems depending on the length of the text.
.dot-expl {
display: flex;
align-items: center;
overflow: visible;
}
.dot {
margin: 0 5px;
display: flex;
align-items: center;
}
.dot::before {
content: '';
height: 20px;
width: 20px;
border-radius: 50%;
display: inline-block;
margin-right: 2px;
}
.dot-red::before {
background: red;
}
.dot-green::before {
background: green;
}
.dot-yellow::before {
background: yellow;
}
<div class="dot-expl">
<div class="dot dot-red">Ended</div>
<div class="dot dot-green">Running</div>
<div class="dot dot-yellow">Ready to start</div>
</div>
Related
I'm currently designing a header bar for a site I'm working on. I would like the header to show a logo at the left, the title in the center, and the user account on the right.
This mockup is what I'm envisioning. Note that the dotted boxes denote a div.
I've made some progress on creating it in HTML/CSS, but I can't seem to get the title to center to the viewport.
As you can see, the title is centering itself between the logo and the account info divs, instead of centering itself on the viewport. This ends up making the page just look a little bit off, and so I would really like to center the title to the viewport.
Here's my code:
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display: inline-block;
text-align: center;
}
.logoArea {
display: inline;
text-align: center;
float: left;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
display: inline-block;
text-align: center;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.siteTitle {
color: white;
}
.pageTitle {
color: white;
}
.userAccountArea {
display: inline;
text-align: center;
float: right;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
<div className="headerBarArea">
<div className="logoArea">
<img src="assets/mines_logo_stacked.png" className="minesLogo" />
</div>
<div className="titleArea">
<h2 className="siteTitle">This is my site Title</h2>
<h3 className="pageTitle">Page Title</h3>
</div>
<div className="userAccountArea">
<img src="assets/user_account.png" className="userAccountIcon" />
<span className="UserAccountText">John Smith (Student)</span>
</div>
</div>
Any ideas on what I could do to make the title div centered to the viewport, instead of centering between the two divs?
html code
<div class="flex-container">
<div class="flex-item">1</div>
<div class="align-self-center">
<span class="siteTitle">This is my site Title</span>
<br>
<div class="text-center ">Page Title</div>
</div>
<div class="flex-item align-self-end third-item">3</div>
</div>
CSS code
.flex-container {
/* We first create a flex layout context */
display: flex;
/* Then we define the flow direction
and if we allow the items to wrap
* Remember this is the same as:
* flex-direction: row;
* flex-wrap: wrap;
*/
flex-flow: row wrap;
/* Then we define how is distributed the remaining space */
justify-content: space-between;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bold;
text-align: center;
}
.third-item {
height: 100px;
}
.text-center{
text-align: center;
}
.align-self-end{
align-self:end;
}
.align-self-center{
align-self:center;
}
code output
code solution
used flex to place the items used .flex-container as parent div where flex items are placed in .justify-content: space-between; is used to place space in between the items. align-self:center; is used to place Page Title at center
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
codepen
.headerBarArea{
display:flex;
justify-content: space-between;
align-items: center
}
It is an easy way to the layout.
you can try it.
Try adding margin-left:auto; and margin-right:auto; to the .titleArea class .
I would suggest using a flex-box though.
Replace your css code with this to make title div centered in all the viewport.
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display:flex;
justify-content: space-between;
}
.logoArea , .titleArea , .userAccountArea {
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
text-align: center;
}
.siteTitle , .pageTitle{
color: white;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
I'm building a line-through header that can span multiple lines. Using the sample code below, is it possible to write my CSS in such a way that the left and right divs are not needed? Where they could be added as pseudo-classes to my header class?
CodePen
.container {
box-sizing: border-box;
display: flex;
place-content: center space-evenly;
align-items: center;
}
.line {
flex: 1;
height: 2px;
background: black;
}
.header {
font-size: 50px;
margin: 0 30px;
text-align: center;
}
.header-broken:after {
content: '';
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
display: inline-flex;
width: 50px;
height: 5px;
flex: auto;
width: 100%;
height: 2px;
background: black;
}
<div class="container">
<div class="line"></div>
<div class="header">Normal Title<br>fdasfsaf</div>
<div class="line"></div>
</div>
It can be done with just one div, see the example below, add some margin to the pseudo elements as needed for spacing.
.container {
display: flex;
text-align: center;
}
.container:before,
.container:after {
content: "";
flex: 1;
background: linear-gradient(black, black) center / 100% 1px no-repeat;
}
<div class="container">
Normal Title<br>fdasfsaf
</div>
You can also try this.
HTML:
<div class="container">
<div class="header">
<h1>Normal Title
<br>fdasfsaf
</h1>
</div>
</div>
CSS:
.container {
display: flex;
text-align: center;
}
.header {
flex: 1;
}
.header h1 {
font-size: 50px;
margin: 0 30px;
text-align: center;
background-color: #fff;
display: inline-block;
}
.header:after {
content: '';
border-bottom: 1px solid #000;
display: block;
margin-top: -58px;
}
I have two divs, but they are at the top, I want them in the middle, also each one has a background color that I'd like to fill their half of the screen.
.contenedor {
display: flex;
justify-content: space-around;
align-items: center;
font-size: 50px;
}
.español {
background: red;
}
.english {
background: blue;
}
a {
text-decoration: none;
}
<div class="contenedor">
<div class="español">
Español
</div>
<div class="english">
English
</div>
</div>
How would I go about doing this?
A picture says more than a thousand words
Thanks!
There's quite a lot to add to your code. If you want to use flex (as you did for the container), use the following settings for the elements:
html,
body {
height: 100%;
margin: 0;
}
.contenedor {
display: flex;
justify-content: space-around;
align-items: center;
font-size: 50px;
height: 100%;
}
.contenedor>div {
width: 100%;
height: 100%;
text-align: center;
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
}
.español {
background: red;
}
.english {
background: blue;
}
a {
text-decoration: none;
color: white;
}
<div class="contenedor">
<div class="español">
Español
</div>
<div class="english">
English
</div>
</div>
There are many approaches you could choose.
A simple one is to set the 2 divs with abolute positionning, with each a width of 50%.
This way each div will occupy the whole height of the page and you don't have to worry about body margins or padding.
.contenedor {
font-size: 50px;
}
.espanol,
.english {
position: absolute;
top: 0;
bottom: 0;
width: 50%;
}
.espanol {
background: red;
left: 0;
}
.english {
background: blue;
left: 50%;
}
.contenedor a {
color: #fff;
text-align: center;
text-decoration: none;
display: flex;
justify-content: center;
flex-direction: column;
height: 100%;
}
<body>
<div class="contenedor">
<div class="espanol">
Español
</div>
<div class="english">
English
</div>
</div>
I am currently using this solution for modified for my use case specifically here. When I originally saw the design I figured that I would make two divs inside one flexbox container, the div on the right would be z index'd above the one on the right. Something like...
.container {
display: flex;
width: 200px;
flex-direction: row;
}
.left-side {
flex: 4;
background-color: red;
}
.right-side {
flex: 1;
background-color: orange;
z-index: 3;
border-style: 2px solid white;
border-radius: 50%;
}
<div class="container">
<div class="left-side">
View Cart
</div>
<div class="right-side">
3
</div>
</div>
This doesn't layer my elements on top of one another at all because they are positioned next to each other. So my question is:
How can I use make a layered layout while still taking advantage of all the nice positioning flexbox allows without the position absolute / position relative solution that I'm hacking together? Or, is this position absolute / relative the correct way to solve my problem?
You can apply the red background to the container, and use transform: translateX(50%) to move the orange circle half way outside of the container to pull off that effect.
.container {
display: flex;
width: 200px;
flex-direction: row;
background: red;
position: relative;
color: white;
}
.container:before {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
content: '';
}
.container:before,
.count {
border: 2px solid white;
}
.left-side {
flex-grow: 1;
}
.center {
display: flex;
justify-content: center;
align-items: center;
}
.count {
width: 50px;
height: 50px;
background-color: orange;
border-radius: 50%;
transform: translateX(50%);
}
<div class="container">
<div class="left-side center">
View Cart
</div>
<div class="right-side">
<div class="count center">3</div>
</div>
</div>
Negative margins
You can use negative margins to solve the problem without using position absolute/relative solution.
.container {
display: flex;
width: 200px;
flex-direction: row;
}
.left-side {
flex: 4;
background-color: red;
}
.right-side {
flex: 1;
background-color: orange;
z-index: 3;
border-style: 2px solid white;
border-radius: 50%;
margin-left: -60px;
text-align: center;
}
<div class="container">
<div class="left-side">
View Cart
</div>
<div class="right-side">
3
</div>
</div>
I have a frame containing a picture, I would like to add 2 buttons on the left and right side of it so users can click on those to view different pictures. I can handle JavaScript but I can't figure out a way to align these 2 buttons in proper positions. Thank you.
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft {
float: left;
background: red;
}
.btnRight {
float: right;
background: blue;
}
<div class="frame">
<div class="content"></div>
</div>
<div class="btn btnLeft"></div>
<div class="btn btnRight"></div>
You were on the right track but needed justify-content: space-between; not justify-content: center; and you needed to put .btnLeft and .btnRight inside .frame.
Here's what different values of justify-content do:
Image from CSS-Tricks
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: space-between; /* not center */
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft{
background: red;
}
.btnRight {
background: blue;
}
<div class="frame">
<div class="btn btnLeft"></div>
<div class="content"></div>
<div class="btn btnRight"></div>
</div>
You may want to put the buttons inside the frame so you can reference the left and right positions. Then make those buttons position:absolute then set left and right position for each buttons
Code:
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.btn{
position: absolute;
top: 50%;
transform:translateY(-50%);
}
.btnLeft{
left: 0;
}
.btnRight{
right: 0;
}
Updated fiddle:
https://jsfiddle.net/y0ty7t80/3/
First you could set the position of the frame to relative so it gets set as a root for following positionings. You could then set the positions of the two buttons both to "absolute" and put them inside of your frame so they get taken out of the text flow. By setting both to a left/right property of 0 and a top property of 50% they get placed exactly in the middle of the frame. Heres an example of what i mean:
.frame {
position:relative;
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft{
position:absolute;
top:50%;
left:0;
background: red;
}
.btnRight {
position:absolute;
top:50%;
right:0;
background: blue;
}
<div class="frame">
<div class="btn btnLeft"></div>
<div class="btn btnRight"></div>
<div class="content"></div>
</div>