CSS - Div Not Centering [duplicate] - css

This question already has answers here:
CSS center display inline block?
(9 answers)
Closed 5 years ago.
body {
background: #212121;
}
.wrapper {
height: 100%;
width: 100%;
overflow: hidden;
}
.holder{
height: 50%;
width: 100%;
position: absolute;
top: 25%;
left: 0%;
}
.content {
height: 100%;
width: 400px;
margin: 10px;
display: inline-block;
background: #424242;
-webkit-box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
-moz-box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
}
.detailBox {
height: 100%;
display: inline-block;
font-size: 0;
margin: 0 auto;
}
<div class="wrapper">
<div class="holder">
<div class="detailBox">
<div class="content" id="row1">
<div class="wordInputContainer">
<div class="inputBox">
</div>
</div>
</div>
<div class="content" id="row2">
<div class="wordOutputContainer">
<div class="listBox">
<!-- List Elements Go Here -->
<!-- Words Output In Alphabetical Order [A - Z] -->
</div>
</div>
</div>
<div class="content" id="row3">
<div class="wordStatisticContainer">
<div class="wordCount"></div>
<div class="commonLetter"></div>
<div class="commonWord"></div>
<div class="longestWord"></div>
<div class="shortestWord"></div>
</div>
</div>
</div>
</div>
</div>
Even though I have margin: 0 auto; on the .detailedBox it still isn't centering. Is it because it doesn't have a fix width? It doesn't have a fixed width because I want the .detailedBox to be the width of all the .content's combined, but also centered.

If you change .detailBox to have display: flex (instead of inline-block) and then do justify-content: center then it'll center that div horizontally.

Since .detailBox is set to inline-block, it cannot be centered by using margin:0 auto. One way to understand this is that the "margin centering" technique relies on "filling up" available horizontal space. But there is no available space when using an inline element. (Reference: margin:auto; with inline-block.)
Also note that:
10.3.9 'Inline-block', non-replaced elements in normal flow
If 'width' is 'auto', the used value is the shrink-to-fit width as for floating elements.
A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
— w3.org
One alternative is to let .detailBox default to display:block and set text-align:center to horizontally center it's child .content elements, which are inline-block.
body {
background: #212121;
}
.wrapper {
height: 100%;
width: 100%;
overflow: hidden;
}
.holder {
height: 50%;
width: 100%;
position: absolute;
top: 25%;
left: 0%;
}
.content {
height: 100%;
width: 100px;
margin: 10px;
display: inline-block;
background: #424242;
-webkit-box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
-moz-box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
}
.detailBox {
height: 100%;
font-size: 0;
text-align:center;
}
<div class="wrapper">
<div class="holder">
<div class="detailBox">
<div class="content" id="row1">
<div class="wordInputContainer">
<div class="inputBox">
</div>
</div>
</div>
<div class="content" id="row2">
<div class="wordOutputContainer">
<div class="listBox">
<!-- List Elements Go Here -->
<!-- Words Output In Alphabetical Order [A - Z] -->
</div>
</div>
</div>
<div class="content" id="row3">
<div class="wordStatisticContainer">
<div class="wordCount"></div>
<div class="commonLetter"></div>
<div class="commonWord"></div>
<div class="longestWord"></div>
<div class="shortestWord"></div>
</div>
</div>
</div>
</div>
</div>
You mentioned that you want .detailBox to be the width of all the content combined. So, if for some reason you need .detailBox to remain inline-block, you could set text-align:center on it's parent, .holder. Like this:
body {
background: #212121;
}
.wrapper {
height: 100%;
width: 100%;
overflow: hidden;
}
.holder {
height: 50%;
width: 100%;
position: absolute;
top: 25%;
left: 0%;
text-align:center;
}
.content {
height: 100%;
width: 100px;
margin: 10px;
display: inline-block;
background: #424242;
-webkit-box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
-moz-box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 1);
}
.detailBox {
height: 100%;
display:inline-block;
font-size: 0;
}
<div class="wrapper">
<div class="holder">
<div class="detailBox">
<div class="content" id="row1">
<div class="wordInputContainer">
<div class="inputBox">
</div>
</div>
</div>
<div class="content" id="row2">
<div class="wordOutputContainer">
<div class="listBox">
<!-- List Elements Go Here -->
<!-- Words Output In Alphabetical Order [A - Z] -->
</div>
</div>
</div>
<div class="content" id="row3">
<div class="wordStatisticContainer">
<div class="wordCount"></div>
<div class="commonLetter"></div>
<div class="commonWord"></div>
<div class="longestWord"></div>
<div class="shortestWord"></div>
</div>
</div>
</div>
</div>
</div>

