This question already has answers here:
CSS two divs next to each other
(13 answers)
Closed 1 year ago.
I tried to align 2 divs on the same line without using float and width. I am using below code.
.first{
color: #377fd9;
font-size: 1.375rem;
margin-bottom: 10px;
margin: 15px 0px 0px 20px;
padding-top: 35px;
border-top: 1px solid #e9ecef;
border-bottom: 1px solid #e9ecef;
}
.second{
cursor: pointer;
text-align: end;
padding: 8px 0;
}
<div class="first">First</div>
<div class="second">Second</div>
Looking at the required layout, the borders and padding refer to the whole thing, not just the First div.
If you put both divs in a container and put that styling on the container, you could use flex to align the divs within container.
.container {
display: flex;
margin-bottom: 10px;
margin: 15px 0px 0px 20px;
padding-top: 35px;
border-top: 1px solid #e9ecef;
border-bottom: 1px solid #e9ecef;
}
.first {
color: #377fd9;
font-size: 1.375rem;
}
.second {
cursor: pointer;
text-align: end;
padding: 8px 0;
}
.first,
.second {
flex: 1;
}
<div class="container">
<div class="first">First</div>
<div class="second">Second</div>
</div>
Div is a block element by default and cannot be place side by side with other elements but you can change that by adding display:inline-block to your CSS like so
.first{
color: #377fd9;
font-size: 1.375rem;
margin-bottom: 10px;
margin: 15px 0px 0px 20px;
padding-top: 35px;
border-top: 1px solid #e9ecef;
border-bottom: 1px solid #e9ecef;
display:inline-block;
}
.second{
cursor: pointer;
text-align: end;
padding: 8px 0;
display:inline-block;
}
<div class="first">First</div>
<div class="second">Second</div>
Just wrap it inside a parent element, and use flexbox.
You can use your other styles with this if you want. If you don't need the space between the div's, you can remove the flex: 1 assigned to the div.
.first{
color: #377fd9;
}
.second{
cursor: pointer;
}
section {
display: flex;
}
div {
flex: 1;
}
<section>
<div class="first">First</div>
<div class="second">Second</div>
</section>
<style>
#main{display:grid;
grid-template-columns:1fr 1fr;
}
</style>
<div id="main">
<div class="first">First</div>
<div class="second">Second</div>
</div>
You can rely on flex box to perform that, For that you have to have a container which has display CSS property set to flex and all sub element will be align on the same line as the default Flex Direction is ROW
.container {
display: flex;
}
.container div {
flex-grow: 1;
}
.first {
background: #34D399;
}
.second {
background: #1E3A8A;
}
<div class="container">
<div class="first">First</div>
<div class="second">Second</div>
<div>
As you can see on the .container div selector I set all item to have equal width using flex-grow: 1;
You can use flex in your layout. Working eg. i hope this will work for you.
https://codesandbox.io/s/quizzical-banzai-dz5sm?file=/index.html
Related
I have a problem with positioning the vertical line. Here's the project:
https://prnt.sc/wp2vh4
div class="col span-1-of-2"
to separate those two lists BUT - there's a grey vertical line in the 'center' between them. When I make border-right for the first div, it's way too on the right side. How can I make this line more in the center?
two elements are block - should it be something connected to that? but I don't want to 'ruin' the column system.
You could essentially take the two columns and give them a box-shadow of a half pixel each (totaling to 1px side by side). Half pixels don't work with border declarations reason being.
.container {
display: flex;
height: 150px;
}
.col {
align-items: center;
display: flex;
flex: 1;
justify-content: center;
}
.left {
box-shadow: .5px 0 0 #000;
}
.right {
box-shadow: -.5px 0 0 #000;
}
<div class="container">
<div class="col left">Left</div>
<div class="col right">Right</div>
</div>
There are a lot of ways to do this, another solution would be using the old columns css property, like this
.container {
columns: 2;
column-gap: 0;
column-fill: balance;
column-rule: 2px solid #ff44cc;
text-align: center;
padding: 10px;
}
<div class="container">
<div>Block</div>
<div>Block</div>
</div>
Take the solution that mosts suits you.
There are many ways to accomplish a vertical divider between columns.
Option #1
The easiest is to utilize CSS flex-box to create the columns. This will cause both columns to be the same height in the container and you can use a border to create the visual divider.
/* this section illustrates the container sizes */
#container {
border: 1px dashed #dadada;
padding: 2px;
}
.col {
padding: 20px;
font-family: Helvetica, Arial, Sans-serif;
background: tan;
border: 1px dashed #333;
}
/* this shows the solution */
#container {
display:flex;
justify-content: space-between;
}
.col {
flex-basis: 50%;
}
.col:first-child {
border-right: 3px solid aqua;
}
<div id="container">
<div class="col">Column 1</div>
<div class="col">Column 2 with lots of content that makes it much taller than the other column and messes with heights.</div>
</div>
Option #2
Use a pseudo element on the parent container to create a border.
/* this section illustrates the container sizes */
#container {
border: 1px dashed #dadada;
padding: 2px;
}
.col {
padding: 20px;
font-family: Helvetica, Arial, Sans-serif;
background: tan;
border: 1px dashed #333;
}
/* The solution */
#container {
position: relative;
overflow: auto;
}
#container:before {
content: '';
width: 2px;
background: aqua;
position: absolute;
top: 0;
left: 50%;
bottom: 0;
transform: translateX(-50%);
}
.col {
float:left;
width: calc(50% - 42px);
/* need to remove the border & padding width from the full width */
}
<div id="container">
<div class="col">Column 1</div>
<div class="col">Column 2 with lots of content that makes it much taller than the other column and messes with heights.</div>
</div>
Option #3
Really there are lots more options, a CSS gradient background, shadows, CSS Grid, CSS Columns, this list goes on.
Im having an issue where my background color for a child element is going past my parent elements borders. How could I remedy this?
.package {
border: 1px solid black;
min-height: 200px;
margin-bottom: 1rem;
border-radius: 20px;
}
.banner {}
.fedex {
background-color: #4D148C;
color: white;
border-bottom: 3px solid #FF6600;
}
.logo {
padding: 1rem;
}
<div class="package">
<div class="banner fedex">
<div class="logo"></div>
</div>
</div>
Edit: I should mention I tried adding the same border radius only to the top of banner but this then left a small gap of white space between the color and the border of the parent.
overflow:hidden will prevent the inner child elements from extending beyond the bounds of the parent.
.package {
border: 1px solid black;
min-height: 200px;
margin-bottom: 1rem;
border-radius: 20px;
overflow:hidden;
}
.banner {}
.fedex {
background-color: #4D148C;
color: white;
border-bottom: 3px solid #FF6600;
}
.logo {
padding: 1rem;
}
<div class="package">
<div class="banner fedex">
<div class="logo"></div>
</div>
</div>
Apart from using overflow: hidden, it's also possible to use contain: content, which tells other elements that the child elements inside that particular element will never affect other elements, and will also never be displayed outside the parent element.
.package {
border: 1px solid black;
min-height: 200px;
margin-bottom: 1rem;
border-radius: 20px;
/* ADDED */
contain: content;
}
.banner {}
.fedex {
background-color: #4D148C;
color: white;
border-bottom: 3px solid #FF6600;
}
.logo {
padding: 1rem;
}
<div class="package">
<div class="banner fedex">
<div class="logo"></div>
</div>
</div>
In the following code when i specify margin-top for #thirdDiv, It doesn't work until i give it 36px.
What is the reason?
#Container {
border: 15px solid orange;
width: 350px;
}
#firstDiv {
display: inline-block;
border: 1px solid red;
font-size: 1em;
}
#secondDiv {
border: 1px solid green;
display: inline-block;
font-size: 2em;
}
#thirdDiv {
display: inline-block;
border: 1px solid pink;
font-size: 1em;
margin-top: 36px;
}
<div id="Container">
<div id="firstDiv"> a </div>
<div id="secondDiv"> b </div>
<div id="thirdDiv"> c </div>
</div>
Because the child elements of your Container element are based on the bottom of that div. If you add vertical-align: top to your child elements, any margin-top is possible. You can try it out in this CodePen where I copied you code and tidied the CSS up a bit. Note that you can choose to only put vertical-align: top in your #thirdDiv element. This way you can keep the other two divs in their original position.
What you are looking for is padding. The CSS margin properties are used to create space around elements, outside of any defined borders whereas the CSS padding properties are used to generate space around an element's content, inside of any defined borders.
Try the following instead of applying margin-top to #thirdDiv.
#Container {
padding-top: 36px;
border: 15px solid orange;
width: 350px;
}
#firstDiv {
display: inline-block;
border: 1px solid red;
font-size: 1em;
}
#secondDiv {
border: 1px solid green;
display: inline-block;
font-size: 2em;
}
#thirdDiv {
display: inline-block;
border: 1px solid pink;
font-size: 1em;
}
<div id="Container">
<div id="firstDiv"> a </div>
<div id="secondDiv"> b </div>
<div id="thirdDiv"> c </div>
</div>
You must specify a value for margin-top else it won't know how much margin to add.
What were typing for margin-top before, margin-top: ;?
What is your desired effect?
I need to make h1 tag center and also need to make a border around it.
To prevent taking up more width I give display: inline-block but the tag does not respond to text-align: center then.
h1 {
text-align: center;
border: 1px solid;
display: inline-block;
}
<div class="container">
<h1>This is the Title</h1>
</div>
You can use display: table on heading. This will make heading to have width depending on its content while it will remain a block level element as well. And you can center it using margin: 0 auto property.
.heading {
border: 1px solid black;
display: table;
margin: 0 auto;
padding: 5px;
}
<h1 class="heading">Heading 1</h1>
Put the following on parent tag instead of h1 tag
text-align: center;
this would be your HTML.
<div class="heading">
<h1>My Heading</h1>
</div>
this will go into CSS
.heading {
text-align: center;
}
.heading h1 {
border: 1px solid black;
display: inline-block;
}
You can follow #gavgrif instructions.
<div class="container">
<h1><span>Hello</span></h1>
</div>
h1 {
text-align: center;
}
span {
border: 1px solid #000;
padding: 5px 10px;
}
You can use position: absolute; and transform
.heading {
border: 1px solid black;
padding: 5px;
transform: translate(-50%, 0%);
position: absolute;
left: 50%;
top: 0;
}
<h1 class="heading">Heading 1</h1>
if you are using bootstrap you can use the class text-center
here is the example what i can understand from your query
HTML:
<div class="head">
<h1>Game Over</h1>
</div>
css
.head{width:100%;float:left;margin:0 auto;text-align:center;}
h1{border:1px solid red;text-align:center;position:relative;}
h1:after{position:absolute;content:"";top:0;width:200px;height:38px;border:1p x solid green;left:50%;transform:translate(-50%, 0);}
here is the fiddle
I agree with #j10i2 - .container{text-align:center;}, but another way to do it is to center content within the h1. The h1 is a block level element - so insert a span into it and place the text within that. Now applying the text-align: center will allow the centering of the span within the h1.
UPDATE - I just noticed that you want a border around the centered content - this is easy - apply the boder to the span that has been centered within the h1.
h1 {
text-align:center;
}
h1 span {
border: solid 1px #333;
padding:5px 15px;
}
<h1><span>Heading 1</span></h1>
I have a footer div with inner span buttons.
Each button have margin-right: 20px and the .footer element has padding: 0 13px.
I want to prevent the margin-right of the most right button by setting negative margin-right to its parent.
How could I get it with margin-right?
.footer {
border: 2px solid blue;
padding: 0 13px;
width: 120px;
text-align: right;
background-color: yellow;
}
.btn {
border: 1px solid black;
margin-right: 20px;
background-color: red;
}
<div class="footer">
<span class="btn">Btn1</span>
<span class="btn">Btn2</span>
</div>
I am not looking for a solution with the last child CSS pseudo-class.
You need to add another container child of .footer and apply a negative margin-right to that element. This approach is also described here:
.footer {
border: 2px solid blue;
padding: 0 13px;
text-align: right;
background-color: yellow;
overflow:hidden;
}
.footer > div {
margin-right:-33px;
}
.btn {
border: 1px solid black;
margin-right: 20px;
background-color: red;
}
<div class="footer">
<div>
<span class="btn">Btn1</span>
<span class="btn">Btn2</span>
</div>
</div>
Note you also need to a d overflow:hidden; to the .footer element to prevent horizontal scrollbar.