CSS navigation bar - centering based on element within nav bar - css

I'm trying to create a nav bar with four elements, two smaller buttons that will link to the homepage and a contact page, and then two larger buttons that will link to the two main sections of the site. I'm trying to position things in such a way that the two large buttons are centered, with the two smaller buttons to the left of them.
I can't "margin: 0 auto;" the two large buttons inside their own div as I would have to set it to "display: block;" which sets them on a different line than the smaller buttons, and absolutely positioning things messes up the layout as soon as the browser window changes.
I feel like I'm missing something simple here. Am I going at this from the wrong angle?
Here's a Dabblet of what I have at the moment... http://dabblet.com/gist/6287837
HTML
<div id="nav-container">
<div id="small-button-container">
<img src="http://placehold.it/47x22" class="small-button01" />
<img src="http://placehold.it/70x42" class="small-button02" />
</div>
<div id="big-button-container">
<img src="http://placehold.it/360x64" />
<img src="http://placehold.it/360x64" />
</div>
</div>
CSS
#nav-container{
outline: 1px red dashed;
width: 1000px;
display: block;
margin: 0 auto;}
#small-button-container{
display: inline-block;
width: 136px;
position: relative;
top: -22px;
left: 40px;}
#big-button-container{
display: inline-block;
width: 780px;
height: 64px;
margin-left: 70px;}
.small-button01{
display: inline;
position: relative;
left: 5px;}
.small-button02{
display: inline;
position: relative;
left: 15px;}

You can apply position: absolute; to your #small-button-container, so the the big buttons will centre.
Note we put position: relative; on the parent container so the #small-button-container will be positioned absolutely relative to it's parent.
CSS
#nav-container {
outline: 1px red dashed;
width: 1000px;
display: block;
margin: 0 auto;
text-align: center;
position: relative;
}
#small-button-container {
position: absolute;
top: 0;
left: 0;
}
Demo

In addition to position: absolute you can also try using a negative left margin on #nav-container if you know the computed width of #small-button-container. You can use text-align: center on #nav-container. Since it's a div you don't need to use display:block too. If you have additional padding on #small-button-container you'll need to take this into account.
#nav-container {
margin: 0px auto;
text-align: center;
margin-left: -136px;
}

Related

While using min-inline-size,is it possible to give direction from right to left in css

I'm trying to give background color to the icon present in the right hand side of the page. But background color is applied only for the image as a block. But actually what I need is background should start from the right side of the page and should end at the left side of the page. So I set the min-inline-size as 100%. This resolved the background issue. Now the image is in the left hand side. But I'm not getting how to achieve this?
<img src="excelpng.jpg" style="height: 25px; overflow: hidden; background-color: darkred; display: inline-block; min-inline-size: 100%" alt="">
Apply width: 100%; text-align: right; to img
img {
position: fixed;
margin: 1px;
right: 0px;
display: inline-block;
min-inline-size: 100%;
height:25px;
overflow: hidden;
background-color: darkred;
width: 100%;
text-align: right;
}
<img src="excelpng.jpg">
Hope this helps:)

Expanding height of parent div based on children's height

I know there are several similar questions answered here, but I can not seem to get this working.
I have two parent divs - one is like a frame with a border and padding, the second is a solid black background, and the third is where a transparent image will actually be placed. I need the two parent divs to expand their height based on the image's height.
I have this working for the div with the black background, but I can't get the parent div with the border to expand it's size:
Here is the fiddle: http://jsfiddle.net/vpdj4kst/
#builder_container {
width: 100%;
/*overflow: auto;*/
position: relative;
padding: 8px;
border: 1px solid #ccc;
margin-bottom: 15px;
display: inline-block;
clear: both;
}
#builder_contents {
background: #000;
width: 100%;
height: 100%;
position: relative;
display: block;
}
.builder_img {
width: 100%;
height: auto;
position: absolute;
}
<div id="builder_container">
<div id="builder_contents">
<img class="builder_img" src="image.png" />
</div>
</div>
This is because you have set the image to position: absolute; which will take it out of the flow causing the parent elements to act as if it wasn't there.
Elements that are positioned relatively are still considered to be in
the normal flow of elements in the document. In contrast, an element
that is positioned absolutely is taken out of the flow and thus takes
up no space when placing other elements.
Position (https://developer.mozilla.org/en-US/docs/Web/CSS/position)
Remove position: absolute; from .builder_img and the parent containers will react to its height.
#builder_container {
width: 100%;
/*overflow: auto;*/
position: relative;
padding: 8px;
border: 1px solid #ccc;
margin-bottom: 15px;
display: inline-block;
clear: both;
}
#builder_contents {
background: #000;
width: 100%;
height: 100%;
position: relative;
display: block;
}
.builder_img {
width: 100%;
height: auto;
}
<div id="builder_container">
<div id="builder_contents">
<img class="builder_img" src="http://coolspotters.com/files/photos/1036167/adidas-st-girls-straw-hat-profile.png" />
</div>
</div>

