Centering div content within multiple nested divs - html

I am trying to center some so that they are displayed within the center, however the tricky part is the html is within a number of (part of the html is static, and part of it is dynamic from a database).
See the screenshot as an example of how it currently looks (I want this content in the centre)
http://postimg.org/image/5fc6ecgy7/
note - the site is using the base 960 grid system
Below is the HTML:
<div class="">
<div class="">
<div class="">
<div id="national-prize-feed" class="panel-wrapper ">
<div class="border">
<div class="content clearfix add-radius">
<div style="padding:10px 0px;">
<div id="nation-prizes-collection" class="clearfix">
<div class="summary"></div>
<div class="">
<div class="national-prizes">
<div class="prizes">
<img class="prizes-img" alt="iPod Shuffle" src="/images/prizes/ipod-shuffle.png">
</div>
</div>
<div class="national-prizes">
<div class="prizes">
<img class="prizes-img" alt="Football" src="/images/prizes/football.png">
</div>
</div>
</div>
<div class="keys" title="/pages/index/slug/prizes" style="display:none">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
My specific css (there is a lot more.. it is based on a grid based layout)
div.prizes {
float: left;
}
div.prizes img {
width: 280px;
}
.prizes-img {
padding: 0 7px 0 7px;
margin: 7px 5px 7px 5px;
max-width: 100%;
height: auto;
}

In order to position a DIV to the center of a screen, you need to set a static width of the div to be centered, left: 0px, right: 0px, and the respective margins to auto.
Here is an example: http://jsfiddle.net/pR4hq/
<style>.center{
position: relative;
display: block;
width: 120px;
background: rgb(90,90,90);
left: 0px;
right: 0px;
margin-left: auto;
margin-right: auto;
text-align: center;
}</style>
<div class="center">Center Content</div>

Related

Position Sticky not working (inside Flexbox)

I already tried using align-self: flex-start, but nothing happened. I got the main flex container on the right of my page (flex-direction: column), and now I want one block of my flex columns to be sticky once I scrolled down to them. I wrapped a container around the flex columns I want to be sticky, and I gave that container the position sticky. I will just post a snippet of my HTML and CSS, so it won't become too long of a code. I commented next to the code where my columns are and what each container does:
.rightside_container {
display: flex;
flex-direction: column;
gap: 0px;
margin-top: 60px;
right: 17%;
position: absolute;
z-index: 0;
border-radius: 10px;
background-color: black;
padding-left: 0px;
padding-right: 20px;
padding-top: 8px;
padding-bottom: 8px;
}
.sticky_container {
border: 1px solid white;
position: sticky;
top: 0px;
}
<div class="rightside_container">
<!--big main container-->
<div class="sticky_container">
<!--since I want a few of my columns to be sticky, I wrapped
a little div container around them which I gave the position sticky-->
<div class="follow_flex">
<!--COLUMN 1-->
<div class="follow_pic_container">
<img src="thumbnails/twitterlogo.jpg" class="follow_pic">
</div>
<div class="follow_text_container">
<div class="follow_name">Twitter</div>
<div class="follow_at">#Twitter</div>
</div>
<div class="follow_button_container">
<button class="follow_button">Follow</button>
</div>
</div>
<div class="follow_flex">
<!--COLUMN 2-->
<div class="follow_pic_container">
<img src="thumbnails/youtubelogo.jpg" class="follow_pic">
</div>
<div class="follow_text_container">
<div class="follow_name">YouTube</div>
<div class="follow_at">#YouTube</div>
</div>
<div class="follow_button_container">
<button class="follow_button">Follow</button>
</div>
</div>
</div>
</div>
I think that the code portion that you provide is not enough, but in your snippet if you set a height (for testing purposes) and remove the margin it does the sticky effect. You need to consider that if you got any parent with overflow: hidden sticky position is not going to work.
.rightside_container {
display: flex;
flex-direction: column;
gap: 0px;
right: 17%;
position: absolute;
z-index: 0;
border-radius: 10px;
background-color: black;
padding-left: 0px;
padding-right: 20px;
padding-top: 8px;
padding-bottom: 8px;
height: 600px;
}
.sticky_container {
border: 1px solid white;
position: sticky;
top: 0px;
}
<div class="rightside_container"> <!--big main container-->
<div class="sticky_container"> <!--since I want a few of my columns to be sticky, I wrapped
a little div container around them which I gave the position sticky-->
<div class="follow_flex"> <!--COLUMN 1-->
<div class="follow_pic_container">
<img src="thumbnails/twitterlogo.jpg" class="follow_pic">
</div>
<div class="follow_text_container">
<div class="follow_name">Twitter</div>
<div class="follow_at">#Twitter</div>
</div>
<div class="follow_button_container">
<button class="follow_button">Follow</button>
</div>
</div>
<div class="follow_flex"> <!--COLUMN 2-->
<div class="follow_pic_container">
<img src="thumbnails/youtubelogo.jpg" class="follow_pic">
</div>
<div class="follow_text_container">
<div class="follow_name">YouTube</div>
<div class="follow_at">#YouTube</div>
</div>
<div class="follow_button_container">
<button class="follow_button">Follow</button>
</div>
</div>
</div>
</div>

