Center a table horizontally in a div [duplicate] - css

This question already has answers here:
CSS way to horizontally align table
(11 answers)
Closed 8 years ago.
I am trying to center a table within a div but the table can be of varying length. The code looks something like this:
<div>
<table>
<tr> .... </tr>
</table>
</div>
The div element is of course display:block and of course takes up the entire horizontal space on the page but I cannot figure out how to set the padding to auto and just center the table in the div
Can anybody help me with this?

Tables are block level elements. You can set the width of the table and set margin: auto
If the width varies you may be able to use a percentage or a combination of width and min/max-width.

If the table has an explicit width, you could just use margin: 0 auto to center it.
table { margin: 0 auto; }
Alternatively, if you want to use flexboxes, set the display of the parent element to flex, and use justify-content: center for horizontal centering.
.parent {
display: flex;
justify-content: center;
}

Related

CSS floating issue id div [duplicate]

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Closed 10 months ago.
How could I centralize all the elements vertically and horizontally?
I tried to fix the div placement horizontally by using CSS float but it too disturbing for me now. Does there any perfect and useful solution. Please help me. I also want them in center
Using
margin: 0 auto;
should work to keep everything centered.
For more detailed answer go here:
https://stackoverflow.com/questions/7217315/html-css-content-always-centered#:~:text=If%20you%20want%20elements%20always,in%20the%20styles%20on%20it.&text=To%20get%20background%20elements%20to,the%20same%20style%20as%20above.
take a parant div and set it css
div {
display: flex;
justify-content: center;
align-items: center;
}
if don't work just
div{
margin:auto;
}

Flex box fit width with it's childs when child is out of window? [duplicate]

This question already has answers here:
When flexbox items wrap in column mode, container does not grow its width
(9 answers)
Closed 4 years ago.
I'm using a column wrap layout, and the page is going to load new content and append them as new children elements to the parent box element, so I hope the box width auto fit to the children elements.
Codes here: https://codepen.io/mashirozx/pen/exgRqV
What it looks now:
My hope (Notice the overflow-x bar):
Problem solved:
Adding overflow: auto to the flex box.
Try to change:
flex-container-content > div {
background-color: #EB213C;
width: auto;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;`

Centering img inside div [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 7 years ago.
Im trying to center a (pumpkinvector.jpg) image inside a .div using this classic trick:
.pumpkin{
position: relative;
top:50%;
left:50%;
transform: translate(-50%,-50%);
}
It seems to work fine on the very bottom div where background is red.
However, that was just a test, it is not were I need the image to center.
I need to center pumpkinvector.jpg image up top in "grid2" div that inside "grid 6 october" div. October div has additional code and inner class to give background image and make it resize proportionally in height and width %. Using the same centering code above, the pumpkinvector.jpg now flows to the next line and starts to position outside of the .october div. Can someone why this is?
Please bear with me here. I thought the addition of images helps you visualize my problem so I uploaded to my own site instead of js.fiddle.
Here is the link:
http://jingsportfolio.com/october.html
Please view source to view code. Thanks.
This question is different because it asks how to center div in the context that its parent div has complex markup that makes any traditional centering off and throws in on a new line below parent div.
Check this little example probably will be useful, how to center a image inside a div
The html code:
<div class="parent">
<img src="http://45reu03dndd711szsx3satxn.wpengine.netdna-cdn.com/wp-content//uploads/2015/08/Top-10-best-CSS-development-tools-2015.png"/>
</div>
The css code:
.parent {
text-align: center;
width: 100%;
border: 1px solid #ccc;
}
img .picture {
width: 40px;
height: 40px;
}
This code is running here
Horizontal alignment is really easy with CSS:
the item you want to align has to have "margin: auto"
And the item wrapping has to have a fixed width.
Vertical alignment is a little more tricky:
This trick only works if your wrapper has a fixed height and your content to align is a textual tag like span or p or h1
use the line-height css attribute of the wrapper andset its value to its own height and it's done.
OR
if your content to align has a height of 300px use margin-top: calc(50% - 150px)
change 'margin-top' for absolute "position" and "top" attribute if needed

css vertical align within float and height 100%

ive got some problem with my css styles:
I have different groups ( <div>'s ) that have subgroups displayed in ONE colum
or MULTIPLE ( max. 3) colums.
The problem i have is, that my vertical-align wont work within float elements with an 100% height.
within the subgroups: ST200 | Überblick... | EN DE should be displayed with vertical-align: middle
Maybe someone could help me.
complete code posted on jfiddle
http://jsfiddle.net/ZAa33/
Andres's comment here seems to address your problem: the line-height of the text is confining it to a smaller area than its container div.
To test, I changed the line-height property on .kursid to 35px, and the ST200 was successfully centered vertically, but only for those divs that had heights of 35px. For a more general solution, you could redesign your code to allow line-height and height to be set explicitly equal on all divs with text you want centered vertically, or you could see the other answers to that question for other options.
Vertical-Align Property works with only inline and inline-block elements. if you float any element, by default that element display property value is changed to block. In your case that is why vertical-alignment is not working on floated element. one solution for your problem is use inline.
[Inline-block demo](http://jsfiddle.net/Mostwanted_cJ/W8nf8)
Your JSFiddle is edited.
change all of your span to div and apply display:inline-block and vertical-align:middle
Inline-block demo
i found a solution to my Question that works pretty nice ;)
i just needed to set:
.kurs {
border-radius: 15px 15px 15px 15px;
background-color: #c5c5c5;
width: 100%;
min-height: 35px;
margin-bottom: 10px;
display: flex;
! display: -webkit-flex; <- remove
! display: -moz-box; <- remove
align-items: center; <- add
justify-content: center; <- add
}
The updated fiddle ;)
http://jsfiddle.net/ZAa33/9/

Horizontally Center a <div> that is not fixed

I want to horizontally center a column in a responsive grid I'm trying to make. I have the grid set up, and it works just fine. But I want to make a .class that will take a column and center it horizontally in the parent (.row). I would use this class to take a one-column-row and center the column in said row. I can center the text using the text-align property, but I want the entire column to be centered that way if I add a border/background to the column it will be centered in the row, not just the column's content.
Here is the codepen.
Given that you are setting a width on the column, simply use margin:0 auto to center it. You would need to overwrite float:left with float:none though..
UPDATED EXAMPLE HERE
.center {
text-align: center;
margin:0 auto;
float:none;
}
Alternatively, if the element doesn't have a fixed width set on it, you could simply make it an inline-block element and add text-align:center to the parent. Perfect for dynamically varying widths.

Resources