Categories
CSS

How to vertically align an image inside a div with CSS?

Sometimes, we want to vertically align an image inside a div with CSS.

In this article, we’ll look at how to vertically align an image inside a div with CSS.

How to vertically align an image inside a div with CSS?

To vertically align an image inside a div with CSS, we use flexbox.

For instance, we write

<div class="parent">
  <img class="child" src="https://picsum.photos/300/300" />
</div>

to add a div with an img element inside.

Then we write

.parent {
  align-items: center;
  background: red;
  display: flex;
  height: 250px;
  width: 250px;
}

to make the div a flex container with display: flex;.

Then we vertically center the img element with align-items: center;.

Conclusion

To vertically align an image inside a div with CSS, we use flexbox.

Categories
CSS

How to hide scroll bar, but while still being able to scroll with CSS?

Sometimes, we want to hide scroll bar, but while still being able to scroll with CSS.

In this article, we’ll look at how to hide scroll bar, but while still being able to scroll with CSS.

How to hide scroll bar, but while still being able to scroll with CSS?

To hide scroll bar, but while still being able to scroll with CSS, we set the width of the scrollbar.

For instance, we write

html {
  overflow: scroll;
  overflow-x: hidden;
}

::-webkit-scrollbar {
  width: 0;
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: #ff0000;
}

to set the scrollbar width to 0 and make it transparent with

::-webkit-scrollbar {
  width: 0;
  background: transparent;
}

Then we change the scrollbar button background with background: #ff0000;

Conclusion

To hide scroll bar, but while still being able to scroll with CSS, we set the width of the scrollbar.

Categories
CSS

How to remove focus border (outline) around text/input boxes with CSS?

Sometimes, we want to remove focus border (outline) around text/input boxes with CSS.

In this article, we’ll look at how to remove focus border (outline) around text/input boxes with CSS.

How to remove focus border (outline) around text/input boxes with CSS?

To remove focus border (outline) around text/input boxes with CSS, we set the outline property.

For instance, we write

textarea:focus,
input:focus {
  outline: none;
}

to remove the outline when we focus on the input or textarea with outline: none;.

Conclusion

To remove focus border (outline) around text/input boxes with CSS, we set the outline property.

Categories
CSS

How to vertically center a div element for all browsers using CSS?

Sometimes, we want to vertically center a div element for all browsers using CSS.

In this article, we’ll look at how to vertically center a div element for all browsers using CSS.

How to vertically align an image inside a div with CSS?

To vertically center a div element for all browsers using CSS, we use flexbox.

For instance, we write

<div class="parent">
  <img class="child" src="https://picsum.photos/300/300" />
</div>

to add a div with an img element inside.

Then we write

.parent {
  align-items: center;
  background: red;
  display: flex;
  height: 250px;
  width: 250px;
}

to make the div a flex container with display: flex;.

Then we vertically center the img element with align-items: center;.

Conclusion

To vertically center a div element for all browsers using CSS, we use flexbox.

Categories
CSS

How to set font scaling based on the width of container with CSS?

Sometimes, we want to set font scaling based on the width of container with CSS.

In this article, we’ll look at how to set font scaling based on the width of container with CSS.

How to set font scaling based on the width of container with CSS?

To set font scaling based on the width of container with CSS, we set the vw unit.

For instance, we write

p {
  font-size: 4vw;
}

to set the p elements’ font size to 4vw, which 4% of the screen’s width.

Conclusion

To set font scaling based on the width of container with CSS, we set the vw unit.