Set the height of the div

I want to set the height of the section based upon the child elements but unfortunately I don't know what's wrong with my code. When I try to decrease the size of the screen the content in the paragraph and the img is overflowing.
HTML
<div id="section2" class="about">
<div class="container-section">
<div class="about-content col-lg-6">
<p></p>
</div>
<div class="about-pic col-lg-2">
<img />
</div>
</div>
</div>
CSS
.about{
height: auto;
margin: 0;
}
.about .container-section{
padding: 0;
height: auto;
position: relative;
}
.about .container-section .about-content p{
padding-bottom: 15px;
font-family: 'Lato', sans-serif;
text-align: justify;
}
.about .container-section .about-pic img{
height: auto;
width: 100%;
padding-bottom: 25%;
}
Any suggestions?
Ok, it looks like you're using Bootstrap, right? And Bootstrap is adding some floats, which removes elements from the page flow, and thus the container behaves as if those elements aren't in it. Try adding a cleared div in various places and see if it solves your issue, like so:
<div id="section2" class="about">
<div class="container-section">
<div class="about-content col-lg-6">
<p></p>
</div>
<div class="about-pic col-lg-2">
<img />
</div>
</div>
<div style="clear:both;"></div>
</div>

Centering grouped inline elements issue

I have a section that I want to look as if it is centered on the page. I have four boxes that appear inline and I believe the reason they do not like they are centered is because everything is aligned naturally to the left. The thing is, I do not want the content that is in these boxes to be centered, I want them aligned left.
So how can I make the boxes appear as if they are centered within the page, but not have it affect my text?
#contact-connect {
width: 80%;
height: auto;
margin: 0 10%;
padding: 80px 0;
}
#contact-connect-box-container {
margin: 0 auto;
width: auto;
}
.contact-connect-box {
width: 25%;
margin: 60px 0 0 0;
display: inline-block;
border: 1px solid black;
vertical-align: top;
transition:1s; -webkit-transition:1s;
}
<div id="contact-connect">
<div id="contact-connect-box-container">
<div class="contact-connect-box">
<h2 class="contact-connect-title">Call</h2>
<div class="contact-connect-description">vcxv</div>
</div>
<div class="contact-connect-box">
<h2 class="contact-connect-title">Write</h2>
<div class="contact-connect-description">
Reach out to us
<br>
<div id="scroll" class="contact-connect-link">Fill out our contact form.</div>
</div>
</div>
<div class="contact-connect-box">
<h2 class="contact-connect-title">Visit</h2>
<div class="contact-connect-description">
fgrge
</div>
</div>
<div class="contact-connect-box">
<h2 class="contact-connect-title">Connect</h2>
<div class="contact-connect-description">
<div class="contact-connect-link">Visit us on Facebook</div>
<div class="contact-connect-link">See us on Youtube</div>
<div class="contact-connect-link"></div>
</div>
</div>
</div>
</div>
You can just add "display: flex" on your #contact-connect-box-container
#contact-connect-box-container {
margin: 0 auto;
width: auto;
display: flex;
}
DEMO :
https://jsfiddle.net/w776Lq1u/8/
The problem is the margin on the right side of "Connect." I would recommend using flexbox and using display: flex; justify-content: space-around; That way you won't need any margins at all.
EDIT: plnkr

Nesting margin to achieve centered content