Related

Extend underlines (or borders) to fill horizontal space

I'm creating a popup which presents the user with choices they've made and allows them to copy to clipboard or cancel. This isn't an actual form (text won't be editable at this point), but the idea is that it should resemble a filled-out typewritten form.
I'm using Flexbox rows for compactness. I would like the horizontal rules (see red) to expand to fill available space, to create the look of an actual form, one where the length of the inputs isn't known.
body em {
font-family: 'Brygada 1918';
font-size: 90%;
}
#border_box {
border: 6px double black;
padding: 12pt;
}
#id_card {
justify-content: center;
width: 540px;
background: url(https://bladesnpc.com/bitd-images/paper_texture.png) rgba(244, 241, 230, 1);
padding: 12pt;
font-family: Courier;
box-shadow: inset 1px 1px 1px rgba(255, 255, 255, 0.4), inset -1px -1px 1px rgba(0, 0, 0, 0.6);
}
.formfill {
display: inline-block;
border-bottom: 1px solid black;
width: auto;
margin-bottom: 1em;
}
.attributes {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
}
.flexbreak {
width: 100%;
}
<div id="id_card">
<div id="border_box">
<div id="chosen_name">
<h3>John Doe</h3>
</div>
<div class="attributes">
<div>
<em>Heritage: </em>
<div class="formfill">heritageval</div>
</div>
<div>
<em>Looks: </em>
<div class="formfill">looksval</div>
</div>
<div>
<em>Style: </em>
<div class="formfill">styleval</div>
</div>
<div>
<em>Profession: </em>
<div class="formfill">profval</div>
</div>
<div>
<em>Trait: </em>
<div class="formfill">traitval</div>
</div>
<div>
<em>Goals: </em>
<div class="formfill">goalsval</div>
</div>
<div>
<em>Interests: </em>
<div class="formfill">interestsval</div>
</div>
<div>
<em>Mayhem: </em>
<div class="formfill">mayhemval</div>
</div>
</div>
<hr class="hr_bottom" style="margin-top: 1em;">
<div class="flexbreak"><span>COPY</span> <span>CANCEL</span></div>
</div>
</div>
Is there any way to achieve this while still using flex?
You can update your code like below (check the comments)
body em {
font-family: 'Brygada 1918';
font-size: 90%;
}
#border_box {
border: 6px double black;
padding: 12pt;
}
#id_card {
justify-content: center;
width: 540px;
background: url(https://bladesnpc.com/bitd-images/paper_texture.png) rgba(244, 241, 230, 1);
padding: 12pt;
font-family: Courier;
box-shadow: inset 1px 1px 1px rgba(255, 255, 255, 0.4), inset -1px -1px 1px rgba(0, 0, 0, 0.6);
}
.formfill {
display: inline-block;
border-bottom: 1px solid black;
width: auto;
margin-bottom: 1em;
}
.attributes {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
}
/* make all the element fill the remaining space except for 3,6 and 8 */
.attributes > *:not(:nth-child(3),:nth-child(6),:nth-child(8)) {
flex-grow:1;
}
/* make the flex container */
.attributes > * {
display:flex;
}
/* the formfill will fill all the space and will stretch the line*/
.attributes > * .formfill {
flex-grow:1;
margin-left:5px;
}
.flexbreak {
width: 100%;
}
<div id="id_card">
<div id="border_box">
<div id="chosen_name">
<h3>John Doe</h3>
</div>
<div class="attributes">
<div>
<em>Heritage: </em>
<div class="formfill">heritageval</div>
</div>
<div>
<em>Looks: </em>
<div class="formfill">looksval</div>
</div>
<div>
<em>Style: </em>
<div class="formfill">styleval</div>
</div>
<div>
<em>Profession: </em>
<div class="formfill">profval</div>
</div>
<div>
<em>Trait: </em>
<div class="formfill">traitval</div>
</div>
<div>
<em>Goals: </em>
<div class="formfill">goalsval</div>
</div>
<div>
<em>Interests: </em>
<div class="formfill">interestsval</div>
</div>
<div>
<em>Mayhem: </em>
<div class="formfill">mayhemval</div>
</div>
</div>
<hr class="hr_bottom" style="margin-top: 1em;">
<div class="flexbreak"><span>COPY</span> <span>CANCEL</span></div>
</div>
</div>
update your css code with this changes:
// add this code:
.attributes > div {
display: flex;
flex: 1;
}
.
.
.
.formfill {
display: inline-block;
border-bottom: 1px solid black;
width: 100%; // edited from auto to 100%
margin-bottom: 1em;
}
I hope this answer help you, good luck.