CSS-Center alignment for image in a carousel ng-repeat

I am using carousel for image display but I would like to cater it for all types of image dimension a user upload. I resized all images before displaying it in a carousel but i cant seem to make in into center. I have tried margin:auto / text-align:center either in the li tag or img tag but it doesn't seem to work for me. Below are some samples of the carousel showing empty spaces and the image should be displayed in the center.
Image restrained by width
Image restrained by height
What i wanted to achieve here is to make the images to display in the center either it is restrained by width or height. This is how my images are rendered
<div class="fanpageCarousel" ng-controller="FanCarouselCtrl"
ng-init="getImage()">
<ul rn-carousel rn-carousel-indicator rn-carousel-auto-slide="3"
rn-carousel-pause-on-hover="true">
<li ng-repeat="image in slides"><img ng-src="{{image.src}}" /></li>
</ul>
And these are my CSS customization
.fanpageCarousel{
position: relative;
margin: auto;
padding: 15px;
background-color: #ffffff;
border: 1px solid #ddd;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
max-width: 800px;
max-height: 450px;
border-radius: 4px;
}
.fanpageCarousel img{
max-width: 100%;
max-height: 400px;
}
here is the JSFiddle link
Hope someone can give me some assistance.. Thank You
Make the following changes to CSS:
.fanpageCarousel img {
bottom: 0;
left: 0;
margin: auto;
max-height: 100%;
max-width: 100%;
position: absolute;
right: 0;
top: 0;
}
.fanpageCarousel li {
height: 450px;
overflow: hidden;
position: relative;
text-align: center;
}
In short the container (li) around each img needs to be set to position: relative; to allow the img to be positioned relative to it. Height needs to be specifically set on the li for this to work. text-align: center; is a fallback for older browsers to at least center the img horizontally. Setting position: absolute;, margin: auto; and bottom, left, right and top to 0 is what makes the img center in the li.
JS Fiddle:
http://jsfiddle.net/h82ajq0c/

div disappears after position: relative

I'm trying to position an image to the right within a container. However, after I set my parent container to position: relative;, my image disappears. Any ideas? Website: http://andrewgu12.kodingen.com/. Thanks!
CSS:
.slide {
padding-top: 138px;
background-attachment: fixed;
width: 100%;
height: 100%;
position: relative;
box-shadow:inset 0px 10px 10px rgba(0,0,0,0.3);
}
div#inner-container {
text-align: left;
width: 960px;
margin: 0 auto;
padding: 0;
white-space: normal;
position: relative;
}
div#name {
right: 0;
bottom: 200px;
position: absolute;
}
HTML:
<div class="slide" id="home" data-slide="1" data-stellar-background-ratio="1">
<div id="inner-container">
<div id="name" clas="row">
<img src="images/names.png">
</div>
</div>
</div>
It does not disappear, it just goes all the way up and out the monitor xD You have to remember that when you use position: absolute the object is going to look for a parent that has position: relative to position itself. When you add position: relative to div#inner-container it changes the div#name.row reference. Maybe adding height: 100%; to the parent div might do the trick for what you want?
Try this:
div#name {
position:fixed;
right: 0px;
bottom: 200px;
}
I Solved it! The parent div that has position:relative actually goes really high up the monitor. So you need to set the the top style to 0px and play around with the top,bottom,right,left styles that come with position relative. So basically add (top: 0px) to your css styling.

How to make my web page display in full in any browser window of any size

I have made a webpage and this is the code to the main div.
#Div {
margin-left: 0px;
position:absolute;
margin-top: -1px;
display: inline;
float: left;
margin-bottom: 0;
background-color: #030;
width: 660px;
margin-left:-330px;
left:50%;
padding-top: 0px;
height: 440px;
}
Is there a css technique i can use to make sure the page occupies the whole page no matter the size of the computer screen the browser is on.
Why are you setting margin-left twice? Why are you floating (and displaying inline) a div that you want to take up the whole screen? Setting a negative left margin will move your whole div to the left, and therefore cause it to not reach all the way to the right even when the width: 100%.
Take away all margins. Do width:100%. change display:inline to display:block. Take away the float. If you have to set this to position:absolute, then be sure to specify: top: 0px; left: 0px
min-height: 100%;
min-width: 100%;
and use a CSS Reset - http://meyerweb.com/eric/tools/css/reset/
You want to center your webpage, without beeing cut at the left on small screens?
Use following:
#outer{
text-align: center;
}
#inner{
margin:0 auto;
width: 660px;
text-align: left;
}
<div id="outer">
<div id="inner">
content
</div>
</div>
Perhaps you can take some inspiration from a CSS framework and strip out the bits that you need? http://cssgrid.net/
#container {
background: blue;
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
2border: 1px solid red;
}
<div id="container"></div>

Resources