I am running into an issue of trying to center children div while keeping the content aligned left. I have four boxes that are inline, the boxes are set to 25% width and are naturally aligned left. My issue is the four boxes does not look like they are centered on the page because of the natural alignment within the boxes. You can see this in the first image.
So what I thought to do was inside of my .contact-connect-box was to make a child div( connect-box-wrap ), somewhat as a wrap for inside of the box and add margin: 0 auto; , however this is not helping at all.
If you look at the second image, that is what I am after. I want the content to still be aligned left, but the margin for everything to be shifted over so that the box positioning appears centered on the page.
#contact-connect {
width: 80%;
height: auto;
margin: 0 10%;
padding: 80px 0;
}
#contact-connect-box-container {
margin: 0 auto;
width: auto;
/*display: flex;
justify-content: space-around;*/
}
.contact-connect-box {
width: 25%;
margin: 60px 0 0 0;
display: inline-block;
/*border: 1px solid black;*/
vertical-align: top;
opacity: 0;
transition:1s; -webkit-transition:1s;
}
.connect-box-wrap {
margin: 0 auto;
}
<div id="contact-connect">
<div id="contact-connect-box-container">
<div class="contact-connect-box">
<div class="connect-box-wrap">
<h2 class="contact-connect-title">Call</h2>
<div class="contact-connect-description">gfdgg</div>
</div>
</div><div class="contact-connect-box">
<div class="contact-connect-box">
<h2 class="contact-connect-title">Write</h2>
<div class="contact-connect-description">
Reach out to us
<br>
<div id="scroll" class="contact-connect-link">Fill out our contact form.</div>
</div>
</div>
</div><div class="contact-connect-box">
<div class="contact-connect-box">
<h2 class="contact-connect-title">Visit</h2>
<div class="contact-connect-description">
gsdfg
</div>
</div>
</div><div class="contact-connect-box">
<div class="contact-connect-box">
<h2 class="contact-connect-title">Connect</h2>
<div class="contact-connect-description">
<div class="contact-connect-link">Visit us on Facebook</div>
<div class="contact-connect-link">See us on Youtube</div>
<div class="contact-connect-link"></div>
</div>
</div>
</div>
</div>
</div>
I would scrap the .content-connect-box-container because you already have the .content-connect-box so that seems redundant. I did this by centre text aligning .content-connect-box and then left text aligning .connect-box-wrap. Also be careful about how you choose to make your spacing. If you're working in % make sure to stick with that, if you mix % with px, certain screen sizes will break your layout. Feel free to play around with different margin %s in my code to see how I spaced everything.
<div id="contact-connect">
<div class="contact-connect-box">
<div class="connect-box-wrap">
<h2 class="contact-connect-title">Call</h2>
<div class="contact-connect-description">gfdgg hgakhglkrjhg kjshfglkjshd flkjgshfd oweir werowheor weriwjeroija
</div>
</div>
</div>
<div class="contact-connect-box">
<div class="connect-box-wrap">
<h2 class="contact-connect-title">Call</h2>
<div class="contact-connect-description">gfdgg
</div>
</div>
</div>
<div class="contact-connect-box">
<div class="connect-box-wrap">
<h2 class="contact-connect-title">Call</h2>
<div class="contact-connect-description">gfdgg
</div>
</div>
</div>
<div class="contact-connect-box">
<div class="connect-box-wrap">
<h2 class="contact-connect-title">Call</h2>
<div class="contact-connect-description">gfdggaksj
</div>
</div>
</div>
</div>
<style type="text/css">
#contact-connect {
width: 80%;
height: auto;
margin: 0 10%;
padding: 80px 0;
display: block;
}
.contact-connect-box {
width: 21%;
margin: 0 0 0 1%;
display: inline-block;
border: 2px solid black;
vertical-align: top;
text-align: center;
}
.connect-box-wrap {
margin: 10% auto;
width: 50%;
text-align: left;
}
</style>

Center Div on-top and in between two divs in Foundation Zurb

I just started using Foundation and I like it a lot. I have a question. I have a design that requires the logo to be horizontally centered, on-top and in between two full divs (see attached picture).
The code below is what I have which seems to work fine but doesn't re-size on screen changes and I was wondering if there was a better way to accomplish this when working with Foundation.
Is there a possible improvement that I could do to better integrate the logo into Foundation? In other words is there a way to integrate the logo in a way that It re-sizes when the screen changes or this is my best option and I would need to re-size it manually for multiple screens?
HTML
<div class="row fullWidth">
<div class="large-12 columns box">
<img src="logo.png" class="logo">
</div>
</div>
<div class="row fullWidth">
<div class="large-12 columns box"> </div>
</div>
CSS
.fullWidth {
width: 100%;
margin-left: auto;
margin-right: auto;
max-width: initial;
position: relative;
}
.logo{
position:absolute;
left: 50%;
top: 20px;
margin-left: -100px;// half-width of logo
z-index: 20;
}
.box{
background-color: #333;
height: 120px;
margin-bottom: 10px;
}
If you know the height of the logo, you can use negative margin-top as well, but you'll probably have to wrap the 2 outer divs with a new div.
HTML:
<div class="outer">
<div class="row fullWidth">
<div class="large-12 columns box">
<img src="logo.png" class="logo">
</div>
</div>
<div class="row fullWidth">
<div class="large-12 columns box"> </div>
</div>
</div>
CSS:
.outer {
width: 100%;
position:relative;
}
.fullWidth {
width: 100%;
margin-left: auto;
margin-right: auto;
max-width: initial;
// position: relative; // REMOVED
}
.logo{
position:absolute;
left: 50%;
top: 50%;
margin-left: -100px;// half-width of logo
margin-top: -100px;// half-height of logo
z-index: 20;
}
You can do the following to make it center
<div class="row fullWidth">
<div class="large-12 columns box">
<img src="logo.png" class="logo">
</div>
<div class="row fullWidth">
<div class="large-12 columns box"> </div>
</div>
</div>
DEMO
Is it the logo that doesn't resize? If so, try this:
.logo{
position:absolute;
left: 50%;
top: 20%;
margin-left: -100px;//half-width of logo
z-index: 20;
}
The logo should be able to resize now.