flex-shrink does not update item width

Update: I think there is no easy solution to this if I have to use flexbox. I'll just use "flex-shrink: 0" and use media queries to adjust the design. Anyway, thanks for your help.
I have got a horizontal list (display: flex) with multiple elements. When I reduce the window size the list elements start to shrink. Unfortunately, the width of the elements nearly stays the same. The li-elements take up too much space. I want the li-element's size to fit the content. If I add "flex-shrink: 0" to the li-element the width is right, but I need this word-wrap.
How to fix this?
With flex-shrink: 0
With flex-shrink: 1
#categories ul {
border-top: 1px solid lightgrey;
display: flex;
margin: 0 0 -1px 0;
padding: 7px 0 0 0;
}
#categories li {
padding: 0 0 7px 0;
margin-right: 15px;
list-style-type: none;
}
<nav id="categories">
<ul>
<li><a href="#">
Thanks & regards,
Mark
Remember that the flex-shrink property sets the flex-shrink of a flex container item. So the items will shrink to fit the container if they are larger than the container's size. In your case, I think that setting flex-shrink to 2 would make the word-wrap as you requested. Check this code snippet. I hope it helps!
function myFunction(val) {
document.getElementById("example-element").style.flexShrink = val;
}
* {
font-family: arial;
}
.buttons-section {
display: flex;
justify-content: space-evenly;
width: 70%;
}
.example-container {
background-color: #eee;
border: .2em solid;
padding: .75em;
border-radius: 10px;
width: 70%;
margin-left: 20px;
max-height: 300px;
display: flex;
}
.example-container>div {
margin: 10px;
}
.first-solution>div {
background-color: rgba(0, 255, 0, 0.2);
border: 3px solid green;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 300px;
}
p {
margin-left: 20px;
}
small {}
.second-solution {
flex: 1 1 0;
justify-content: space-evenly;
}
.second-solution>div {
background-color: rgba(255, 0, 0, 0.2);
border: 3px solid red;
text-align: center;
}
.third-solution {
flex-wrap: wrap;
}
.third-solution>div {
background-color: rgba(0, 0, 255, 0.2);
border: 3px solid blue;
flex-basis: 100px;
text-align: center;
}
<section class="buttons-section">
<div>
<pre><code>flex-shrink: 0;</code></pre>
<button type="button" onclick="myFunction(0)" aria-hidden="true">Apply
</button>
</div>
<div>
<pre><code>flex-shrink: 1;</code></pre>
<button type="button" onclick="myFunction(1)" aria-hidden="true">Apply
</button>
</div>
<div>
<pre><code>flex-shrink: 2;</code></pre>
<button type="button" onclick="myFunction(2)" aria-hidden="true">Apply
</button>
</div>
</section>
<br>
<div>
<section>
<div class="example-container first-solution">
<div id="example-element">I shrink</div>
<div id="example-element">Item Two</div>
<div id="example-element">Item Three</div>
</div>
</section>
</div>
<p>Added more examples after the first comment. Feel free to resize the screen to see the behaviour😉</p>
<p>All elements same size, container with flex: 1 1 0;<br>
<small>Added <b>justify-content: space-evenly;</b> which helps to distribute the extra free space </small>
</p>
<div>
<section>
<div class="example-container second-solution">
<div>Item one</div>
<div>Item Two</div>
<div>Item Three</div>
</div>
</section>
</div>
<p>Suggestion: using flex-wrap: wrap; on the container so the items will always fit the container</p>
<div>
<section>
<div class="example-container third-solution">
<div>Item one</div>
<div>Item Two</div>
<div>Item Three</div>
</div>
</section>
</div>
Can you paste your code here so we can see more info about your problem. Also did you try with flex-grow property?

