I am trying to achieve the following:
The text inside the square (example text: "1/4") should be centered horizontally
and vertically.
The four squares should be spread with the same
distance over the main container (red).
The text (example text: "Name", "Longer Name" etc.) should be centered over the squares, and not affect the spreading of the squares (point 2)
What I got this far is the code below. I would greatly appreciate if somebody could point me in the correct direction!
.container {
background-color: red;
width: 320px;
height: 320px;
margin: 30px auto 30px auto;
}
.inner {
display: flex;
}
.inner-content {
display: block;
/* width: 25%;*/
margin-left: auto;
margin-right: auto;
background-color: green;
}
.text {
width: 25%;
text-align: center;
}
.square {
margin-top: 5px;
height: 25px;
width: 25px;
vertical-align: top;
text-align: center;
color: #fff;
font-size: 14px;
font-weight: bold;
text-align: center;
background-color: black;
box-shadow: 0 0 0 2px red, 0 0 0 5px black;
}
<div class="container">
<div class="inner">
<div class="inner-content">
<span class="text">Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Longer Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Yo</span>
<div class="square">
1/4
</div>
</div>
</div>
Take a look at the following example:
* {
box-sizing: border-box;
}
.container {
background-color: red;
width: 320px;
height: 320px;
margin: 30px auto 30px auto;
padding: 5px;
}
.inner {
display: grid;
grid-template-columns: repeat(4, 1fr); /* fix spreading */
grid-column-gap: 5px;
}
.inner-content {
background-color: green;
text-align: center;
}
.text {
width: 100%;
}
.square {
margin: 5px auto; /* fix box centering */
height: 25px;
width: 25px;
vertical-align: top;
text-align: center;
color: #fff;
font-size: 14px;
font-weight: bold;
background-color: black;
box-shadow: 0 0 0 2px red, 0 0 0 5px black;
line-height: 25px; /* fix vertical center */
}
<div class="container">
<div class="inner">
<div class="inner-content">
<span class="text">Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Longer Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Name</span>
<div class="square">
1/4
</div>
</div>
<div class="inner-content">
<span class="text">Yo</span>
<div class="square">
1/4
</div>
</div>
</div>
Related
I have cards that are aligned next to each other on full screen, but when I resize the screen they go one under each other, how to have them always aligned horizontally? I would also like to resize them accordingly so that the content is justified.
.actions {
justify-content: center;
display: inline-block;
height: 200px;
width: auto;
margin: 0 15px;
margin-left: 250px;
margin-top: 400px;
}
.cards {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
opacity: 0.5;
height: 200px;
width: calc(100/4);
text-align: center;
background-color: #f1f1f1;
padding-right: 20px;
padding-left: 20px;
padding-top: 2px;
border-radius: 20px;
color: #95a7b7;
margin: auto;
}
.column {
float: left;
width: 200px;
padding: 20px 80px;
margin: auto;
}
<div class="actions">
<div class="column">
<div class="cards">
<div class="content">
<h2> About </h1>
<p> Here you can find more about myself </p>
</div>
</div>
</div>
<div class="column">
<div class="cards">
<div class="content">
<h2> Interest </h1>
<p> Here you can find the list of my interests and the blogs that I have created</p>
</div>
</div>
</div>
<div class="column">
<div class="cards">
<div class="content">
<h2> Notes </h1>
<p> Here you can find the list of my notes and things that I like to keep track of. You might not be able to access to all informatio</p>
</div>
</div>
</div>
</div>
If you are perfectly okay to got with display: flex, go with this approach.
Add display: flex to .actions
Add display: flex; flex: 0 1 200px; to .column. This will set flex-grow: 0, flex-shrink: 1 and flex-basis: 200px; This will the element a maximum width of 200px and shrink below that if resized.
.actions {
justify-content: center;
display: flex;
height: 200px;
width: auto;
margin: 0 15px;
margin-left: 250px;
margin-top: 400px;
flex-wrap: nowrap;
}
.cards {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
opacity: 0.5;
height: 200px;
width: calc(100/4);
text-align: center;
background-color: #f1f1f1;
padding-right: 20px;
padding-left: 20px;
padding-top: 2px;
border-radius: 20px;
color: #95a7b7;
margin: auto;
}
.column {
/* float: left; */
margin: auto;
display: flex;
flex: 0 1 200px;
max-width: 100%;
}
<div class="actions">
<div class="column">
<div class="cards">
<div class="content">
<h2> About </h1>
<p> Here you can find more about myself </p>
</div>
</div>
</div>
<div class="column">
<div class="cards">
<div class="content">
<h2> Interest </h1>
<p> Here you can find the list of my interests and the blogs that I have created</p>
</div>
</div>
</div>
<div class="column">
<div class="cards">
<div class="content">
<h2> Notes </h1>
<p> Here you can find the list of my notes and things that I like to keep track of. You might not be able to
access to all informatio</p>
</div>
</div>
</div>
</div>
I think this will help you.
I made actions div to display flex, so all columns get align next to each other and set justify-content:space-between.
.actions {
display: flex;
justify-content: space-between;
height: 200px;
max-width: 1120px;
margin: 0 auto;
margin-top: 40px;
}
.cards {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
opacity: 0.5;
height: 200px;
width: calc(100/4);
text-align: center;
background-color: #f1f1f1;
padding-right: 20px;
padding-left: 20px;
padding-top: 2px;
border-radius: 20px;
color: #95a7b7;
margin: auto;
}
.column {
width: 200px;
margin: 0 15px;
}
<div class="actions">
<div class="column">
<div class="cards">
<div class="content">
<h2> About </h1>
<p> Here you can find more about myself </p>
</div>
</div>
</div>
<div class="column">
<div class="cards">
<div class="content">
<h2> Interest </h1>
<p> Here you can find the list of my interests and the blogs that I have created</p>
</div>
</div>
</div>
<div class="column">
<div class="cards">
<div class="content">
<h2> Notes </h1>
<p> Here you can find the list of my notes and things that I like to keep track of. You might not be able to access to all informatio</p>
</div>
</div>
</div>
</div>
the real qustion is how did you get it to go under each other bc i need that
.actions {
justify-content: center;
display: inline-block;
height: 200px;
width: auto;
margin: 0 15px;
margin-left: 250px;
margin-top: 400px;
}
.cards {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
opacity: 0.5;
height: 200px;
width: calc(100/4);
text-align: center;
background-color: #f1f1f1;
padding-right: 20px;
padding-left: 20px;
padding-top: 2px;
border-radius: 20px;
color: #95a7b7;
margin: auto;
}
.column {
float: left;
width: 200px;
padding: 20px 80px;
margin: auto;
}
<div class="actions">
<div class="column">
<div class="cards">
<div class="content">
<h2> About </h1>
<p> Here you can find more about myself </p>
</div>
</div>
</div>
<div class="column">
<div class="cards">
<div class="content">
<h2> Interest </h1>
<p> Here you can find the list of my interests and the blogs that I have created</p>
</div>
</div>
</div>
<div class="column">
<div class="cards">
<div class="content">
<h2> Notes </h1>
<p> Here you can find the list of my notes and things that I like to keep track of. You might not be able to access to all informatio</p>
</div>
</div>
</div>
</div>
How would i align all these 6 divs vertically in a 3x3 pattern so that the top and bottom divs content are aligned with each other so it looks good. i've tried some vertical-align: middle; with no sucess.
It's a must to be 100% responsive and that the number also is centered and aligned so whatever number gets there is aligned.
.top-right-container {
position: absolute;
border: 1px solid white;
height: 20%;
width: 50%;
right: 0;
top: 0;
}
.stats-container {
position: relative;
float: left;
border: 1px solid white;
width: 75%;
height: 80%;
}
.Agility,
.Stamina,
.Respect,
.Intelligence,
.Strength,
.Cash {
display: inline-block;
color: black;
}
.Agility,
.Intelligence {
float: left;
margin-left: 10%;
}
.Stamina,
.Strength {
margin: 0 auto;
}
.Respect,
.Cash {
margin-right: 10%;
float: right;
}
.stats-container h2 {
font-family: Marker-Felt;
margin: 0;
font-size: calc(0.7vh + 1.2vw);
}
.stats-container p {
margin: 5%;
text-align: center;
font-size: calc(0.5vh + 0.8vw);
}
.top-stats,
.bottom-stats {
width: 100%;
text-align: center;
}
<div class="top-right-container">
<div class="stats-container">
<div class="top-stats">
<div class="Agility">
<h2>Agility</h2>
<p>10</p>
</div>
<div class="Stamina">
<h2>Stamina</h2>
<p>10</p>
</div>
<div class="Respect">
<h2>Respect</h2>
<p>10</p>
</div>
</div>
<div class="bottom-stats">
<div class="Intelligence">
<h2>Intelligence</h2>
<p>10</p>
</div>
<div class="Strength">
<h2>Strength</h2>
<p>10</p>
</div>
<div class="Cash">
<h2>Cash</h2>
<p>10</p>
</div>
</div>
</div>
</div>
You can do it with the Flexbox:
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100%}
.stats-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
.top-stats,
.bottom-stats {
display: flex;
width: 100%;
text-align: center;
}
.Agility,
.Stamina,
.Respect,
.Intelligence,
.Strength,
.Cash {
flex: 1;
}
.stats-container h2 {
font-size: calc(0.7vh + 1.2vw);
}
.stats-container p {
font-size: calc(0.5vh + 0.8vw);
}
<div class="top-right-container">
<div class="stats-container">
<div class="top-stats">
<div class="Agility">
<h2>Agility</h2>
<p>10</p>
</div>
<div class="Stamina">
<h2>Stamina</h2>
<p>10</p>
</div>
<div class="Respect">
<h2>Respect</h2>
<p>10</p>
</div>
</div>
<div class="bottom-stats">
<div class="Intelligence">
<h2>Intelligence</h2>
<p>10</p>
</div>
<div class="Strength">
<h2>Strength</h2>
<p>10</p>
</div>
<div class="Cash">
<h2>Cash</h2>
<p>10</p>
</div>
</div>
</div>
</div>
responsive 2 rows and 6 boxes
Here is some code you can work with.
The container of all the divs .container will take 100% of the page eg. its <body> .
The rows .statRow will take 100% of its parent the container.
Now the boxes .box will take 33% of its parent width.
Then adding 3 of these boxes 33%+33%+33% will take up 99% of the container.
Additionally borders usually take up more space so width + border is its actual width.
This is fixed with chancing the elements box-sizing to border-box.
.container {
border: 10px solid black;
width: 100%;
box-sizing: border-box;
border-radius: 5px;
}
.statRow {
width: 100%;
display: block;
}
.box {
color: white;
display: inline-block;
text-align: center;
width: 33%;
border: 10px solid white;
box-sizing: border-box;
border-radius: 15px;
background-color: #222;
}
<div class="container">
<div class="statBubble">
<div class="box">
<h5>Agility</h5>
<p>10</p>
</div><!--
--><div class="box">
<h5>Strength</h5>
<p>10</p>
</div><!--
--><div class="box">
<h5>Stat</h5>
<p>number</p>
</div>
</div>
<div class="statRow">
<div class="box">
<h5>Wisdom</h5>
<p>100</p>
</div><!--
--><div class="box">
<h5>Stat</h5>
<p>number</p>
</div><!--
--><div class="box">
<h5>Stat</h5>
<p>number</p>
</div>
</div>
</div>
I am totally new into programming. I've aligned 5 boxed horizontally , but what I want is to center the boxes. The boxes should also be responsive what they are right now. I am searching and searching for how to center a div, but as result I get
margin: 0 auto; width: 800px;. When I adjust my browser to 100%, the 5th box get under the other boxes.
.square {
float:left;
width: 12.5vw;
height: 12.5vw;
margin-left: 10px;
margin-right: 10px;
position: relative;
border: 0.5px solid red;
text-align: center;
}
.square span {
font-size: 3vw;
}
<div class="outside">
<div class="square">
<span>2.1</span>
</div>
<div class="square">
<span>2.3</span>
</div>
<div class="square">
<span>2.5</span>
</div>
<div class="square">
<span>2.6</span>
</div>
<div class="square">
<span>2.7</span>
</div>
</div>
I think the easiest way would be to remove the floating from the inner divs, and display them as inline blocks instead. The outside would have the auto margins and a center text aligning, like this:
.outside {
border: 1px solid blue;
text-align: center;
margin: 0 auto;
}
.square {
display: inline-block;
width: 12.5vw;
height: 12.5vw;
margin-left: 10px;
margin-right: 10px;
position: relative;
border: 0.5px solid red;
text-align: center;
}
.square span {
font-size: 3vw;
}
<div class="outside">
<div class="square">
<span>2.1</span>
</div>
<div class="square">
<span>2.3</span>
</div>
<div class="square">
<span>2.5</span>
</div>
<div class="square">
<span>2.6</span>
</div>
<div class="square">
<span>2.7</span>
</div>
</div>
Add a max-width or width to the parent container. Then center it with margin: 0 auto. Of course set the width of the parent element to you needs. I just set it to 520px as an example.
W3Schools says this:
Center Align Elements To horizontally center a block element (like ), use margin: auto;
Setting the width of the element will prevent it from stretching out
to the edges of its container.
The element will then take up the specified width, and the remaining
space will be split equally between the two margins:
See example below.
.outside {
max-width: 520px;
margin: 0 auto;
}
.square {
float:left;
width: 12.5vw;
height: 12.5vw;
margin-left: 10px;
margin-right: 10px;
position: relative;
border: 0.5px solid red;
text-align: center;
}
.square span {
font-size: 3vw;
}
<div class="outside">
<div class="square">
<span>2.1</span>
</div>
<div class="square">
<span>2.3</span>
</div>
<div class="square">
<span>2.5</span>
</div>
<div class="square">
<span>2.6</span>
</div>
<div class="square">
<span>2.7</span>
</div>
</div>
add text-align center for outside
.outside{
text-align:center;
}
Remove float & add display inline-block square
.square{
display:inline-block;
width: 12.5vw;
height: 12.5vw;
margin-left: 10px;
margin-right: 10px;
position: relative;
border: 0.5px solid red;
text-align: center;
}
I didn't understand your requirement entirely but try the following. It will center your boxes and also won't allow them to wrap to next line on reducing the resolution.
Replace your css with:
.square {
float: left;
width: 19%;
height: 12.5vw;
margin-right: 1%;
position: relative;
border: 0.5px solid red;
text-align: center;
box-sizing: border-box;
}
.square span {
font-size: 3vw;
}
.outside {
width: 100%;
}
.inside {
width: 69%;
margin: 0 auto;
overflow: auto;
}
Html with:
<div class="outside">
<div class="inside">
<div class="square">
<span>2.1</span>
</div>
<div class="square">
<span>2.3</span>
</div>
<div class="square">
<span>2.5</span>
</div>
<div class="square">
<span>2.6</span>
</div>
<div class="square">
<span>2.7</span>
</div>
</div>
</div>
I am trying to vertically align a div in my code but with no success. This div contains sub divs. The first one
I want this to look like this :
but at the moment it is not aligned. This is my HTML code :
body {
display: block;
margin-left: auto;
margin-right: auto;
background: #f1f1f1;
}
.content {
float: left;
margin: 20px auto;
text-align: center;
width: 300px;
}
.content h1 {
text-transform: uppercase;
font-weight: 700;
margin: 0 0 40px 0;
font-size: 25px;
line-height: 30px;
}
.circle {
width: 100px;
height: 100px;
line-height: 100px;
border-radius: 50%;
/* the magic */
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
text-align: center;
color: white;
font-size: 16px;
text-transform: uppercase;
font-weight: 700;
margin: 0 auto 20px;
}
.blue {
background-color: #052D72;
}
.green {
background-color: #16a085;
}
.red {
background-color: #e74c3c;
}
<div class="content">
<div class="circle blue"></div>
</div>
<div class="content">
<div class="circle blue">Blue</div>
<div class="circle blue"></div>
<div class="circle blue"></div>
<div class="circle blue"></div>
</div>
<div class="content">
<div class="circle blue"></div>
<div class="circle blue"></div>
</div>
So, in the .content I tried adding this :
vertical-align:baseline;
but I saw no difference.
Add display:inline-block & Remove float for #content
.content {
display: inline-block;
margin: 20px auto;
text-align: center;
vertical-align: middle;
width: 200px;
}
https://jsfiddle.net/k0fx384a/1/
EDIT with class: https://jsfiddle.net/k0fx384a/2/
You have used same #id with multiple elements. That is not allowed in HTML across all browsers(seems like IE and FF allow multiple #ids).
So just change all the occurances of id="content" to class="content" and the CSS should start working.
DEMO
change <div id="content"> to <div class="content"> so the styles will be applied.
If you want them both vertically and horizontally aligned, I would recommend using flex. This offers more flexibility and is more forward-facing.
Mozilla Docs on Flex
If you use the rules align-items and justify-content, you'll get magic workings. Check out an example: https://jsfiddle.net/vrad7yuj/
.container {
display: flex;
flex-direction: row;
height: 100%;
width: 100%;
border: 2px solid #f00;
}
.col {
border: 2px solid #00f;
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.ball {
border-radius: 50%;
border: 1px solid #0f0;
height: 60px;
width: 60px;
}
<div class="container">
<div class="col">
<div class="ball"></div>
</div>
<div class="col">
<div class="ball"></div>
<div class="ball"></div>
<div class="ball"></div>
<div class="ball"></div>
</div>
<div class="col">
<div class="ball"></div>
<div class="ball"></div>
</div>
</div>
Alternative, if you want to do this with a little count of codelines, you can use flexbox:
body {
display: flex;
margin-left: auto;
margin-right: auto;
background: #f1f1f1;
}
.content {
display: flex;
flex-direction: column;
margin: 20px auto;
text-align: center;
width: 300px;
}
.content h1 {
text-transform: uppercase;
font-weight: 700;
margin: 0 0 40px 0;
font-size: 25px;
line-height: 30px;
}
.circle {
width: 100px;
height: 100px;
line-height: 100px;
border-radius: 50%;
/* the magic */
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
text-align: center;
color: white;
font-size: 16px;
text-transform: uppercase;
font-weight: 700;
margin: 0 auto 20px;
}
.blue {
background-color: #052D72;
}
.green {
background-color: #16a085;
}
.red {
background-color: #e74c3c;
}
<div class="content">
<div class="circle blue"></div>
</div>
<div class="content">
<div class="circle blue">Blue</div>
<div class="circle blue"></div>
<div class="circle blue"></div>
<div class="circle blue"></div>
</div>
<div class="content">
<div class="circle blue"></div>
<div class="circle blue"></div>
</div>
Take a look on flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Should just be an alternative solution and new knowledge for you. ;-) Cheers.
I want to align vertically 3 'title' divs. When the title is one word it must to be in the middle, when is longer must automatically align.
One example of what I want to achieve: http://jsfiddle.net/526TD/4/
My problem is that I have my divs in differents containers and not in a same div.
My HTML
<div class="related_news_filter">
<a href="/news/1">
<div class="round-image"></div>
<div>
<div class="title">title</div>
</div>
</a>
<a href="/news/2">
<div class="round-image">title longeeer</div>
<div>
<div class="title">title 2</div>
</div>
</a>
<a href="/news/3">
<div class="round-image"></div>
<div>
<div class="title">title with more words</div>
</div>
</a> </div>
My css
a {
display: inline-block;
width: 81px;
margin: 0 27px 0 8px;
}
.title {
margin-top: 7px;
width: 96px;
text-transform: uppercase;
line-height: 16px;
}
What I see now:
The word "new" and "inbound" are in the first line and not in the middle. They are not correctly vertical aligned.
Just give vertical-align: middle; to a
a {
display: inline-block;
width: 81px;
margin: 0 27px 0 8px;
vertical-align: middle;
}
.title {
margin-top: 7px;
width: 96px;
text-transform: uppercase;
line-height: 16px;
}
<div class="related_news_filter">
<a href="/news/1">
<div class="round-image"></div>
<div>
<div class="title">title</div>
</div>
</a>
<a href="/news/2">
<div class="round-image">title longeeer</div>
<div>
<div class="title">title 2</div>
</div>
</a>
<a href="/news/3">
<div class="round-image"></div>
<div>
<div class="title">title with more words</div>
</div>
</a> </div>
Flexbox can do that....
Support is IE10 and up.
.related_news_filter {
display: flex;
margin: 1em auto;
width: 80%;
justify-content: center;
}
a {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
flex: 0 0 96px;
margin: 0 27px 0 8px;
text-decoration: none;
}
.round-image {
display: flex;
justify-content: center;
align-items: center;
width: 81px;
flex: 0 0 81px;
margin-bottom: 1em;
background: #000;
border-radius: 50%;
color: white;
}
.title {
flex: 1;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
text-transform: uppercase;
}
<div class="related_news_filter">
<a href="/news/1">
<div class="round-image">title</div>
<div class="title">title</div>
</a>
<a href="/news/2">
<div class="round-image">title longeeer</div>
<div class="title">title 2</div>
</a>
<a href="/news/3">
<div class="round-image">title</div>
<div class="title">title with more words</div>
</a>
</div>
Note: This is not actually aligning the "titles" to one another. It merely lays out all the contents of the link 'containers' in the same way after making them all the same height.
There are many ways of doing this. The best method accroding to me would be to use the display:table method. Check the below fiddle -
http://jsfiddle.net/526TD/29/
.related_news_filter {
display: table;
}
a {
display: table-cell;
position: relative;
width: 81px;
margin: 0 27px 0 8px;
vertical-align: middle;
}
.title {
margin-top: 7px;
width: 96px;
text-transform: uppercase;
line-height: 16px;
text-align: center;
}
for more methods of achieving this go to the link -http://www.jakpsatweb.cz/css/css-vertical-center-solution.html