I have a block with information, floating left and a button group, floating right.
The information block has 2 rows. The first row holds information on top, but the 2nd line should be vertically centered using the whitespace available (the white space is created by the floating right button group).
How can this be done ?
HTML:
<div class="rowWrapper">
<div class="moduleInfo">
<div class="moduleTitle">Module1</div>
<div class="moduleTime">08-05-2013 11:12:33</div>
</div>
<div class="actions">
<button type="submit">Action1</button>
<button type="submit">Action2</button>
<button type="submit">Action3</button>
<button type="submit">Action4</button>
<button type="submit">Action5</button>
</div>
</div>
CSS:
.rowWrapper {
border: 1px solid #a29791;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
margin: 4px 0;
padding: 4px;
overflow: hidden;
}
.moduleInfo {
float: left;
}
.actions {
float: right;
}
.actions button {
display: block;
}
I have a fiddle created for this:
http://jsfiddle.net/DrDelete/bLw79/
If the available size is always the same, you can use line-height:
http://jsfiddle.net/bLw79/2/
if you try center-vertically, it won't work,you can try 2 options
1) line-height:40px;
2)position:absolute; top:50%; height:240px; margin-top:-120px; //margin-top = 1/2 height
i would prefer using the 1st option
Add the following in your CSS:
.moduleTime{
padding-top: 35%;
}
But I suggest you to use table here since it will provide your moduleTitle text more flexibility as you can see by changing the text in your code and then changing the moduleTitle text in the code provided on this Fiddle
Vertical centering is a little tricky one. If there's only one line of text you can always use line height. But, in case you have multiple lines of text or images this technique is not going to work. Instead, change the parent display property to table (display:table). And give the vertical align to center (vertical-align:center) to the child elements. That's it, now child elements will be vertically aligned within parent. And don't forget to give a height for parent.
Related
I want to align an text to an exactly position, like 2cm from right not the pre-stabled positions like right, left and center. There's an example: Example
How can I center the Regular, upgraded and exclusive cases like that?
per my comment, here is how the text is centered, but the price tag is included as an inline element with the text.
html
<p class="container">
Text goes here
<span class="block">
$3
</span>
</p>
CSS
.block {
padding: 4px;
background: lightblue;
border-radius: 2px;
margin-left: 4px;
}
.container {
max-width: 400px;
text-align: center;
border: 1px solid;
}
EDIT:
based on the additional information you provided, it's not just aligning text that you were having an issue with, but also layout in general.
I've updated my example to show how you could align elements within a container using text-align: center; and converting the different text/price items to have display: inline-block;.
This is just an entry point to get you started. There are many other ways to layout content with CSS, from floats, flex-box and new CSS Grids. I suggest you review each to determine what is appropriate for your project.
Updated Demo: https://codepen.io/scottohara/pen/oWLxXN
<div class="cat-left">
</div>
<div class="cat-right">
</div>
.cat-left {
float: left;
width: 233px;
border: 1px solid #c5c5c5;
}
.cat-right {
margin-left: 12px;
float: left;
}
I write this for make two box (one in left and one in right). now I want to make another
<div class="cat-left"></div>
if i put my cat-left after my cat-left then it's shown in right of first cat-left. You have seen that I have used border. Is their any option for me to make it in bottom on existing cat-left.
check http://s7.postimage.org/3t6rfpdih/demo.png for figure out what exactly I want my code.
I have tried by giving position absolute to second cat-left but not sure how it's help me without setting margin in pixel.
You need to include two sub-divs in your cat-left box like so:
<div class="cat-left">
<div class="cat-left-inner">
Left top
</div>
<div class="cat-left-inner">
Left bottom
</div>
</div>
<div class="cat-right">
Right
</div>
where
.cat-left-inner {
height: 10em;
}
You need to provide set heights to your divs to make them fill out their containers in the desired way. See this fiddle for a more concrete example: http://jsfiddle.net/A6axj/2/.
I am trying to float multiple divs (2 pictured, another will be added later). The problem is that the site container seems to ignore them when set to auto. I am not sure if this is something wrong with the container css, or the css used to with the divs. How would I go about getting the site container to recognize the floats? Thanks in advance.
Relevant HTML:
<div id="storehours">
<div id="hoursheader"><p>Shop Hours</p></div>
</div>
Relevant CSS:
#storehours {
border: 1px solid black;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
margin-left: 11px;
margin-bottom: 15px;
width: 250px;
height: auto;
float: left;
}
(That code is for the div on the right, the left one is the same but contains the form, which I don't think is the problem.)
Edit: Updated with clears and fixed cap letter. Still having the same problem.
Edit2: For clarification. The html is within the container that also contains the google maps. As you cans see, the floats cause the container to ignore them and they start at the bottom of the container. I could potentially fix the problem by setting a height on the container instead of leaving it auto, but is that good practice?
You should add overflow:hidden; on the style of the container <div id="storehours">
.
It is because you are not clearing your floats use this
<div style="clear: both;"></div>
I have two button bars- each contains links, but one also contains a submit button of a certain height. The one with the submit button has all the elements vertically centered. I want the other button bar, without the submit button, to look the same, so I gave it an explicit height. However, the links within it align to the top instead of in the middle.
What's going on here, and how can I make link bars that are of a consistent height, with vertically centered elements?
HTML:
<div class="link-bar">
<input type="submit" value="Save"/>
link
link
</div>
<div class="link-bar">
link
link
</div>
CSS:
input[type='submit'] {
width:100px;
height:40px;
border:solid red 1px;
}
.link-bar {
height:40px;
background:#EEE;
border:blue 1px solid;
margin:10px;
vertical-align: middle;
}
See jsFiddle for example
Simply add line-height equal to the height. By default, any text on that line will be vertically centered. The exception occurs if you wrap the text to a new line.
http://www.w3.org/wiki/CSS/Properties/line-height
I also removed your vertical-align as it's superfluous to content in block level elements. It only applies to inline elements.
.link-bar {
height: 40px;
background: #EEE;
border:blue 1px solid;
margin: 10px;
}
.link-bar a {
line-height: 40px; /* equal to the height of the container */
}
DEMO:
http://jsfiddle.net/SLqbk/9/
Use the line-height property.
.link-bar a {
line-height: 40px;
}
http://jsfiddle.net/SLqbk/7/
Add this to your css
.link-bar a {line-height: 40px; }
http://jsfiddle.net/xYVRj/
I gave #Sparky672 the answer because he correctly addressed my specific question and led me on the right path, but I want to share what I ended up doing, which I think is more effective overall:
Instead of explicitly setting the line-height of .link-bar a to try to match up to the container and button heights, I just set ALL the elements in the toolbar to the same line-height, and make them display:inline-bock. While the normal caveats of using inline-block apply (See here and here), the end result is consistent sizing and vertical centering for all the elements you throw in your toolbar, with less css to manage:
.link-bar * {
line-height: 30px;
display:inline-block;
/* Keep top-bottom padding of elements zeroed for consistent heights: */
padding-top:0; padding-bottom:0;
}
See the updated fiddle.
I defined two CSS classes that set the background to an image (shown below). One is a yellow block, and another is a grey block.
.block-gray { background: url('grey.gif'); width: 15px; height: 3px; }
.block-yellow { background: url('yellow.gif'); width: 15px; height: 3px; }
What I want to be able to do is to define a variable number of div's using one of the classes above, and have them horizontally stacked, and centered within a larger container.
So if I defined 3 blocks like so:
<div>
<!-- The # of these is variable, and not necessarily fixed at 3 -->
<div class="block-yellow"></div>
<div class="block-yellow"></div>
<div class="block-grey"></div>
<div>
Then I would like them to be centered within the outer div, no matter how many inner divs there are. I can use float:left to stack them horizontally, but I'm not sure how to keep them centered.
| |
Any ideas?
Thanks.
.container { text-align: center; }
.block-yellow { display: inline-block; }
and you might want to reset that text-align:
.block-yellow { text-align: left; }
well, don't use float:left; instead, use display:block and set the outer div as text-align:center