Line divs up using % width

I have a page where there is a line of 4 cards, each with width: 25%. Below that I have a line with 3 cards that I want to line up with the first 3 cards of the line above. The cards also have a margin which means that when it divides the card's width with 25% the 3 cards are bigger. Any idea how to fix this?
.section-wrapper {display: flex}
.sessions-card, .earnings-card {flex:0 0 25%; margin: 0 10px; border: 2px solid black; height: 200px}
.session-wrapper:first-child {margin-left: -10px}
.session-wrapper:last-child {margin-right: -10px}
<h1>Sessions</h1>
<div class="section-wrapper">
<div class="sessions-card approved"></div>
<div class="sessions-card completed"></div>
<div class="sessions-card pending"></div>
<div class="sessions-card cancelled"></div>
</div>
<h1>Earnings</h1>
<div class="section-wrapper">
<div class="earnings-card"></div>
<div class="earnings-card"></div>
<div class="earnings-card"></div>
</div>
Instead of the width, you can do it with the flex property and if 20px is the desired margin between them, you'd need to use some calc(), esp. if you want for the first and last one to be touching the parents edges:
.section-wrapper {display: flex; background: lightblue}
/* can also do it with the "justify-content: space-between;" and "margin-right: auto" set on the last ".earnings-card" */
.sessions-card, .earnings-card {
/*width: 25%;*/
flex: 0 0 calc(25% - 15px); /* -15px because of the -20px left & right margin devided by 4 = 5px; 20px - 5px = 15px */
margin: 0 10px;
border: 2px solid black;
height: 200px;
box-sizing: border-box;
}
.sessions-card:first-child,
.earnings-card:first-child {
margin-left: 0;
}
.sessions-card:last-child,
.earnings-card:last-child {
margin-right: 0;
}
<h1>Sessions</h1>
<div class="section-wrapper">
<div class="sessions-card approved"></div>
<div class="sessions-card completed"></div>
<div class="sessions-card pending"></div>
<div class="sessions-card cancelled"></div>
</div>
<h1>Earnings</h1>
<div class="section-wrapper">
<div class="earnings-card"></div>
<div class="earnings-card"></div>
<div class="earnings-card"></div>
</div>

center arrow over img with css

I want to show an arrow over an image. This arrow should be centered every time.
How can I do it?
Here is my code in jsfiddle:
CSS:
.t1_img {
background-image:url('http://www.4freephotos.com/medium/batch/Branch-of-acacia-with-flowers777.jpg');
position: relative;
background-size: cover;
background-position: center center;
overflow: hidden;
border-radius: 10px 0 0 0;
height: 200px;
}
.t1_arrow {
position: absolute;
bottom: 0;
width: 60px;
border-left: 40px solid #cfcfcf;
border-right: 40px solid #cfcfcf;
border-top: 20px solid transparent;
}
.t1_content {
background-color: #cfcfcf;
height: 150px;
padding-top: 20px;
margin-bottom: 40px;
}
HTML:
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="t1_img">
<div class="t1_arrow"></div>
</div>
<div class="t1_content">
Textinhalt
</div>
</div>
<div class="col-md-3">
<div class="t1_img">
<div class="t1_arrow"></div>
</div>
<div class="t1_content">
Textinhalt
</div>
</div>
<div class="col-md-3">
<div class="t1_img">
<div class="t1_arrow"></div>
</div>
<div class="t1_content">
Textinhalt
</div>
</div>
<div class="col-md-3">
<div class="t1_img">
<div class="t1_arrow"></div>
</div>
<div class="t1_content">
Textinhalt
</div>
</div>
</div>
</div>
You know the width of the arrow so if you put it left:50%; and margin-left:-30px; because its width is 60px it is centered everytime.
So add this
.t1_arrow {left:50%;margin-left:-30px;}
Your fiddle edited
You could use the following line:
left: calc((100% - 60px)/2);
Where 60 px refers to the width of the arrow. This method is not supported by all browsers, unfortunately. For safety, add the following three lines to the .t1_arrow class:
.t1-arrow {
left: 45%;
left: calc((100%-60px)/2);
left: -webkit-calc((100%-60px)/2);
}
Or you could use jquery to set the height after pageload:
$(function() {
$(".t1-arrow").each( function() {
var w = $(this).parent(0).width();
$(this).css('left',(w-60)/2 + 'px');
});
});
If you only want it centred horizontally, add the following to your Arrow's CSS;
left: 0; right: 0;
margin: auto;
Updated Fiddle. https://jsfiddle.net/86e0gg99/12/
Here you can see a picture of the solution, i'm looking for.
https://db.tt/1UFk3baW

CSS - Float Left/Right Issue

You can see the problem here: http://baycity2014.weebly.com/
I'm having the weirdest issue. I set-up these divs, and everything works fine. But when I float a div to the left and the other to right, they seem to "come out" of their container div. The container div collapses, and the div below goes BELOW the parent div and BEHIND the child div.
If I remove the float: right, no problem. What's the issue?
HTML:
<div id="main-wrap2">
<div class="block2">
<div id="left_content">
{content}
</div>
<div id="right_content">
test<br/>
test<br/>
test<br/>
</div>
</div><!-- end container -->
</div><!-- end main-wrap -->
CSS:
#main-wrap2 {
width: 100%;
border: 5px solid;
}
.block2 {
margin: 0 auto;
width: 940px;
}
#left_content {
background:#ffffff;
border-radius:5px;
-webkit-box-shadow: 0px 0px 3px 0px rgba(0, 0, 0, 0.23);
box-shadow: 0px 0px 3px 0px rgba(0, 0, 0, 0.23);
margin-bottom:6px;
width: 560px;
margin-top: 10px;
float: left;
}
#right_content {
width: 270px;
border: 1px solid;
margin-top: 10px;
float:right;
}
set
#main-wrap2 {
display: inline-block;
}
Try it :
make a css class .clr:{clear:both} and modify ur html code to :
<div id="main-wrap2">
<div class="block2">
<div id="left_content">
{content}
</div>
<div id="right_content">
test<br/>
test<br/>
test<br/>
</div> <div class="clr"></div>
</div><!-- end container -->
</div><!-- end main-wrap -->
Hope it will help u :)
Make their parent have:
.block2 {
overflow:auto;
}
This will allow it to resize with the children.
It's a classic clearfix issue. Explanation: http://nicolasgallagher.com/micro-clearfix-hack/
Basically it uses pseudo-elements to add a div with "clear: both;" on it so it clears.
just like Simon says,put some background to see the difference
display:inline-block;
background:aqua;
<div id="main-wrap2">
<div class="block2">
<div id="left_content">
{content}
</div>
<div id="right_content">
test<br/>
test<br/>
test<br/>
</div>
</div><!-- end container -->
</div><!-- end main-wrap -->
#main-wrap2 {
width: 940px;
border: 5px solid;
margin: 0 auto;
}
.block2 {
display: inline-table !important;
}
#left_content {
background:#ffffff;
border-radius:5px;
-webkit-box-shadow: 0px 0px 3px 0px rgba(0, 0, 0, 0.23);
box-shadow: 0px 0px 3px 0px rgba(0, 0, 0, 0.23);
margin-bottom:6px;
width: 560px;
margin-top: 10px;
float: left;
}
#right_content {
width: 270px;
border: 1px solid;
margin-top: 10px;
float:right;
}
I hope this works for you!
#main-wrap2 {
width: 100%;
border: 5px solid;
overflow:auto;
}
.block2 {
margin: 0 auto;
width: 940px;
overflow:auto;
}
/*use overflow:hidden; or overflow:auto; in parent div tags.. */
If you float an element, you need to make sure its parent is also floated.

Resources